Failed calling GetOpenFileName using local OPENFILENAME variable

Started by rickyliu, August 28, 2007, 01:34:00 PM

Previous topic - Next topic

rickyliu

Hi All,

I am a newbie in masm32. I am trying to modify the program in Iczelion's Tutorial 11 - More about Dialog Box. I just changed one thing - declared ofn (type OPENFILENAME) as a local variable in WinProc instead of global. After successfully compiled, I ran it and found that the call to GetOpenFileName failed with CDERR_INITIALIZATION (verified by calling CommDlgExtendedError).

Would anyone please tell me what's wrong. Many thanks.

Regards,
Ricky Liu

BogdanOntanu

The LOCAL variables exist only during PROC execution.
Local variables are automatically created on the stack frame each time when the PROC starts and are destroyed when the PROC returns.

Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

MichaelW

I'm guessing here, but I think your problem is that in the original code the OPENFILENAME structure in the .data section was initially filled with zeros, but the local structure contains whatever values happen to be in the stack memory that it occupies. Here are the contents of the original structure, after the initialization code in the IDM_OPEN handler has run:

lStructSize       76
hwndOwner         011F0292
hInstance         00400000
lpstrFilter       00403075
lpstrCustomFilter 00000000
nMaxCustFilter    0
nFilterIndex      0
lpstrFile         00403095
nMaxFile          260
lpstrFileTitle    00000000
nMaxFileTitle     0
lpstrInitialDir   00000000
lpstrTitle        00403199
Flags             00281804
nFileOffset       0
nFileExtension    0
lpstrDefExt       00000000
lCustData         00000000
lpfnHook          00000000
lpTemplateName    00000000


And the same for the local structure:

lStructSize       76
hwndOwner         016701E2
hInstance         00400000
lpstrFilter       00403029
lpstrCustomFilter 004015F6
nMaxCustFilter    23527906
nFilterIndex      15
lpstrFile         00403049
nMaxFile          260
lpstrFileTitle    00000000
nMaxFileTitle     3670022
lpstrInitialDir   00000000
lpstrTitle        0040314D
Flags             00281804
nFileOffset       65535
nFileExtension    65535
lpstrDefExt       0012FECC
lCustData         00130000
lpfnHook          00000005
lpTemplateName    77E1CE00


I was able to correct the problem by zeroing  the local structure before the initialization code runs:

invoke RtlZeroMemory, ADDR ofn, SIZEOF ofn
eschew obfuscation

rickyliu

BogdanOntanu, sorry that I did not state it clearly. The local variable was only referenced within WinProc procedure.

MichaelW, thanks for reply. It works now. I forgot to zero out the structure when calling Win32 API - a good lesson for me to learn. Thanks again. :U