When trying to force the creation of the UserProfile using LoadUserProfile I came accross the fact that directory under Documents And Settings was composed of unreadable chars (squares). It should be the username specified by profile.lpUserName = @"mypc\newuser"; (in red in code below).
I used the following code (this is only part of the code)
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)] public struct PROFILEINFO { public int dwSize; public int dwFlags; public String lpUserName; public String lpProfilePath; public String lpDefaultPath; public String lpServerName; public String lpPolicyPath; public IntPtr hProfile; }
[DllImport("Userenv.dll", SetLastError=true, CharSet=System.Runtime.InteropServices.CharSet.Auto)] internal static extern bool LoadUserProfile(IntPtr hToken, ref PROFILEINFO lpProfileInfo);
// Load the profile. ProfileManager.PROFILEINFO profile = new ProfileManager.PROFILEINFO(); profile.dwSize = 32; profile.lpUserName = @"mypc\newuser"; retVal = ProfileManager.LoadUserProfile(dupeTokenHandle, ref profile);
After searching a while I found that changing [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)] above the PROFILEINFO struct to [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)] solved the problem.
CharSet enumeration specifies how managed strings should be marshaled to unmanaged code.
By the way: There seems to be a new property LoadUserProfile in ProcessStartInfo class in .Net 2.0. Hopes this solves the problem in Win2K that you need special privileges to be able to use LogonUser ('Act as part of the operating system'). LogonUser returns a usertoken needed in the LoadUserProfile method. The privilege is very powerfull and poses a security risk.
There seems to be another way to validate a User described here at msdn but need to investigate further to see if I can get a token that way.
9:50:04 PM
|