简体   繁体   English

在C ++中使用ac#dll

[英]using a c# dll in c++

I am trying to use ac# dll in c++. 我正在尝试在C ++中使用ac#dll。 I developed ac# dll. 我开发了ac#dll。

I tried to add it as a reference to a c++ file, it showed warning as 我试图将其添加为对c ++文件的引用,它显示警告为

Could not add a reference 无法添加参考
- Targets a higher version of .NET Framework -针对更高版本的.NET Framework
- Not a .Net assembly -不是.Net程序集
- Not a registered ActiveX control -不是注册的ActiveX控件

so I added the dll like as follows in my c++wrapper file. 所以我在c ++ wrapper文件中添加了如下所示的dll。 I could able to access the classes present in the c# dll. 我可以访问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 然后我将包装器dll导入到c ++控制台应用程序,如下

#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). C ++ ClientPro.exe中0x7479D928(KernelBase.dll)的未处理异常:0xE0434352(参数:0x80070002、0x00000000、0x00000000、0x00000000、0x73790000)。

I am getting the same error when I am trying to load the dll using LoadLibrary(libpath) function. 我在尝试使用LoadLibrary(libpath)函数加载dll时遇到相同的错误。
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++? 我必须纠正在C ++中使用C#dll的问题?
Edit 编辑
1) I've edited my c++wrapper file. 1)我已经编辑了我的c ++ wrapper文件。 I want to create an instance for a class in c# dll and also pass it as a function parameter to the same dll. 我想为c#dll中的类创建实例,并将其作为函数参数传递给同一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. 2)我在项目中启用了/ clr。

1) Use CCW (Com Callable Wraper). 1)使用CCW(Com Callable Wraper)。 To mark your managed(c#) assembly with "Register for COM interop" please follow below steps 要使用“注册COM互操作”标记您的托管(c#)程序集,请按照以下步骤操作

Step 1. With a project selected in Solution Explorer, on the Project menu, click Properties. 步骤1.在“解决方案资源管理器”中选择一个项目后,在“项目”菜单上,单击“属性”。 Step 2. Click the Compile tab in Visual Basic. 步骤2.在Visual Basic中单击“编译”选项卡。 Click the Build tab in C#. 单击C#中的“生成”选项卡。 Step 3. Select the Register for COM interop check box. 步骤3.选中“注册COM互操作”复选框。

else you can visit 否则你可以参观

a) http://msdn.microsoft.com/en-us/library/ms404285.aspx 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 有关“如何从本地Visual C ++代码调用托管DLL”的更多详细信息,

For example see here 例如看这里

http://www.codeproject.com/Articles/16206/Call-C-code-from-C-and-read-an-array-of-struct-whi 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 2)如下所示使用反向P /调用

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

when you use Reverse P/Invoke be careful for marshalling of parameters. 使用反向P /调用时,请注意编组参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM