使用CoCreateInstance将C# COM服务器与C# COM客户端连接

4
我需要创建一个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服务器示例中没有的额外规范。

迄今为止我的尝试如下。
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;

        }

    }
}

1
大多数COM相关的工作都是由C++专家完成的,因此我认为那个团队中的某个人可能更了解这个领域底层的情况。 - Ant Waters
1
我按照你发布的第一个链接上的说明进行操作,第一次尝试就一切正常。我建议你更仔细地遵循这些说明。(另外,不要自己初始化COM——.NET会为你调用CoInitialize。) - Michael Gunter
@AntWaters 这是滥用标签。大多数C++的东西都是由汇编专家完成的,所以我们应该在C++问题中添加汇编标签吗?我认为不应该。只需添加与问题相关的标签即可。 - Vada Poché
@Michael,感谢您的评论。以上代码是这个32位测试的完整代码。在C#COm服务器属性中,我已选择“使装配件COM可见”和“为COM互操作注册”,因此注册是在构建时完成的。在调试C#客户端时,我可以看到转换为接口失败。为了明确起见,您是否已成功使用以上代码,还是只是给我一般性建议? - Ant Waters
@Michael Gunter,我认为你是对的。当我切换到外部进程时,我可以在不使用C++的情况下使其正常工作。所以我最初认为最简单的测试(32位到32位的内部进程)结果却不能工作!感谢你的帮助。 - Ant Waters
显示剩余14条评论
1个回答

4
您正在以进程内方式加载COM服务器。这会导致CLR以标准程序集加载方式将您的程序集加载到进程中。从CoCreateInstance获得的对象是一个字面上的AntCsServer.InterfaceImplementation类,它实现了服务器DLL中的接口,而不是客户端EXE中的接口。
可以通过检查从CoCreateInstance返回的对象类型来确认这一点。如果是System.__ComObject,则您获得了真正的COM对象(.NET对象的代理对象)。如果不是,则问题就如我所描述的一样——您实际上获得了程序集的类型实例。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接