用英特尔编译器编译DLL时出错

Error with compiling DLL with intel compiler

本文关键字:出错 DLL 编译 英特尔 编译器      更新时间:2023-10-16

我正在尝试从控制台编译DLL,而不使用任何IDE并面临下一个错误。

我写了下面的代码:

test_dll.cpp

#include <windows.h>
#define DLL_EI __declspec(dllexport)
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID lpvReserved){
  return 1;
}
extern "C" int DLL_EI func (int a, int b){
  return a + b;
}

然后用icl /LD test_dll.cpp命令编译。我试着从另一个程序调用func:

prog.cpp

int main(){
  HMODULE hLib;
  hLib = LoadLibrary("test_dll.dll");  
  double (*pFunction)(int a, int b);
  (FARPROC &)pFunction = GetProcAddress(hLib, "Function");
  printf("beginn");
  Rss = pFunction(1, 2);
}

icl prog.cpp编译。然后我运行它,它失败了,标准窗口"程序不工作"。可能有分段错误错误。

我做错了什么?

检查LoadLibrary()GetProcAddress()是否成功,在这种情况下,它们肯定不会因为导出的函数被称为func,而不是"Function",如GetProcAddress()的参数中指定的那样,这意味着当尝试调用它时,函数指针将是NULL

函数指针的签名也与导出函数的签名不匹配,导出函数返回的是int,而函数指针期望的是double

例如:

typedef int (*func_t)(int, int);
HMODULE hLib = LoadLibrary("test_dll.dll");
if (hLib)
{
    func_t pFunction = (func_t)GetProcAddress(hLib, "func");
    if (pFunction)
    {
        Rss = pFunction(1, 2);
    }
    else
    {
        // Check GetLastError() to determine
        // reason for failure.
    }
    FreeLibrary(hLib);
}
else
{
    // Check GetLastError() to determine
    // reason for failure.
}