i have:
- a .dll has been compiled , linked g++ -static
- a client application loads .dll dynamically
to clarify: not catching exception outside .dll, but inside. issue here if compile .dll "-static" , program crashes, without "-static", runs expect do. why "-static" have effect , there way solve this?
i'll give full, minimalistic exsample:
compiling , linking .dll:
g++ -d_debug -ddll_export -d_win32 -o0 -g3 -wall -c -fmessage-length=0 -std=c++11 -m32 -o "src\\abcdll.o" "..\\src\\abcdll.cpp" g++ -static -shared -o libabcdll.dll "src\\abcdll.o" compiling , linking client program:
g++ -o0 -g3 -wall -c -fmessage-length=0 -o "src\\abc.o" "..\\src\\abc.cpp" g++ -o abc.exe "src\\abc.o" code .dll:
#include "abcdll.h" #include <stdexcept> #include <iostream> int abc_func() { try { std::cout << "test" << std::endl; throw std::runtime_error ("error"); } catch (...) { std::cout << "cought"; } return 1; } the header file:
#ifndef abc_h_ #define abc_h_ #if defined dll_export #define decldir extern __declspec(dllexport) #else #define decldir extern __declspec(dllimport) #endif #ifdef __cplusplus extern "c" { #endif decldir int __cdecl abc_func(); #ifdef __cplusplus } // __cplusplus defined. #endif #endif /* abc_h_ */ client program:
#include <iostream> #include <windows.h> #include "abcdll.h" int main() { typedef int (*abc_func) (); abc_func _abc_func; hmodule ldll; ldll = loadlibrary("abcdll.dll"); if(ldll > (void*)hinstance_error){ _abc_func = (abc_func)getprocaddress(ldll, "abc_func"); if (_abc_func != 0) { int ret = _abc_func(); } else { std::cout << "error loading function pointers!" << std::endl; } } else { std::cout << "error loading .dll!" << std::endl; } return 0; }
Comments
Post a Comment