使用来自 32 位 Windows 服务的 64 位进程外 COM DLL

Using 64-bit out of process COM DLL from 32-bit Windows Serivce

我按照文章中提到的步骤操作,

https://www.codeproject.com/Tips/1199539/Using-64-bit-DLLs-in-32-bit-Processes-with-Out-of?msg=5709592#xx5709592xx

创建了一个 64 位 COM DLL (MyCOMdll.dll) 并创建了一个 Class COMServer,如下所示,

namespace MyCOMdll
{
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    [GuidAttribute("SOMGUID")]
    public class COMServer
    {
        public ComServer() { }

        public void TestMethod()
        {
            MathClass mathObj = new MathClass();
            mathObj.Calc();
        }
    }
}

然后使用以下命令使用 64 位版本的 Regasm 注册 MyCOMdll.dll,

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\regasm.exe MyCOMdll.dll /codebase

然后按照上述文章中的说明添加以下注册表项,

Windows 注册表编辑器版本 5.00

[HKEY_CLASSES_ROOT\AppID\{GUIDOFCOMSERVER}]
"DllSurrogate"=""

[HKEY_CLASSES_ROOT\CLSID\{GUIDOFCOMSERVER}]
"AppID"="{GUIDOFCOMSERVER}"

然后从 32 位控制台应用程序调用 TestMethod,如下所示,

    // Access COM Object through registered Class Id
    Type ComType = Type.GetTypeFromProgID("MyCOMdll.ComServer");

    // Create an instance of the COM object
    // This will invoke the default constructor of class ComServer
    object ComObject = Activator.CreateInstance(ComType);

    // Calling the Method "TestMethod" from 64-Bit COM server
    ComType.InvokeMember("TestMethod", BindingFlags.InvokeMethod, null, ComObject, null);

此客户端代码在 32 位控制台应用程序中使用时工作正常,没有任何问题。 我尝试使用来自 32 位 windows 服务的相同代码,然后在这一行出现异常失败,

数学Class mathObj = 新数学Class();

当从 Windows 服务中使用 64 位 outproc 代理 dll 时,是否需要进行任何特殊设置?

路径中缺少 MathClass.dll 的依赖项。现在我已经将目录路径(所有依赖项都可用)添加到 PATH 变量,它开始工作了。我不确定为什么控制台应用程序没有出现此问题。