Most likely the problem is to do with path
if this is simply a const char *
or char *
declared outside the function.
When the openDialog
function returns, both the ofn
structure of type OPENFILENAME
and character array szFile
go out of scope (along with all the other local, stack-allocated variables) and are no longer valid. As a result, the character array that ofn.lpstrFile
and consequently path
point to is no longer valid.
You should allocate space for the file name outside the function and perform a copy with strcpy
, strncpy
or equivalent before returning from it. Another approach would be to move the szFile
character array out of the function and into the containing class as a member variable. Either way, the scope of the character array containing the file name returned needs to extend beyond the lifetime of the execution of the openDialog
function.