Shaitan00 Posted September 26, 2009 Posted September 26, 2009 I have a tool which I use to programmatically create local user accounts as follows: DirectoryEntry NewUser = dirEntryLocalMachine.Children.Add("UserName", "user"); NewUser.Invoke("SetPassword", new object[] { "Passsord" }); NewUser.Invoke("Put", new object[] { "Description", "Description" }); NewUser.CommitChanges(); [/Code] 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); [/Code] 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, Quote
Administrators PlausiblyDamp Posted September 26, 2009 Administrators Posted September 26, 2009 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? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Shaitan00 Posted September 26, 2009 Author Posted September 26, 2009 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...! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.