Well inside the windows operating system there are predefinided
dialog boxes. We can use it freely with little effort.

Think that you have to get a input file name or file path. The traditional
way of doing this is parsing the command line arguments.But here we are in win32 , so we should use GetOpenFileName

simple GetOpenFileName just getting an pointer to a data structure
OPENFILENAME that defined on Commdlg.h header file.

so we just create that structure on the memory and passing pass it's
pointer as an argument to the GetOpenFileName function.

and after that returns it normally fills up the OPENFILENAME data structure. So lpszFileName contains the path and we just simply
display that using a MessageBox.

#include "stdafx.h"
#include <windows.h>
#include "resource.h"
#include <Commdlg.h>

// 
// Gobal Variables and declarations.
// 
OPENFILENAME ofn ;


// a another memory buffer to contain the file name
char szFile[100] ;


int WINAPI WinMain( HINSTANCE hInstance , HINSTANCE hPrevInstance , LPSTR lpCmdLine , int nCmdShow )
{
	
    // open a file name
	ZeroMemory( &ofn , sizeof( ofn));
	ofn.lStructSize = sizeof ( ofn );
	ofn.hwndOwner = NULL  ;
	ofn.lpstrFile = szFile ;
	ofn.lpstrFile[0] = '\0';
	ofn.nMaxFile = sizeof( szFile );
	ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0";
	ofn.nFilterIndex =1;
	ofn.lpstrFileTitle = NULL ;
	ofn.nMaxFileTitle = 0 ;
	ofn.lpstrInitialDir=NULL ;
	ofn.Flags = OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST ;

	GetOpenFileName( &ofn );
	
	// Now simpley display the file name 
	MessageBox ( NULL , ofn.lpstrFile , "File Name" , MB_OK);
	return 0;
}

this generates me C2440 error while compiling says that
cannot convert from 'char [100]' to 'LPWSTR'
and
cannot convert from 'const char [20]' to 'LPCWSTR'

That error message means you compiled the program for UNICODE. You can do that, but you have to change strings to unicode strings. There are several ways to do it, but IMHO the best way is to use the _TEXT() macro, for example ofn.lpstrFilter = _TEXT("All\0*.*\0Text\0*.TXT\0"); Doing that the program will compile correctly whether or not the program is compiled for UNICODE.

Hmm yeah it works for this, but there is also another point where I _TEXT()
doesnt work, ofn.lpstrFile = szFile ; putting ofn.lpstrFile = _TEXT(szFile);
gives me another error.
Well I dont wanna compile in unicode, I dont really mind, I just want my program to work can u tell me how not to compile in unicode? or else tell me
how to fix this szFile?

If you are using either VC++ 2008 or 2010 then to go menu item Project --> Propertiess (last item in the list), expand the Configuration Properties tab, General, then on the right side of the screen, third item from bottm change Character Set to Not Set AFAIK that's the only way to do it. I wish Microsoft had set an option in Options to make that the default.

Edited 6 Years Ago by Ancient Dragon: n/a

The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.