/// <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); }
public bool SetProps(PropTags[] tags, object[] values) { int num = tags.Length; if (num != values.Length) throw new Exception("Num tags must be same as num of values!!"); IntPtr array = Marshal.AllocHGlobal(cemapi.SizeOfSPropValue * num); for (int i = 0; i < num; i++) { PropTags tag = tags[i]; object value = values[i]; pSPropValue val = new pSPropValue(); val.ulPropTag = (uint)tag; switch ((PT)((uint)tag & 0xFFFF)) { case PT.PT_BINARY: case PT.PT_TSTRING: throw new Exception("Can't set property " + tag.ToString() + '!'); case PT.PT_BOOLEAN: val.Value.li = (bool)value ? 1UL : 0UL; break; case PT.PT_SYSTIME: case PT.PT_I8: val.Value.li = (ulong)value; break; case PT.PT_I2: val.Value.li = (ulong)((short)value); break; case PT.PT_LONG: val.Value.li = (ulong)((uint)value); break; default: throw new Exception("Can't set property " + tag.ToString() + '!'); } Marshal.StructureToPtr(val, (IntPtr)((uint)array + cemapi.SizeOfSPropValue * i), false); } HRESULT hr = pIMAPIPropSetProps(this.ptr, (uint)num, array); Marshal.FreeHGlobal(array); if (hr == HRESULT.S_OK) return true; else if (hr == HRESULT.MAPI_E_COMPUTED) return false; else throw new Exception("SetProps failed: " + hr.ToString()); }
//~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> /// 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> /// 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; }
/// <summary> /// Defines the particular properties and order of properties to appear as columns in the table. /// </summary> /// <param name="tags">An array of property tags identifying properties to be included as columns in the table</param> /// <returns></returns> public bool SetColumns(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]; return tb_.SetColumns(t, 0) == HRESULT.S_OK; }
public SPropValue(pSPropValue prop) { this.tag = (PropTags)prop.ulPropTag; switch ((PT)((uint)this.tag & 0xFFFF)) { case PT.PT_TSTRING: this.t = typeof(string); this.str = Marshal.PtrToStringUni(prop.Value.lpszW); break; case PT.PT_LONG: case PT.PT_I2: case PT.PT_BOOLEAN: this.t = typeof(int); this.ul = prop.Value.ul; break; case PT.PT_BINARY: this.t = typeof(SBinary); this.binary = prop.Value.bin.AsBytes; break; case PT.PT_SYSTIME: case PT.PT_I8: this.t = typeof(UInt64); this.p_li = prop.Value.li; break; default: this.t = null; break; } }
public void SortRows(PropTags tag, TableSortOrder order) { int sizeS = Marshal.SizeOf(typeof(SSortOrder)); IntPtr sortArray = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(uint)) * 3 + sizeS); Marshal.WriteInt32(sortArray, 1); Marshal.WriteInt64(sortArray, Marshal.SizeOf(typeof(uint)), 0); SSortOrder s; s.ulOrder = order; s.ulPropTag = tag; Marshal.StructureToPtr(s, (IntPtr)(((uint)sortArray) + Marshal.SizeOf(typeof(uint)) * 3), false); HRESULT hr = pIMAPITableSortTable(this.ptr, sortArray); if (hr != HRESULT.S_OK) throw new Exception("pIMAPITableSetColumns failed: " + hr.ToString()); Marshal.FreeHGlobal(sortArray); }
public void SetColumns(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]; HRESULT hr = pIMAPITableSetColumns(this.ptr, t); //if (hr != HRESULT.S_OK) // throw new Exception("pIMAPITableSetColumns failed: " + hr.ToString()); }