Solved

Writing an OLE Automation Container with VC++

Posted on 1997-02-04
1
390 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Save copy of document in Word 4 88
Duplicates in Aggregate Query 3 44
Query performance - SQL Server 20 84
Build VS2015 project in bat command 3 93
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…

856 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