Click here to Skip to main content
16,007,163 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Dll Class Export Problem Pin
6-Jul-01 12:20
suss6-Jul-01 12:20 
GeneralRe: Dll Class Export Problem Pin
Pavlos Touboulidis6-Jul-01 14:16
Pavlos Touboulidis6-Jul-01 14:16 
GeneralRe: Dll Class Export Problem Pin
6-Jul-01 14:54
suss6-Jul-01 14:54 
QuestionPopup menus without MFC? Pin
6-Jul-01 6:13
suss6-Jul-01 6:13 
AnswerRe: Popup menus without MFC? Pin
Tomasz Sowinski6-Jul-01 6:33
Tomasz Sowinski6-Jul-01 6:33 
GeneralRe: Popup menus without MFC? Pin
6-Jul-01 8:25
suss6-Jul-01 8:25 
QuestionHow can I convert a LOGFONT's lfHeight into a CHARFORMAT's yHeight without a handle to a dc? Pin
Bart-Man6-Jul-01 6:02
Bart-Man6-Jul-01 6:02 
Generalquestion about memory allocation in win32 dll Pin
Jim Howard6-Jul-01 6:01
Jim Howard6-Jul-01 6:01 
Howdy folks,

Please help me understand the concept of memory allocation in win32 dll's written in c++. I have found that when I use 'new' and 'delete' I often get access violations. I've done some reading, but am still confused. Blaszczak is silent on this issue. Brain ("Win32 System Services") says to use 'GlobalAlloc' in dlls, but doesn't really say why.

Here is an example that confuses me:

<br />
//Junkdll.cpp<br />
#include <windows.h><br />
#define _COMPILING_DCC237C0_6FEE_11d5_A91C_0050DAC6D85C_<br />
#include "JunkDll.h"<br />
<br />
HGLOBAL secret;	 //char* secret<br />
<br />
BOOL WINAPI main(HINSTANCE dllhandle, DWORD reason, LPVOID situation)<br />
{<br />
	 switch(reason)<br />
	  {<br />
	  case DLL_PROCESS_ATTACH :<br />
		   OutputDebugString("DLL_PROCESS_ATTACH\n");<br />
		   secret = GlobalAlloc(GPTR,100*sizeof(char));     //new char[100] causes access violation;<br />
		   strcpy((char*)secret,"The meaning of life is 42.\n");<br />
	   	break;<br />
	  case DLL_PROCESS_DETACH:<br />
		    OutputDebugString("DLL_PROCESS_DETACH\n");   //delete(secret)doesn't work<br />
   if(secret)<br />
			     GlobalFree(secret);<br />
		    break;<br />
	  	  }<br />
<br />
    return TRUE;<br />
}<br />
<br />
char*  getMeaningOfLife(char* buffer)<br />
{<br />
	 if(buffer != 0)<br />
	 strcpy(buffer,(char*)secret);<br />
	 return buffer;<br />
}<br />
<code> <br />
<br />
In the above example, if I define 'secret' as char* secret, and then use new and delete to allocate a buffer for it I get an acess violation.  When I use GlobalAlloc as shown, it works.  The help files say to use the heap function instead of GlobalAlloc, but I've never used them.<br />
<br />
I'm clearly missing the big picture on memory allocation for dlls.  Brain talks about four different dll memory models, but I'm not grocking the concept.<br />
<br />
thanks<br />
<br />
Jim  <br />
:confused:

GeneralRe: question about memory allocation in win32 dll Pin
Tomasz Sowinski6-Jul-01 6:40
Tomasz Sowinski6-Jul-01 6:40 
GeneralRe: question about memory allocation in win32 dll Pin
Jim Howard6-Jul-01 6:58
Jim Howard6-Jul-01 6:58 
GeneralRe: question about memory allocation in win32 dll Pin
Tomasz Sowinski6-Jul-01 7:07
Tomasz Sowinski6-Jul-01 7:07 
GeneralRe: question about memory allocation in win32 dll Pin
Jim Howard6-Jul-01 8:20
Jim Howard6-Jul-01 8:20 
GeneralFollowup to : question about memory allocation in win32 dll Pin
Jim Howard6-Jul-01 8:59
Jim Howard6-Jul-01 8:59 
GeneralWindow 2000 COM issue (maybe) Pin
Chris Klecker6-Jul-01 5:54
Chris Klecker6-Jul-01 5:54 
GeneralDrag and drop Pin
Kevnar6-Jul-01 5:52
Kevnar6-Jul-01 5:52 
GeneralRe: Drag and drop Pin
Tomasz Sowinski6-Jul-01 6:07
Tomasz Sowinski6-Jul-01 6:07 
Generalstl algorithms Pin
6-Jul-01 4:47
suss6-Jul-01 4:47 
GeneralRe: stl algorithms Pin
Chris Losinger6-Jul-01 5:07
professionalChris Losinger6-Jul-01 5:07 
GeneralRe: stl algorithms Pin
markkuk8-Jul-01 20:52
markkuk8-Jul-01 20:52 
GeneralUnzip a file through VC++ Pin
6-Jul-01 4:01
suss6-Jul-01 4:01 
GeneralRe: Unzip a file through VC++ Pin
Chris Losinger6-Jul-01 4:09
professionalChris Losinger6-Jul-01 4:09 
GeneralRe: Unzip a file through VC++ Pin
6-Jul-01 4:22
suss6-Jul-01 4:22 
GeneralRe: Unzip a file through VC++ Pin
Jack Mott6-Jul-01 8:26
Jack Mott6-Jul-01 8:26 
GeneralRe: Unzip a file through VC++ Pin
PJ Arends6-Jul-01 10:54
professionalPJ Arends6-Jul-01 10:54 
GeneralRe: Unzip a file through VC++ Pin
Bjarke Viksoe7-Jul-01 4:55
Bjarke Viksoe7-Jul-01 4:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.