Use managed c (.NET) COM DLL in VC
本來想打[原創]~是看對岸博客都這麼用的XD
不過其實也是參考別人就是。
使用第三方dll,貌似從.NET編的,沒有提供其他檔案,只能從VisualStudio物件管理員看class。若在C#,VB等,可以直接加入參考使用。在VC++就沒辦法,觀察後發現該dll是COM元件(ActiveX),於是就由此下手。
其他方法
1. 在VC++中設置CLR選項,使用managed C去寫,選了CLR後,就可以把該library加入參考使用,較為方便,但可能會影響到原有unmanaged C的部分。
2. 用C#再包一層dll,並加入COM interop,生成tlb檔在VC import作為COM使用。
最後解決方案:
1. 利用LoadLibrary找出DllGetClassObject,用IClassFactory取出Interface
#import " Test.dll" no_namespace
CoInitialize(0);
IBaseConnection *baseconn = NULL;
IClassFactory *pCF=NULL;
MYPROC myproc = NULL;
HMODULE hin=LoadLibrary(_T("Test.dll"));
myproc = (MYPROC)GetProcAddress(hin, "DllGetClassObject");
HRESULT hr = (myproc)(__uuidof(BaseConnection),IID_IClassFactory ,(void**)(&pCF)); //參數1放coclass 的uuid
hr = pCF->CreateInstance(NULL,__uuidof(IBaseConnection), (void**)&baseconn);
if(SUCCEEDED(hr)&&baseconn!=NULL)
{
AxAdviseAll(baseconn); //處理event相關,參閱第2點
baseconn->func(); //使用function!
baseconn->Release();
}
CoUninitialize();
2. 為了可以接COM傳來的event,利用IConnectionPointContainer連結,必須實做一個IDispatch去接。可發現event發生時,invoke()函數會被呼叫,在此處理訊息。
HRESULT AxAdviseAll(IUnknown * pUnk)
{
HRESULT hr;
IConnectionPointContainer * pContainer = NULL;
IConnectionPoint * pConnectionPoint=NULL;
IEnumConnectionPoints * pEnum = NULL;
hr = pUnk->QueryInterface(IID_IConnectionPointContainer,(void**)&pContainer);
if (FAILED(hr)) goto error1;
hr = pContainer->EnumConnectionPoints(&pEnum);
if (FAILED(hr)) goto error1;
ULONG uFetched;
while(S_OK == (pEnum->Next(1,&pConnectionPoint,&uFetched)) && uFetched>=1)
{
DWORD dwCookie;
IID iid;
hr = pConnectionPoint->GetConnectionInterface(&iid);
if (FAILED(hr)) iid = IID_NULL;
IUnknown * pSink = new CGeneralEventSink(iid,pUnk);
// CGeneralEventSink 是自己implement的class,參考資料:Win32编程点滴5 - 响应ActiveX控件的事件
hr = pConnectionPoint->Advise(pSink,&dwCookie);
if (FAILED(hr))
{
return hr;
}
pSink->Release();
pConnectionPoint->Release();
pConnectionPoint = NULL;
pSink = NULL;
}
hr = S_OK;
error1:
if (pEnum)pEnum->Release();
if (pContainer) pContainer->Release();
if (pConnectionPoint) pConnectionPoint->Release();
return hr;
}
參考資料:
Win32编程点滴5 - 响应ActiveX控件的事件http://kb.cnblogs.com/a/1563173/ (有source code)
其他參考資料:
Regular DLL Tutor For Beginners
http://www.codeproject.com/KB/DLL/RegDLL.aspx
Calling Managed .NET C# COM Objects from Unmanaged C++ Code
http://www.codeproject.com/KB/cs/ManagedCOM.aspx
http://cppkid.wordpress.com/2009/01/02/how-to-call-a-managed-dll-from-unmanaged-code/
VC++ 调用 C#生成DLL的两种方法(转)
http://xiaoyueweiguang.blog.163.com/blog/static/117267556201082510413610/
Add events to your application with COM connection points http://www.techrepublic.com/article/add-events-to-your-application-with-com-connection-points/1050003
An Overview of Managed/Unmanaged Code Interoperability
http://msdn.microsoft.com/en-us/library/ms973872.aspx (MSDN上有不少COM interop的資料不過對我來說不好用)
http://msdn.microsoft.com/zh-cn/library/ms379603(vs.80).aspx
http://msdn.microsoft.com/en-us/magazine/cc163567.aspx
Dispinterface vs. Events and Runtime Sinks
http://www.codeguru.com/cpp/com-tech/atl/article.php/c3573
COM技术初探(二)http://www.vckbase.com/document/viewdoc/?id=916#2.4 客户端
vc中调用Com组件的方法详解
http://www.cnblogs.com/ylhome/archive/2010/07/10/1774927.html
Introduction to COM Part II - Behind the Scenes of a COM Server
http://www.codeproject.com/KB/COM/comintro2.aspx
A Beginner's Tutorial for Connection Points Using VC++ and ATL
http://www.codeproject.com/KB/COM/connection.aspx
Building COM Objects in C#
http://www.codeproject.com/KB/COM/com_object_in_c_.aspx
[转]C#中的IntPtr类型
http://www.cnblogs.com/freeliver54/archive/2008/10/15/1311371.html
COM 组件设计与应用(楊老師)
http://vckbase.com/vckbase/columnist/yangfeng/
COM技术内幕 (書籍)
留言列表