我需要创建一个C# COM服务器和一个C# COM客户端,并使用CoCreateInstance在两者之间进行通信。
这样客户端可以是64位的,而服务器是32位的,使用“DllSurrogate”方法描述在托管.NET DLL作为进程外COM服务器(EXE)中。
然而,目前我两个都是32位的,但我很难让它正常工作。
有在线示例可创建C# COM服务器:COM互操作性第2部分:C#服务器教程。
还有创建C# COM客户端的示例:COM互操作性第1部分:C#客户端教程。
但我还没有找到同时执行这两个操作的示例。 迄今为止我的尝试都失败了,我怀疑用于C# COM客户端示例的Microsoft COM服务器包括了C# COM服务器示例中没有的额外规范。
这样客户端可以是64位的,而服务器是32位的,使用“DllSurrogate”方法描述在托管.NET DLL作为进程外COM服务器(EXE)中。
然而,目前我两个都是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;
}
}
}