Skip to main content
GameDev.net gamedev.net
🔒 Locked

[WinApi] Open file dialog

Started by Ripiz Aug 10, 2012 at 7:03 PM 1 replies 4.3k views
Original Post
Ripiz
Ripiz
Hello,
I am trying to create open file dialog. In the past I used to use GetOpenFileName function, however MSDN suggests not to use it and it's not available in Visual Studio 2012 at all, so I had to use IFileOpenDialog interface. Sadly I ran into some problems, it just fails randomly with HRESULT of RPC_E_WRONG_THREAD, even though whole dialog is handled on the single thread. Anyone knows solution to this? Here's my code:

COMDLG_FILTERSPEC c_rgSaveTypes[] = {
{L"Any File", L"*.*"},
};
IFileOpenDialog *pfd;
CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pfd));
assert(pfd);
DWORD dwFlags;
pfd->GetOptions(&dwFlags);
pfd->SetOptions(dwFlags | FOS_FORCEFILESYSTEM);
pfd->SetFileTypes(ARRAYSIZE(c_rgSaveTypes), c_rgSaveTypes);
pfd->SetFileTypeIndex(0);
if(FAILED(pfd->Show(0)))
return;
IShellItem *result;
pfd->GetResult(&result);
PWSTR pszFilePath;
HRESULT hr = result->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); // this call fails
if(FAILED(hr))
return; // breakpoint gets triggered here
pathToFile = string_cast<string>(wstring(pszFilePath));
CoTaskMemFree(pszFilePath);


Thank you in advance.
adeyblue
adeyblue
Did you CoInitialize() or CoInitializeEx(COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE, NULL) before running this code? You'll also need CoUninitialize() after you've finished with the COM stuff.

This code also leaks either the IFileOpenDialog or both it and the IShellItem on those returns (just incase you haven't ellided the cleanup).
Ripiz
Ripiz

Did you CoInitialize() or CoInitializeEx(COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE, NULL) before running this code? You'll also need CoUninitialize() after you've finished with the COM stuff.

No I did not, thank you for pointing out. Seems code sample on MSDN doesn't mention that.


This code also leaks either the IFileOpenDialog or both it and the IShellItem on those returns (just incase you haven't ellided the cleanup).
[/quote]
I didn't omit it, just forgot to write. It just don't feel like I allocate something because of all those functions, therefore I forget to cleanup, thanks for this too, added cleanup.

I ran application a few times and I it doesn't seem to crash anymore, though crashes were quite random so it might be just bad luck.

Thank you for reply.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.