设置显式链接DLL的内部变量
Setting an internal variable of an explicitly linked DLL
我正在用LoadLibrary
显式加载DLL,并使用GetProcAddress
从中加载函数。到目前为止一切顺利。以下变量是在DLL的头文件(readline.h)中定义的:
READLINE_DLL_IMPEXP FILE *rl_outstream;
这个变量由DLL内部使用,这就是为什么我必须在"DLL内部"更改它的原因。我是c++的新手,找不到在父cpp文件中设置此变量的方法。这是我尝试过的:
hDLL = LoadLibrary("readline.dll");
hDLL.rl_outstream = fopen("outstream.txt","w");
只产生以下错误:
error C2228: left of '.rl_outstream' must have class/struct/union
type is 'HINSTANCE'
did you intend to use '->' instead?
- 如何正确设置此DLL变量?
- 我应该在哪里搜索找到解决我的问题的方法?
如果需要在DLL中设置变量。您需要导出这个变量。考虑下面的例子:
我们有这个DLL:
#include <cstdio>
#include <cassert>
#ifdef __cplusplus
extern "C"
{
#endif
FILE *rl_outstream;
void
do_something()
{
assert(rl_outstream);
fputs("HELLO", rl_outstream);
}
#ifdef __cplusplus
};
#endif
如果有一个名为rl_outstream
的导出变量,可以这样设置它的值:
#include <windows.h>
#include <cstdio>
#include <cassert>
int
main(int argc, char **argv)
{
FILE **rl_outstream;
void (*do_something)(void);
HMODULE hModule;
hModule = LoadLibraryW(L"./lib.dll");
assert(hModule);
rl_outstream = (FILE **) GetProcAddress(hModule, "rl_outstream");
assert(rl_outstream);
do_something = (void(*)(void)) GetProcAddress(hModule, "do_something");
assert(do_something);
*rl_outstream = fopen("out.txt", "w");
do_something();
fclose(*rl_outstream);
FreeLibrary(hModule);
return 0;
}
编译执行test.exe
后:
>g++ -Wall lib.cpp -shared -o lib.dll
>g++ -Wall test.cpp -o test.exe
>test.exe
>type out.txt
HELLO
>
相关文章:
- 内联函数中具有内部链接的全局变量
- C++ SSE 内部函数:将结果存储在变量中
- 为什么从另一个构造函数内部调用C++构造函数不修改类变量?
- 是否可以禁止在for循环体内部修改循环变量
- 当使用lambda进行变量的复杂初始化时,如何处理从内部抛出的lambda外部异常
- 在变量名后声明带有 () 的非内部类型与不使用变量名的行为不同。即 std::map<int,char>x(); - 这是怎么回事?
- 使用虚拟变量对 std::vector 内部循环进行切片的最佳方法
- 访问lambda内部循环控制变量
- 如何通过从变量中获取类型来访问内部 typedef
- 解决具有嵌套模板化变量的 Visual Studio 内部编译器错误
- 内部循环的变量
- lambda内部捕获的constexpr变量失去了其constexpr-ness
- 循环时内部的C 声明和初始化变量
- 用名称为“ this”内部括号内的变量在3个不同的编译器上导致不同的结果
- 静态变量是C++中的内部变量还是外部变量
- 在没有“new”关键字的情况下实例化类会导致在堆栈或堆上创建其内部变量
- C ++奇怪的模板对内部变量的专用化
- QT c++内部变量切换
- 函数内部变量与C++中用作参数的变量之间的差异
- 设置显式链接DLL的内部变量