1

I am trying to use a c# dll in c++. I developed a c# dll.

I tried to add it as a reference to a c++ file, it showed warning as

Could not add a reference
- Targets a higher version of .NET Framework
- Not a .Net assembly
- Not a registered ActiveX control

so I added the dll like as follows in my c++wrapper file. I could able to access the classes present in the c# dll.

#using <C:\Users\New\Documents\Visual Studio 2015\Projects\ClassLibrary1\ClassLibrary1\bin\Debug\ClassLibrary1.dll>
using namespace csharp; //namespace in which managed class is present
#include "Header.h"
extern "C"
{
    __declspec(dllexport)  int func()
    {
        managedClass^ r = gcnew managedClass();
        someClass^s = gcnew someClass();
        s->name = "xyz";
        return r->getString(s);
    }
} 

Then I imported the wrapper dll to c++ console application as follows

#include <iostream>
#include <Windows.h>
#pragma comment(lib,"C:\\Users\\New\\Documents\\Visual Studio 2015\\Projects\\C++Wrapper\\Debug\\C++Wrapper.lib")
#include <C:\Users\New\Documents\Visual Studio 2015\Projects\C++Wrapper\C++Wrapper\Header.h>
void main()
{

    int result = func();
    std::cout << result;
    getchar();
}

But I am getting error as follows:

Unhandled exception at 0x7479D928 (KernelBase.dll) in C++ClientPro.exe: 0xE0434352 (parameters: 0x80070002, 0x00000000, 0x00000000, 0x00000000, 0x73790000).

I am getting the same error when I am trying to load the dll using LoadLibrary(libpath) function.
When I searched for this error, I got to know that It could not able to locate the file. In which step I was wrong. What I have to correct to use the c# dll in c++?
Edit
1) I've edited my c++wrapper file. I want to create an instance for a class in c# dll and also pass it as a function parameter to the same dll.
That's why I did not go for another method in which we could export only the functions using public interface, we could not able to access the classes.
2) I enabled /clr in my projects.

Fresher
  • 493
  • 6
  • 18
  • Possible duplicate - http://stackoverflow.com/questions/778590/calling-c-sharp-code-from-c – ChrisF Feb 25 '16 at 13:53
  • Possible duplicate of [Can you call a C# DLL from a C DLL?](http://stackoverflow.com/questions/728325/can-you-call-a-c-sharp-dll-from-a-c-dll) – Zippy Feb 25 '16 at 13:53
  • Those links did not help. I dont know whether I dont know to use them. I have edited my question. Will the links still help? – Fresher Feb 25 '16 at 14:18
  • 1
    As you found out, error reporting is *very* lousy when you do it like this. Surely you forgot to copy ClassLibrary1.dll into the same directory as your test EXE. So the CLR cannot find it, 0xE0434352 is the exception code for a managed exception, 0x80070002 is the error code for FileNotFoundException. – Hans Passant Feb 25 '16 at 14:22
  • @HansPassant That was the exact mistake that I had done. Thanks a lot. You saved me. Can you please share it as the answer. – Fresher Feb 25 '16 at 14:33
  • Just post the answer yourself and mark it as the answer. – Hans Passant Feb 25 '16 at 14:35

1 Answers1

0

1) Use CCW (Com Callable Wraper). To mark your managed(c#) assembly with "Register for COM interop" please follow below steps

Step 1. With a project selected in Solution Explorer, on the Project menu, click Properties. Step 2. Click the Compile tab in Visual Basic. Click the Build tab in C#. Step 3. Select the Register for COM interop check box.

else you can visit

a) http://msdn.microsoft.com/en-us/library/ms404285.aspx

For more details on "How to call a managed DLL from native Visual C++ code

For example see here

http://www.codeproject.com/Articles/16206/Call-C-code-from-C-and-read-an-array-of-struct-whi

2) Use Reverse P/Invoke as shown below

http://tigerang.blogspot.in/2008/09/reverse-pinvoke.html

when you use Reverse P/Invoke be careful for marshalling of parameters.

JanMer
  • 1,198
  • 2
  • 17
  • 27