Sign in to follow this  
Johannes1991

Win32 IO

Recommended Posts

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);

}


Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this