GetOpenFileName( )

Started by
1 comment, last by Exellion 20 years, 11 months ago
Okey, here''s my problem: Im tried to use GetOpenFileName() to open a file, but it seems like someone dont want me to. (Almost) everything is working out fine if I set the lpstrFile OPENFILENAME-member to NULL, but then I cant retrieve the full path to the file. So then I try to set lpstrFile to: new CHAR[260] But what happens? The GetOpenFileName() function fails and CommDlgExtendedError() returns 2 (CDERR_INITIALIZATION). I looked it up in msdn and it said: "The common dialog box function failed during initialization. This error often occurs when sufficient memory is not available." I just dont see how this can be possible when this is all my program consists of. I''d be really, really appreciative for some help on how to do this properly. Byt the way, here''s the code:
  

OPENFILENAME file;
file.lStructSize	= sizeof(OPENFILENAME);
file.hwndOwner		= m_hWnd;
file.hInstance		= hInstance;

file.lpstrFilter	= NULL;
file.lpstrCustomFilter	= NULL;
file.nMaxCustFilter	= NULL;
file.nFilterIndex	= NULL;
file.lpfnHook		= NULL;
file.lpTemplateName	= NULL;
file.lCustData		= NULL;
file.lpstrInitialDir	= NULL;
file.lpstrDefExt	= NULL;

file.lpstrTitle		= "Test";

file.Flags		= OFN_EXPLORER | OFN_FILEMUSTEXIST;

file.lpstrFileTitle	= new CHAR[260];
file.nMaxFileTitle	= 260;

file.lpstrFile		= NULL;
//file.lpstrFile	= new CHAR[260];

file.nMaxFile		= 260;

GetOpenFileName(&file);
DWORD dwResult = CommDlgExtendedError();
  
"He who hasn''t hacked assembly language as a youth has no heart. He who does as an adult has no brain."
Advertisement
The memory you''re allocating is not properly zeroed. GetOpenFilename is trying to interpret it as a proper filename (the default selection) and fails. Instead, try something like this:


  char filename [FILENAME_MAX] = "";file.lpstrFile = filename;  


That way you don''t have to delete[] the memory after you''re done either.

Kippesoep
oh, it worked great! thanks for the fast reply!

"He who hasn''t hacked assembly language as a youth has no heart. He who does as an adult has no brain."

This topic is closed to new replies.

Advertisement