private static IAddrBook ADX_GetAddrBook() { if (MAPI.MAPIInitialize(IntPtr.Zero) == MAPI.S_OK) { IntPtr sessionPtr = IntPtr.Zero; MAPI.MAPILogonEx(0, null, null, MAPI.MAPI_EXTENDED | MAPI.MAPI_ALLOW_OTHERS, out sessionPtr); if (sessionPtr == IntPtr.Zero) { MAPI.MAPILogonEx(0, null, null, MAPI.MAPI_EXTENDED | MAPI.MAPI_NEW_SESSION | MAPI.MAPI_USE_DEFAULT, out sessionPtr); } if (sessionPtr != IntPtr.Zero) { try { object sessionObj = Marshal.GetObjectForIUnknown(sessionPtr); if (sessionObj != null) { try { IMAPISession session = sessionObj as IMAPISession; if (session != null) { IntPtr addrBookPtr = IntPtr.Zero; session.OpenAddressBook(0, IntPtr.Zero, MAPI.AB_NO_DIALOG, out addrBookPtr); if (addrBookPtr != IntPtr.Zero) { try { object addrBookObj = Marshal.GetObjectForIUnknown(addrBookPtr); if (addrBookObj != null) { return(addrBookObj as IAddrBook); } } finally { Marshal.Release(addrBookPtr); } } } } finally { Marshal.ReleaseComObject(sessionObj); } } } finally { Marshal.Release(sessionPtr); } } } else { throw new ApplicationException("MAPI can not be initialized."); } return(null); }