【发布时间】:2017-08-30 22:57:28
【问题描述】:
我需要创建一个 C# COM 服务器和一个 C# COM 客户端,并使用 CoCreateInstance 在两者之间进行通信。
这是为了让客户端可以是 64 位,而服务器可以是 32 位,使用 Hosting a .NET DLL as an Out-Of-Process COM Server (EXE) 中描述的“DllSurrogate”方法。
但是,目前我都在 32 位,但我正在努力让它工作。
有创建C# COM服务器的在线示例:COM Interop Part 2: C# Server Tutorial
并创建一个 C# COM 客户端:COM Interop Part 1: C# Client Tutorial
但是我还没有找到一个可以同时做到这两个的例子。 到目前为止,我的尝试都失败了,我怀疑用于 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;
}
}
}
【问题讨论】:
-
这和 C++ 有什么关系?请不要发送垃圾标签。
-
大多数 COM 的东西都是由 C++ 专家完成的,所以我认为该小组中的某个人可能更了解该领域的幕后情况。
-
使用 CoCreateInstance 从 C# 实例化 COM 瞬间是一种奇怪的方式。我认为使用 Activator 类要容易得多。
-
我按照您发布的第一个链接上的说明进行操作,第一次尝试一切正常。我建议您更仔细地按照说明进行操作。 (另外,不要自己初始化 COM——.NET 会为你调用 CoInitialize。)
-
另外,use the first
CoCreateInstancedefinition from here 而不是你拥有的那个。 .NET 内置支持处理从 p/invoked 方法返回的HRESULT值。您可以用单个 p/invoke 方法(和 the correspondingCLSCTXenum)从字面上替换整个Ole32Methods类。