c# - 在 C# 代码中导入 DLL 函数

标签 c# c++ c dllimport extern

我有一个 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 似乎是用其他语言编写的?

最佳答案

请看下面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));
   }
}

关于c# - 在 C# 代码中导入 DLL 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15895112/

相关文章:

c# - 返回日期和时间值的日期类型列

c++ - 升级 XCode 后出现 "Parse issue: Unknown type name"错误

C++ 继承自未定义的模板类型

c - 在 C# 中设置 Internet Explorer/Windows 使用 Socks5 代理

c# - 多重等待什么时候有意义?

c# - "could not be loaded"是通过重启修复的错误吗?

c# - XNA WP7 上的广告

C++ CreateProcess 无法从 Windows 7 上的套接字接收路径 (64)

c - Freertos + STM32F2 - 将堆栈分配给线程后总堆大小错误

c - 为什么我在使用 GCC 编译时不必包含我的头文件?