【发布时间】:2015-08-14 18:24:18
【问题描述】:
我保留了必需品。
caller.exe 加载 dll,然后调用 dll 中声明的函数 dll_init。
最后一个函数调用公共库中声明的函数“get_ptr”,它应该返回一个指向全局变量的指针。
问题是:
- 每当 caller.exe 调用“get_ptr”时,它都会返回一个与之前在 caller.exe 开始正常分配的指针相同的有效指针。
- 每当 dll 通过它的函数“dll_init”(在被 caller.exe) 调用“get_ptr”(知道 dll 链接到 static lib) 它返回 NULL 指针。
我错过了什么?
libcommon.c
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
/***************header****************/
int *get_ptr(void);
int set_ptr(void);
/**********************************/
int *global=NULL;
//extern __declspec(dllimport) int *global; doesnt work
int set_ptr() {
global = (int *) malloc(sizeof(int));
printf("global allocated to %p\n",global);
*global=485; //random value
return 0;
}
int *get_ptr() {
return global;
}
这里是编译命令(简化的makefile):
gcc.exe -c libcommon.c -o libcommon.o -m32
ar r libcommon.a libcommon.o
ranlib libcommon.a
模块.c
#include <windows.h>
#include <stdio.h>
#if BUILDING_DLL
#define DLLIMPORT __declspec(dllexport)
#else
#define DLLIMPORT __declspec(dllimport)
#endif
DLLIMPORT int dll_init(void) {
int *ptr=(int *) get_ptr();
puts("dll run");
printf("from dll global: %p\n",ptr);
puts("dll end");
return 0;
}
编译:
gcc.exe -c module.c -o module.o -m32 -DBUILDING_DLL=1
gcc.exe -shared module.o -o module.dll -static-libgcc -lws2_32 -m32 -s -L"." -lcommon -Wl,--output-def,libmodule.def,--out-implib,libmodule.a,--add-stdcall-alias
调用者.c
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
typedef void(voidfunc)(void);
voidfunc *fct_ptr;
int test(int a) {
printf("%d",a++);
}
int main() {
set_ptr();
printf("::%p\n",get_ptr());
/* Load the dll then call dll_init */
HMODULE dllptr = LoadLibrary("module.dll");
if (dllptr != NULL) {
fct_ptr = (voidfunc *) GetProcAddress(dllptr, "dll_init");
puts("loaded");
if (fct_ptr != NULL)
fct_ptr();
FreeLibrary(dllptr);
}
}
编译:
gcc.exe caller.c -o caller.exe -m32 -static-libgcc -lws2_32 -L. -lcommon -m32
【问题讨论】:
-
我相信你在内存中有两个不同的地方,名为
global。一个在调用者中,因为您使用 lcommon 对其进行了静态编译。通过set_ptr在那里设置数据。第二个是在module空间中(出于相同的原因),但您从未在那里调用set_ptr()。在dll_init和main中检查global的内存地址。如果它们不相同,则需要在 `dll_init` 中调用set_ptr。
标签: c dll hyperlink static-libraries