473,402 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Problem with Creating User Profiles on Windows XP

Hi all,
Please ignore the other post.. The cat jumped on the machine and sent it
before I could stop it!!
Was wondering if anybody has expericence this problem..
I am writting an application in C# using windows forms, to create windows
user profiles and then updating the registry information for the new
profiles.
I have managed to create the user successfully and thought that I was
writting the registry information correctly.

That was until I checked the 'C:\Documents and Settings' Directory, when I
noticed that the new users directories did not reflect their names. But
instead comprised of non printable characters!!!

Here is the code sample in C#

//////////////////////////////////////////
DirectoryEntry NewUser;
DirectoryEntry AD = new DirectoryEntry("WinNT://" + Environment.MachineName
+ ",computer");

// delete user when existing
try
{
// this throws when no such user
NewUser = AD.Children.Find(strNodeName, "User");
MessageBox.Show("User already exists!!");
return;
}// Catch not found exception
catch(COMException cex)
{
Console.WriteLine(cex.Message);
}
// Add user using the user schema
NewUser = AD.Children.Add(strNodeName, "user");
NewUser.Properties["description"].Add(this.txtFirstName.Text + " " +
this.txtSurname.Text);

// NewUser.Properties["PasswordExpired"].Add(1); // user must change
password at next login

// Set user flags sample here sets Account disabled, pwd can't change
// this flag is different for LDAP accounts
NewUser.Properties["userFlags"].Add( UF_NORMAL_ACCOUNT |
UF_DONT_EXPIRE_PASSWD );
// invoke native method 'SetPassword' before commiting
// for AD domain accounts this must be done after commiting
NewUser.Invoke("SetPassword", new Object[] { this.txtPassword.Text });
NewUser.CommitChanges();
// Add user to guests alias
DirectoryEntry grp = AD.Children.Find("Guests", "group");
if(grp.Name != null)
{
grp.Invoke("Add", new Object[] {NewUser.Path.ToString()});
}

MessageBox.Show("Account Created Successfully");
AD.Close();
//////////////////////////////////////////
Then I proceed to update the users Registry information!!
At this point their is no Directory for the user in "C:\Documents and
Settings\" directory

//////////////////////////////////////////////////
NativeMethods._PROFILEINFO profile;
IntPtr hToken = IntPtr.Zero;

hToken = NativeMethods.CreateLogonToken(UserAccount, UserDomain,
UserPassword);
if(hToken != IntPtr.Zero)
{
profile = new NativeMethods._PROFILEINFO();
profile.dwSize = Marshal.SizeOf(profile);
profile.lpUserName = UserAccount;

// Directory Still Not Created at this Point!!
int ret = Win32.LoadUserProfile(hToken, ref profile); // This function
Invokes
if(ret != 0)
{
// Use WMI to get the SID of the user (this requires W2K3 or XP)
using (ManagementObject account = new
ManagementObject("Win32_UserAccount.Domain='" + UserDomain +"',Name='" +
UserAccount + "'"))
{
account.Get();
// Get key wich is the root for the users hive loaded in HKU
UserSID = account["SID"].ToString();

if(UserSID.Length != 0)
{ // Now update the registry information for this user!!
UpdateRegistryInformation();
}
}

ret = NativeMethods.UnloadUserProfile(hToken, profile.hProfile);
}
}

[DllImport("advapi32", CharSet=CharSet.Auto, SetLastError=true)]
static extern int LogonUser(string lpszUserName, string lpszDomain, string
lpszPassword, int dwLogonType, int dwLogonProvider, ref HANDLE hToken);

[DllImport("userenv", CharSet=CharSet.Auto)]
internal static extern int LoadUserProfile(HANDLE hToken,ref _PROFILEINFO
lpProfileInfo);
public static HANDLE CreateLogonToken(string name, string domain, string
passwd)
{
HANDLE ret = IntPtr.Zero;
HANDLE tok = IntPtr.Zero;
System.Management.ManagementObject mo = new
System.Management.ManagementObject(new ManagementPath());
mo.Scope.Options.EnablePrivileges = true;

int result = LogonUser(name, ".", passwd, LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, ref tok);
if (result != 0) // If success
{
ret = tok;
}
else
{
string temp = "LogonUser Error: ";
temp += Marshal.GetLastWin32Error().ToString();
MessageBox.Show( temp );
}

return ret;
}
///////////////////////////////

So I stopped writting to the registry information and then physically logged
in as the user via the windows login screen.
This then created the directory correctly!!

Any ideas?

Thanks

Wayne

Nov 16 '05 #1
0 1570

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Wayne Gibson | last post by:
Hi all, Please ignore the other post.. The cat jumped on the machine and sent it before I could stop it!! Was wondering if anybody has expericence this problem.. I am writting an application...
6
by: Rajesh Kumar Mallah | last post by:
Hi, I face the following problem in transferring data from pgsql 7.3.4 to pgsql 7.4 RC1 . I am piping the output of pg_dumpall from 7.3 to 7.4 running on different port. The problem is there...
12
by: SJD | last post by:
I've just read Christoph Schittko's article on XmlSerializer: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnxmlnet/html/trblshtxsd.asp . . . and very informative it is too....
2
by: Paul Lemke | last post by:
Hello, I currently have an application that uses authenticates users via NTLM. I would like to provide a user a directory listing of a folder. Everyone has read access to the folder, but the only...
0
by: Rafa | last post by:
Hello, First, sorry because I am spanish and my english are not good. My problem is that i am trying to work with profiles in visual studio 2005, but i have an error. the error is that: ...
9
by: =?Utf-8?B?Sm9obiBBdXN0aW4=?= | last post by:
I have an app that prints entry tickets. If the printer driver is not set up exactly to detect the black mark on the back of the ticket, the tickets do not print correctly. Because of this, all...
5
by: =?Utf-8?B?cnZhbmdlbGRyb3A=?= | last post by:
Hello, I have a problem with our OnlineBackupService.exe. This is a Windows Service which is built in .Net 1.1 and basically grabs files from the file system and will try to upload them using...
6
by: goraya | last post by:
This is design level discussion about web applications. How I design application that support 1 million concurrent requests??
3
by: samirgulhane | last post by:
Hi, We have developed an application.We are using launcher(Full screen window) to open it(just to prevent a user to type any action in the url) Now i opened two session windows with two...
1
by: =?Utf-8?B?VGVycnk=?= | last post by:
I am using the My.Settings object to save user settings. During testing, I want to 'zero' out what has been saved to start with the defaults again. Where doies the framwork actually persist these...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.