0

I have a mfc projecet and a dll project. My mfc deliver a vector named "out" to a dll's function.When the dll call out.push_back(),the system crash. These only occur in release mode,the debug mode work well.The ide is vs2015.

dll.h:

struct dll_param{
    int x=0;
}

dll.cpp:

void  getParm(vector<dll_param>& out) {
   dll_param t;
   t.x=10;
   out.push_back(t);  //crash here
}

dll.def:

LIBRARY  "mylib"
DESCRIPTION 'KBhook Windows Dynamic Link Library'
EXPORTS
; Explicit exports can Go here
getParm @1

xxx.cpp from mfc:

 HMODULE dll = LoadLibrary(TEXT("mylib.dll")); 
 typedef void(*Fun1)(vector<dll_param>&);
 Fun1 getParm = (Fun1)GetProcAddress(dll, "getParm"); 
 if (getParm) {
     vector<dll_param> out;
     getParm(out);
 }
dino
  • 27
  • 4
  • 1
    Both DLL and application must be built using the same runtime library, and the runtime library must be the DLL version. You more than likely did not do this. – PaulMcKenzie Jan 02 '20 at 14:14
  • @PaulMcKenzie Thank you.I found that my mfc was build in "DL(/MDd)",but my dll was build in "DL(/MD)" – dino Jan 02 '20 at 14:25
  • 1
    Then that will not work, since a `std::vector`'s internals in the debug version is much different than the one in release version. Both DLL and app must use the same runtime library. – PaulMcKenzie Jan 02 '20 at 14:26
  • Does this answer your question? [Implications of using std::vector in a dll exported function](https://stackoverflow.com/questions/10573130/implications-of-using-stdvector-in-a-dll-exported-function) – Raymond Chen Jan 02 '20 at 15:24
  • 1
    The same runtime library is not enough. All modules must use the same *physical* copy of the runtime library. See [Potential Errors Passing CRT Objects Across DLL Boundaries](https://learn.microsoft.com/en-us/cpp/c-runtime-library/potential-errors-passing-crt-objects-across-dll-boundaries) for details. – IInspectable Jan 02 '20 at 15:45
  • @RaymondChen It's a little difficult for me to understand this link.But something in this link show that maybe it's diffcult to share a std::vector from a dll if the version of compiler is different. – dino Jan 02 '20 at 15:48
  • 2
    It's beyond difficult. It's impossible. The modules must agree perfectly on the compiler and runtime library if you are going to pass compiler and runtime-library-specific objects. Since you seem to have the compiler already matched, you just need to match the runtime libraries,. For example, you might have two versions of the DLL, one called "mydll.dll" that is built Release, and another called "mydlld.dll" that is built Debug. The main program then links to the appropriate DLL. Another common solution is to restrict yourself to ABI types when crossing module boundaries. – Raymond Chen Jan 02 '20 at 17:10

0 Answers0