/// <summary> /// Use MAPI32.DLL "HrGetOneProp" from managed code /// </summary> /// <param name="attachment"></param> /// <param name="proptag"></param> /// <returns></returns> public static string GetMAPIProperty(Attachment attachment, PropTags proptag) { object mapiObject = attachment.MAPIOBJECT; if (mapiObject == null) { return(""); } string sProperty = ""; IntPtr pPropValue = IntPtr.Zero; IntPtr IUnknown = IntPtr.Zero; IntPtr IMAPIProperty = IntPtr.Zero; try { MAPIInitialize(IntPtr.Zero); IUnknown = Marshal.GetIUnknownForObject(mapiObject); Guid guidMAPIProp = new Guid(IID_IMAPIProp); if (Marshal.QueryInterface(IUnknown, ref guidMAPIProp, out IMAPIProperty) != 0) { return(""); } try { HrGetOneProp(IMAPIProperty, (uint)proptag, out pPropValue); if (pPropValue == IntPtr.Zero) { return(""); } SPropValue propValue = (SPropValue)Marshal.PtrToStructure(pPropValue, typeof(SPropValue)); sProperty = Marshal.PtrToStringUni(propValue.Value); } catch (System.Exception ex) { throw ex; } } finally { if (pPropValue != IntPtr.Zero) { MAPIFreeBuffer(pPropValue); } if (IMAPIProperty != IntPtr.Zero) { Marshal.Release(IMAPIProperty); } if (IUnknown != IntPtr.Zero) { Marshal.Release(IUnknown); } MAPIUninitialize(); } return(sProperty); }
//~MAPIProp() { } public IPropValue[] GetProps(PropTags[] tags) { uint[] t = new uint[tags.Length + 1]; t[0] = (uint)tags.Length; for (int i = 0; i < tags.Length; i++) t[i + 1] = (uint)tags[i]; IntPtr propVals = IntPtr.Zero; uint count = 0; HRESULT hr = pIMAPIPropGetProps(this.ptr, t, out count, ref propVals); if (hr != HRESULT.S_OK) throw new Exception("GetProps failed: " + hr.ToString()); IPropValue[] props = new IPropValue[count]; uint pProps = (uint)propVals; for (int i = 0; i < count; i++) { pSPropValue lpProp = (pSPropValue)Marshal.PtrToStructure((IntPtr)(pProps + i * cemapi.SizeOfSPropValue), typeof(pSPropValue)); props[i] = new SPropValue(lpProp); } cemapi.MAPIFreeBuffer(propVals); return props; }
/// <summary> /// Use MAPI32.DLL "HrSetOneProp" from managed code /// </summary> /// <param name="attachment"></param> /// <param name="proptag"></param> /// <param name="propertyValue"></param> /// <returns></returns> public static bool SetMAPIProperty(IAttachment attachment, PropTags proptag, string propertyValue) { // Pointer to IUnknown Interface IntPtr IUnknown = IntPtr.Zero; // Pointer to IMAPIProp Interface IntPtr IMAPIProp = IntPtr.Zero; // Structure that will hold the Property Value SPropValue propValue; // A pointer that points to the SPropValue structure IntPtr ptrPropValue = IntPtr.Zero; object mapiObject = attachment.MAPIOBJECT; // if we have no MAPIObject everything is senseless... if (mapiObject == null) { return(false); } try { // We can pass NULL here as parameter, so we do it. MAPIInitialize(IntPtr.Zero); // retrive the IUnknon Interface from our MAPIObject comming from Outlook. IUnknown = Marshal.GetIUnknownForObject(mapiObject); // create a Guid that we pass to retreive the IMAPIProp Interface. Guid guidIMAPIProp = new Guid(IID_IMAPIProp); // try to retrieve the IMAPIProp interface from IMessage Interface, everything else is sensless. if (Marshal.QueryInterface(IUnknown, ref guidIMAPIProp, out IMAPIProp) != 0) { return(false); } // double check, if we wave no pointer, exit... if (IMAPIProp == IntPtr.Zero) { return(false); } // Create structure propValue = new SPropValue(); propValue.propTag = (uint)proptag; // Create Ansi string propValue.Value = Marshal.StringToHGlobalUni(propertyValue); // Create unmanaged memory for structure ptrPropValue = Marshal.AllocHGlobal(Marshal.SizeOf(propValue)); // Copy structure to unmanged memory Marshal.StructureToPtr(propValue, ptrPropValue, false); // Set the property HrSetOneProp(IMAPIProp, ptrPropValue); // Free string Marshal.FreeHGlobal(propValue.Value); IMAPIProp mapiProp = (IMAPIProp)Marshal.GetTypedObjectForIUnknown(IUnknown, typeof(IMAPIProp)); return(mapiProp.SaveChanges(4) == 0); } catch (Exception ex) { LOG.Error(ex); return(false); } finally { // Free used Memory structures if (ptrPropValue != IntPtr.Zero) { Marshal.FreeHGlobal(ptrPropValue); } // cleanup all references to COM Objects if (IMAPIProp != IntPtr.Zero) { Marshal.Release(IMAPIProp); } //if (IMessage != IntPtr.Zero) Marshal.Release(IMessage); if (IUnknown != IntPtr.Zero) { Marshal.Release(IUnknown); } MAPIUninitialize(); } }
/// <summary> /// Uses the IMAPIPROP.SetProps to set the content ID /// </summary> /// <param name="attachment"></param> /// <param name="contentId"></param> public static void SetContentID(IAttachment attachment, string contentId) { // Pointer to IUnknown Interface IntPtr IUnknown = IntPtr.Zero; // Pointer to IMAPIProp Interface IntPtr IMAPIProp = IntPtr.Zero; // A pointer that points to the SPropValue structure IntPtr ptrPropValue = IntPtr.Zero; // Structure that will hold the Property Value SPropValue propValue; // if we have no MAPIObject everything is senseless... if (attachment == null) { return; } try { // We can pass NULL here as parameter, so we do it. MAPIInitialize(IntPtr.Zero); // retrive the IUnknon Interface from our MAPIObject comming from Outlook. IUnknown = Marshal.GetIUnknownForObject(attachment.MAPIOBJECT); IMAPIProp mapiProp = (IMAPIProp)Marshal.GetTypedObjectForIUnknown(IUnknown, typeof(IMAPIProp)); // Create structure propValue = new SPropValue(); propValue.propTag = (uint)PropTags.PR_ATTACH_CONTENT_ID; //propValue.propTag = 0x3712001E; // Create Ansi string propValue.Value = Marshal.StringToHGlobalUni(contentId); // Create unmanaged memory for structure ptrPropValue = Marshal.AllocHGlobal(Marshal.SizeOf(propValue)); // Copy structure to unmanged memory Marshal.StructureToPtr(propValue, ptrPropValue, false); mapiProp.SetProps(1, ptrPropValue, IntPtr.Zero); propValue.propTag = (uint)PropTags.PR_ATTACH_CONTENT_LOCATION; // Copy structure to unmanged memory Marshal.StructureToPtr(propValue, ptrPropValue, false); mapiProp.SetProps(1, ptrPropValue, IntPtr.Zero); // Free string Marshal.FreeHGlobal(propValue.Value); mapiProp.SaveChanges(KEEP_OPEN_READWRITE); } catch (Exception ex) { LOG.Error(ex); } finally { // Free used Memory structures if (ptrPropValue != IntPtr.Zero) { Marshal.FreeHGlobal(ptrPropValue); } // cleanup all references to COM Objects if (IMAPIProp != IntPtr.Zero) { Marshal.Release(IMAPIProp); } //if (IMessage != IntPtr.Zero) Marshal.Release(IMessage); if (IUnknown != IntPtr.Zero) { Marshal.Release(IUnknown); } } }
//This code has been sourced from: //https://www.add-in-express.com/creating-addins-blog/2009/05/08/outlook-exchange-email-address-smtp/ //https://www.add-in-express.com/files/howtos/blog/adx-ol-smtp-address-cs.zip public static string ADX_GetSMTPAddress(string exchangeAddress) { string smtpAddress = string.Empty; IAddrBook addrBook = ADX_GetAddrBook(); if (addrBook != null) { try { IntPtr szPtr = IntPtr.Zero; IntPtr propValuePtr = Marshal.AllocHGlobal(16); IntPtr adrListPtr = Marshal.AllocHGlobal(16); Marshal.WriteInt32(propValuePtr, (int)MAPI.PR_DISPLAY_NAME); Marshal.WriteInt32(new IntPtr(propValuePtr.ToInt32() + 4), 0); szPtr = Marshal.StringToHGlobalAnsi(exchangeAddress); Marshal.WriteInt64(new IntPtr(propValuePtr.ToInt32() + 8), szPtr.ToInt32()); Marshal.WriteInt32(adrListPtr, 1); Marshal.WriteInt32(new IntPtr(adrListPtr.ToInt32() + 4), 0); Marshal.WriteInt32(new IntPtr(adrListPtr.ToInt32() + 8), 1); Marshal.WriteInt32(new IntPtr(adrListPtr.ToInt32() + 12), propValuePtr.ToInt32()); try { if (addrBook.ResolveName(0, MAPI.MAPI_DIALOG, null, adrListPtr) == MAPI.S_OK) { SPropValue spValue = new SPropValue(); int pcount = Marshal.ReadInt32(new IntPtr(adrListPtr.ToInt32() + 8)); IntPtr props = new IntPtr(Marshal.ReadInt32(new IntPtr(adrListPtr.ToInt32() + 12))); for (int i = 0; i < pcount; i++) { spValue = (SPropValue)Marshal.PtrToStructure( new IntPtr(props.ToInt32() + (16 * i)), typeof(SPropValue)); if (spValue.ulPropTag == MAPI.PR_ENTRYID) { IntPtr addrEntryPtr = IntPtr.Zero; IntPtr propAddressPtr = IntPtr.Zero; uint objType = 0; uint cb = (uint)(spValue.Value & 0xFFFFFFFF); IntPtr entryID = new IntPtr((int)(spValue.Value >> 32)); if (addrBook.OpenEntry(cb, entryID, IntPtr.Zero, 0, out objType, out addrEntryPtr) == MAPI.S_OK) { try { if (MAPI.HrGetOneProp(addrEntryPtr, MAPI.PR_EMS_AB_PROXY_ADDRESSES, out propAddressPtr) == MAPI.S_OK) { IntPtr emails = IntPtr.Zero; SPropValue addrValue = (SPropValue)Marshal.PtrToStructure(propAddressPtr, typeof(SPropValue)); int acount = (int)(addrValue.Value & 0xFFFFFFFF); IntPtr pemails = new IntPtr((int)(addrValue.Value >> 32)); for (int j = 0; j < acount; j++) { emails = new IntPtr(Marshal.ReadInt32(new IntPtr(pemails.ToInt32() + (4 * j)))); smtpAddress = Marshal.PtrToStringAnsi(emails); if (smtpAddress.IndexOf("SMTP:") == 0) { smtpAddress = smtpAddress.Substring(5, smtpAddress.Length - 5); break; } } } } finally { if (propAddressPtr != IntPtr.Zero) { Marshal.Release(propAddressPtr); } if (addrEntryPtr != IntPtr.Zero) { Marshal.Release(addrEntryPtr); } } } } } } } finally { Marshal.FreeHGlobal(szPtr); Marshal.FreeHGlobal(propValuePtr); Marshal.FreeHGlobal(adrListPtr); } } finally { Marshal.ReleaseComObject(addrBook); } } return(smtpAddress); }
/// <summary> /// Use MAPI32.DLL "HrSetOneProp" from managed code /// </summary> /// <param name="attachment"></param> /// <param name="proptag"></param> /// <param name="propertyValue"></param> /// <returns></returns> public static bool SetMAPIProperty(Attachment attachment, PropTags proptag, string propertyValue) { // Pointer to IUnknown Interface IntPtr IUnknown = IntPtr.Zero; // Pointer to IMAPIProp Interface IntPtr IMAPIProp = IntPtr.Zero; // Structure that will hold the Property Value SPropValue propValue; // A pointer that points to the SPropValue structure IntPtr ptrPropValue = IntPtr.Zero; object mapiObject = attachment.MAPIOBJECT; // if we have no MAPIObject everything is senseless... if (mapiObject == null) { return false; } try { // We can pass NULL here as parameter, so we do it. MAPIInitialize(IntPtr.Zero); // retrive the IUnknon Interface from our MAPIObject comming from Outlook. IUnknown = Marshal.GetIUnknownForObject(mapiObject); // create a Guid that we pass to retreive the IMAPIProp Interface. Guid guidIMAPIProp = new Guid(IID_IMAPIProp); // try to retrieve the IMAPIProp interface from IMessage Interface, everything else is sensless. if (Marshal.QueryInterface(IUnknown, ref guidIMAPIProp, out IMAPIProp) != 0) { return false; } // double check, if we wave no pointer, exit... if (IMAPIProp == IntPtr.Zero) { return false; } // Create structure propValue = new SPropValue(); propValue.propTag = (uint)proptag; // Create Ansi string propValue.Value = Marshal.StringToHGlobalUni(propertyValue); // Create unmanaged memory for structure ptrPropValue = Marshal.AllocHGlobal(Marshal.SizeOf(propValue)); // Copy structure to unmanged memory Marshal.StructureToPtr(propValue, ptrPropValue, false); // Set the property HrSetOneProp(IMAPIProp, ptrPropValue); // Free string Marshal.FreeHGlobal(propValue.Value); IMAPIProp mapiProp = (IMAPIProp)Marshal.GetTypedObjectForIUnknown(IUnknown, typeof(IMAPIProp)); return mapiProp.SaveChanges(4) == 0; } catch (System.Exception ex) { LOG.Error(ex); return false; } finally { // Free used Memory structures if (ptrPropValue != IntPtr.Zero) Marshal.FreeHGlobal(ptrPropValue); // cleanup all references to COM Objects if (IMAPIProp != IntPtr.Zero) Marshal.Release(IMAPIProp); //if (IMessage != IntPtr.Zero) Marshal.Release(IMessage); if (IUnknown != IntPtr.Zero) Marshal.Release(IUnknown); MAPIUninitialize(); } }
/// <summary> /// Uses the IMAPIPROP.SetProps to set the content ID /// </summary> /// <param name="attachment"></param> /// <param name="contentId"></param> public static void SetContentID(Attachment attachment, string contentId) { // Pointer to IUnknown Interface IntPtr IUnknown = IntPtr.Zero; // Pointer to IMAPIProp Interface IntPtr IMAPIProp = IntPtr.Zero; // A pointer that points to the SPropValue structure IntPtr ptrPropValue = IntPtr.Zero; // Structure that will hold the Property Value SPropValue propValue; // if we have no MAPIObject everything is senseless... if (attachment == null) { return; } try { // We can pass NULL here as parameter, so we do it. MAPIInitialize(IntPtr.Zero); // retrive the IUnknon Interface from our MAPIObject comming from Outlook. IUnknown = Marshal.GetIUnknownForObject(attachment.MAPIOBJECT); IMAPIProp mapiProp = (IMAPIProp)Marshal.GetTypedObjectForIUnknown(IUnknown, typeof(IMAPIProp)); // Create structure propValue = new SPropValue(); propValue.propTag = (uint)PropTags.PR_ATTACH_CONTENT_ID; //propValue.propTag = 0x3712001E; // Create Ansi string propValue.Value = Marshal.StringToHGlobalUni(contentId); // Create unmanaged memory for structure ptrPropValue = Marshal.AllocHGlobal(Marshal.SizeOf(propValue)); // Copy structure to unmanged memory Marshal.StructureToPtr(propValue, ptrPropValue, false); mapiProp.SetProps(1, ptrPropValue, IntPtr.Zero); propValue.propTag = (uint)PropTags.PR_ATTACH_CONTENT_LOCATION; // Copy structure to unmanged memory Marshal.StructureToPtr(propValue, ptrPropValue, false); mapiProp.SetProps(1, ptrPropValue, IntPtr.Zero); // Free string Marshal.FreeHGlobal(propValue.Value); mapiProp.SaveChanges(KEEP_OPEN_READWRITE); } catch (Exception ex) { LOG.Error(ex); } finally { // Free used Memory structures if (ptrPropValue != IntPtr.Zero) Marshal.FreeHGlobal(ptrPropValue); // cleanup all references to COM Objects if (IMAPIProp != IntPtr.Zero) Marshal.Release(IMAPIProp); //if (IMessage != IntPtr.Zero) Marshal.Release(IMessage); if (IUnknown != IntPtr.Zero) Marshal.Release(IUnknown); } }
//This code has been sourced from: //https://www.add-in-express.com/creating-addins-blog/2009/05/08/outlook-exchange-email-address-smtp/ //https://www.add-in-express.com/files/howtos/blog/adx-ol-smtp-address-cs.zip public static string ADX_GetSMTPAddress(string exchangeAddress) { string smtpAddress = string.Empty; IAddrBook addrBook = ADX_GetAddrBook(); if (addrBook != null) try { IntPtr szPtr = IntPtr.Zero; IntPtr propValuePtr = Marshal.AllocHGlobal(16); IntPtr adrListPtr = Marshal.AllocHGlobal(16); Marshal.WriteInt32(propValuePtr, (int)MAPI.PR_DISPLAY_NAME); Marshal.WriteInt32(new IntPtr(propValuePtr.ToInt32() + 4), 0); szPtr = Marshal.StringToHGlobalAnsi(exchangeAddress); Marshal.WriteInt64(new IntPtr(propValuePtr.ToInt32() + 8), szPtr.ToInt32()); Marshal.WriteInt32(adrListPtr, 1); Marshal.WriteInt32(new IntPtr(adrListPtr.ToInt32() + 4), 0); Marshal.WriteInt32(new IntPtr(adrListPtr.ToInt32() + 8), 1); Marshal.WriteInt32(new IntPtr(adrListPtr.ToInt32() + 12), propValuePtr.ToInt32()); try { if (addrBook.ResolveName(0, MAPI.MAPI_DIALOG, null, adrListPtr) == MAPI.S_OK) { SPropValue spValue = new SPropValue(); int pcount = Marshal.ReadInt32(new IntPtr(adrListPtr.ToInt32() + 8)); IntPtr props = new IntPtr(Marshal.ReadInt32(new IntPtr(adrListPtr.ToInt32() + 12))); for (int i = 0; i < pcount; i++) { spValue = (SPropValue)Marshal.PtrToStructure( new IntPtr(props.ToInt32() + (16 * i)), typeof(SPropValue)); if (spValue.ulPropTag == MAPI.PR_ENTRYID) { IntPtr addrEntryPtr = IntPtr.Zero; IntPtr propAddressPtr = IntPtr.Zero; uint objType = 0; uint cb = (uint)(spValue.Value & 0xFFFFFFFF); IntPtr entryID = new IntPtr((int)(spValue.Value >> 32)); if (addrBook.OpenEntry(cb, entryID, IntPtr.Zero, 0, out objType, out addrEntryPtr) == MAPI.S_OK) try { if (MAPI.HrGetOneProp(addrEntryPtr, MAPI.PR_EMS_AB_PROXY_ADDRESSES, out propAddressPtr) == MAPI.S_OK) { IntPtr emails = IntPtr.Zero; SPropValue addrValue = (SPropValue)Marshal.PtrToStructure(propAddressPtr, typeof(SPropValue)); int acount = (int)(addrValue.Value & 0xFFFFFFFF); IntPtr pemails = new IntPtr((int)(addrValue.Value >> 32)); for (int j = 0; j < acount; j++) { emails = new IntPtr(Marshal.ReadInt32(new IntPtr(pemails.ToInt32() + (4 * j)))); smtpAddress = Marshal.PtrToStringAnsi(emails); if (smtpAddress.IndexOf("SMTP:") == 0) { smtpAddress = smtpAddress.Substring(5, smtpAddress.Length - 5); break; } } } } finally { if (propAddressPtr != IntPtr.Zero) Marshal.Release(propAddressPtr); if (addrEntryPtr != IntPtr.Zero) Marshal.Release(addrEntryPtr); } } } } } finally { Marshal.FreeHGlobal(szPtr); Marshal.FreeHGlobal(propValuePtr); Marshal.FreeHGlobal(adrListPtr); } } finally { Marshal.ReleaseComObject(addrBook); } return smtpAddress; }
static int Main(string[] args) { uint PT_UNICODE = 31; uint PT_TSTRING = PT_UNICODE; uint PT_BINARY = 258; uint FL_FULLSTRING = 0x00000000; uint RES_CONTENT = 0x00000003; uint pidProfileMin = 0x6600; uint PR_DISPLAY_NAME_W = PROP_TAG(PT_UNICODE, 0x3001); uint PR_PROFILE_USER_SMTP_EMAIL_ADDRESS = PROP_TAG(PT_TSTRING, pidProfileMin + 0x41); uint PR_PROFILE_USER_SMTP_EMAIL_ADDRESS_W = PROP_TAG(PT_UNICODE, pidProfileMin + 0x41); uint PR_EMSMDB_SECTION_UID = PROP_TAG(PT_BINARY, 0x3D15); uint PR_SERVICE_NAME = PROP_TAG(PT_TSTRING, 0x3D09); uint MAPI_FORCE_ACCESS = 0x80000; Dictionary <uint, string> PropValueMap = new Dictionary <uint, string>() { { PR_DISPLAY_NAME_W, "Anthony" }, { PR_PROFILE_USER_SMTP_EMAIL_ADDRESS_W, "SMTP:[email protected]" } }; int hRes = 0; IProfAdmin lpProfAdmin; IMsgServiceAdmin lpSvcAdmin; IMAPITable lpMsgSvcTable; IntPtr lpSvcRows; List <SPropValue> rgvals = new List <SPropValue>(); SRestriction sres = new SRestriction(); SPropValue SvcProps = new SPropValue(); IProfSect lpGlobalProfSection; IProfSect lpEmsMdbVarProfSect; SPropValue spvSmtpAddressW = new SPropValue(); SPropValue spvDisplayName = new SPropValue(); //enum { iSvcname, iSvcUID, cptaSvc }; //SizedSPropTagArray(cptaSvc, sptCols) = { cptaSvc, PR_SERVICE_NAME, PR_SERVICE_UID }; string profileName = "MAPIProfile"; hRes = MAPIInitialize(IntPtr.Zero); if (hRes != 0) { Console.WriteLine("Error initializing MAPI."); goto error; } hRes = MAPIAdminProfiles( 0, out lpProfAdmin); // Pointer to new IProfAdmin. if (hRes != 0) { Console.WriteLine("Error getting IProfAdmin interface."); goto error; } hRes = lpProfAdmin.CreateProfile( Marshal.StringToHGlobalAnsi(profileName), // Name of new profile. IntPtr.Zero, // Password for profile. IntPtr.Zero, // Handle to parent window. 0); // Flags. if (hRes != 0) { Console.WriteLine("Error creating profile."); goto error; } hRes = lpProfAdmin.AdminServices( Marshal.StringToHGlobalAnsi(profileName), // Profile that we want to modify. IntPtr.Zero, // Password for that profile. 0, // Handle to parent window. 0, // Flags. out lpSvcAdmin); // Pointer to new IMsgServiceAdmin. if (hRes != 0) { Console.WriteLine("Error getting IMsgServiceAdmin interface."); goto error; } hRes = lpSvcAdmin.CreateMsgService( Marshal.StringToHGlobalAnsi("MSEMS"), // Name of service from MAPISVC.INF. IntPtr.Zero, // Display name of service. IntPtr.Zero, // Handle to parent window. 0); // Flags. if (hRes != 0) { Console.WriteLine("Error creating Exchange message service."); goto error; } hRes = lpSvcAdmin.GetMsgServiceTable( 0, // Flags. out lpMsgSvcTable); // Pointer to table. if (hRes != 0) { Console.WriteLine("Error getting Message Service Table."); goto error; } sres.rt = RES_CONTENT; sres.resContent.ulFuzzyLevel = FL_FULLSTRING; sres.resContent.ulPropTag = PR_SERVICE_NAME; IntPtr svcPropsPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(SvcProps)); sres.resContent.lpProp = svcPropsPtr; SvcProps.ulPropTag = PR_SERVICE_NAME; SvcProps.lpszA = "MSEMS"; Marshal.StructureToPtr(SvcProps, svcPropsPtr, true); IntPtr sresPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(sres)); Marshal.StructureToPtr(sres, sresPtr, true); hRes = HrQueryAllRows( lpMsgSvcTable, IntPtr.Zero, sresPtr, IntPtr.Zero, 0, out lpSvcRows); if (hRes != 0) { Console.WriteLine("Error querying table for new message service."); goto error; } Marshal.FreeCoTaskMem(svcPropsPtr); Marshal.FreeCoTaskMem(sresPtr); cleanup: MAPIUninitialize(); return(0); error: Console.WriteLine(string.Format("hRes = {0}", hRes.ToString("X"))); goto cleanup; }
private string GetMAPIProperty(object oMAPIObject, uint uiPropertyTag) { if (oMAPIObject == null) { return(""); } string sProperty = ""; IntPtr pPropValue = IntPtr.Zero; IntPtr IUnknown = IntPtr.Zero; IntPtr IMAPIProperty = IntPtr.Zero; try { MAPIInitialize(IntPtr.Zero); IUnknown = Marshal.GetIUnknownForObject(oMAPIObject); Guid guidMAPIProp = new Guid(IID_IMAPIProp); if (Marshal.QueryInterface(IUnknown, ref guidMAPIProp, out IMAPIProperty) != S_OK) { return(""); } try { HrGetOneProp(IMAPIProperty, uiPropertyTag, out pPropValue); if (pPropValue == IntPtr.Zero) { return(""); } SPropValue propValue = (SPropValue)Marshal.PtrToStructure(pPropValue, typeof(SPropValue)); sProperty = Marshal.PtrToStringAnsi(new IntPtr(propValue.Value)); } catch (System.Exception ex) { throw ex; } } finally { if (pPropValue != IntPtr.Zero) { MAPIFreeBuffer(pPropValue); } if (IMAPIProperty != IntPtr.Zero) { Marshal.Release(IMAPIProperty); } if (IUnknown != IntPtr.Zero) { Marshal.Release(IUnknown); } MAPIUninitialize(); } return(sProperty); }
public SRow[] QueryRows(int lRowCount) { if (lRowCount > MaxQueryRowCount) throw new Exception("Max row count is " + MaxQueryRowCount.ToString()); IntPtr pRowSet = IntPtr.Zero; HRESULT hr = pIMAPITableQueryRows(this.ptr, lRowCount, out pRowSet); if (hr != HRESULT.S_OK) { cemapi.MAPIFreeBuffer(pRowSet); throw new Exception("QueryRows failed: " + hr.ToString()); } uint cRows = (uint)Marshal.ReadInt32(pRowSet); SRow[] sRows = new SRow[cRows]; if (cRows < 1) { FreeProws(pRowSet); return sRows; } int pIntSize = IntPtr.Size, intSize = Marshal.SizeOf(typeof(Int32)); IntPtr rows = (IntPtr)(((uint)pRowSet) + intSize); for (int i = 0; i < cRows; i++) { uint pRowOffset = (uint)(rows) + (uint)(i * SizeOfSRow); uint cValues = (uint)Marshal.ReadInt32((IntPtr)(pRowOffset + intSize)); uint pProps = (uint)Marshal.ReadInt32((IntPtr)(pRowOffset + intSize * 2)); IPropValue[] lpProps = new IPropValue[cValues]; for (int j = 0; j < cValues; j++) // each column { pSPropValue lpProp = (pSPropValue)Marshal.PtrToStructure((IntPtr)(pProps + j * cemapi.SizeOfSPropValue), typeof(pSPropValue)); lpProps[j] = new SPropValue(lpProp); } sRows[i].propVals = lpProps; } FreeProws(pRowSet); return sRows; }