BillyX
asked on
From managed Object* to unmanaged IUnknown
I would like to use Extended MAPI in my C# programm, read a String-Property from a Message.
For this purpose I write a managed/unmanaged C++ DLL.
I have an Object* from mail.MAPIOBJECT which should be of the type IUnknown according to Microsoft documentation.
I need an LPMAPIPROP for HrGetOneProp(LPMAPIPROP pmp, int ulPropTag, LPVOID lppPro) which can be (I'm not sure, but I found it on a webpage) a pointer to a IUnknown.
But what are the lines between?
I tried:
static String* GetStringProperty(Object* MAPIObject, int PropertyNumber) {
String* Val;
char* bufVal;
IntPtr pUnk = Marshal::GetIUnknownForObj ect(MAPIOb ject);
LPMAPIPROP pMsgMAPI = (LPMAPIPROP)pUnk.ToPointer ();
if (pMsgMAPI!=0) {
int hRes = HrGetOneProp(pMsgMAPI, PropertyNumber, (LPBYTE*)&bufVal);
if (bufVal != 0) {
if (hRes == 0) {
Val = Marshal::PtrToStringUni(bu fVal);
}
MAPIFreeBuffer(bufVal);
}
}
return Val;
}
pUnk gets a Value but pMsgMAPI/IUnknown says undefined.
For this purpose I write a managed/unmanaged C++ DLL.
I have an Object* from mail.MAPIOBJECT which should be of the type IUnknown according to Microsoft documentation.
I need an LPMAPIPROP for HrGetOneProp(LPMAPIPROP pmp, int ulPropTag, LPVOID lppPro) which can be (I'm not sure, but I found it on a webpage) a pointer to a IUnknown.
But what are the lines between?
I tried:
static String* GetStringProperty(Object* MAPIObject, int PropertyNumber) {
String* Val;
char* bufVal;
IntPtr pUnk = Marshal::GetIUnknownForObj
LPMAPIPROP pMsgMAPI = (LPMAPIPROP)pUnk.ToPointer
if (pMsgMAPI!=0) {
int hRes = HrGetOneProp(pMsgMAPI, PropertyNumber, (LPBYTE*)&bufVal);
if (bufVal != 0) {
if (hRes == 0) {
Val = Marshal::PtrToStringUni(bu
}
MAPIFreeBuffer(bufVal);
}
}
return Val;
}
pUnk gets a Value but pMsgMAPI/IUnknown says undefined.
ASKER
I tried:
EXTERN_C const GUID DECLSPEC_SELECTANY IMAPIProp = { 0x00020303, 0, 0, { 0xC0, 0, 0, 0, 0, 0, 0, 0x46 } };
EXTERN_C const GUID DECLSPEC_SELECTANY IMAPIMessage = { 0x00020307, 0, 0, { 0xC0, 0, 0, 0, 0, 0, 0, 0x46 } };
Microsoft::Office::Interop ::Outlook: :MailItem * mi = ... ;
Object* MAPIObject;
MAPIObject = mi->MAPIOBJECT;
IUnknown* pUnk = (IUnknown*)Marshal::GetIUn knownForOb ject(MAPIO bject).ToP ointer();
LPMAPIPROP pMsgMAPI;
int res = pUnk->QueryInterface(IMAPI Prop, (void**)&pMsgMAPI);
pUnk->Release();
res gets the value -2147467259 which is MAPI_E_CALL_FAILED
I also tried to convert mi the same way to IMAPIMessage and that returned the same error :-(
EXTERN_C const GUID DECLSPEC_SELECTANY IMAPIProp = { 0x00020303, 0, 0, { 0xC0, 0, 0, 0, 0, 0, 0, 0x46 } };
EXTERN_C const GUID DECLSPEC_SELECTANY IMAPIMessage = { 0x00020307, 0, 0, { 0xC0, 0, 0, 0, 0, 0, 0, 0x46 } };
Microsoft::Office::Interop
Object* MAPIObject;
MAPIObject = mi->MAPIOBJECT;
IUnknown* pUnk = (IUnknown*)Marshal::GetIUn
LPMAPIPROP pMsgMAPI;
int res = pUnk->QueryInterface(IMAPI
pUnk->Release();
res gets the value -2147467259 which is MAPI_E_CALL_FAILED
I also tried to convert mi the same way to IMAPIMessage and that returned the same error :-(
Don't you run this code in windows service or ASP page or MTS or schedulers?
look here:
INFO: Outlook Object Model Unsuitable to Run in a Windows Service
http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/q237/9/13.asp&NoWebContent=1
look here:
INFO: Outlook Object Model Unsuitable to Run in a Windows Service
http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/q237/9/13.asp&NoWebContent=1
ASKER
No, it dosen't.
Also
String* Val;
Val = mi->get_To();
works correctly. It is only the cast of mi->MAPIOBJECT or mi that fails...
Also
String* Val;
Val = mi->get_To();
works correctly. It is only the cast of mi->MAPIOBJECT or mi that fails...
And if you write
Microsoft::Office::Interop ::Outlook: :MailItem * mi = ... ;
HrGetOneProp(mi->MAPIOBJEC T, PropertyNumber, (LPBYTE*)&bufVal);
Do you get compile time error?
Microsoft::Office::Interop
HrGetOneProp(mi->MAPIOBJEC
Do you get compile time error?
ASKER
error C2440: 'type cast' : cannot convert from 'System::Object __gc *' to 'LPMAPIPROP'
ASKER
I have found a solution myself:
http://www.mapilab.com/dev/mapiprop/
This is a library which allows accessing of the mapi-properties directly from .NET. It is also with sourcecode so I should be able compare how they solve this.
http://www.mapilab.com/dev/mapiprop/
This is a library which allows accessing of the mapi-properties directly from .NET. It is also with sourcecode so I should be able compare how they solve this.
ASKER
@Mod Please close question and refund points!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
IUnknown *pUnk = (IUnknown*)Marshal::GetIUn
LPMAPIPROP pMsgMAPI;
pUnk->QueryInterface(&pMsg
pUnk->Release();