1

I'm trying to include C# dll library to Native C++ console application. I'm using this article: https://support.microsoft.com/en-us/kb/828736#bookmark-7

However the article is written for Visual Studio 2015 and I'm using Visual Studio 2015.

First I have checked a couple of answers here on Stackoverflow, but none of this questions was exactly what I'm looking for.

The question that is closest to my is here: C# library to native C++ application

So, I have create a C# library project, with the following code inside:

namespace Testing_Library
{

    // Interface declaration
    public interface iCalculator
    {
        int Add(int number1, int number2);
    }

    public class ManagedClass:iCalculator
    {
        public int Add(int number1, int number2)
        {
            return number1 + number2;
        }
    }
}

Then I have compiled the project to dll file, and then I have registered the dll file via RegAsm.exe. Now I have two output files:

Testing_Library.dll

and

Testing_Library.tlb

Also I have created Native C++ Windows Console Application Project. Inside this project I have one .cpp file with the following code:

// Import the type library.
#import "..\Testing_Library\bin\Debug\Testing_Library.tlb" raw_interfaces_only

using namespace std;
using namespace Testing_Library;

void main()
{
    // Initialize COM.
    HRESULT hr = CoInitialize(NULL);

    // Create the interface pointer.
    iCalculatorPtr pICalc(__uuidof(ManagedClass));

    long lResult = 0;

    // Call the Add method.
    pICalc->Add(5, 10, &lResult);

    wprintf(L"The result is %d", lResult);

    // Uninitialize COM.
    CoUninitialize();

}

And when I try to run the project I receive the following error screen:

enter image description here

Currently I have no idea why am I receiving this error Windows and I can not find information on the internet. Could you please help me? Here you can find the whole Solution with all projects: Onedrive folder

I'm trying to compile and run currently:

Test_me

and

Testing_Library

Community
  • 1
  • 1
Mario
  • 87
  • 2
  • 10
  • Can't you debug the app? – Alex Mar 18 '16 at 12:13
  • I can, but I'm new to C# and COM, I'm trying to currently learn them, and I will not find a lot of useful information there, at least for me. CoInitialize is returning S_OK to HRESULT which is ok, and then the app crashes on interface pointer. And the last screen gives me this: pICalc.m_pInterface was nullptr Also I was able to find this over the microsoft forums: This is standard behavior for code generated by #import. Whenever the interface method returns a failure HRESULT, it will translate the error to an exception. But in my case the result is OK – Mario Mar 18 '16 at 12:24
  • What happens after this line executed? `iCalculatorPtr pICalc(__uuidof(ManagedClass));` Do you get an instance? I'm not a C++ expert but it looks as if and equivalent of C#'s null reference exception is occuring. E.g. pICalc is null – Alex Mar 18 '16 at 12:32
  • Nothing happens, because the program gets into exception on that line, and cannot create the interface pointer. Maybe I need to understand why the program cannot create the pointer to the iCalculator class. – Mario Mar 18 '16 at 12:38
  • Can you try declaring an empty constructor in ManagedClass? – Alex Mar 18 '16 at 12:46
  • 2
    Code like this only ever works when you do *everything* right, error handling is never optional in COM code. Just like you can't ignore the return value of Add(). If you don't know how to step through the iCalculatorPtr constructor then wrap this code with try { } catch (_com_error &ex) { ... } so you can diagnose ex. And do spend a minimal amount of time on the documentation, trial-and-error coding is lots of error and our trial. – Hans Passant Mar 18 '16 at 12:48
  • Also, how what CPU configuration you are using? Maybe try building your C# assembly for x86 – Alex Mar 18 '16 at 12:52
  • Thank you for all comments, I changed the configuration for x86 and now I dont have problems. Thank you all again. :) – Mario Mar 18 '16 at 13:43
  • @Mario can you please accept the answer then? Glad it helped. – Alex Mar 18 '16 at 16:30

1 Answers1

1

Please change the Platform Target in Project Properties to x86 for your .NET assembly.

enter image description here

Alex
  • 937
  • 3
  • 20
  • 44