本问题作废,错误: if (hinstLib = NULL)
源程序改自 《程序员的自我修养:链接、装载与库》 第1版 第9 章
编译环境 VS2017/Win SDK 10.0.
问题概要
DLL动态库,LoadLibrary() 成功, GetProcAddress()失败, GetLastError()返回127.
但是 dumpbin 能够找到所需要的符号.
请问如何改可以正确执行
详细描述
动态库 Math.dll
代码 Math.c
__declspec(dllexport) double Add(double a, double b)
{
return a + b;
}
生成 DLL
cl /LDd Math.c
查看导出结果
dumpbin /EXPORTS Math.dll
...
ordinal hint RVA name
1 0 00001000 Add
...
主程序调用
程序代码 main.c
#include <windows.h>
#include <stdio.h>
typedef double (*Func)(double, double);
int main(int argc, char **argv)
{
Func function;
double result;
// Load DLL.
HINSTANCE hinstLib = LoadLibrary("Math.dll");
if (hinstLib = NULL)
{
printf("ERROR: unable to load DLL\n");
return 1;
}
// Get function address.
function = (Func)GetProcAddress(hinstLib, "Add");
if (function == NULL)
{
printf("ERROR: unable to find DLL function, "
"GetLastError() returns %d.\n", GetLastError());
FreeLibrary(hinstLib);
return 1;
}
// Call function.
result = function(1.0, 2.0);
// Unload DLL file.
FreeLibrary(hinstLib);
// Display result.
printf("Result = %f\n", result);
return 0;
}
编译链接
cl main.c
运行
ERROR: unable to find DLL function, GetLastError() returns 127.