COM Interop 允许 COM 开发人 员像访问其他 COM 对象一样轻松访问托管代码。本教程说明如 何将 C# 服务器与 C++ COM 客户端一起使用。它还解 释了下列活动:
如何创建 C# 服务 器
如何创建 COM 客户端
该教程还简要说明了在托管和非托管组件之间自动应用的封 送处理。
COM Interop 第一部分:C # 客户端教程显示使用 C# 与 COM 对象交互操作的基础 知识,这是该教程的前提。有关两个教程的概述,请参见 COM Interop 教程。
教程
该 教程说明下列创建 C# 服务器的活动:
如何使用接口和类上的 Guid 属性将其作为 COM 对 象公开以及如何为 Guid 属性生成全局唯一标识符 (GUI D)。
如何使用 RegAsm 注册供 CO M 客户端使用的 .NET Framework 程序以及从 .NET Framework 程序创建类型库(.tlb 文件 )。
该教程还说明下列创建 COM 客 户端的活动:
如何导出托管服务器以及 如何使用它们创建 COM 对象。
如何将由 RegAsm 生成的 .tlb 文件导入到 COM 客户端, 以及如何使用 CoCreateInstance 创建 .NE T Framework coclass 的实例。
注意 若要为导出到 COM 客户端的接口和 cocl ass 创建 GUID,请使用 Guidgen.exe 工具 ,此工具是作为 Visual Studio 的一部分交付的。 Guidgen 使您可以选择 GUID 的表示格式,这样您就 不必重新键入它。有关 Guidgen 的更多信息,请参见知识 库文章 Q168318“XADM: Guidgen.exe Available Only for Intel Platf orms”。知识库文章可以在 MSDN Library 中以 及 Web 站点 http://support.micros oft.com 上找到。
示例
本示例由两个文件组成:
C# 文件 CSharpServer.cs,它创建 CS harpServer.dll 文件。.dll 用于创建 CS harpServer.tlb 文件。
C++ 文件 COMClient.cpp,它创建可执行客户端 CO MClient.exe。
文件 1:C SharpServer.cs
// CShar pServer.cs
// compile with: /target:library
// post-bu ild command: regasm CSharpServ er.dll /tlb:CSharpServer.tlb
using System;
using S ystem.Runtime.InteropServices;
namespace CSharpServer
{
// Since the .NET Fram ework interface and coclass ha ve to behave as
// COM objects, we have to give them guids.
[Guid("DBE0E8C4-1 C61-41f3-B6A4-4E2F353D3D05")]< br> public interface IManage dInterface
{
in t PrintHi(string name);
}
[Guid("C6659361-16 25-4746-931C-36014B146679")] public class InterfaceImp lementation : IManagedInterfac e
{
public int PrintHi(string name)
{
Console.WriteLin e("Hello, {0}!", name);
return 33;
}
}
}
文件 2:COMClie nt.cpp
// COMClient.cpp
// Build with "cl COMClien t.cpp"
// arguments: friend
#include <
windows.h >
#include <
stdio.h&g t;
#pragma warning (dis able: 4278)
// To use m anaged-code servers like the C # server,
// we have to im port the common language runti me:
#import <
mscorlib.tl b>
raw_interfaces_only
< br>// For simplicity, we ignor e the server namespace and use named guids:
#if defined ( USINGPROJECTSYSTEM)
#import "..\RegisterCSharpServerAndEx portTLB\CSharpServer.tlb" no_n amespace named_guids
#else // Compiling from the command line, all files in the same d irectory
#import "CSharpSer ver.tlb" no_namespace named_gu ids
#endif
int main(int argc, char* argv[])
{
IManagedInterface *cpi = NULL ;
int retval = 1;
// Initialize COM and crea te an instance of the Interfac eImplementation class:
C oInitialize(NULL);
HRESU LT hr = CoCreateInstance(CLSID _InterfaceImplementation,
NULL, CLSCTX_INP ROC_SERVER,
IID_IManagedInterface, reinter pret_cast<
void**>
(&
c pi));
if (FAILED(hr) )
{
printf("Cou ldn't create the instance!... 0x%x\n", hr);
}
el se
{
if (argc & gt;
1)
{
printf("Calling function.\n");
fflush(stdout);
// The variable cpi now holds an interface pointe r
// to the manag ed interface.
// I f you are on an OS that uses A SCII characters at the
// command prompt, notic e that the ASCII characters ar e
// automaticall y marshaled to Unicode for the C# code.
if (cpi- >
PrintHi(argv[1]) == 33)
retval = 0;
printf("Returned from f unction.\n");
}
else
printf (" Usage: COMClient <
name>
\n");
cpi->
Release ();
cpi = NULL;
}
// Be a good citi zen and clean up COM:
Co Uninitialize();
return r etval;
}
输出
可 以用以下命令行调用可执行客户端:COMClient <
name>
,其中 <
name>
表示要使用 的任何字符串,如 COMClient friend。
Calling function.
Hello , friend!
Returned from fun ction.
在示例 IDE 项目中,将项目“ 属性页”中的“命令行参数”属性设置为需要的字符串(例如,“f riend”)。