动态库加载问题

时间:2015-11-15 20:35:05

标签: c++ windows dll

我正在尝试在Windows上创建一个非常简单的运行时动态库加载示例,但GetProcAddress()返回错误,我不明白为什么。

dll_test.cpp:

#include "stdafx.h"
#include "dll_test.h"

static const char* const helloWorldStr = "Hello world!";
static const int age = 25;

DLL_TEST_API const char* helloWorld()
{
    return helloWorldStr;
}

DLL_TEST_API const int getAge()
{
    return age;
}

dll_test.h:

#ifdef DLL_TEST_EXPORTS
#define DLL_TEST_API __declspec(dllexport)
#else
#define DLL_TEST_API __declspec(dllimport)
#endif

DLL_TEST_API const char* helloWorld();
DLL_TEST_API const int getAge();

现在是动态链接代码 dll_dynamic.cpp:

#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

typedef const char* (__cdecl *helloWorldFunc)();
typedef const int (__cdecl *getAgeFunc)();

int main()
{
    HMODULE lib = LoadLibrary(L"C:\\VS\\dll_compile\\Release\\dll_test.dll");

    if (lib == NULL)
    {
        fprintf(stderr, "Failed to open lib (%lu)\n", GetLastError());
        return EXIT_FAILURE;
    }

    helloWorldFunc myHelloWorld = (helloWorldFunc) GetProcAddress(lib, "helloWorld");

    if (myHelloWorld == NULL)
    {
        fprintf(stderr, "Failed to open helloWorld() (%lu)\n", GetLastError());
        return EXIT_FAILURE;
    }

    getAgeFunc myGetAge = (getAgeFunc) GetProcAddress(lib, "getAge");

    if (myGetAge == NULL)
    {
        fprintf(stderr, "Failed to open getAge() (%lu)\n", GetLastError());
        return EXIT_FAILURE;
    }

    printf("\"%s\"\n", myHelloWorld());
    printf("age = %d\n", myGetAge());

    if (!FreeLibrary(lib))
    {
        fprintf(stderr, "Failed to free lib (%lu)\n", GetLastError());
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

此代码打印以下内容:

Failed to open helloWorld() (127)

此错误定义为:

  

ERROR_PROC_NOT_FOUND

     

127(0x7F)

     

找不到指定的程序。

我已经使用objconv检查函数是否确实已导出,如果我正确解释了以下内容,我认为它们是正确的:

; Disassembly of file: dll_test.dll
; Sun Nov 15 20:06:06 2015
; Mode: 32 bits
; Syntax: MASM/ML
; Instruction set: 80386

.386
option dotname
.model flat
assume fs:nothing

public ?helloWorld@@YAPBDXZ
public ?getAge@@YA?BHXZ
public Entry_point

extern EncodePointer: near                              ; KERNEL32.dll
extern GetSystemTimeAsFileTime: near                    ; KERNEL32.dll
extern GetCurrentProcessId: near                        ; KERNEL32.dll
extern GetCurrentThreadId: near                         ; KERNEL32.dll
extern GetTickCount: near                               ; KERNEL32.dll
extern QueryPerformanceCounter: near                    ; KERNEL32.dll
extern IsDebuggerPresent: near                          ; KERNEL32.dll
extern SetUnhandledExceptionFilter: near                ; KERNEL32.dll
extern UnhandledExceptionFilter: near                   ; KERNEL32.dll
extern GetCurrentProcess: near                          ; KERNEL32.dll
extern TerminateProcess: near                           ; KERNEL32.dll
extern InterlockedCompareExchange: near                 ; KERNEL32.dll
extern Sleep: near                                      ; KERNEL32.dll
extern InterlockedExchange: near                        ; KERNEL32.dll
extern DecodePointer: near                              ; KERNEL32.dll
extern _except_handler4_common: near                    ; MSVCR100.dll
extern _onexit: near                                    ; MSVCR100.dll
extern _lock: near                                      ; MSVCR100.dll
extern __dllonexit: near                                ; MSVCR100.dll
extern _unlock: near                                    ; MSVCR100.dll
extern __clean_type_info_names_internal: near           ; MSVCR100.dll
extern _crt_debugger_hook: near                         ; MSVCR100.dll
extern __CppXcptFilter: near                            ; MSVCR100.dll
extern _amsg_exit: near                                 ; MSVCR100.dll
extern _initterm_e: near                                ; MSVCR100.dll
extern _initterm: near                                  ; MSVCR100.dll
extern _encoded_null: near                              ; MSVCR100.dll
extern free: near                                       ; MSVCR100.dll
extern _malloc_crt: near                                ; MSVCR100.dll

1 个答案:

答案 0 :(得分:3)

您需要确保导出的函数名称被视为C,否则名称会被破坏。你可以这样做:

extern "C" {

    DLL_TEST_API const char* helloWorld()
    {
        return helloWorldStr;
    }

    DLL_TEST_API const int getAge()
    {
        return age;
    }

}

如果您不知道C头文件是否将在C ++中使用,您可以通过使用linkage中导出符号。 >

#ifdef __cplusplus
extern "C" {
#endif

#ifdef __cplusplus
}
#endif