Solved

Writing an OLE Automation Container with VC++

Posted on 1997-02-04
1
392 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

733 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