We help IT Professionals succeed at work.
Get Started

SHBrowseForFolder - Preselecting a folder

Treppenmeister
on
595 Views
Last Modified: 2015-04-13
I am trying to use SHBrowseForFolder to select a folder.
The callback procedure is preselecting a specific folder.


function SelectFolder(Caption, DefPath: string): string;
var
  bi: TBrowseInfo; 
  lpBuffer: PChar; 
  pidlPrograms, pidlBrowse: PItemIDList; 

  function BrowseCallbackProc(hwnd: HWND; uMsg: UINT; lParam: Cardinal; 
    lpData: Cardinal): Integer; stdcall; 
  var 
    PathName: array[0..MAX_PATH] of Char; 
  begin 
    case uMsg of 
      BFFM_INITIALIZED: 
        SendMessage(Hwnd, BFFM_SETSELECTION, Ord(True), Integer(lpData));
      BFFM_SELCHANGED:
        begin 
          SHGetPathFromIDList(PItemIDList(lParam), @PathName); 
          SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0, Longint(PChar(@PathName))); 
        end;
    end; 
    Result := 0; 
  end;
begin
  if (not SUCCEEDED(SHGetSpecialFolderLocation(GetActiveWindow, CSIDL_DRIVES, 
    pidlPrograms))) then exit; 

  GetMem(lpBuffer, MAX_PATH );

  FillChar(BI, SizeOf(BrowseInfo), 0); 
  bi.hwndOwner := GetActiveWindow; 
  bi.pidlRoot := pidlPrograms; 
  bi.pszDisplayName := lpBuffer; 
  bi.lpszTitle := PChar(Caption); 
  bi.ulFlags := BIF_RETURNONLYFSDIRS or BIF_STATUSTEXT; 
  bi.lpfn := @BrowseCallbackProc; 
  bi.lParam := Integer(PChar(DefPath)); 

  pidlBrowse := SHBrowseForFolder(bi); 
...
end;

Open in new window


This works fine with 32-bit.
If i compile with 64 bit then it does not work.
BrowseCallbackProc is called twice - but uMsg is not 1 and 2 (like in the 32 bit version) but 0 the first time and the second time it is a high value.

Can anybody tell me where the problem is?


Windows 7 64 bit, Delphi XE6 & XE7
Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE