memory allocation in dll
-
Hello. my problem is that i'm loading a dll to extract some data but it gets lost when i unload the library, i have the following struct
struct tagPLUGIN{ PCHAR Name; PCHAR FileName; UINT Category; UINT Vital; ULONG Id; UINT isUI; };
and from my main exe i just load the library, get the proccess address and call a dll function to fill the structure// CODE IN DLL extern "C" __declspec(dllexport) void GetPlugInfo (tagPLUGIN *plugin) { AFX_MANAGE_STATE(AfxGetStaticModuleState()); //plugin.FileName="" plugin->Category=TOOL; plugin->Id=0; plugin->isUI=TRUE; plugin->Name=new char[25]; strcpy(plugin->Name,"Configuración de Sistema\0"); plugin->Vital=FALSE; }
loading the dlltagPLUGIN plugin; typedef void (*GetPlugInfo)(tagPLUGIN *info); HMODULE dll=LoadLibrary(tmp_string); GetPlugInfo getPlugInfo = (GetPlugInfo)GetProcAddress(dll,"GetPlugInfo"); (getPlugInfo)(&plugin); plugin.Id=ID++; plugin.FileName=new char[tmp_string.GetLength()+1]; strcpy(plugin.FileName,tmp_string.GetBuffer()); FreeLibrary(dll);
but right after FreeLibrary(dll) my two PCHAR values are corrupt, i know the problem is in thenew char[size]
, so how can i pass a PCHAR to a dll function and allocate and assign a string without loosing it after freelibrary? -
Hello. my problem is that i'm loading a dll to extract some data but it gets lost when i unload the library, i have the following struct
struct tagPLUGIN{ PCHAR Name; PCHAR FileName; UINT Category; UINT Vital; ULONG Id; UINT isUI; };
and from my main exe i just load the library, get the proccess address and call a dll function to fill the structure// CODE IN DLL extern "C" __declspec(dllexport) void GetPlugInfo (tagPLUGIN *plugin) { AFX_MANAGE_STATE(AfxGetStaticModuleState()); //plugin.FileName="" plugin->Category=TOOL; plugin->Id=0; plugin->isUI=TRUE; plugin->Name=new char[25]; strcpy(plugin->Name,"Configuración de Sistema\0"); plugin->Vital=FALSE; }
loading the dlltagPLUGIN plugin; typedef void (*GetPlugInfo)(tagPLUGIN *info); HMODULE dll=LoadLibrary(tmp_string); GetPlugInfo getPlugInfo = (GetPlugInfo)GetProcAddress(dll,"GetPlugInfo"); (getPlugInfo)(&plugin); plugin.Id=ID++; plugin.FileName=new char[tmp_string.GetLength()+1]; strcpy(plugin.FileName,tmp_string.GetBuffer()); FreeLibrary(dll);
but right after FreeLibrary(dll) my two PCHAR values are corrupt, i know the problem is in thenew char[size]
, so how can i pass a PCHAR to a dll function and allocate and assign a string without loosing it after freelibrary?you could do the allocation in the calling module, not in the DLL. or, just use a fixed-size buffer for the text in the structure. or, use global memory (ie. GlobalAlloc, etc) to allocate the strings. Image Toolkits | Image Processing | Cleek
-
you could do the allocation in the calling module, not in the DLL. or, just use a fixed-size buffer for the text in the structure. or, use global memory (ie. GlobalAlloc, etc) to allocate the strings. Image Toolkits | Image Processing | Cleek
-
Hello. my problem is that i'm loading a dll to extract some data but it gets lost when i unload the library, i have the following struct
struct tagPLUGIN{ PCHAR Name; PCHAR FileName; UINT Category; UINT Vital; ULONG Id; UINT isUI; };
and from my main exe i just load the library, get the proccess address and call a dll function to fill the structure// CODE IN DLL extern "C" __declspec(dllexport) void GetPlugInfo (tagPLUGIN *plugin) { AFX_MANAGE_STATE(AfxGetStaticModuleState()); //plugin.FileName="" plugin->Category=TOOL; plugin->Id=0; plugin->isUI=TRUE; plugin->Name=new char[25]; strcpy(plugin->Name,"Configuración de Sistema\0"); plugin->Vital=FALSE; }
loading the dlltagPLUGIN plugin; typedef void (*GetPlugInfo)(tagPLUGIN *info); HMODULE dll=LoadLibrary(tmp_string); GetPlugInfo getPlugInfo = (GetPlugInfo)GetProcAddress(dll,"GetPlugInfo"); (getPlugInfo)(&plugin); plugin.Id=ID++; plugin.FileName=new char[tmp_string.GetLength()+1]; strcpy(plugin.FileName,tmp_string.GetBuffer()); FreeLibrary(dll);
but right after FreeLibrary(dll) my two PCHAR values are corrupt, i know the problem is in thenew char[size]
, so how can i pass a PCHAR to a dll function and allocate and assign a string without loosing it after freelibrary?Your dll, and or exe, is likely being staticly linked to the CRT. That is, the dll new, malloc, alloc, calloc functions are using a heap local to the dll. When the dll is unloaded the heap is destroyed. The debug version is likely set to dynamicly link to the CRT so the exe and dll use the same heap for memory allocations. ...cmk Save the whales - collect the whole set
-
Your dll, and or exe, is likely being staticly linked to the CRT. That is, the dll new, malloc, alloc, calloc functions are using a heap local to the dll. When the dll is unloaded the heap is destroyed. The debug version is likely set to dynamicly link to the CRT so the exe and dll use the same heap for memory allocations. ...cmk Save the whales - collect the whole set
Tthanks for your help. No, i'm using both debug and release in shared dll, the dll and exe both debug uses debug and release uses release, i've tried, new,malloc,GlobalAlloc and Heap Alloc, same result. i'll do some more reading about dlls and retry this, but for now i have an idea, can you tell me if this could be the good way: near that code i have some code to read from the registry and i've noted that when in RegQueryValueEx you pass a buffer too small for the required data (REG_SZ) it returns the size of the buffer needed so you can reallocate the memory for the buffer with that size, so i'll code my GetPlugInfo in that way and i'll pass the PCHAR vars with the memory preallocated. Is that a good solution? Thanks in advance.
-
Tthanks for your help. No, i'm using both debug and release in shared dll, the dll and exe both debug uses debug and release uses release, i've tried, new,malloc,GlobalAlloc and Heap Alloc, same result. i'll do some more reading about dlls and retry this, but for now i have an idea, can you tell me if this could be the good way: near that code i have some code to read from the registry and i've noted that when in RegQueryValueEx you pass a buffer too small for the required data (REG_SZ) it returns the size of the buffer needed so you can reallocate the memory for the buffer with that size, so i'll code my GetPlugInfo in that way and i'll pass the PCHAR vars with the memory preallocated. Is that a good solution? Thanks in advance.
I'm not sure that you understood what i was talking about. In VC++ Goto Projects - Settings. Goto C/C++ - Code Generation. For both your exe and dll the 'Use run-time library' should be: 'Debug Multithreaded DLL' for your debug builds. and 'Multithreaded DLL' for your release builds. There is no reason you can't do what you originally specified. ...cmk Save the whales - collect the whole set