簡體   English   中英

在C#代碼中使用cpp dll

[英]using cpp dll in c# code

我正在嘗試在ac#應用程序中使用本機c ++ dll。

我跟着這個文章開始。

當dll僅使用c文件(如本教程中所述)時,它的工作正常,但是當使用cpp文件時,我無法使其工作。

使用dumpbin工具,我可以看到導出的函數名稱已更改。 例如,將函數調用“ next”更改為“?next @@ YAHH @ Z”,當我嘗試在c#代碼中調用它時,找不到它。

我的dll代碼是:

__declspec(dllexport)
int next(int n)
{
    return n + 1;
}

C#代碼

[DllImport("lib.dll", CallingConvention = CallingConvention.Cdecl)]
    extern static int next(int n);

使用c文件或cpp文件時使用相同的代碼

在此先感謝Amichai

您正在嘗試做一個蹦床功能,以外部化C ++對象的使用。

您遇到的情況稱為符號錯誤處理。 這是一種c ++特定的方式,可以在動態庫和可執行文件中命名符號(如函數),以實現多態。

您必須指定要通過使用庫代碼中的extern "C"語句來取消破壞效果,如下所示:

extern "C" __declspec(dllexport)
int next(int n)
{
    return n + 1;
}

有關此內容的更多信息:使用dllexport從DLL導出函數

請提供入口點

下面的例子

[DllImport("lib.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "next", CharSet = CharSet.Ansi)]
static extern int next(int n);

希望它會有所幫助。

暫無
暫無

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

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