Sign in to follow this  

c++ and GetOpenFileName

This topic is 2049 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i have tryed to write a general file opening method but its not working
[CODE]
bool GetFileName(string& file, string Message, string fileExtention)
{
char fileName[MAX_PATH] = "";
OPENFILENAME openFile;
ZeroMemory( &openFile, sizeof(OPENFILENAME) );
openFile.lStructSize = sizeof(OPENFILENAME);
openFile.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY ;
string messageString = "All\0*.*\0" + fileExtention + "\0*." + fileExtention + "\0";
openFile.lpstrFilter = messageString.c_str();
openFile.lpstrDefExt = fileExtention.c_str();
openFile.lpstrFile = fileName;
openFile.nMaxFile = MAX_PATH;
// Open file as a text stream
if (!GetOpenFileName(&openFile)) return false;
file = fileName;
return true;
}
[/CODE]

so i can do GetFileName(fileName, "please enter an xml file" , "xml");

but it really is not working , can any one tell me were i am going wrong

Share this post


Link to post
Share on other sites
Not really sure what it is but you could take a look at my implementation for reference (or just use it [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img])

It handles open and save and you can use it like this:

[CODE]
CeFileDlg dlg;
dlg.addFilter("XML-Document", "xml");
std::string fname;
dlg.showOpen(fname);
[/CODE]

.h
[CODE]
#pragma once

#define MAXFILENAMESIZE 1024

class CeFileDlg
{

public:

CeFileDlg(void);
~CeFileDlg(void);

bool showOpen(std::string& fname);
bool showSave(std::string& fname);
void addFilter(std::string title, std::string extension);
void clearFilter();

private:

bool show(std::string& fname, bool open);

OPENFILENAME ofn;
std::string filter;
std::string defExt;
};
[/CODE]

.cpp
[CODE]
#include "PCH.h"
#include "CeFileDlg.h"
#include "Ce.h"

CeFileDlg::CeFileDlg(void)
{
ZeroMemory(&ofn, sizeof(OPENFILENAME));

ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = NULL;
ofn.nMaxFile = MAXFILENAMESIZE;
ofn.lpstrFileTitle = NULL;
ofn.nFilterIndex = 1;
ofn.lpstrInitialDir = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrFilter = "\0";
}

CeFileDlg::~CeFileDlg(void)
{
}

void CeFileDlg::addFilter(std::string title, std::string extension)
{
filter.append(title);
filter.push_back('\0');
filter.append("*.");
filter.append(extension);
filter.push_back('\0');

defExt.clear();
defExt.append(extension);

ofn.lpstrDefExt = &extension[0];
ofn.lpstrFilter = &filter[0];
}

void CeFileDlg::clearFilter()
{
filter = "";
ofn.lpstrFilter = "\0";
}

bool CeFileDlg::showOpen(std::string& fname)
{
return show(fname, true);
}

bool CeFileDlg::showSave(std::string& fname)
{
return show(fname, false);
}

bool CeFileDlg::show(std::string& fname, bool open)
{
char szFile[MAXFILENAMESIZE];

ofn.lpstrFile = szFile;
ofn.lpstrFile[0] = '\0';

if(open)
{
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR | OFN_NONETWORKBUTTON;
GetOpenFileNameA(&ofn);
}
else
{
ofn.Flags = OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR | OFN_NONETWORKBUTTON;
GetSaveFileNameA(&ofn);
}

fname = ofn.lpstrFile;

return !fname.empty();
}
[/CODE]

Share this post


Link to post
Share on other sites
My guess without more information is a unicide problem, as all modern (say within the last 10 years) visual studio projects default to unicode, so UTF-16 version of GetOpenFileName are going to be used by default. The non-unicode versions of the functions exist for backwards compatibility with Win9x apps for the most part and shouldn't be used.

You can explicitly call the ascii version GetOpenFileNameA, but you are almost always better off using GetOpenFileNameW and using wchar_t instead of char. The unicode settings of the project dictate which function the macro GetOpenFileName really points to.

When using the unicode versions of the APis, this also means you need to convert strings made of char[] or std::string to utf-16 with something like MultiByteToWideChar before sending it to GetOpenFileName.

Personally I perfer using UTF-8 globally in applications if possible, and only convert to UTF-16 when dealing with the windows API directly. Edited by Zoner

Share this post


Link to post
Share on other sites

This topic is 2049 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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