COM cannot start out-of-process .Net server compiled as AnyCPU

前端 未结 2 1089
青春惊慌失措
青春惊慌失措 2021-02-04 14:24

I am trying to get COM to start my out-of-process .NET COM server. It works if the server process is compiled with x64, but if I use AnyCPU (which is what I want) then it hangs

相关标签:
2条回答
  • 2021-02-04 14:54

    I guess the problem is at run-time. I have created a COM Server which registers using a C++ library (registration is performed flawlessly). I have run into problems when switching to AnyCPU from .NET (CS).

    The architecture:

    • C++ library interfacing COM (built on both x64 and x86 platforms)
    • .NET library wrapper (CS) (correctly instantiates the required x64/x86 C++ library)
    • .NET application (CS) - COM client or COM server

    Ugly things happen when registering the .NET application built as "AnyCPU". Once the COM Client invokes the COM Server through DCOM, the server application starts but the client receives error that the COM Server could not be started.

    I went some steps further, analyzed registration data with procmon and other tools and I reached the same conclusion:

    • x86 registers the classes in CLASSES\Wow6432Node
    • x64 and AnyCPU register the classes in CLASSES (on a x64 windows machine, exactly the same keys; I bet that x86 and AnyCPU would register the same on an x86 machine)

    Now, I did some more experiments: the x86/x64/AnyCPU COM Client can connect with no problems to any x86/x64 COM Server but it cannot connect anyhow to an AnyCPU COM Server...

    I then performed the following test cases:

    1. Have the x86 COM Server register, replace the executable with the AnyCPU COM Server: COM Client was starting the x86 COM Server, but no communication... it was starting the server over and over again..
    2. Have the x64 COM Server register, replace the executable with the AnyCPU COM Server: COM Client was starting the x64 COM Server, but no communication... it was starting the server over and over again..
    3. Have the AnyCPU COM Server register, replace the executable with the x86 COM Server: COM Client was able to successfully start the and connect to the x86 COM Server.
    4. Have the AnyCPU COM Server register, replace the executable with the x64 COM Server: COM Client was able to successfully start the and connect to the x64 COM Server.
    5. Have the x86 COM Server register, replace the executable with the x64 COM Server: COM Client was able to successfully start the and connect to the x64 COM Server.
    6. Have the x64 COM Server register, replace the executable with the x86 COM Server: COM Client was able to successfully start the and connect to the x86 COM Server.

    Where the hell is the communication problem? This is very odd... None of the presented solutions (CLSCTX_ACTIVATE_64_BIT_SERVER, PreferredServerBitness or corflags) helped.

    Anyone else did some progress on this matter? Should we contact Microsoft?

    0 讨论(0)
  • 2021-02-04 15:12

    Try to use the RegistrationServices class to register your com assembly. It will also choose the correct registry pathes and do some other things.

    Example:

    Assembly currentAssembly = Assembly.GetExecutingAssembly();
    System.Runtime.InteropServices.RegistrationServices regAsm = new System.Runtime.InteropServices.RegistrationServices();
    bool isRegistered = regAsm.RegisterAssembly(currentAssembly, System.Runtime.InteropServices.AssemblyRegistrationFlags.SetCodeBase);
    

    Also I think, that .NET client assemblies have some trouble according to .NET com servers, but I can't find any resource for it...

    Hope, it will help...

    0 讨论(0)
提交回复
热议问题