Back ground:
We use WMI to do remote install. We create a process to start MSI installation. And it is set to impersonate the user specified.
But we get problem when user was log off.
We failed to do something to the CURRENT_USER node in the registry.
After reviewing the MSI log, we find out that the profile used is the default profile, not the profile for the user specified.
After some search, I find that only impersonate is not enough, we need to load the specific user profile.
It will be loaded when user logon.
It will be loaded if a windows service is running with the specific user.
So, we get the problem when the user is logoff.
Solution:
We need to load user profile by function LoadUserProfile.
BOOL WINAPI LoadUserProfile(
__in HANDLE hToken,
__inout LPPROFILEINFO lpProfileInfo
);
Token for the user, which is returned by the LogonUser, CreateRestrictedToken, DuplicateToken, OpenProcessToken, or OpenThreadToken function. The token must have TOKEN_QUERY, TOKEN_IMPERSONATE, and TOKEN_DUPLICATE access
You may get token handle by function:
BOOL WINAPI OpenProcessToken(
__in HANDLE ProcessHandle,
__in DWORD DesiredAccess,
__out PHANDLE TokenHandle
);
You may get user sid by GetTokenInformation.
BOOL WINAPI GetTokenInformation(
__in HANDLE TokenHandle,
__in TOKEN_INFORMATION_CLASS TokenInformationClass,
__out_opt LPVOID TokenInformation,
__in DWORD TokenInformationLength,
__out PDWORD ReturnLength
);
Get User information by
BOOL WINAPI LookupAccountSid(
__in_opt LPCTSTR lpSystemName,
__in PSID lpSid,
__out_opt LPTSTR lpName,
__inout LPDWORD cchName,
__out_opt LPTSTR lpReferencedDomainName,
__inout LPDWORD cchReferencedDomainName,
__out PSID_NAME_USE peUse
);
And you may get usr profile path by function below in USER_INFO_4
NET_API_STATUS NetUserGetInfo(
__in LPCWSTR servername,
__in LPCWSTR username,
__in DWORD level,
__out LPBYTE *bufptr
);