LoadUserProfile not creating local user profile when it does not exist [C#]

Shaitan00

Junior Contributor
Joined
Aug 11, 2003
Messages
358
Location
Hell
I have a tool which I use to programmatically create local user accounts as follows:

Code:
    DirectoryEntry NewUser = dirEntryLocalMachine.Children.Add("UserName", "user");
    NewUser.Invoke("SetPassword", new object[] { "Passsord" });
    NewUser.Invoke("Put", new object[] { "Description", "Description" });
    NewUser.CommitChanges();

The account is created fine but at at this point the User Profile does not exists (no HKEY CURRENT USER, no Documents & Settings, etc...), I was doing some research into this and found the following MSDN article that says calling LoadUserProfile(...) will actually create the profile if it does not exist:
http://support.microsoft.com/kb/196070/en-us

So I added the code as follows:
Code:
    IntPtr hToken = IntPtr.Zero;
    bool bLogon = LogonUser(
            sName,
            sDomain,
            sPassword,
            LOGON32_LOGON_NETWORK,
            LOGON32_PROVIDER_DEFAULT,
            out hToken
            );

    PROFILEINFO profileInfo = new PROFILEINFO();    
    profileInfo.dwSize = Marshal.SizeOf(profileInfo);
    profileInfo.dwFlags = 1;
    profileInfo.lpUserName = sName;
    bool bLoad = LoadUserProfile(hToken, ref profileInfo);

Now, both bLogon and bLoad are true, no exceptions occur, everything "seems" to work fine ...
The contents of profileInfo are not updated (I would have assumed field like .lpProfilePath should have good values) and GetUserProfileDirectory() fails to find the path (obviously - it doesn't exist) - I also check manually and there is nothing under "documents & settings" for the new account.

Anyone have any clues as to what I am doing wrong?

Any help would be much appreciated.
Thanks,
 
Will this be a roaming profile or a local one? The documentation seems to be a bit unclear about the need to specify the .lpProfilePath even for a first access....
Have you tried setting .lpProfilePath to a suitable value and seeing if that works?
 
Issue resolved - the actual MSDN link works fine, the issue was that I was not correctly porting the code to C# and my LogonUser p/invoke was incorrectly implemented with ENUMS which caused for valid results (thus no errors or exceptions) but provided with invalid token types for performing the LoadUserProfile.
(http://support.microsoft.com/kb/196070/en-us)

Thanks to all that helped...!
 
Back
Top