Solved

From managed Object* to unmanaged IUnknown

Posted on 2004-04-09
10
744 Views
Last Modified: 2013-12-03
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::GetIUnknownForObject(MAPIObject);
       LPMAPIPROP pMsgMAPI = (LPMAPIPROP)pUnk.ToPointer();
       if (pMsgMAPI!=0) {
         int hRes = HrGetOneProp(pMsgMAPI, PropertyNumber, (LPBYTE*)&bufVal);
         if (bufVal != 0) {
           if (hRes == 0) {
             Val = Marshal::PtrToStringUni(bufVal);
           }
           MAPIFreeBuffer(bufVal);
         }
       }
       return Val;
     }

pUnk gets a Value but pMsgMAPI/IUnknown says undefined.

0
Comment
Question by:BillyX
  • 5
  • 3
10 Comments
 
LVL 8

Expert Comment

by:RomanPetrenko
ID: 10826295
Try this code:

IUnknown *pUnk = (IUnknown*)Marshal::GetIUnknownForObject(MAPIObject).ToPointer();
LPMAPIPROP pMsgMAPI;
pUnk->QueryInterface(&pMsgMAPI); // or pUnk->QueryInterface(<GUID_OF_MAPIPROP>, (void**)&pMsgMAPI);
pUnk->Release();
0
 

Author Comment

by:BillyX
ID: 10829106
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::GetIUnknownForObject(MAPIObject).ToPointer();
LPMAPIPROP pMsgMAPI;
int res = pUnk->QueryInterface(IMAPIProp, (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 :-(
0
 
LVL 8

Expert Comment

by:RomanPetrenko
ID: 10830749
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
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:BillyX
ID: 10832376
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...
0
 
LVL 8

Expert Comment

by:RomanPetrenko
ID: 10832831
And if you write
Microsoft::Office::Interop::Outlook::MailItem * mi = ... ;
HrGetOneProp(mi->MAPIOBJECT, PropertyNumber, (LPBYTE*)&bufVal);
Do you get compile time error?
0
 

Author Comment

by:BillyX
ID: 10834345
error C2440: 'type cast' : cannot convert from 'System::Object __gc *' to 'LPMAPIPROP'
0
 

Author Comment

by:BillyX
ID: 10873411
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.
0
 

Author Comment

by:BillyX
ID: 10873636
@Mod Please close question and refund points!
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 11249525
PAQed, with points refunded (250)

modulo
Community Support Moderator
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question