简体   繁体   中英

How do I find a Profile Dir of a Windows User (not the current user) using Delphi?

Question could be simple enough I was trying with this code:

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);
    ...

Now, this returns the current users Profile Directory. Bare in mind I would like to use this app under Windows XP/Vista/7/8.

Try GetUserProfileDirectory instead of SHGetFolderPath .

Sample (you need bindings for GetUserProfileDirectory in UserEnv.dll):

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM