isaaccwoo 2017-05-05 09:11 采纳率: 0%
浏览 2792

GetProcAddress 返回 NULL, 错误代码 127

本问题作废,错误: 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.
  • 写回答

2条回答 默认 最新

  • oyljerry 2017-05-05 09:24
    关注

    指定一下函数调用约定,比如都指定为stdcall

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘