Get Extension from a CLSID_FileSaveDialog

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello, I have some code to show a dialog to open or save a file. However the user is forced to write the file extension when saving because the function returns the file path without it. I dont know how to fix this, I want to know what extension did the
user pick from the combobox to append it.
Here is my code:

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; typedef ::std::vector< ::std::pair< ::std::wstring, ::std::wstring> > file_filter;

::std::wstring file_menu_w(<span style="color:Blue; unsigned <span style="color:Blue; int t, file_filter <span style="color:Blue; const& Filter)
{
::std::wstring RetValue;

IFileDialog *pfd;

IID id;
<span style="color:Blue; switch(t)
{
<span style="color:Blue; case 0:
id = CLSID_FileOpenDialog;
<span style="color:Blue; break;
<span style="color:Blue; case 1:
id = CLSID_FileSaveDialog;
<span style="color:Blue; break;
}

<span style="color:Green; // CoCreate the dialog object.
HRESULT hr = CoCreateInstance(id, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pfd));
<span style="color:Blue; if (FAILED(hr))
<span style="color:Blue; return L<span style="color:#A31515; "";

COMDLG_FILTERSPEC* rgSpec = <span style="color:Blue; new COMDLG_FILTERSPEC[Filter.size()];

<span style="color:Blue; for (size_t i = 0; i < Filter.size(); ++i)
{
rgSpec.pszName = Filter.first.c_str(); <span style="color:Green; // "Smart Lib Scene"
rgSpec.pszSpec = Filter.second.c_str(); <span style="color:Green; // "*.sls"
}

hr = pfd->SetFileTypes(<span style="color:Blue; static_cast<<span style="color:Blue; unsigned <span style="color:Blue; int>(Filter.size()), rgSpec);

<span style="color:Blue; if (FAILED(hr))
<span style="color:Blue; return L<span style="color:#A31515; "";

DWORD dwOptions;
hr = pfd->GetOptions(&dwOptions);

<span style="color:Blue; if (SUCCEEDED(hr))
{
<span style="color:Blue; switch(t)
{
<span style="color:Blue; case 0:
hr = pfd->SetOptions(dwOptions | FOS_FORCEFILESYSTEM);
<span style="color:Blue; break;
<span style="color:Blue; case 1:
hr = pfd->SetOptions(dwOptions | FOS_STRICTFILETYPES);
<span style="color:Blue; break;
}
}

<span style="color:Blue; if (SUCCEEDED(hr))
{
<span style="color:Green; // Show the dialog
hr = pfd->Show(0); <span style="color:Green; // hack : deberia pasarsele el hwnd
<span style="color:Green; // if (SUCCEEDED(hr))
{
<span style="color:Green; // Obtain the result of the users interaction with the dialog.
IShellItem *psiResult;
hr = pfd->GetResult(&psiResult);
<span style="color:Blue; if (SUCCEEDED(hr))
{
<span style="color:Green; // Do something with the result.
LPWSTR pPath;
hr = psiResult->GetDisplayName(SIGDN_FILESYSPATH, &pPath);
RetValue = pPath;

psiResult->Release();
}
}
pfd->Release();
}
<span style="color:Blue; return RetValue;
}

[/code]
<br/>
Thanks.

<hr class="sig Isaac Lascasas

View the full article
 
Back
Top