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.

4,026 Views
#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;
}
About the Author

well I love technology and want to implement new and make the human life easy.ppl say I am kind thanks and I love to hear that.In programming I am not a expert , but love expertise advice , I want to listen and debug and read than code.My #1 programming language is C++. and I love VLSI and FPGA electronic subjects too.Currently I am a 3rd year student of BIT externaland I just completing my degree in informationtechnology. My favourate linux distro is slax. (there is an old story if you know RH you know RH ,if you know slax you know linux) and I love programming with the xlib and still learning it.In this semester we have a compiler theory , AI as subjects. I love those subjects. and Open,and want to think and implement something new. and my idea about the technology is Technologyshould be easily accessible , low cost , and reliable and should make the human life easy.and I living in the bambalapitiya city north colombo sri lanka.where is a best place to learn electronics. :S

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.

Thank you so much!