Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用英特尔编译器编译DLL时出错_C++_Winapi_Dll_Compiler Construction_Intel - Fatal编程技术网

C++ 使用英特尔编译器编译DLL时出错

C++ 使用英特尔编译器编译DLL时出错,c++,winapi,dll,compiler-construction,intel,C++,Winapi,Dll,Compiler Construction,Intel,我试图从控制台编译DLL,不使用任何IDE,并且面临下一个错误 我写了这段代码: 测试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){

我试图从控制台编译DLL,不使用任何IDE,并且面临下一个错误

我写了这段代码:

测试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;
}
int main(){
  HMODULE hLib;
  hLib = LoadLibrary("test_dll.dll");  
  double (*pFunction)(int a, int b);
  (FARPROC &)pFunction = GetProcAddress(hLib, "Function");
  printf("begin\n");
  Rss = pFunction(1, 2);
}
使用
icl prog.cpp
编译它。然后我运行它,它在标准窗口“程序不工作”下失败。可能存在分段错误


我做错了什么?

检查
LoadLibrary()
GetProcAddress()
是否都成功,在这种情况下,它们肯定不会成功,因为导出的函数调用的是
func
,而不是
GetProcAddress()的参数中指定的
“function”
意味着在尝试调用函数指针时,函数指针将为
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.
}

快速浏览一下,应该使用函数名而不是“函数”调用GetProcAddress。但是在每次调用之后添加一些正确的错误检测代码,这样您就可以看到它失败的地方