Open file dialog not opening (using GetOpenFileNameA)

Hi,

I'm trying to get an "Open file" dialog box to open using the following code. When I run the program, however, the "Open file" dialog box does not open.

Edit: This function is, in fact, being called when it is supposed to be.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void GetInputFile()

{
	char szFileNameIN[MAX_PATH];
	char szFileNameOUT[MAX_PATH];

	// get the input file name
	OPENFILENAME ofn;
	ZeroMemory(&fInputPath, sizeof(fInputPath));
	ZeroMemory(&ofn, sizeof(ofn));
	ofn.lStructSize = sizeof(ofn);
	ofn.hwndOwner = NULL;
	ofn.lpstrFilter = LPWSTR("Any File\0*.*\0");
	ofn.lpstrFile = LPWSTR(fInputPath);
	ofn.nMaxFile = MAX_PATH;
	ofn.lpstrFileTitle = LPWSTR(szFileNameIN);
	ofn.nMaxFileTitle = MAX_PATH;
	ofn.lpstrTitle = LPWSTR("Select an input File");
	ofn.Flags = OFN_DONTADDTORECENT | OFN_FILEMUSTEXIST;
	if (GetOpenFileNameA(LPOPENFILENAMEA(&ofn))) // user selected an input file
	{
		MessageBox(NULL,TEXT("It works!"),TEXT(""),MB_OK);
	}
}


Note: Any variable not declared here is a global variable specific to the file that contains this code.

Any help is greatly appreciated!
Last edited on
I'm not really an expert on this, but if the application you are working uses Unicode (I assumed that because you are trying to cast strings into LPWSTRs), you should use GetOpenFileNameW() instead of GetOpenFileNameA(). W is the Unicode version, while A is the ANSI version. Or, you can simply just use GetOpenFileName() without A or W, which is a macro that is defined as the A or W version automatically.
Last edited on
This should work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <tchar.h>
void GetInputFile()
{
  OPENFILENAME ofn = {0};
  TCHAR szFileName[MAX_PATH] = _T("");

  ofn.lStructSize = sizeof(ofn); // SEE NOTE BELOW
  ofn.hwndOwner = 0;
  ofn.lpstrFilter = _T("Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0\0");
  ofn.lpstrFile = szFileName;
  ofn.nMaxFile = MAX_PATH;
  ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
  ofn.lpstrDefExt = _T("txt");

  if (GetOpenFileName(&ofn)) // user selected an input file
  {
    MessageBox(NULL, szFileName, _T("It works!"), MB_OK);
  }
}
Topic archived. No new replies allowed.