Issue
When I use an ActiveX client application on a computer that has AutoCAD based products installed, I don't know which 'AutoCAD' I am accessing. Is there a way of determining this?
Solution
The following code obtains the running (or creates a new instance if none exists) of AutoCAD. Its version is determined by the registry key.
HKEY_CLASSES_ROOT\AutoCAD.Application
CComQIPtr<IAcadApplication, &IID_IAcadApplication> gpApp;
HRESULT startAcad()
{
HRESULT hr = S_OK;
CLSID clsid;
//get the current version's CLSID
hr = CLSIDFromProgID(L"AutoCAD.Application", &clsid);
if(FAILED(hr))
return hr;
CComPtr<IUnknown> pIUnk;
hr = GetActiveObject(clsid, NULL, &pIUnk);
if(!pIUnk)
{
hr = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER,
IID_IAcadApplication, (void**)&gpApp);
}
else
gpApp = pIUnk;
if(FAILED(hr) || !gpApp)
return hr;
gpApp->put_Visible(VARIANT_TRUE);
return hr;
}
Therefore, if CoCreateInstance() on the CLSID we got from CLSIDFromProgID() of "AutoCAD.Application" succeeded, we know AutoCAD is running.
The problem is that all the other family products that have an
AutoCAD engine use the same ProID, "AutoCAD.Application" thus one can't tell which family member receives the client's request.
However, each family product has its own Automation Extension with specific interface progID, and it's this information that determines which AutoCAD based program is running.
For example, once you have the IAcadApplication object, you can call its GetInterfaceObject() method.
IAcadApplication.GetInterfaceObject("AutoCADMap.Application")