【问题标题】:Importing DLL functions in c# code在 C# 代码中导入 DLL 函数
【发布时间】:2013-04-09 06:47:00
【问题描述】:

我有一个 DLL,我想在我的 c# 代码中使用它的函数 以下是该 DLL 的功能:

extern "C"
{
  __declspec(dllimport)
  const char* __stdcall ZAJsonRequestA(const char *szReq);

  __declspec(dllimport)
  const wchar_t* __stdcall ZAJsonRequestW(const wchar_t *szReq);

  __declspec(dllimport)
  const BSTR __stdcall ZAJsonRequestBSTR(BSTR sReq);
}

谁能告诉我如何在 c# 项目中使用它,因为这个 dll 似乎是其他语言的?

【问题讨论】:

  • 不应该__declspec(dllimport)__declspec(dllexport)
  • 我从 dll 的文档中复制了这个。而且,在那个 dll 中,使用 dllimport 而不是 dllexport

标签: c# c++ c dllimport extern


【解决方案1】:

请在 Code Project 上查看以下article 以获得深入的解释

链接文章中的一个小样本如下所示

要调用函数,请说 methodName

int __declspec(dllexport) methodName(int b)
{
      return b;
}

在c#中包含如下所示的包含上述方法的类库(MethodNameLibrary.dll)

class Program
{
   [DllImport(@"c:\MethodNameLibrary.dll")]
   private static extern int methodName(int b);
   static void Main(string[] args)
   {
       Console.WriteLine(methodName(3));
   }
}

【讨论】:

  • 我试过这个,但是当我尝试调用该函数时,它会退出块并且以下语句不会被执行。我的声明是这样的: [DllImport("FANselect.dll", EntryPoint = "ZAJsonRequestA")] private static extern string ZAJsonRequestA(string szReq);我尝试使用以下方法调用它: string sResult = ZAJsonRequestA(sRequest);这对我来说似乎很好,但我没有得到结果
  • @AmitAgrawal 请查看以下SO Question 以获得有关声明的帮助
【解决方案2】:

使用DllImportMarshalAs 属性。

【讨论】:

    猜你喜欢
    • 2016-05-23
    • 1970-01-01
    • 2013-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多