I am using the following function to get the path to the desktop of the user running the application:
uses windows, shlobj, ActiveX, ComObj;
function _GetSpecialFolder(const CSIDL: integer) : string;
var
RecPath : PWideChar;
begin
result := '';
RecPath := WideStrAlloc(MAX_PATH);
try
FillChar(RecPath^, MAX_PATH, 0);
if SHGetFolderPathW(0, CSIDL, 0, 0, RecPath) = 0 then result := RecPath;
finally
StrDispose(RecPath);
end;
end;
You can call it like that:
APathToDesktop := _GetSpecialFolder(CSIDL_DESKTOPDIRECTORY);
That's working fine. When the application is run as normal user. However, it's not working when run as administrator. Then the result is the desktop of the administrator, but I need the desktop of the current user running the current windows session.
I have found out that you can use the third parameter of SHGetFolderPathW to path a user token id to get the right path.
But how to find out that user id? I have tried approaches like:
OpenThreadToken(GetCurrentThread, TOKEN_QUERY, True, TokenResult)
But again, TokenResult is always the administrator token (the current thread is run as administrator, that's right). How to get the token of the user that have started Windows and only clicked on "Run as Administrator"?