NO

Author Topic: Help with IFileDialogEvents  (Read 7768 times)

x79

  • Guest
Help with IFileDialogEvents
« on: October 13, 2015, 04:27:42 AM »
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.
Code: [Select]

#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);
}



Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Help with IFileDialogEvents
« Reply #1 on: October 13, 2015, 11:42:57 AM »
Not a real solution, but something to start of.
Run it in debugger.
« Last Edit: October 13, 2015, 01:00:33 PM by TimoVJL »
May the source be with you

x79

  • Guest
Re: Help with IFileDialogEvents
« Reply #2 on: October 13, 2015, 01:32:06 PM »
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

  • Guest
Re: Help with IFileDialogEvents
« Reply #3 on: October 14, 2015, 05:43:37 AM »
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.

Code: [Select]
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;
}

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Help with IFileDialogEvents
« Reply #4 on: October 14, 2015, 09:14:56 AM »
You can't use that pointer without initializing it.
Code: [Select]
...
#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);
}
...
« Last Edit: October 14, 2015, 11:41:17 AM by TimoVJL »
May the source be with you

x79

  • Guest
Re: Help with IFileDialogEvents
« Reply #5 on: October 14, 2015, 10:43:45 AM »
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.
Code: [Select]
...
#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);
}
...

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Help with IFileDialogEvents
« Reply #6 on: October 14, 2015, 12:43:43 PM »
It is a browser problem (pobr), not code.
May the source be with you

x79

  • Guest
Re: Help with IFileDialogEvents
« Reply #7 on: October 19, 2015, 01:38:37 AM »
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)

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Help with IFileDialogEvents
« Reply #8 on: October 19, 2015, 10:25:00 AM »
Check right parameters for CoInitializeEx() from MSDN ;)
Code: [Select]
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);and why you move
Code: [Select]
pFD->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

  • Guest
Re: Help with IFileDialogEvents
« Reply #9 on: October 19, 2015, 01:17:02 PM »
Check right parameters for CoInitializeEx() from MSDN ;)
Code: [Select]
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
LOL!


and why you move
Code: [Select]
pFD->lpVtbl->Unadvise(pFD, dwCookie); to elsewhere?
After dialog is closed, it is not needed any more, so Unadvise it there?
fixed, TY