open file dialog

i cannot seem to find a good example on the internet for how to open a file dialog and save it to a string this is what i made:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
char *openfilename(char *filter, HWND owner) {
	 
  OPENFILENAME ofn;
  char fileName[MAX_PATH] = "";
  ZeroMemory(&ofn, sizeof(ofn));

  ofn.lStructSize = sizeof(OPENFILENAME);
  ofn.hwndOwner = owner;
  ofn.lpstrFilter = filter;
  ofn.lpstrFile = fileName;
  ofn.nMaxFile = MAX_PATH;
  ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
  ofn.lpstrDefExt = "";

  char* fileNameStr;

  if ( GetOpenFileName(&ofn) )
    fileNameStr = fileName;

  return fileNameStr;
}

but it doesn't work, it only returns a bunch of numbers, does anyone know whats wrong with it or know an example where it will open a dialog and then actually use the string for something?
It may just not be written here but you never give a path or a file name
It may just not be written here but you never give a path or a file name

its supposed to prompt the user for a filename using an "open file" dialog and then return the path, it opens the dialog but just returns a bunch of numbers. how do i fix it?
Ok, I see your code diplays the prompt on line "17" and tests if for true, by the way BOOL in Windows is NOT the same as bool for C++ make sure you've taken that into account, but then if it's true you assign fileName to fileNameStr and return that. I don't see what you've done with the data stored in ofn. ofn should probably be a pointer to a OPENFILENAME Structure that exists in your main body, and is passed into your openfilename function. Without seeing the rest of your code I would say give that a shot.
Last edited on
Topic archived. No new replies allowed.