簡體   English   中英

在C ++中使用ac#dll

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

我正在嘗試在C ++中使用ac#dll。 我開發了ac#dll。

我試圖將其添加為對c ++文件的引用,它顯示警告為

無法添加參考
-針對更高版本的.NET Framework
-不是.Net程序集
-不是注冊的ActiveX控件

所以我在c ++ wrapper文件中添加了如下所示的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);
    }
} 

然后我將包裝器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();
}

但是我收到如下錯誤:

C ++ ClientPro.exe中0x7479D928(KernelBase.dll)的未處理異常:0xE0434352(參數:0x80070002、0x00000000、0x00000000、0x00000000、0x73790000)。

我在嘗試使用LoadLibrary(libpath)函數加載dll時遇到相同的錯誤。
當我搜索此錯誤時,我知道它無法找到該文件。 在哪一步我錯了。 我必須糾正在C ++中使用C#dll的問題?
編輯
1)我已經編輯了我的c ++ wrapper文件。 我想為c#dll中的類創建實例,並將其作為函數參數傳遞給同一dll。
這就是為什么我沒有選擇另一種方法的方法,在該方法中,只能使用公共接口導出函數,而無法訪問類。
2)我在項目中啟用了/ clr。

1)使用CCW(Com Callable Wraper)。 要使用“注冊COM互操作”標記您的托管(c#)程序集,請按照以下步驟操作

步驟1.在“解決方案資源管理器”中選擇一個項目后,在“項目”菜單上,單擊“屬性”。 步驟2.在Visual Basic中單擊“編譯”選項卡。 單擊C#中的“生成”選項卡。 步驟3.選中“注冊COM互操作”復選框。

否則你可以參觀

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

有關“如何從本地Visual C ++代碼調用托管DLL”的更多詳細信息,

例如看這里

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

2)如下所示使用反向P /調用

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

使用反向P /調用時,請注意編組參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM