c++ - Using a .def in Visual Studio instead of dllimport/dllexport with global variables -
in pluginloader.exe's main.cpp:
int lives = 9; the project contains .def file exports lives variable (mangled c++):
exports ?lives@@3ha using dependency walker, verified when opening pluginloader.exe ?lives@@3ha indeed being exported. .lib being exported should contain stubs can link against in other projects. using dumpbin.exe on stub pluginloader.lib get:
4 ?lives@@3ha 1 __import_descriptor_pluginloader 2 __null_import_descriptor 4 __imp_?lives@@3ha 3 ⌂pluginloader_null_thunk_data pluginloader loading simpleplugin.dll using loadlibrary / getprocaddress. simpleplugin.dll has main.cpp looks this:
extern int lives; extern "c" __declspec(dllexport) void pluginmain() { ++lives; } simpleplugin links against stub pluginloader.lib. when trying increment lives, crash access violation. appear simpleplugin.dll pseudo getting own version of lives variable, though linked against stub.
if change simpleplugin's lives to:
__declspec(dllimport) extern int lives; everything works expected. why this? thought purpose of .def not have use dllexport/dllimport. current hypothesis dllimport global variable doing trickery behind scenes (how &lives work in dll vs exe?). have __imp_?lives@@3ha?
note: importing function pointers without dllimport works fine. global variables crash. reproduces in vs 2010 , 2012
sample project: https://db.tt/mav0owop
compiler indeed generates indirection code when use dllimport on data.
the reason dll can export pointer exported data , dllimport magic of dereferencing pointer you. not required function pointers.
Comments
Post a Comment