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.