Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a C# library with some interfaces and need to use it from an MFC application.
To accomplish this, i registered C# dll as COM and i used the COM interfaces from a C++ dll. This C++ dll is statically linked to the MFC app and uses its methods.

E.g.:
CSharpTest.dll (Register it as COM)
| CSharpDependent1.dll
| CSharpDependent2.dll
| ......

CPPTest.dll -- Uses COM interfaces and makes methods for MFC app

MFCApp.exe -- Uses the methods from CPPTest.dll by static linking
| CPPTest.dll

The problem is, to launch MFCApp.exe, i need to have the CPPTest.dll (of course!) and copy CSharpTest.dll and all its dependent dlls to the exe folder.

But, I need to have those C# dlls in some other folder and still make MFCApp.exe work without any issues.
P.S: It is not good to have C# dlls inside the folder of a C++ application.

Could you please provide a solution?

Regards,
Vineeth

What I have tried:

From Googling it, I found some ways like modify PATH environment variable, etc. but none of them was clear to me.
Posted
Updated 18-Aug-22 3:25am
Comments
0x01AA 9-Aug-22 6:29am    
If you register the .net COM assembly with RegAsm.exe than it does not have to be in the same directory with your MFCApp.exe.

If you register your 'COM Visible .NET Assembly' with regasm.exe you can place it (and it's dependencies) wherever you want. At least this works for me until today.
"C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe" "<path>\NetComAssembly.dll"  /codebase

From more information about regasm.exe see here:
Regasm.exe (Assembly Registration Tool) - .NET Framework | Microsoft Docs[^]
 
Share this answer
 
v2
Comments
Vineeth.B 9-Aug-22 13:41pm    
Thanks for the suggestion. But, it did not work for me.

I registered COM .NET assembly from path "E:\XYZ\CSharpAssembly.dll" using the command above (with /codebase).
I double-checked registry and in that also, the path shown is "E:\XYZ\CSharpAssembly.dll"

But, when I launch the exe from "E:\ABC\MFCApp.exe", it will not launch.

If I copy the MFCApp.exe from "E:\ABC" folder to "E:\XYZ" folder and launch it from there, then MFCApp is launched properly.
0x01AA 9-Aug-22 13:51pm    
a.) How do you instanciate the COM class in c++?
b.) Is maybe a previous version of your COM assembly registered from former trials?
c.) Are all depended assemblies located in ""E:\XYZ\"?

Btw. I instantiate in c++ like that:
   TCOMIMyComClass  myComClass;
   if (!myComClass.IsBound())
   {
      HRESULT  xHRes;
      xHRes = CoInitialize(NULL);
      xHRes= CoCreateInstance(CLSID_MyComClass,
                              NULL,
                              CLSCTX_INPROC_SERVER,
                              IID_IMyComClass,
                              (LPVOID*)&myComClass);
      myComClass->DoSomething();
   }



From my point of view this should work. I use that in one of our software in production. But maybe I forgot something at the moment ;)
Vineeth.B 10-Aug-22 1:28am    
a) I instantiate COM class as below

CSharpTest::ISamplePtr testCOMClass = NULL; // this Interface type is autogenerated in the tlb file which is created after registering using RegAsm.
// In C# dll, ISample interface is available.

HRESULT result = CoInitializeEx(NULL, COINIT::COINIT_MULTITHREADED);
testCOMClass.CreateInstance("CSharpTest.Sample");
testCOMClass->Execute();

b) No, I checked registry also. Contains only one entry (and it is of dll from XYZ folder).
Also, I usually unregisters (using regAsm /u) previous versions before registering new versions.
(Is there a good way to unregister previous versions alltogether? )

c)Yes, all dependent dlls located in "E:\XYZ". As mentioned in my previous reply, If I copy the same MFCApp.exe (and its static linked CPPTest.dll) from ABC folder to XYZ and launch it from XYZ, then it is launched without any issue.
The problem is when launching from ABC folder which only contains the exe and and its static linked CPPTest.dll.
0x01AA 10-Aug-22 2:10am    
What is the value of result from result= CoInitializeEx when you start from ABC folder?

Unregister: I also do it always in same way like you with /u. I don't know another way.

Btw. I assume (hope) in the project options, the checkbox for COM registration after build is _unchecked_.
Vineeth.B 10-Aug-22 2:59am    
The "Register for COM Interop" check box is OFF in the project settings of CSharpTest project.

when launched from ABC, result of CoInitializeEx is below. (SAME is getting when launch from XYZ folder also, but in this case, application gets launched properly from XYZ folder)
result = 0x80010106 : Cannot change thread mode after it is set.
To simplify things, if you place the DLL in the system directory (%SystemRoot%\system32).
it will work.
That would usually be:
c:\Windows\System32
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900