Win32 IO

Started by
4 comments, last by Johannes1991 14 years, 9 months ago
I need some serious help here. I've been googling like hell to find some decent win32 c++ IO tutorial. I've been testing GetOpenFileName() but it doesn't write me anything to a string when i select a file. Can you PROs share some knowledge?
EnJOJ Gaming
Advertisement
Do you mean you don't get a string with the filename of the selected file, or do you want to open the file and read/write data to it?
You get the filename to the lpstrFile member of the OPENFILENAME structure you pass to the GetOpenFileName function. MSDN documentation: GetOpenFilename, OPENFILENAME Structure.

An example, read the documentation for OPENFILENAME for details on flags etc.
OPENFILENAME ofn;TCHAR szFileName[MAX_PATH];ZeroMemory(&ofn, sizeof(ofn));ofn.lStructSize = sizeof(ofn);ofn.lpstrFilter = "All Files\0*.*\0";ofn.lpstrFile = szFileName;ofn.nMaxFile = MAX_PATH;ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;if(GetOpenFileName(&ofn) != 0) {		std::string fileName(szFileName);	}
Do you have to use a char? cause i tried with a LPWSTR. When i tried using char the window were you choose file wont open.

Here's my code for it

LPWSTR Namn=NULL;

OPENFILENAME ofn;
memset( &ofn, 0, sizeof(ofn) );


ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hWnd;
ofn.lpstrFilter = L"X Filer (*.x)\0*.x\0Alla Files (*.*)\0*.*\0";
ofn.lpstrCustomFilter = NULL;
ofn.lpstrFile = Namn;
ofn.nMaxFile = sizeof(Namn);
ofn.lpstrDefExt = L"*.x";
ofn.lpstrTitle = L"Välj en Modell(.x fil)";
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_NOCHANGEDIR | OFN_OVERWRITEPROMPT;

GetOpenFileName(&ofn);

[Edited by - Johannes1991 on July 21, 2009 2:42:15 PM]
EnJOJ Gaming
ofn.lpstrFile must not be null. It must be a valid memory location.
ofn.nMaxFile should contain the length of that string, sizeof(Namn) does not do what you think it does.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

The code I posted had Unicode-problems.. sorry about that. This is the way it should be:
OPENFILENAME ofn;TCHAR fileName[MAX_PATH];ZeroMemory(&ofn, sizeof(ofn));ofn.lStructSize = sizeof(ofn);ofn.lpstrFilter = TEXT("X Files\0*.x\0All Files\0*.*\0");ofn.lpstrFile = fileName;ofn.nMaxFile = MAX_PATH;ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;if(GetOpenFileName(&ofn) != 0) {	// fileName contains the string here		// You can do the following, since an LPWSTR is a TCHAR* for unicode programs:	LPWSTR Namn = fileName;}

Every member of the OPENFILENAME structure is explained in detail here: OPENFILENAME Structure.
Any additional information you might need should be available in that documentation, such as what values are acceptable for every input.
I got it to work! i used memset(&Namn,MAX_PATH,sizeof(Namn));

ThanX to everyone you all pointed me to the direction!
EnJOJ Gaming

This topic is closed to new replies.

Advertisement