Writing an OLE Automation Container with VC++

Posted on 1997-02-04
Last Modified: 2013-12-04
I want to access a simple OLE Automation Server I wrote in VB, but I can't get it working. Ok, I am a relative newbie to OLE programming with VC++, but I think I did everything as described in the books (Brockschmidt etc.), but no.

Anyway, here's what I am doing. I'd be very grateful if anybody has an idea what's wrong.

I first initialize OLE, create the object, get its IDispatch interface and the DISPID for a property called 'Color' with the following statements:



    hr = CoCreateInstance(CLSID_LayoutServer, NULL,                           CLSCTX_LOCAL_SERVER,
                          IID_IDispatch,                           (LPVOID*)&m_pIDispatch);
    hr = NameToID(T2OLE(_T("Color")), &m_DILayoutColor);

This seems to work fine (hr is always 0, I deleted these checks here to save some space). The variables used are defined as follows:

        IDispatch* m_pIDispatch;        //Interface we use
        DISPID     m_DILayoutColor;     //long (I4) Prop

        CLSID       CLSID_LayoutServer;

The NameToID function is just a wrapper around GetIDsOfNames without all the parameters of that function to retrieve DISPID's more easily:

   HRESULT CLayoutClientDlg::NameToID(OLECHAR* pszName,                                          DISPID* pDispID)
         HRESULT hr;

         hr = m_pIDispatch->GetIDsOfNames(IID_NULL,                                      &pszName, 1,
                                     LOCALE_SYSTEM_DEFAULT,                                      pDispID);
         return hr;

Now I should be able to get and put the value of this property. I have the following variables to provide the necessary parameters to Invoke:

        HRESULT       hr;
        DISPPARAMS    dp;
        VARIANTARG    va;
        EXCEPINFO     ei;
        unsigned int  uArgErr;

Also two macros to facilitate the setting of parameters:

   #define SETDISPPARAMS(dp, numArgs, pvArgs, numNamed, pNamed) \


To get the value of the color property, I know call Invoke as follows:

        hr = m_pIDispatch->Invoke(m_DILayoutColor,                                   IID_NULL,
                                  LOCALE_SYSTEM_DEFAULT,                                   DISPATCH_PROPERTYGET,                                   &dp, &va, &ei, &uArgErr);

But, the application crashes with an error message "No handle exception in LayoutClient.exe (Oleaut32.dll): 0xC0000005 Access violation" (this is only a translation
of the original japanese message, so it might look somewhat different on an English system). Why?

Setting the property as follows:

        va.vt = VT_I4;
        va.lVal = 255L;

        SETDISPPARAMS(dp, 1, &va, 0, NULL);
        hr = m_pIDispatch->Invoke(m_DILayoutColor,                                   IID_NULL,
                                  LOCALE_SYSTEM_DEFAULT,                                   DISPATCH_PROPERTYPUT,                                   &dp, &va, &ei, &uArgErr);

This produces no error (hr=0), but it doesn't set the value correctly. If I have a VB client reading the value, it is not 255 (some other, non-constant value). 2 VB clients can exchange data this way (one client setting, one reading).

Any help appreciated, thanks.

Question by:swh062500
1 Comment
LVL 11

Accepted Solution

mikeblas earned 100 total points
ID: 1298755

Your code suffers from a few different problems.

For the GET case:

1) You're not initializing the EXECEPINFO structure.
   You should either pass NULL or memset() the structure
   you send to zeros.
2) You're not initializing the VARIANT for the return value.

For the PUT case:

1) You're not initializing the EXECEPINFO structure.
   You should either pass NULL or memset() the structure
   you send to zeros.
2) You're not initializing the VARIANT for the return value.

For the setup:

1) You should pass IID_IUnknown and separately QI the object
   for IID_IDispatch and not ask CoCreateInstance() for
   IID_IDispatch.  Many objects get sick if created by a query
   for IID_IDistpach instead of IID_IUnknown.

.B ekiM


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Automating Allen Browne's Audit Trail for a large Database 7 52
TFS Branching 4 77
Which Microsoft MSDN Package Is The One I need? 3 42
Is COM supported from Apache 1 44
This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now