halildurmus / filepicker_windows

File and directory picker for Windows that uses common dialog controls.
BSD 3-Clause "New" or "Revised" License
68 stars 19 forks source link

Can't set initial folder #3

Closed jchown closed 1 year ago

jchown commented 3 years ago

For most usages, it would be useful to set the initial folder (e.g. to the parent folder of the last loaded file).

Looking at the underlying code & win32 API, I think it would need to call SHGetKnownFolderPath from shlobj.h, and that this would need to be exposed in the win32 project by adding it to win32api.csv.

If this is the right thing to do I could have a go at it myself?

jchown commented 3 years ago

Sorry, copy and paste mistake there. I'll need to call SHCreateItemFromParsingName (in order to call IFileDialog::SetFolder)

timsneath commented 3 years ago

Sounds good. I've just updated win32 to add this function.

jchown commented 3 years ago

My naive first attempt at this doesn't work:

    if (folder != null && folder.isNotEmpty) {
      final guid = GUID.fromString(IID_IShellItem);
      final shellItem = COMObject.allocate();

      hr = SHCreateItemFromParsingName(TEXT(folder), nullptr, guid.addressOf, shellItem.addressOf);  //  This succeeds
      if (FAILED(hr)) throw WindowsException(hr);

      hr = fileDialog.SetFolder(shellItem.addressOf);   // <-- Fails and takes down the Dart VM
      if (FAILED(hr)) throw WindowsException(hr);
    }

It feels wrong to be using shellItem.addressOf to both functions, as one should be a pointer-to-a-pointer and the other a plain pointer, I think? But I couldn't see how to get the type of shellItem right otherwise.

I'm unfamiliar with the COM model, so I am a little lost.

juandausa commented 1 year ago

Hi @timsneath, we are facing the same issue, do you have any update on this?