06.05.03

By Michael Bright

Welcome to another "How-to" article in Visual C#.NET. This article will focus on the Network functions within the Win32 API library associated with Network Management. The first thing is to point out is that there are two ways to manage users using the .NET Framework, the first being the Active Directory method, as you will notice you need to have Active Directory Installed and if you were managing users on a small network or on a stand alone station Active Directory won't be install and it would be not worth installing it.

Therefore the other method and focus of this article is using the Platform Invoke layer and the Network functions of the Win32 API library. In this article we will look at how to Add, Delete and Modify Users and Groups using C# and also how we can query User and Network information for a machine or network, we will look at the following functions:
  • NetUserAdd

  • NetUserDel

  • NetUserGetInfo

  • NetUserSetInfo

  • NetUserChangePassword

  • NetUserEnum

  • NetUserGetLocalGroups
Getting Started

First of all as I'm sure most C# developers out there know we have to include the InteropServices namespace in our project to allow us to access functions from DLL's and this can be done using the following snippet of code:


Programming Resource Downloads


///// CODE SNIPPET 1.0

using System.Runtime.InteropServices;

//// END OF CODE


Once we have included the access we can now include the declarations of the DLL's we are going to use and the Structures (structs) associated with them. Each of the calls needed will be discussed under the headings relating to what they do.

Adding a User using C#

One of the most important operations within the Network functions is that of Adding Users to either a network or Computer. To Add a user via C# we will need to use the NetAddUser function, using this function allows us to specify a machine to add the user to or we can leave that null an the User is added to the local machine. In the below code snippet we can see how we can declare and use the NetUserAdd function. One important item associated with the NetUserAdd function is that the user you are added must be in the form of a specific structure. The structures (structs) that are used in association with this call are USER INFO 1, USER INFO 2, USER INFO 3 or USER INFO 4. In this practical example we shall use USER_INFO_1 to define our UserArea to add.

///// CODE SNIPPET 1.1 Declaration

[DllImport("Netapi32.dll")]
extern static int NetUserAdd([MarshalAs(UnmanagedType.LPWStr)] string
servername, int level, ref USER_INFO_1 buf, int parm_err);

//// END OF CODE


You should also note that when i used this code, i didn't need to know about the errors returned, hence the use of an int as the last parameter, if you did want to know about errors returned you would need to amend this code. Now that we have declared our external API to use we should also include our USER_INFO_1 structure, using the following code snippet:

///// CODE SNIPPET 1.2 Structure Declaration

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct USER_INFO_1
{
public string usri1_name;
public string usri1_password;
public int usri1_password_age;
public int usri1_priv;
public string usri1_home_dir;
public string comment;
public int usri1_flags;
public string usri1_script_path;
}

//// END OF CODE


Once we have both of these block of code declare we can now use the call from within our program, i should also note that in this case we have used the structure about you can use any of the other three, and if you click the link you can convert the code from C++ to C# pretty easily. Below is a code snippet to show the usage of the NetUserAdd function.

Click Here to Read the Full Article

About the Author:
My name is Michael Bright, I'm a university student studying a Bsc in Computer Science at University College Chester. I have a love for all thing computers and have only worked with C# for about 3 months. I am currently training for my MCP in it. My other interests are C, VB, HTML, ASP and also Flash. I have interests in Developing Network app's and Network Security. My Web site is csharp.brightweb.co.uk where you can find examples of my Work, including my Defender Security applications.








 

-- CProgrammingTrends is an iEntry, Inc. publication --
2003 iEntry, Inc. All Rights Reserved Privacy Policy  Legal