如何使用Delphi查找Windows用户(不是当前用户)的Profile Dir?

时间:2013-10-13 17:08:08

标签: windows delphi winapi

问题可能很简单我尝试使用此代码:

var
 lpProfileDir            : tChar;
 lpProfileSize           : Cardinal;
 token                   : tHandle;
 GuestDir,GuestUser      : String;

begin
 GuestUser:=RadioGroup1.Items[RadioGroup1.ItemIndex];
 if LogonUser(PChar(GuestUser), nil, nil, LOGON32_LOGON_SERVICE, LOGON32_PROVIDER_DEFAULT, token) then
  begin
    SetLength(GuestDir, MAX_PATH);
    ZeroMemory(@GuestDir[1], MAX_PATH);
    lpProfileSize:=MAX_PATH;
    if GetUserProfileDirectoryA(token, PChar(GuestDir), lpProfileSize) then
     begin
       ShowMessage(GuestDir);
    ...

现在,这将返回当前用户的配置文件目录。请记住,我想在Windows XP / Vista / 7/8下使用这个应用程序。

1 个答案:

答案 0 :(得分:4)

尝试使用GetUserProfileDirectory代替SHGetFolderPath

示例(您需要在UserEnv.dll中绑定GetUserProfileDirectory):

if LogonUser(PChar(GuestUser), 0, 0, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, token) then 
begin
  SetLength(GuestDir, MAX_PATH);
  ZeroMemory(@GuestDir[1], MAX_PATH);
  if Succeeded(GetUserProfileDirectoryA(token, PChar(GuestDir), MAX_PATH)) then 
    ShowMessage(GuestDir); 
end;