I want to use IMAPIProp :: SaveChanges(ulong) to save changes in the Value i have done using the following code :

private static void SetMessageProperty(object pMAPIObject, uint pPropertyTag, object pValue)


// Pointer to IUnknown Interface

IntPtr IUnknown = NULL;

// Pointer to IMessage Interface

IntPtr IMessage = NULL;

// Pointer to IMAPIProp Interface

IntPtr IMAPIProp = NULL;

// Structure that will hold the Property Value

SPropValue propValue;

// A pointer that points to the SPropValue structure

IntPtr ptrPropValue = NULL;

// if we have no MAPIObject everything is senseless...

if (pMAPIObject == null) return;



// retrieve the IUnknown Interface from our MAPIObject comming from Outlook.

IUnknown = Marshal.GetIUnknownForObject(pMAPIObject);

// since HrGetOneProp needs a IMessage Interface, we must query our IUnknown interface for the IMessage interface.

// create a Guid that we pass to retreive the IMessage Interface.

Guid guidIMessage = new Guid(IID_IMessage);

// try to retrieve the IMessage interface, if we don't get it, everything else is sensless.

if (Marshal.QueryInterface(IUnknown, ref guidIMessage, out IMessage) != S_OK) return;

// 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(IMessage, ref guidIMAPIProp, out IMAPIProp) != S_OK) return;

// double check, if we wave no pointer, exit...

if (IMAPIProp == NULL) return;

// Alloc memory for the text and create a pointer to it

IntPtr ptrToValue = Marshal.StringToHGlobalAnsi(pValue.ToString());

// Create our structure with data

propValue = new SPropValue();

// Which property should be set

propValue.ulPropTag = pPropertyTag;

propValue.dwAlignPad = 0;

//propValue.Value = (long)ptrToValue;

propValue.Value = Convert.ToInt64(pValue);

ptrPropValue = Marshal.AllocHGlobal(Marshal.SizeOf(propValue));

Marshal.StructureToPtr(propValue, ptrPropValue, false);

// try to set the Property ( Property Tags can be found with Outlook Spy from Dmitry Streblechenko )

HrSetOneProp(IMAPIProp, ptrPropValue);



// Only when necessary

if (ptrToValue != NULL)





catch (System.Exception ex)





// Free used Memory structures

if (ptrPropValue != NULL) MAPIFreeBuffer(ptrPropValue);

// cleanup all references to COM Objects

if (IMAPIProp != NULL) Marshal.Release(IMAPIProp);

if (IMessage != NULL) Marshal.Release(IMessage);

if (IUnknown != NULL) Marshal.Release(IUnknown);



I want to know how to write the DLLImport Attribute for Savechanges : I mean the dll to be imported (i think which is MAPI32.dll), EntryPoint ect.

Re: Visual C# Language using DLLImport Attribute


Check out this article Extended MAPI with C# to see if it answers any of your usage questions with mapi.

Re: Visual C# Language using DLLImport Attribute


Thanks for your effort but that doesnt help.

I just need to know how to write DllImport for a dll which is not written in C#.NET

Re: Visual C# Language using DLLImport Attribute

Peter Ritchie

SaveChanges is an interface method, you can't DllImport that; you must call it from the interface pointer.

You need to add a reference to an assembly that includes the IMAPIProp interface (likely an interop assembly). You'll probably need to call Marshal.GetTypedObjectForIUnknown and cast that to a reference to the interface--which can then call SaveChanges.