[comp.windows.ms.programmer] Strange clobberings.

memetral@athena.mit.edu (Max E. Metral) (06/12/91)

I am trying to call MessageBox() from a Dialog Box.  If I call
it with MB(hDlg, (LPSTR) "No filename specified", NULL, MB...);

the string is not displayed, and junk is in it's place, and memory
is cold screwed up.

If I call it with a string used elsewhere in the program, defined at the
top as:
	static char DefSpec[10] = "*.*";

It works.

But if I define my own:
	static char Err[100] = "Yo mama";

I get the same error.

Any clues??????

hdg@otter.hpl.hp.com (Hugh Duggan) (06/12/91)

> 
> I am trying to call MessageBox() from a Dialog Box.  If I call
> it with MB(hDlg, (LPSTR) "No filename specified", NULL, MB...);
> 
> the string is not displayed, and junk is in it's place, and memory
> is cold screwed up.
> 
> If I call it with a string used elsewhere in the program, defined at the
> top as:
> 	static char DefSpec[10] = "*.*";
> 
> It works.
> 
> But if I define my own:
> 	static char Err[100] = "Yo mama";
> 
> I get the same error.
> 
> Any clues??????
> ----------

    Your dialogue box function is being called by Windows, not directly
by any code you have written.  Consequently, you cannot guarantee to
have the correct data segment loaded (it is just chance that your
second example worked- in the other cases, Windows has loaded its own
data segment).
    Solution: make sure that the dialogue box function is declared
with the _loadds keyword, or compile your code with a switch which
forces DS to be reloaded on entry to all functions.  This problem will
be seen whenever you try to use any static or global data (ie
allocated from the heap) within a callback function.

    Hugh Duggan
    HP Labs, Bristol

Norbert_Unterberg@p4.f36.n245.z2.fidonet.org (Norbert Unterberg) (06/16/91)

 >     Your dialogue box function is being called by Windows, not directly
 > by any code you have written.  Consequently, you cannot guarantee to
 > have the correct data segment loaded (it is just chance that your
 > second example worked- in the other cases, Windows has loaded its own
 > data segment).

You CAN guaranty that the data segment is set uo correctly. That's one of the 
important tasks of MakeProcInstance. If you pass the `procedure instance 
address' to the DialogBox() function AND list the function in the EXPORT 
section of the DEF file, Windows will load your application's data segment into 
DS whenever it calls the dialog box procedure.

Norbert
Dortmund, FRG