With technology sometimes debugging and diagnostics are required, COM is no exception. Presently, I have been debugging a Python COM class that was not instantiating in VBA using the New keyword. That code works now, so watch out for a post on that soon. The following code was to be an appendix to that post but is useful in its own right so I am depositing it here.
Low level C++ Code to instantiate a COM Component
This code is to probe error messages of a COM component instantiation. VBA gives some error codes which are not necessarily useful. In VBA, when an object is created with the New keyword, 'under-the-hood' a call to CoCreateInstance is made, which in turn is made up of calls to CoGetClassObject to get a class factory and then a call to CreateInstance is called on the class factory. These steps which are implicit to VBA are given explicitly below in C++ so one can step through and better diagnose any errors.
In you want to use this code you will no doubt have to change CLSID_FooBar and IID_FooBar for your specific case.
Problems that can be examined with this technique include but are not limited to (a) registration issues, (b) path problems, (c) 32bit/64 bit mismatch problems.
The code is for a C++ console application.
#include <iostream>
#include "objbase.h"
#include <combaseapi.h>
#include <assert.h>
int main()
{
::CoInitialize(0);
HRESULT hr = S_OK;
GUID CLSID_FooBar;
CLSIDFromString(L"{25F9C67B-8DBB-4787-AA84-D3D667ED0457}", &CLSID_FooBar);
GUID IID_FooBar;
CLSIDFromString(L"{B8FFDEFA-3EFB-4725-8CDD-1F6A9E35DD7C}", &IID_FooBar);
GUID IID_IUnknown;
CLSIDFromString(L"{00000000-0000-0000-c000-000000000046", &IID_IUnknown);
GUID IID_IDispatch;
CLSIDFromString(L"{00020400-0000-0000-c000-000000000046", &IID_IDispatch);
GUID IID_IClassFactory;
CLSIDFromString(L"{00000001-0000-0000-c000-000000000046", &IID_IClassFactory);
IClassFactory *pFactoryFooBar;
// CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER
{ // Test 1 Create the FooBar class via class factory requesting IUnknown
hr = ::CoGetClassObject(CLSID_FooBar, CLSCTX_INPROC_SERVER, NULL, IID_IClassFactory, reinterpret_cast<void**>(&pFactoryFooBar));
assert(S_OK == hr);
IUnknown *pUnkFooBar;
hr = pFactoryFooBar->CreateInstance(NULL, IID_IUnknown, reinterpret_cast<void**>(&pUnkFooBar));
assert(S_OK == hr);
IUnknown *pFooBar;
hr = pUnkFooBar->QueryInterface(IID_FooBar, reinterpret_cast<void**>(&pUnkFooBar));
assert(S_OK == hr);
}
IDispatch *pDispFooBar;
hr = CoCreateInstance(CLSID_FooBar, NULL, CLSCTX_INPROC_SERVER, IID_IDispatch,
reinterpret_cast<void**>(&pDispFooBar));
assert(S_OK == hr);
// get disp id
DISPID id = -1; //default
LPOLESTR string = const_cast <LPOLESTR>(L"Sum");
hr = pDispFooBar->GetIDsOfNames(IID_NULL, &string, DISPATCH_METHOD, LOCALE_USER_DEFAULT, &id);
assert(S_OK == hr);
UINT ctinfo = -1;
hr = pDispFooBar->GetTypeInfoCount(&ctinfo);
assert(S_OK == hr);
::CoUninitialize();
}
P.S. It is possible to drill down even further because many COM servers are implemented as DLLs, we could write code to load the DLL into memory and then manually get the entry point DllGetClassFactory and call into it to get the class factory manually. One for another day perhaps because at the moment I am working with Python which does not use Dlls in that sense (at least I don't think so)
No comments:
Post a Comment