Solved

Writing an OLE Automation Container with VC++

Posted on 1997-02-04
1
382 Views
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:

    CoInitialize(NULL);

    hr=::CLSIDFromProgID(T2COLE(_T("LayoutServer.Layout")),
                         &CLSID_LayoutServer);

    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) \
       {\
       (dp).cArgs=numArgs;\
       (dp).rgvarg=pvArgs;\
       (dp).cNamedArgs=numNamed;\
       (dp).rgdispidNamedArgs=pNamed;\
       }

   #define SETNOPARAMS(dp) SETDISPPARAMS(dp, 0, NULL, 0, NULL)

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

        SETNOPARAMS(dp);
        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.

-Stephan
0
Comment
Question by:swh062500
1 Comment
 
LVL 11

Accepted Solution

by:
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


   
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
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…
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 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

12 Experts available now in Live!

Get 1:1 Help Now