How to start Outlook from unmanaged VC++ Code?

814 views Asked by At

Long story: I've managed quite a few tasks with the extended Mapi libary from http://www.codeproject.com/Articles/10881/MAPIEx-Extended-MAPI-Wrapper. The problem is, it does not send the actual messages if Outlook is not running, it stores the messages in the outbox though, and to send them with a libary function require a login with a profile which I'll not alwas have.

Short story: I need to start an installed Outlook (did already check) with its default profault profile so that it can send the messages in it's outbox, if any.

There are very mice and slick solutions in C#, 2 to 6 lines of Code but unfortunately I have to stick to unmanaged VC++.

First try: I've already done quite a few things with the WIN32 OLE Interface and Microsoft Word. This time I only want the program to start, if not already running. So I tried

// Outlook Test
CoInitialize(NULL);
CLSID clsid;
HRESULT hr = CLSIDFromProgID(L"Outlook.Application", &clsid);

IDispatch *pOApp;
    if(SUCCEEDED(hr))
{
    IUnknown *pUnk;
    hr = GetActiveObject(clsid, NULL, (IUnknown**)&pUnk);
    if(FAILED(hr)) 
    {
        hr = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER, 
                               IID_IDispatch, (void **)&pOApp);
        if(FAILED(hr)) pOApp=NULL;
    }
    else if(pUnk) 
    {
        hr = pUnk->QueryInterface(IID_IDispatch, (void **)&pOApp);
        if(FAILED(hr)) pOApp=NULL;
    }
}

But something is still missing.

I really appreciate any help you can provide

2

There are 2 answers

0
Eugene Astafiev On

You may consider using a raw Extended MAPI code in unmanaged C++ without even automating Outlook. Moreover, in case of using the Outlook object model you will get a security prompt which the Send method triggers. You can read about this in the Outlook "Object Model Guard" Security Issues for Developers article.

See How To Create a Message With an Attachment and Send It Using MAPI/VC++ for the sample in C++ (Extended MAPI).

0
Dmitry Streblechenko On

Import the Outlook type library (#import) so you can work with Outlook specific interfaces (such as Application).

#import "libid:00062FFF-0000-0000-C000-000000000046" rename_namespace("Outlook") raw_interfaces_only

Or you can use late binding and call IDispatch::GetIdsOfNames / Invoke. You can see the properties / methods / events and their dispids in OutlookSpy (I am its author - click, for example, Application button on the OutlookSpy ribbon in Outlook).