#import "..\\Debug\\ComInteropDemo.tlb" (路徑需正確)。
#include "stdafx.h"
#include
using namespace std;
#import "..\\Debug\\ComInteropDemo.tlb"
int _tmain(int argc, _TCHAR* argv[])
{
HRESULT hr;
CoInitialize(NULL);
// 建立介面智慧指標
ComInteropDemo::ComInteropInterfacePtr ptr;
hr = ptr.CreateInstance(__uuidof(ComInteropDemo::ComInterop));
if (hr == S_OK)
{
cout << ptr->Add(1.0, 2.0) << endl; // 輸出 3
}
CoUninitialize();
return 0;
}
[Guid]、[ComVisible(true)]。ServicedComponent,介面方法標註 [DispId]。REGASM ComInteropDemo.dll /tlb ComInteropDemo.tlb 產生類型庫;重新建置前需 REGASM /u 解除註冊。_com_ptr_t (如 ComInteropInterfacePtr) 自動管理 AddRef/Release,避免記憶體洩漏。