简体   繁体   中英

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

I followed the steps mentioned in the article,

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

Created a 64 bit COM DLL (MyCOMdll.dll) and created a Class COMServer as below,

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

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

Then registered MyCOMdll.dll using the 64bit version of Regasm using below command,

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

Then added the below registry entries as described in the above article,

Windows Registry Editor Version 5.00

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

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

Then from a 32 bit Console application calling the TestMethod as below,

    // 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);

This client code works fine without any issue when it is used from 32 bit console application. I tried to use this same code from a 32 bit windows service then it fails with exception in this line,

MathClass mathObj = new MathClass();

Is there any special settings to be done when a 64 bit outproc surrogate dll is consumed from Windows Service?

The dependencies of MathClass.dll was missed in the path. Now I have added the directory path (where all the dependencies are available) to PATH variable, it started working. I am not sure why this issue didn't come for console application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM