Microsoft KB Archive/268981

= PRB: Microsoft Virtual Machine Generates DISPIDs on the Fly =

Article ID: 268981

Article Last Modified on 6/14/2006

-

APPLIES TO


 * Microsoft Java Virtual Machine

-



This article was previously published under Q268981



SYMPTOMS
When you use the Auto-IDispatch feature of the Microsoft virtual machine for Java (Microsoft VM) to implement a Component Object Model (COM) object in Java, the Microsoft VM may dynamically generate DISPIDs on the fly and ignore any DISPIDs that are specified in a type library. In particular, the Microsoft VM dynamically generates DISPIDs for objects that are created in Java and passed to a COM method.

As a result, when users call a Java COM object from Microsoft Visual C++ or Microsoft Visual Basic, incorrect methods may be called or the calls may fail altogether.



RESOLUTION
To work around this problem, you can call the GetIDsOfNames function or implement a dual interface to call a COM object that is implemented in Java.

Call the GetIDsOfNames Function
Call the GetIDsOfNames function. For languages that do not automatically call GetIDsOfNames, such as Visual C++, you must call the function yourself. GetIDsOfNames queries the COM object for the DISPIDs for one or more method names. GetIDsOfNames takes the following parameters:

REFIID            riid       Must be IID_NULL OLECHAR FAR* FAR* rgszNames  Array of names to be mapped unsigned int      cNames     Number of names to be mapped LCID              lcid       Locale DISPID FAR*       rgDispId   Caller allocated array of DISPIDs

The first parameter is an array of names to be mapped. The first name must be the name of a method; all other names are for parameters to the named method. The following sample code illustrates how to call GetIDsOfNames: HRESULT hresult; IDispatch FAR* pdisp = (IDispatch FAR*)NULL; DISPID dispid; OLECHAR FAR* szMember = &quot;color&quot;;

// Code that sets a pointer to the dispatch (pdisp) is omitted.

hresult = pdisp->GetIDsOfNames(IID_NULL,&szMember,1, LOCALE_SYSTEM_DEFAULT,&dispid); After you have the DISPIDs, you can call IDispatch->Invoke to call a particular method. Invoke takes the following parameters:

DISPID            dispIdMember  DISPID of method to be called REFIID            riid          Must be IID_NULL LCID              lcid          Locale WORD              wFlags        Flag that specifies the type of call DISPPARAMS FAR*   pDispParams   Array of DISPPARAMs that contain parameters to method VARIANT FAR*      pVarResult    Location for storage of return value EXCEPINFO FAR*    pExcepInfo    Exception info structure or NULL unsigned int FAR* puArgErg      Index of error in param array

The sample code appears as follows: int nArgErr; pdisp->Invoke(szMember, IID_NULL, LOCALE_SYSTEM_DEFAULT,DISPATCH_METHOD, NULL, NULL, &nArgErr);

Implement a Dual Interface
You can implement your COM object with a dual interface so that when you call an application, you bypass the Auto-IDispatch mechanism altogether. To do this, write an .idl file and compile it into a type library. Then, in the Microsoft Visual J++ project settings, click Use existing type library, and specify your typelib.

