Domanda

I'm trying to create a standalone Solidworks application (I want my c++ program to create new geometry in Solidworks, running in the background). I'm using msvc++ express 2010.

I have tried to implement the following code suggested here

//Import the SolidWorks type library
#import "sldworks.tlb" raw_interfaces_only, raw_native_types, no_namespace, named_guids

//Import the SolidWorks constant type library    
#import "swconst.tlb"  raw_interfaces_only, raw_native_types, no_namespace, named_guids  

int _tmain(int argc, _TCHAR* argv[])  
{
//Initialize COM
CoInitialize(NULL);

//Use ATL smart pointers       
CComPtr<ISldWorks> swApp;

//Create an instance of SolidWorks
HRESULT hres = swApp.CoCreateInstance(__uuidof(SldWorks), NULL, CLSCTX_LOCAL_SERVER);

//My Code here

//Shut down SolidWorks    
swApp->ExitApp();

// Release COM reference
swApp = NULL;

//Uninitialize COM
CoUninitialize();

return 0;
}

It doesn't complain about the import statements for the libraries but it won't build due to the following errors:

1>main.cpp(19): error C2065: 'CComPtr' : undeclared identifier
1>main.cpp(19): error C2275: 'ISldWorks' : illegal use of this type as an expression
1>          c:\users\nolan\documents\c++\solidworks_test\solidworks_test\debug\sldworks.tlh(7515) : see declaration of 'ISldWorks'
1>main.cpp(19): error C2065: 'swApp' : undeclared identifier
1>main.cpp(22): error C2065: 'swApp' : undeclared identifier
1>main.cpp(22): error C2228: left of '.CoCreateInstance' must have class/struct/union
1>          type is ''unknown-type''
1>main.cpp(26): error C2065: 'swApp' : undeclared identifier
1>main.cpp(26): error C2227: left of '->ExitApp' must point to class/struct/union/generic type
1>          type is ''unknown-type''
1>main.cpp(29): error C2065: 'swApp' : undeclared identifier

Obviously I am missing something, but I can't figure out what that is. I feel like it has something to do with ATL but I'm not sure... Please help.

Thanks

EDIT:

Ok I've downloaded the windows development kit 8.0 and all the files are there. I've statically linked to ATL in the property pages, I've also tried linking the library files in the directory: C:\Program Files\Windows Kits\8.0\Lib\Atl

But those header files are nowhere to be found... please help.

È stato utile?

Soluzione

Ok, so I found a solution. It may not be the most elegant, but it works.

Unfortunately, the ATL object file libraries in the WDK do not help, as the header files are nowhere to be found.

So after more digging I found out that the full version of Visual Studio (not Express) allows you to use the ATL library. I got lucky as it turns out because Microsoft gives full versions of visual studio out to students (see the Dreamspark webpage), and I happen to be a student. :)

So after downloading, installing, and installing any service packs (I just had one) there was just one more step I needed to take to get it to work:

I navigated to Property Pages -> C/C++ -> General I included the directory where the .tlb files can be found (In my case C:\Program Files\SolidWorks Corp\SolidWorks)

Then I ran the following code:

//main.cpp

#include <afxwin.h>
#include <iostream>

#import "sldworks.tlb"

void main()  
{
//Initialize COM
CoInitialize(NULL);

//Use ATL smart pointers       
CComPtr<SldWorks::ISldWorks> swApp;

//Create an instance of SolidWorks
HRESULT hres = swApp.CoCreateInstance(__uuidof(SldWorks), NULL, CLSCTX_LOCAL_SERVER);

//Make the instance visible to the user
swApp->put_Visible(VARIANT_TRUE);
std::cin.get();

//Shut down SolidWorks    
swApp->ExitApp();

// Release COM reference
swApp = NULL;

//Uninitialize COM
CoUninitialize();
}

And that was that. Solidworks opens when the program is run (the additional put_Visible function allows the user to see the window), and closes without complaining when the user presses enter in the console window.

Altri suggerimenti

It looks like the compiler is having trouble finding definitions for certain things, specifically the CComPtr which is part of ATL (as you mentioned). I haven't worked with ATL, but would it work to go to your project properties in Visual Studio and make sure you have "Use of ATL" set to either Static or Dynamic as appropriate?

As Jerry mentioned, you'll also need to include the relevant headers.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top