本文详细介绍了如何在 windows ce 6.0 系统上开发 dcom 服务进程,以提供跨进程的 com 组件服务,并创建和注册自定义接口代理/存根 dll。
假设您需要开发一个实时控制程序,该程序需要在后台持续运行,而客户端可能多样且使用不同的开发语言。这就需要创建一个 COM 服务进程程序。
在 Windows CE 镜像中,必须包含 DCOM 组件服务。然而,由于系统空间限制,许多基于 Windows CE 的操作系统不支持 DCOM。您需要使用 Platform Builder 向 Windows CE 镜像中添加 DCOM。此外,还必须导出支持 DCOM API 的 Windows CE SDK 开发包。
其他必备工具包括用于注册 COM DLL 的 GuiRegsvrCE.exe,这类工具可以在网上找到。
创建 COM Server
由于 Windows CE 不支持自动化封送/解封送,我们需要为 COM Server 和 Client 之间的接口方法参数创建自己的代理/存根。在 Windows XP/2000 系统中,如果 COM 方法使用 OLE 兼容的数据类型,我们可以不提供代理/存根 DLL。但如果使用自定义数据类型,则需要创建并注册相应的代理/存根代码。
在开发 Windows CE COM Server 时,需要将 IDL 文件中的 LIBRARY 块中的 dispinterface 定义移到 LIBRARY 块外部。所有接口都必须在 LIBRARY 块外部定义,这样 MIDL 编译器才能生成正确的代理/存根代码。
注意,sink dispinterface 接口需要重新定义为 dual 并从 IDispatch 接口派生。
[object, uuid(8D2D2A49-E8D3-4630-924D-1F83A4B063DB), dual, nonextensible, helpstring("IAlgorithm 接口"), pointer_default(unique)] interface IAlgorithm : IDispatch { [id(1), helpstring("方法Add")] HRESULT Add([in] LONG n1, [in] LONG n2, [out,retval]LONG* nVal); [id(2), helpstring("方法Minus")] HRESULT Minus([in] LONG n1, [in] LONG n2, [out,retval] LONG* nVal); [id(3), helpstring("方法Input")] HRESULT Input([in] BSTR str); }; <h1>ifdef UNDER_CE</h1><p>[uuid(C33B6BCD-ABBB-4E80-8E55-F34CC867BE83), dual, helpstring("_IAlgorithmEvents 接口")] interface _IAlgorithmEvents : IDispatch { //properties: //methods: [id(1), helpstring("方法Output")] HRESULT Output([in] BSTR str); };</p><h1>endif //UNDER_CE</h1><p>[uuid(4EC8BE3C-DF5C-4E56-B1F5-9350266E32FC), version(1.0), helpstring("ServDemo 1.0 类型库")] library ServDemoLib { importlib("stdole2.tlb"); interface IDocHostUIHandlerDispatch; interface IAxWinAmbientDispatchEx;</p><pre class="brush:php;toolbar:false">#ifndef UNDER_CE [uuid(C33B6BCD-ABBB-4E80-8E55-F34CC867BE83), helpstring("_IAlgorithmEvents 接口")] dispinterface _IAlgorithmEvents { properties: methods: }; #endif //UNDER_CE [uuid(9EEFFB69-1604-4DA2-A12A-FAB65CE9D587), helpstring("Algorithm Class")] coclass Algorithm { [default] interface IAlgorithm; [default, source] dispinterface _IAlgorithmEvents; };
};
代理存根 DLL 的创建
创建代理存根 DLL 与 PC 端类似,但需要定义一些宏才能编译通过,并定义一个 def 文件,注明要导出的函数。
// dlldata.c 的包装</p><h1>define REGISTER_PROXY_DLL //DllRegisterServer 等</h1><h1>ifndef _WIN32_WCE</h1><h1>define _WIN32_WINNT 0x0400//对于 WinNT 4.0 或安装了 DCOM 的 Win95</h1><h1>else</h1><h1>define WIN32</h1><h1>endif</h1><h1>define USE_STUBLESS_PROXY//仅当使用 MIDL 开关 /Oicf 时定义</h1><h1>ifndef _WIN32_WCE</h1><h1>pragma comment(lib, "rpcns4.lib")</h1><h1>endif</h1><h1>pragma comment(lib, "rpcrt4.lib")</h1><p>//#define ENTRY_PREFIXPrx</p><h1>include "dlldata.c"</h1><h1>include "ServDemo_p.c"
编译完成后,进程外 COM Server 的开发基本完成,但需要在机器上注册才能使用。
如何调用进程外组件(Client 程序)
对于客户端,调用进程外组件与调用进程内组件基本相同。我提供了一些辅助代码《EventHandler.h》,帮助客户端在不使用 ATL 的情况下接收 COM Server 的事件。
源代码下载:https://www.php.cn/link/85d52e4d4b40d62fa159037686630a7c