契约接口:IDTExtensibility2
插件:实现了IDTExtensibility2接口
问题1:如何做一个C++宿主程序,加载该插件?
问题2:在CoCreateInstance时,返回no support such interface,但是注册表中有插件CLSID,也有接口的CLSID。
契约接口:ATL项目-ATLProject1
定义.idl接口,代码片段如下
[
object,
uuid(BB5F5302-CAD2-4A1C-B701-D4C127B2563F),
dual,
nonextensible,
pointer_default(unique)
]
interface IDTExtensibility2 : IDispatch{
[id(1)] HRESULT OnConnection([in] IDispatch* application, [in] enum ext_ConnectMode ConnectMode, [in] IDispatch* AddInInst, [in] SAFEARRAY(BSTR)* custom);
};
插件:C#类库-ClassLibrary1
引入ATLProject生成的COM库,实现IDTExtensibility2接口,代码片段如下
using ATLProject1Lib;
namespace ClassLibrary1
{
public class Class1 : IDTExtensibility2
{
public Class1() { }
public void OnConnection(object Application, ext_ConnectMode ConnectMode, object AddInInst, ref Array custom)
{
MessageBox.Show("OnConnection");
}
}
}
宿主:C++项目-
代码片段如下
#include "..\ATLProject1\ATLProject1_i.c"
#include "..\ATLProject1\ATLProject1_i.h"_
int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);
CLSID clsid;
HRESULT hResult = CLSIDFromProgID(OLESTR("ClassLibrary1.Class1"), &clsid);
CComPtr<IDTExtensibility2> pFirstClass;//智能指针
hResult = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, IID_IDTExtensibility2, (void**)&pFirstClass);
IDispatch* application = NULL;
enum ext_ConnectMode ConnectMode = ext_cm_AfterStartup;
IDispatch* AddInInst = NULL;
SAFEARRAY** custom = NULL;
hResult = pFirstClass->OnConnection(application, ConnectMode, AddInInst, custom);
pFirstClass.Release();
CoUninitialize();
return 0;
}