由于 我在开发 CF_CAN 设备的时候,需要采集CAN 总线的数据,由于使用了DeviceIoControl 直接操作,使的用户协议,与 CF_CAN 协议无法分开,我打算用DLL 的方式,把用户协议 封装到的DLL 里去,但在处理 CF_CAN卡,主动接收数据,来响应消息,处理DLL 回调,和资源分配上有一定的困难,所以我试一试用COM 的方式来处理。
1。首先 按照 vs2005 c++ 的向导 创建一个 ATL Smart Device Project,我这里用的 CANBUSDEV 名称。Build 一下。
2。在CANBUSDEV 项目上 右击,添加一个 ALT Simple Object 到项目中,我这里 用的是 CANCOM 名称。Build 一下。
3,打开 cancom.h 文件
//自动创建的文件
#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
#error "Single-threaded COM objects are not properly supported on Windows CE platform, such as the Windows Mobile platforms that do not include full DCOM support. Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating single-thread COM object's and allow use of it's single-threaded COM object implementations. The threading model in your rgs file was set to 'Free' as that is the only threading model supported in non DCOM Windows CE platforms."
#endif
// CCANCOM
class ATL_NO_VTABLE CCANCOM :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<CCANCOM, &CLSID_CANCOM>,
public IDispatchImpl<ICANCOM, &IID_ICANCOM, &LIBID_TestCOMLib, /*wMajor =*/ 1, /*wMinor =*/ 0>
{
public:
CCANCOM()
{
}
#ifndef _CE_DCOM
DECLARE_REGISTRY_RESOURCEID(IDR_CANCOM)
#endif
BEGIN_COM_MAP(CCANCOM)
COM_INTERFACE_ENTRY(ICANCOM)
COM_INTERFACE_ENTRY(IDispatch)
END_COM_MAP()
DECLARE_PROTECT_FINAL_CONSTRUCT()
HRESULT FinalConstruct()
{
return S_OK;
}
void FinalRelease()
{
}
public:
};
OBJECT_ENTRY_AUTO(__uuidof(CANCOM), CCANCOM)
//修改后文件
// CCANCOM
class ATL_NO_VTABLE CCANCOM :
public CComObjectRootEx<CComMultiThreadModel>,
public CComCoClass<CCANCOM, &CLSID_CANCOM>,
public ICANCOM
{
public:
CCANCOM()
{
}
#ifndef _CE_DCOM
DECLARE_REGISTRY_RESOURCEID(IDR_CANCOM)
#endif
BEGIN_COM_MAP(CCANCOM)
COM_INTERFACE_ENTRY(ICANCOM)
END_COM_MAP()
DECLARE_PROTECT_FINAL_CONSTRUCT()
HRESULT FinalConstruct()
{
return S_OK;
}
void FinalRelease()
{
}
public:
};
OBJECT_ENTRY_AUTO(__uuidof(CANCOM), CCANCOM)
4。打开 canbusdev.idl 文件
interface ICANCOM : IDispatch{ //改成
interface ICANCOM : IUnknown{
5。把原来的修改成这样,如果原来有其他接口在里面,删除掉,有接口参数也删除掉。在COM 注册的时候,回自动把要用的接口注册上去的。
library CANBUSDEVLib
{
importlib("stdole2.tlb");
[
uuid(A24D2862-27BA-482C-9AFB-C157B550028E),
helpstring("CANCOM Class")
]
coclass CANCOM
{
[default] interface ICANCOM;
};
};
6。添加 一些方法,和 回调函数 在 canbusdev.idl 文件里
[
object,
uuid(1E728E41-182E-457C-AE89-AC83916FFCDF),
dual,
nonextensible,
helpstring("IShowMsg Interface"),
pointer_default(unique)
]
interface IShowMsg : IUnknown{
[, helpstring("method ShowMsg")] HRESULT ShowMsg([in] LPTSTR pMsg);
};
[
object,
uuid(77A4BB80-DAE0-4772-89FD-CA2B0A7B7FCF),
dual,
nonextensible,
helpstring("ICOMF Interface"),
pointer_default(unique)
]
interface ICOMF : IUnknown{
[, helpstring("method Add")] HRESULT Add([in] int firstValue, [in] int secondValue, [out,retval] int* pResult);
[, helpstring("method SetCallback")] HRESULT SetCallback([in] IShowMsg* callbackInterface);
[, helpstring("method ResetCallback")] HRESULT ResetCallback();
};
7。相映的在 CANCOM.h,CANCOM.cpp 也添加相应的方法,IShowMsg 是个接口,用来传递方法参数用的,没有具体实现的过程,所以只要用就可以了,不用做实现。
//.h
DECLARE_PROTECT_FINAL_CONSTRUCT()
HRESULT FinalConstruct()
{
return S_OK;
}
void FinalRelease()
{
if (pClientCallback)
pClientCallback->Release();
}
public:
STDMETHOD(Add)(int firstValue, int secondValue, int* pResult);
STDMETHOD(SetCallback)(IShowMsg* callbackInterface);
STDMETHOD(ResetCallback)();
private:
IShowMsg* pClientCallback;
};
//.cpp
// COMF.cpp : Implementation of CCOMF
#include "stdafx.h"
#include "COMF.h"
// CCOMF
STDMETHODIMP CCOMF::Add(int firstValue, int secondValue, int* pResult)
{
*pResult = firstValue + secondValue;
return S_OK;
}
STDMETHODIMP CCOMF::SetCallback(IShowMsg* callbackInterface)
{
pClientCallback = callbackInterface;
// Since we are using a new interface for the first time, make sure
// to increment its reference count.
pClientCallback->AddRef();
// and callback into managed code for the first time
pClientCallback->ShowMsg(_T("fanronghua"));
return S_OK;
}
STDMETHODIMP CCOMF::ResetCallback()
{
if (pClientCallback)
pClientCallback->Release();
pClientCallback = NULL;
return S_OK;
}
8。调用过程,首先要用个类 把这个接口个 搞出来,写个实现, MsgNotification : IShowMsg
然后 private ICOMF aa = new COMFClass(); 创建个新类
在 把他这个实现参数传递过去,就可以了,其他的直接调用就OK了
private MsgNotification bb = new MsgNotification();
//
public class MsgNotification : IShowMsg
{
public void ShowMsg(string message)
{
MessageBox.Show(message);
}
}
public partial class Form1 : Form
{
private ICOMF aa = new COMFClass();
private MsgNotification bb = new MsgNotification();
public Form1()
{
// InitializeComponent();
MessageBox.Show(aa.Add(1,2).ToString());
aa.SetCallback((IShowMsg)bb);
}
}
具体代码可以参考