Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


[WinApi] Open file dialog


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Ripiz   Members   -  Reputation: 529

Like
0Likes
Like

Posted 10 August 2012 - 01:03 PM

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.

Edited by Ripiz, 10 August 2012 - 01:05 PM.


Sponsor:

#2 adeyblue   Members   -  Reputation: 518

Like
1Likes
Like

Posted 10 August 2012 - 01:49 PM

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

#3 Ripiz   Members   -  Reputation: 529

Like
0Likes
Like

Posted 10 August 2012 - 02:03 PM

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

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS