簡體   English   中英

動態庫加載問題

[英]Dynamic library loading issue

我試圖在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

您需要確保導出的函數名稱被視為C,否則名稱會被破壞。 你可以這樣做:

extern "C" {

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

    DLL_TEST_API const int getAge()
    {
        return age;
    }

}

如果您不知道C頭文件是否將在C ++中使用,您可以通過使用包裝頭文件來確保在C 鏈接中導出符號

#ifdef __cplusplus
extern "C" {
#endif

#ifdef __cplusplus
}
#endif

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM