关于c ++:getProcAddress-返回NULL

getProcAddress - NULL is returned

我有以下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  //mydll.cpp
    #include <Windows.h>
    #include <io.h>

    #define STDOUT_FILEDESC 1

    class MYSTDOUT {
        bool shouldClose;
        bool isBuffered;
    public:
        MYSTDOUT(bool buf = true, bool cl = true)
            : isBuffered(buf),
              shouldClose(cl)
        {}
        ~MYSTDOUT() {
            if (shouldClose) {
                close(STDOUT_FILEDESC);
            }
        }
    };

    __declspec(dllexport) void* mydll_init_stdout()
    {
        static MYSTDOUT outs;
        return &outs;
    }
//test_dll.cpp
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <io.h>


typedef void* (__cdecl *MYPROC)(void);


int main(void)
{
  int fd;
  void *pstdout;

  MYPROC init_stdout;
  HMODULE handle = LoadLibrary(TEXT("mydll.dll"));

  init_stdout = (MYPROC)GetProcAddress(handle,"mydll_init_stdout");//NULL

  FreeLibrary((HMODULE) handle);
  return 0;
}

我知道init_stdout为NULL。可能是什么问题?
句柄确定(非NULL)
谢谢


那是由于名字处理。

您需要将导出的函数包装在extern"C"中,如下所示:

1
2
3
4
5
6
7
8
extern"C"
{
    __declspec(dllexport) void* mydll_init_stdout()
    {
        static MYSTDOUT outs;
        return &outs;
    }
}


在Dependency Walker或dumpbin /exports中进行检查,您会看到mydll_init_stdout已用错误的C ++名称导出。 这就是GetProcAddress调用失败的原因。

使用extern"C"停止重整。

1
2
3
4
5
6
7
8
extern"C"
{
    __declspec(dllexport) void* mydll_init_stdout()
    {
        static MYSTDOUT outs;
        return &outs;
    }
}