如何在 C++ 项目中使用非托管 .dll、.lib、.exp

how to use unmanaged .dll, .lib, .exp in c++ project

我在这方面有点不适应,所以我希望有人能给我一些关于如何进行的建议。

长话短说,我从一个 jar 开始,我在其中 运行 一个名为 ikvm 的应用程序来生成我的 java classes 的 .net 库。这已经过测试并且工作正常。然后我有了一个 .net dll,我使用 mono aot 生成了一个非托管的 .dll、.exp 和 .lib,但没有头文件。我确实知道所涉及的 class 名称和方法。

现在我想不通的是如何在没有头文件的情况下使用这些文件在 c++ 项目中处理这些 classes。我正在使用 visual studio。如果有任何我遗漏的信息,请发表评论。

如果您没有头文件,则需要 "manufacture" 一个(根据 .dll 中的确切规范),或者动态加载库和函数 - 请参阅 LoadLibrary and GetProcAddress.

要查看 dll 中的确切功能规范,您可以使用例如DependencyWalker(外部工具)或直接由 Visual Studio:

提供的 dumpbin 实用程序
dumpbin /exports thename.dll

这将显示所有导出。

尤其要注意调用约定(stdcall、fastcall 等)。

如果你有一个非托管的 DLL,那么有很多方法可以用 C++ 来使用它。

一种简单的方法是使用 LoadLibrary() 和 GetProcAddress() 函数。例如:

//Define the function prototype
typedef int (CALLBACK* FirstFunction)(LPCTSTR);

void main()
{
   HINSTANCE dllHandle = NULL;              
   FirstFunction pFirstFunction = NULL;

   //Load DLL
    dllHandle = LoadLibrary("Your.dll");

   if(dllHandle != NULL)
   {
      //Get pointer to function FindBook
      pFirstFunction = (FirstFunction)GetProcAddress(dllHandle,
         "FindBook");

      // If function pointer is valid then you can use it 
      if (pFirstFunction != NULL)
      {
         LPCTSTR strBook = "Duchamp";
         short nSuccessCode = pFirstFunction(strBook);
      }

   }

}