3

我正在尝试创建一个独立的 Solidworks 应用程序(我希望我的 c++ 程序在 Solidworks 中创建新的几何图形,在后台运行)。我正在使用 msvc++ express 2010。

我已尝试实现此处建议的以下代码

//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;
}

它不会抱怨库的导入语句,但由于以下错误而无法构建:

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

显然我错过了一些东西,但我无法弄清楚那是什么。我觉得它与 ATL 有关,但我不确定......请帮忙。

谢谢

编辑:

好的,我已经下载了 windows development kit 8.0,所有的文件都在那里。我已经在属性页中静态链接到 ATL,我还尝试链接目录中的库文件:C:\Program Files\Windows Kits\8.0\Lib\Atl

但是那些头文件无处可寻……请帮忙。

4

2 回答 2

3

好的,所以我找到了解决方案。它可能不是最优雅的,但它确实有效。

不幸的是,WDK 中的 ATL 目标文件库无济于事,因为找不到头文件。

因此,经过更多挖掘后,我发现完整版的 Visual Studio(不是 Express)允许您使用 ATL 库。事实证明,我很幸运,因为 Microsoft 向学生提供了完整版本的 Visual Studio(请参阅Dreamspark网页),而我恰好是一名学生。:)

因此,在下载、安装和安装任何服务包(我只有一个)之后,我只需要再采取一个步骤来让它工作:

我导航到 Property Pages -> C/C++ -> General 我包含了可以找到 .tlb 文件的目录(在我的例子中是 C:\Program Files\SolidWorks Corp\SolidWorks)

然后我运行以下代码:

//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();
}

就是这样。Solidworks 在程序运行时打开(附加put_Visible功能允许用户查看窗口),并在用户在控制台窗口中按 Enter 键时关闭且不抱怨。

于 2013-04-05T15:43:22.560 回答
0

看起来编译器在查找某些事物的定义时遇到了麻烦,特别CComPtr是作为 ATL 的一部分(正如您所提到的)。我没有使用过 ATL,但是在 Visual Studio 中转到您的项目属性并确保您将“ATL 的使用”设置为适当的静态或动态是否可行?

正如 Jerry 所提到的,您还需要包含相关的标题。

于 2013-02-28T21:33:03.743 回答