gpt4 book ai didi

c++ - 动态库加载问题

转载 作者:可可西里 更新时间:2023-11-01 11:26:35 24 4
gpt4 key购买 nike

我正在尝试制作一个非常简单的 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)

The specified procedure could not be found.

我已经使用 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 中导出 linkage通过使用

包装你的头文件
#ifdef __cplusplus
extern "C" {
#endif

#ifdef __cplusplus
}
#endif

关于c++ - 动态库加载问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33724736/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com