COM server C# (use x86 dll in x64 app)

1.2k views Asked by At

I want to use x86 dll in in my x64 C# application! On forum I read that com object will help me! This is my ComServerSample. It is compile like x86.

    [ComVisible(true)]
    public class MyComServer : IMyComSample
    {        
        private dahuaIp.fDisConnect dissconn;

        public void con(int lLoginID, StringBuilder pchDVRIP, int nDVRPort, int dwUser)
        {

        }

        public string GetString()
        {
            dissconn = new dahuaIp.fDisConnect(con);
            var zdsc = dahuaIp.CLIENT_Init(dissconn, 0);

            return zdsc.ToString();
        }
    }

    [ComVisible(true), Guid("DBE0E8C4-2222-41f3-B6A4-4E2F353D3D05")]
    public interface IMyComSample
    {
        string GetString();
    }

And this is test application for using this com server

Type CSI = Type.GetTypeFromProgID("ComServerSample.MyComServer");
var COMobj = Activator.CreateInstance(CSI);
MethodInfo method = CSI.GetMethod("GetString", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);

var sdfc = method.Invoke(COMobj, null);

When test app is x86!This two applications work fine! But when test app is x64 there is an eroor :

Failed to get the COM class factory for component with CLSID {7B9F9A71-8E1B-3470-8A79-EEB4DA9B25A4} due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

How solve problem? I need to use my x86 dll in x64 application!

1

There are 1 answers

2
Hans Passant On BEST ANSWER

You are getting the "Class not registered" error because you used the wrong version of Regasm.exe to get the class library registered. Or because you let VS register it. You must use the 64-bit version of Regasm, it is present in the c:\windows\microsoft.net\framework64 subdirectory.

This however doesn't solve your real problem, COM can only bridge the bitness gap for an out-of-process COM server. .NET does not directly support creating them, only in-process servers are easy. Which is what you got, it will still fail because an in-process server must match the bitness of the EXE.

Getting an out-of-process COM server in .NET requires using COM+ and deriving from the ServicedComponent class. A how-to article that shows step-by-step instructions is here.

Frankly, you are not ahead by doing this. You are much better off by using .NET Remoting or WCF to allow your 64-bit process to talk to a 32-bit host process that loads the 32-bit component.