C# COM Server to C# COM Client using CoCreateInstance



我需要创建一个C# COM服务器和一个C# COM客户端,并使用CoCreateInstance在两者之间进行通信。

这样,客户端可以是 64 位,服务器可以是 32 位,使用将 .NET DLL 托管为进程外 COM 服务器 (EXE) 中所述的"DllSurrogate"方法。

但是,目前我两者都有 32 位,但我正在努力让它工作。

提供了创建 C# COM 服务器的联机示例:COM 互操作第 2 部分:C# 服务器教程

创建 C# COM 客户端:COM 互操作第 1 部分:C# 客户端教程

但我还没有找到一个同时做到这两点的例子。 到目前为止,我的尝试都失败了,我怀疑用于 C# COM 客户端示例的 Microsoft COM 服务器包含 C# COM 服务器示例中未包含的额外规范。

到目前为止,我的尝试如下。
C# 客户端对 CoCreateInstance 的调用显然成功,但返回的对象似乎为空(不为 null),并且尝试将其强制转换为接口失败。

任何人都可以发现问题,或者提供一个完全连接的示例吗?

这是我的服务器代码:

// AntCsServer.cs
// Project settings:
// compile as a library
// Select 'Make assembly COM Visible'
// Select 'Register for COM Interop'
using System;
using System.Runtime.InteropServices;
namespace AntCsServer
{
// Since the .NET Framework interface and coclass have to behave as 
// COM objects, we have to give them guids.
[Guid("555E2D2B-EE00-47AA-AB2B-39F953F6B339")]
public interface IManagedInterface
{
int PrintHi(string name);
}
[Guid("0190D7A6-8D8D-4031-810A-627BA3EE68A6")]
public class InterfaceImplementation : IManagedInterface
{
public int PrintHi(string name)
{
Console.WriteLine("Hello, {0}!", name);
return 33;
}
}
}

这是我的客户端代码:

using System;
using System.Runtime.InteropServices;
namespace ComClient
{
class Program
{
public const string ComSvrInterface_GUID = "555E2D2B-EE00-47AA-AB2B-39F953F6B339";
public const string ComSvrClass_GUID = "0190D7A6-8D8D-4031-810A-627BA3EE68A6";
[STAThread]
static void Main(string[] args)
{
Ole32Methods.CoInitialize((IntPtr)0);
object instance1 = null;
string sErr1;
bool b1;
IManagedInterface cCom = null;
b1 = Ole32Methods.CreateComObject(ComSvrClass_GUID, ComSvrInterface_GUID, out instance1, out sErr1);
if (b1)
{
cCom = instance1 as IManagedInterface;
}
if (cCom != null)
{
cCom.PrintHi("Santa Claus");
Console.WriteLine("Should have just printed Santa Claus");
}
}
}
// -------------------------------------------
// Reproduce the interface here so we can cast to it
[Guid("555E2D2B-EE00-47AA-AB2B-39F953F6B339")]
public interface IManagedInterface
{
int PrintHi(string name);
}
// -------------------------------------------
public class Ole32Methods
{
[DllImport("ole32.Dll")]
static public extern uint CoCreateInstance(ref Guid clsid,
[MarshalAs(UnmanagedType.IUnknown)] object inner,
uint context,
ref Guid uuid,
[MarshalAs(UnmanagedType.IUnknown)] out object rReturnedComObject);
[DllImport("ole32.dll")]
public static extern int CoInitialize(IntPtr pvReserved);
// ------------------------
public static bool CreateComObject(string sClassGuid, string sInterfaceGuid, out object instance, out string sErr)
{
const uint CLSCTX_INPROC_SERVER = 1;
//const uint CLSCTX_LOCAL_SERVER = 4;
// CLSID of the COM object
Guid clsid = new Guid(sClassGuid);
// GUID of the required interface
//Guid IID_IUnknown = new Guid("00000000-0000-0000-C000-000000000046");
Guid IID_Interface = new Guid(sInterfaceGuid);
instance = null;
uint hResult = Ole32Methods.CoCreateInstance(ref clsid, null,
CLSCTX_INPROC_SERVER, ref IID_Interface, out instance);

// Some error codes. See 'winerror.h for more, and use the following to convert the debug value to Hex: http://www.rapidtables.com/convert/number/decimal-to-hex.htm
const uint S_OK = 0x00000000;       //Operation successful
const uint E_NOTIMPL = 0x80004001;       //Not implemented
const uint E_NOINTERFACE = 0x80004002;       //No such interface supported
const uint E_POINTER = 0x80004003;       //Pointer that is not valid
const uint E_ABORT = 0x80004004;       //Operation aborted
const uint E_FAIL = 0x80004005;       //Unspecified failure
const uint E_UNEXPECTED = 0x8000FFFF;       //Unexpected failure
const uint E_ACCESSDENIED = 0x80070005;       //General access denied error
const uint E_HANDLE = 0x80070006;       //Handle that is not valid
const uint E_OUTOFMEMORY = 0x8007000E;       //Failed to allocate necessary memory
const uint E_INVALIDARG = 0x80070057;       //One or more arguments are not valid
const uint E_CLASSNOTREG = 0x80040154;      // Class not registered
sErr = "";
switch (hResult)
{
case S_OK:
sErr = "";
break;
case E_NOTIMPL:
sErr = "E_NOTIMPL: Not implemented";
break;
case E_NOINTERFACE:
sErr = "E_NOINTERFACE: No such interface supported";
break;
case E_POINTER:
sErr = "E_POINTER: Pointer that is not valid";
break;
case E_ABORT:
sErr = "E_ABORT: Operation aborted";
break;
case E_FAIL:
sErr = "E_FAIL: Unspecified failure";
break;
case E_UNEXPECTED:
sErr = "E_UNEXPECTED: Unexpected failure";
break;
case E_ACCESSDENIED:
sErr = "E_ACCESSDENIED: General access denied error";
break;
case E_HANDLE:
sErr = "E_HANDLE: Handle that is not valid";
break;
case E_OUTOFMEMORY:
sErr = "E_OUTOFMEMORY: Failed to allocate necessary memory";
break;
case E_INVALIDARG:
sErr = "E_INVALIDARG: One or more arguments are not valid";
break;
case E_CLASSNOTREG:
sErr = "E_CLASSNOTREG: Class not registered";
break;
}
return hResult == 0;
}
}
}

您正在进程中加载 COM 服务器。这会导致 CLR 在进程中加载程序集,因此它使用标准程序集加载来加载程序集。从CoCreateInstance获取的结果对象是一个文本AntCsServer.InterfaceImplementation类,它实现服务器 DLL 中的接口,而不是客户端 EXE 中的接口。

这可以通过检查您从CoCreateInstance返回的对象的类型来确认。如果是System.__ComObject,则会获得一个真正的 COM 对象(.NET 对象的代理对象)。如果不是,那么问题就如我所描述的 -- 您实际上得到了程序集类型的实例。

最新更新