Home > Blockchain >  Profile folder, NetGetUserInfo and SHGetKnownFolderPath return different things for Active Directory
Profile folder, NetGetUserInfo and SHGetKnownFolderPath return different things for Active Directory

Time:01-19

Provided LogonUser can be used to retrieve an impersonation token also for an Active Directory user, I found myself at odds when trying to retrieve that user's profile directory.

SHGetKnownFolderPath(FOLDERID_Profile, ...) works for non AD users just fine, but it errors out for an AD user, even if their Profile path in the user's properties' Profile tab is set. The HRESULT it returns is 0x80070002. I tried passing the flags KF_FLAG_CREATE and KF_FLAG_DONT_VERIFY to SHGetKnownFolderPath, but it doesn't help, same error.

On the other hand, NetGetUserInfo(...) returns an AD user's profile path correctly set in the USER_INFO_4::usri4_profile field, but returns an empty string for non-AD users.

Why is it so? How to get a consistent behavior? Is there any other API I could/should use?

CodePudding user response:

When you pass a token to SHGetKnownFolderPath, all it does is extracting the SID from the token and use that to look up the path under that users registry in HKU.

MSDN hints at this:

In addition to passing the user's hToken, the registry hive of that specific user must be mounted.

You could try helping it by calling LoadUserProfile but I'm not sure what happens if that user has never logged into that specific machine before.

I'd say, try SHGetKnownFolderPath first since it can return paths customized on that specific computer. If it fails, try NetGetUserInfo.

Retrieving a users profile directory is not a common operation because regular programs are not supposed to store files there...

  • Related