3

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.

n00begon
  • 3,503
  • 3
  • 29
  • 42
NauticalMile
  • 1,625
  • 3
  • 16
  • 29
  • It looks like you're trying to use ATL types without including the ATL headers that define those types (atlbase.h). – Jerry Coffin Feb 28 '13 at 21:45
  • Check out this [link](http://stackoverflow.com/questions/2681050/atlbase-h-not-found-when-using-visual-c-express-2010) and see it helps to make the project compiled – Andrey.Dankevich Mar 19 '13 at 10:42
  • I actually just ended up using Visual Studio 2010 and it worked out. I'm just looking for some time to answer the question in full. – NauticalMile Mar 22 '13 at 15:57

2 Answers2

3

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.

NauticalMile
  • 1,625
  • 3
  • 16
  • 29
0

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.

aardvarkk
  • 14,955
  • 7
  • 67
  • 96
  • ,@JerryCoffin, you guys are right. However, as has already been indicated in several other SO questions, ATL is not included with express editions of ms visual studio. I am downloading the windows driver kit now (as suggested [here](http://stackoverflow.com/questions/71659/how-to-add-wtl-and-atl-to-visual-studio-c-express-2008?lq=1) ) Let's see if it works... – NauticalMile Feb 28 '13 at 22:09