Updated: 11/10/2004; 7:22:38.
Hannes 'Shorty' Decorte's Radio Weblog
Learning .Net and the Experiences doing so


donderdag 30 september 2004

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    comment []

© Copyright 2004 Hannes 'Shorty' Decorte.
 
September 2004
Sun Mon Tue Wed Thu Fri Sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Aug   Oct



Other .Net Bloggers

Bart De Smet
Jan Tielens
Patrick Verbruggen
Patrick Tisseghem
Tom van de Kerkhof's Bloggings
Tom's Corner
Yves Hanoulle

Aggregated Feeds

Belgian .NET Bloggers