A simple GetOpenFileName example

NicAx64 0 Tallied Votes 18K Views Share

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;
}
D4n1sD 0 Light Poster

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'

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

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.

D4n1sD 0 Light Poster

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?

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

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.

Y_2 0 Newbie Poster

Thank you so much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.