2013-02-28 164 views
2

我試圖創建一個獨立的Solidworks應用程序(我想讓我的C++程序在後臺運行的Solidworks中創建新的幾何體)。我使用的是MSVC++表示2010年在C++中創建獨立的Solidworks應用程序

我試圖實現下面的代碼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; 
} 

它不抱怨的導入語句庫,但它不會建立由於以下錯誤:

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開發工具包8.0,所有的文件都在那裏。我已經在屬性頁中靜態鏈接到ATL,我也嘗試鏈接目錄中的庫文件:C:\Program Files\Windows Kits\8.0\Lib\Atl

但是這些頭文件無處可查...請幫助。

+0

它看起來像你試圖使用ATL類型,而不包括定義這些類型的ATL頭文件(atlbase.h)。 – 2013-02-28 21:45:26

+0

看看這個[鏈接](http://stackoverflow.com/questions/2681050/atlbase-h-not-found-when-using-visual-c-express-2010),看到它有助於使項目編譯 – 2013-03-19 10:42:08

+0

我其實剛剛結束了使用Visual Studio 2010,它解決了。我只是想找點時間來回答這個問題。 – NauticalMile 2013-03-22 15:57:58

回答

2

好的,所以我找到了解決方案。它可能不是最優雅的,但它的工作原理。

不幸的是,WDK中的ATL對象文件庫沒有幫助,因爲無法找到頭文件。

因此,經過更多挖掘,我發現完整版本的Visual Studio(不是Express)允許您使用ATL庫。我很幸運,因爲微軟給學生們提供了完整版本的視覺工作室(見Dreamspark網頁),而我恰好是一名學生。 :)

所以下載,安裝,以及安裝任何服務包(我剛做了一個),只是有一個步驟我需要採取讓它開始工作後:

我瀏覽到屬性頁 - > C/C++ - >常規 我包括在.TLB文件可以找到的目錄:

然後,我跑到下面的代碼:

//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(); 
} 
(在我的情況C \ Program Files文件\ SolidWorks的公司目錄\ SolidWorks)

就是這樣。程序運行時Solidworks會打開(額外的put_Visible函數允許用戶看到窗口),並在用戶按下控制檯窗口中的Enter時不會發出抱怨而關閉。

0

看起來編譯器在找到某些東西的定義時遇到問題,特別是ATL中的一部分CComPtr(正如您所提到的)。我沒有和ATL一起工作,但是它可以在Visual Studio中使用你的項目屬性,並確保你有「使用ATL」設置爲靜態或動態?

正如傑裏提到的,你還需要包含相關的標題。

+0

,@ JerryCoffin,你們是對的。但是,正如其他幾個SO問題中已經指出的,ATL不包含在ms visual studio的快速版本中。我正在下載Windows驅動程序工具包(按照建議[這裏](http://stackoverflow.com/questions/71659/how-to-add-wtl-and-atl-to-visual-studio-c-express-2008 ?lq = 1))讓我們看看它是否工作... – NauticalMile 2013-02-28 22:09:15