Help with IFileDialogEvents

Started by x79, October 13, 2015, 04:27:42 AM

Previous topic - Next topic

x79

After much research, trial, and error, I can open an IFileOpenDialog in Windows 7 and even add a custom button. I'm a little lost as to how to catch the IFileDialogEvents. Here is relevant code and attached is my project.

#include <shobjidl.h>
#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "uuid.lib")

void BrowseForFile(void)
{
HRESULT hr;
IFileOpenDialog *pfd = NULL;

hr = CoCreateInstance(&CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, &IID_IFileOpenDialog, (void**)&pfd);
if (SUCCEEDED(hr))
{
IFileDialogCustomize *pfdc = NULL;
hr = pfd->lpVtbl->QueryInterface(pfd, &IID_IFileDialogCustomize, (void**)&pfdc);
if (SUCCEEDED(hr))
{
hr = pfdc->lpVtbl->AddPushButton(pfdc, 6001UL, L"Use Folder");
pfdc->lpVtbl->Release(pfdc);
}
}
if (SUCCEEDED(hr))
{
hr = pfd->lpVtbl->Show(pfd,NULL);
}
if (SUCCEEDED(hr))
{
IShellItem *pItemList = NULL;
hr = pfd->lpVtbl->GetResult(pfd, &pItemList);
if (SUCCEEDED(hr))
{
PWSTR pszPath = NULL;
hr = pItemList->lpVtbl->GetDisplayName(pItemList, SIGDN_FILESYSPATH, &pszPath);
if (SUCCEEDED(hr))
{
MessageBoxW(NULL, pszPath, L"", MB_OK);
}
pItemList->lpVtbl->Release(pItemList);
}
}
pfd->lpVtbl->Release(pfd);
}



TimoVJL

#1
Not a real solution, but something to start of.
Run it in debugger.
May the source be with you

x79

Thank you very much, Timo. This is exactly the information I was looking for. I searched google for this and others many times but it seems hard to search for examples in C but exclude c++ and C#.

x79

Here I am, stuck again  :-[

After I declare IShellItemArray *pSIA when I type 'pSIA->' in the editor, I get an error sound and nothing populates. If I type out a method (GetCount is what I need) and compile, I get no error but when I run the exe and click the custom button, I get an access violation.

HRESULT STDMETHODCALLTYPE IControlEvents_OnButtonClicked(IFileDialogControlEvents * This, IFileDialogCustomize * pfdc, DWORD dwIDCtl)
{
if (dwIDCtl == 6001UL)
{
IShellItemArray *pSIA;
IFolderView2 *pFV2 = NULL;
// HRESULT hr = IUnknown_QueryService(pfdc, SID_SFolderView, (void**)&pFV2);
HRESULT hr = pFV2->lpVtbl->GetSelection(pFV2, -1, &pSIA);
DWORD cItems = 0;
pSIA //->lpVtbl->GetCount(pSIA, &cItems);
DWORD i = 0;
/* while (SUCCEEDED(hr) && i < cItems)
{

}*/
pFV2->lpVtbl->Release;
pSIA->lpVtbl->Release;

MessageBoxA(NULL, "OnButtonClicked", "", MB_OK);
}
return E_NOTIMPL;
}

TimoVJL

#4
You can't use that pointer without initializing it.
...
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

...
if (dwIDCtl == 6001UL)
{
IShellItemArray *pSIA;
IFolderView2 *pFV2 = NULL;
HRESULT hr = IUnknown_QueryService((LPUNKNOWN)pfdc, &SID_SFolderView, &IID_IFileOpenDialog, (void**)&pFV2);
if (!hr) {
HRESULT hr = pFV2->lpVtbl->GetSelection(pFV2, -1, &pSIA);
if (!hr) {
DWORD cItems = 0;
pSIA->lpVtbl->GetCount(pSIA, &cItems);
//pSIA->lpVtbl->
DWORD i = 0;
/* while (SUCCEEDED(hr) && i < cItems)
{

}*/
pSIA->lpVtbl->Release(pSIA);
}
pFV2->lpVtbl->Release(pFV2);
}

MessageBoxA(NULL, "OnButtonClicked", "", MB_OK);
}
...
May the source be with you

x79

Thank you for showing me what I did wrong with IUnknown_QueryService but this is for pFV2. My error is with pSIA. I initialized it to NULL but I still have same problem.
...
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

...
if (dwIDCtl == 6001UL)
{
IShellItemArray *pSIA = NULL;
IFolderView2 *pFV2 = NULL;
HRESULT hr = IUnknown_QueryService((LPUNKNOWN)pfdc, &SID_SFolderView, &IID_IFileOpenDialog, (void**)&pFV2);
if (!hr) {
HRESULT hr = pFV2->lpVtbl->GetSelection(pFV2, -1, &pSIA);
DWORD cItems = 0;
pSIA //  when I type -> here, I get error sound and it does not populate
DWORD i = 0;
/* while (SUCCEEDED(hr) && i < cItems)
{

}*/
pFV2->lpVtbl->Release(pFV2);
pSIA->lpVtbl->Release(pSIA);
}

MessageBoxA(NULL, "OnButtonClicked", "", MB_OK);
}
...

TimoVJL

It is a browser problem (pobr), not code.
May the source be with you

x79

I have been fiddling with this all week and I still can't get it working right.
The error I get is at the pFD->lpVtbl->Advise line. It returns 8000FFFF (Unknown Failure)

TimoVJL

Check right parameters for CoInitializeEx() from MSDN ;)
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);and why you movepFD->lpVtbl->Unadvise(pFD, dwCookie); to elsewhere?
After dialog is closed, it is not needed any more, so Unadvise it there?
May the source be with you

x79

Quote from: TimoVJL on October 19, 2015, 10:25:00 AM
Check right parameters for CoInitializeEx() from MSDN ;)
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
LOL!


Quote from: TimoVJL on October 19, 2015, 10:25:00 AM
and why you movepFD->lpVtbl->Unadvise(pFD, dwCookie); to elsewhere?
After dialog is closed, it is not needed any more, so Unadvise it there?
fixed, TY