0

I've already tried using COM to do this but every time, I allow this line to run:

ICalculatorPtr pICalc(__uuidof(ManagedClass));

This line breaks my C++ function that calls it. I know the Microsoft Article is made for a C++ Client Console Application but shouldn't the COM approach work for a C++ DLL?

FYI, the C++ DLL is getting import into another program at runtime. (Design of the main application).

Is the only way to make three projects in my Visual Studio Solution: The C#.net DLL, the C++/CLI managed.DLL, then the C++ Native.DLL.

I added a catch to the com error:

try {
        ICalculatorPtr myvalue(__uuidof(ManagedClass));
    }
    catch (_com_error &e)
    {
        // Crack _com_error
        _bstr_t bstrSource(e.Source());
        _bstr_t bstrDescription(e.Description());

        std::ofstream out("C:\\Samples\\output.txt");

        out << "Error 1: " << e.Error() << "\n" ;
        out << "Error 2: " << e.ErrorMessage() << "\n";
        out << "Error 3: " << (LPCTSTR)bstrSource << "\n";
        out << "Error 4: " << (LPCTSTR)bstrDescription << "\n";

        out.close();

    }

Output was

Error 1: -2147221164 Error 2: 00000272EED86F40 Error 3: 0000000000000000 Error 4: 0000000000000000

This code is class not registered.

I did register the C# .dll in registry. RegAsm.exe ManagedDLL.dll /tlb:ManagedDLL.tlb /codebase

The code all works with a C++ Console App but not as a C++ DLL getting loaded into the client application.

user1752179
  • 29
  • 1
  • 1
  • 6
  • The DLL's code is loaded into the executable and then executed just as if it was compiled into directly. So COM should work from DLL as well, thus I'd assume you did something wrong somewhere else... On the other hand, I consider COM a horrible interface, the C++/CLI approach in comparison always appeared to me the way of least resistance... – Aconcagua Jul 27 '18 at 09:15
  • Your code has an error. To help you fix it we need to see that code. Consider a [mcve] – J... Jul 27 '18 at 09:15
  • The code sample is missing an important detail, it has no useful error handling. Which does get you a program that can fail without being able to find out why. It needs try/catch to catch the `_com_error` exception. Only when you have that in place can you/we make headway on figuring out what went wrong. Google "how to catch a _com_error exception" for useful hits. – Hans Passant Jul 27 '18 at 09:51
  • I'm new to stackoverflow. Can I just upload my Visual Studio Solution as a zip file? The error handling is poor because I had to toggle them in the Project Properties. Once I followed the instructions of the MS Article to enable /clr Common Language Runtime, it kept getting upset about different flags like /Zi or /RCHP(*sp) or /EHSa and so on. So I just kept going onward to remove the build errors in the Project Properties. The project will build but will not work. – user1752179 Jul 27 '18 at 10:03
  • I have the exact same project with a C++ Console Application making the calls and the call to the C# DLL method works perfectly, the second I try adding the same approach to a C++ DLL then loading my DLL into the client application it breaks. – user1752179 Jul 27 '18 at 10:03
  • Hans Passant, Error 1: -2147221164 Error 2: 00000272EED86F40 Error 3: 0000000000000000 Error 4: 0000000000000000 – user1752179 Jul 27 '18 at 10:21
  • try { ICalculatorPtr myvalue(__uuidof(ManagedClass)); } catch (_com_error &e) { // Crack _com_error _bstr_t bstrSource(e.Source()); _bstr_t bstrDescription(e.Description()); std::ofstream out("C:\\Samples\\output.txt"); out << "Error 1: " << e.Error() << "\n" ; out << "Error 2: " << e.ErrorMessage() << "\n"; out << "Error 3: " << (LPCTSTR)bstrSource << "\n"; out << "Error 4: " << (LPCTSTR)bstrDescription << "\n"; out.close(); } – user1752179 Jul 27 '18 at 10:22
  • https://stackoverflow.com/a/3534693/17034 – Hans Passant Jul 27 '18 at 10:32
  • where and when do you call the c++ code that breaks? – alangab Jul 27 '18 at 11:37
  • Is breaks at the ICalculatorPTr? Maybe I need to use something like a CoInstance? – user1752179 Jul 28 '18 at 08:13
  • I think I may need to use a LRESULT CALLBACK type call. – user1752179 Jul 28 '18 at 08:16

1 Answers1

0

I found this issue, I wasn't registering the DLL properly in the RegAsm.exe cmd. Once this was successfully the COM would work properly. I think I was using the 32bit Visual Studio CMD Line Tools instead of 64bit.

user1752179
  • 29
  • 1
  • 1
  • 6