c++ - Setting an internal variable of an explicitly linked DLL -
i loading dll explicitly loadlibrary
, use getprocaddress
load function it. far good. following variable defined in header file of dll (readline.h):
readline_dll_impexp file *rl_outstream;
this variable used internally dll, why have change "inside dll". new c++ , couldn't find way set variable in parent cpp file. tried:
hdll = loadlibrary("readline.dll"); hdll.rl_outstream = fopen("outstream.txt","w");
which yields following error:
error c2228: left of '.rl_outstream' must have class/struct/union type 'hinstance' did intend use '->' instead?
- how set dll variable correctly?
- where should have searched find solution problem?
if need set variable inside dll. need export variable. consider following example:
we have 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
where there exported variable named rl_outstream
, can set value this:
#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; }
after compiling , executing test.exe
:
>g++ -wall lib.cpp -shared -o lib.dll >g++ -wall test.cpp -o test.exe >test.exe >type out.txt hello >
Comments
Post a Comment