modzsi
modzsi

Reputation: 185

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.

Upvotes: 3

Views: 1604

Answers (1)

Sedat Kapanoglu
Sedat Kapanoglu

Reputation: 47690

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;

Upvotes: 4

Related Questions