我要调用SHBrowseForFolder来弹出特殊的文件夹窗体;
编译时没问题; 但程序运行到
SHGetPathFromIDList(idBrowse,tmp);
时DELPHI一直报错,请教一下错在哪里了*_*?
但是我在VB中都没有这种问题的!uses shlobj;procedure TForm1.Button1Click(Sender: TObject);
var
  BrowseInfo: TBrowseInfo;
  DisplayName: array[0..MAX_PATH] of char;
  idBrowse,idl : PItemIDList;
  tmp : PChar;
  tmp1:string;
begin
SHGetSpecialFolderLocation(form1.Handle,17, idl);
  with BrowseInfo do begin
    hWndOwner:=form1.Handle;
    pidlRoot:=idl;
    pszDisplayName:=DisplayName;
    lpszTitle:='Please choose a folder!';
    ulFlags:=BIF_RETURNONLYFSDIRS;
    lpfn:=nil;
    lParam:=0;
  end;
  idBrowse:=SHBrowseForFolder(BrowseInfo);
  if assigned(idBrowse) then
  Begin
     SHGetPathFromIDList(idBrowse,tmp); // SHELL32。DLL错误?
     edit1.Text :=strpas(tmp);
  end;
end;

解决方案 »

  1.   

    {***************************************************************
     * 方 法 名  : GetPath
     * 编写目的   : 选择目录
     * 作    者  : 黄仁光
     * 参    数  : Handle:HWnd
     * 结    果  : string
     * 编写日期   :2002年10月08日
     ****************************************************************}
    function GetPath(Handle:HWnd): string;
    var
      bInfo: ^BROWSEINFO;
      FolderName: array[0..MAX_PATH] of char;
      DirPath: array[0..MAX_PATH] of char;
      ItemID: PITEMIDLIST;
    begin
      DirPath := '';
      bInfo := AllocMem(sizeof(BROWSEINFOA));
      bInfo.hwndOwner := Handle;
      bInfo.pszDisplayName := FolderName;
      bInfo.lpszTitle := '请选择文件夹';
      ItemID := SHBrowseForFolder(bInfo^);
      if ItemID <> nil then
      begin
        SHGetPathFromIDList(ItemID, DirPath);
        FreeMem(bInfo, sizeof(BROWSEINFOA));
      end;
      result := DirPath;
    end;
      

  2.   

    还可以使用SelectDirectory
    在FileCtrl单元里。
    selectdirectory有两种形式,分别是:
    function SelectDirectory(const Caption: string; 
        const Root: WideString; out Directory: string): Boolean; overload;

    function SelectDirectory(var Directory: string; 
        Options: TSelectDirOpts; HelpCtx: Longint): Boolean; overload;第一种形式就是程序中常用的格式,是中文的
      

  3.   

    PChar相当于C中的指针,你指声明了一个指针,却没有为它分配空间当然会报错了!应该用GetMem为tmp分配空间。或者用下面两个解决办法:
    1.在前面把tmp声明为array[0..max_path] of char;再把tmp传过去就好了2.把tmp声明为string;
      SetLength(tmp, max_path);
      再把PChar(tmp)作为参数传过去!