/// <summary> /// MAPI session intialization and logon. /// </summary> /// <returns>true if successful; otherwise, false</returns> private bool Initialize() { IntPtr pSession = IntPtr.Zero; if (MAPINative.MAPIInitialize(IntPtr.Zero) == HRESULT.S_OK) { MAPINative.MAPILogonEx(0, null, null, (uint)(MAPIFlag.EXTENDED | MAPIFlag.USE_DEFAULT), out pSession); if (pSession == IntPtr.Zero) { MAPINative.MAPILogonEx(0, null, null, (uint)(MAPIFlag.EXTENDED | MAPIFlag.NEW_SESSION | MAPIFlag.USE_DEFAULT), out pSession); } } if (pSession != IntPtr.Zero) { object sessionObj = null; try { sessionObj = Marshal.GetObjectForIUnknown(pSession); session_ = sessionObj as IMAPISession; } catch { } } return(session_ != null); }
/// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected void Dispose(bool disposing) { if (disposing) { if (session_ != null) { Marshal.ReleaseComObject(session_); session_ = null; MAPINative.MAPIUninitialize(); } } }
/// <summary> /// Returns one or more rows from a table, beginning at the current cursor position. /// </summary> /// <param name="lRowCount">Maximum number of rows to be returned.</param> /// <param name="sRows">an SRow array holding the table rows.</param> /// <returns></returns> public bool QueryRows(int lRowCount, out SRow[] sRows) { IntPtr pRowSet = IntPtr.Zero; HRESULT hr = tb_.QueryRows(lRowCount, 0, out pRowSet); if (hr != HRESULT.S_OK) { MAPINative.MAPIFreeBuffer(pRowSet); } uint cRows = (uint)Marshal.ReadInt32(pRowSet); sRows = new SRow[cRows]; if (cRows < 1) { MAPINative.MAPIFreeBuffer(pRowSet); return(false); } int pIntSize = IntPtr.Size, intSize = Marshal.SizeOf(typeof(Int32)); int sizeOfSRow = 2 * intSize + pIntSize; IntPtr rows = pRowSet + intSize; for (int i = 0; i < cRows; i++) { IntPtr pRowOffset = rows + i * sizeOfSRow; uint cValues = (uint)Marshal.ReadInt32(pRowOffset + pIntSize); IntPtr pProps = Marshal.ReadIntPtr(pRowOffset + pIntSize + intSize); IPropValue[] lpProps = new IPropValue[cValues]; for (int j = 0; j < cValues; j++) // each column { SPropValue lpProp = (SPropValue)Marshal.PtrToStructure(pProps + j * Marshal.SizeOf(typeof(SPropValue)), typeof(SPropValue)); lpProps[j] = new MAPIProp(lpProp); } sRows[i].propVals = lpProps; } MAPINative.MAPIFreeBuffer(pRowSet); return(true); }
/// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected void Dispose(bool disposing) { if (disposing) { if (session_ != null) { if (CurrentStore != null) { CurrentStore.Dispose(); CurrentStore = null; } if (content_ != null) { content_.Dispose(); content_ = null; } Marshal.ReleaseComObject(session_); session_ = null; MAPINative.MAPIUninitialize(); } } }