Connecting to SolidWorks via API fails in 2012?

Hi there,

I've written a C++ program to connect an out-of-process application to SolidWorks, and this program works just fine on computers running SolidWorks 2011 Standard. However, one of my colleagues has SolidWorks 2012 Premium, and the same code is failing. The code is simply this:

CComPtr swApp;

HRESULT hres = NOERROR;

CoInitialize(NULL);

hres = swApp.CoCreateInstance(_uuidof(SldWorks), NULL, CLSCTX_LOCAL_SERVER);

The return value I'm getting for hres on my colleague's computer is -2147221164. Any ideas? Thank you!

SolidworksApi macros
Answers 1
SF
SF 2012-02-24 Best Answer

After discussions with SW API support, I've found that the solution to the above issue is to use the following code:

CComPtr swApp;

CLSID clsid;

HRESULT hres = NOERROR;

CoInitialize(NULL); 

CLSIDFromProgID(OLESTR("SldWorks.Application"), &clsid);

hres = swApp.CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER);

Can anyone help me to determine why the above code - in which the program id is obtained using CLSIDFromProgID - would work when _uuidof() does not? And why would this be different for SolidWorks 2011 and 2012? Thank you!