用c++创建一个独立的Solidworks应用程序
Creating a standalone Solidworks application in c++
我正在尝试创建一个独立的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;
}
它不会抱怨库的import语句,但由于以下错误而无法构建:
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:usersnolandocumentsc++solidworks_testsolidworks_testdebugsldworks.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开发工具包8.0,所有的文件都在那里。我在属性页中静态链接到ATL,我还尝试链接目录中的库文件:C:Program FilesWindows Kits8.0LibAtl
但是这些头文件无处可寻…请帮助。
好的,我找到了一个解决方案。这可能不是最优雅的,但它是有效的。
不幸的是,WDK中的ATL对象文件库没有帮助,因为头文件无处可寻。
因此,经过更多的挖掘,我发现Visual Studio的完整版本(不是Express)允许您使用ATL库。我很幸运,因为微软向学生提供了visual studio的完整版本(参见Dreamspark网页),而我恰好是一名学生。:)
所以在下载、安装和安装任何服务包(我只有一个)之后,我只需要再做一步就可以让它工作了:
我导航到属性页-> C/c++ ->通用我包含了可以找到.tlb文件的目录(在我的情况下是C:Program FilesSolidWorks CorpSolidWorks)
然后运行以下代码:
//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键时关闭而没有抱怨。
看起来编译器很难找到某些东西的定义,特别是CComPtr
,这是ATL的一部分(如您所提到的)。我没有使用ATL,但它会工作去你的项目属性在Visual Studio,并确保你有"使用ATL"设置为静态或动态适当?
正如Jerry所提到的,您还需要包含相关的标题。
- C 中是否有一个无法更改的时间戳?(Internet独立)
- 一个使用fortran/C/C++的独立软件
- 打开文本文件并读取一个带有空格的字符串,然后将两个独立的int放入一个结构中
- 为什么std::whatever::erase()是一个方法,std::remove()是独立函数
- 执行一个新的完全独立的进程,该进程封装在一个可执行文件中,而不需要为独立进程将文件写入磁盘
- 如何使用生成文件独立编译和更新一个文件夹中的所有文件
- 创建一个独立的gtkmm对话框
- 我应该把这些函数放在一个独立的.h文件中吗
- c++,类,向量,优化:多个独立向量vs一个类向量
- 一个应用程序实例,独立于平台,C++11
- 如何将两个独立的boost::bind()组合为一个boost::函数
- 我需要一个Windows 8 WinRT应用程序来与用C编写的独立应用程序通信
- 在一个独立的C++程序中编译用C++编写的Festival代码的部分
- 是否有一个易于使用的,独立于平台的包,我们可以用它来读取&在c++下编写gzip流
- 如何在c++中使用qmake构建一个独立的共享库
- 函数应该在两个类中的一个/两个中运行,还是独立运行?
- 如何将两个独立的c++文件合并到一个程序中
- 用c++插件为nodejs应用程序构建一个独立的可执行文件
- 使用cmake将第三方静态库组合成一个独立的静态库
- 懒惰的程序员将vbscript/hta文件包装在一个独立的静态链接的c++ exe中