473,287 Members | 1,663 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,287 software developers and data experts.

Managing DHCP Servers using C#

After finding nothing anywhere in google I am posting this so everyone
can benefit by it. The formating is not pretty since I copied it from
my word document, but you should benefit by it.

Managing DHCP Servers using C#

They said it was impossible. It couldn't be done. But you can in fact
manage DHCP servers using C#. This includes creating and deleting
Scopes, SuperScopes, Reservations, Exclusions, Options and so forth.
Since the dll that is used was written several years ago by Microsoft,
you cannot manage things on the DNS tab when looking at a reservation
and few things such as that however it gives you main things you would
want to do. The DLL is also written to give you a snapshot view when
querying, which is fine for most C# applications.

Follow these 5 basic steps to get started:
1. Get the DHCPOBJS.DLL from the Windows 2000 Resource Kit
2. Create a COM+ Application for the DHCPOBJS.DLL
3. Convert the DHCPOBJS.DLL to a Common Language runtime assembly using
tlbimp
4. Copy the DHCPOBJECTS.DLL you created in step 3 to the bin directory
of your web application (or add a reference).
5. Setup permissions on your DHCP Servers
6. Start Coding

Get the DHCPOBJS.DLL from the Windows 2000 Resource Kit
Get out your trusty Windows 2000 Server Resource Kit books and break
out that CD in the back that contains all the utilities you thought you
would ever need. If that doesn't work try googling for "Windows
2000 Server Resource Kit" or "dhcpobjs.exe" or
"dhcpobjs.exe". Everything I've read in the newsgroups says that
this tool isn't supported by Microsoft, but then again that is there
standard disclaimer for all Windows 2000 resource kit tools.
Once you get the DHCPOBJS.EXE extract it. Along with the dll there is
a help file that gives a rough overview and some code usage in VBA. It
also includes the object model.

Create a COM+ Application for the DHCPOBJS.DLL

You could simply use regsvr32 to register the DLL, but since you want
to use it in IIS, it is necessary to place the dhcpobjs dll in a COM+
application so that you do not receive "access denied" errors from
your C# web application. If you have already registered this DLL for
any reason, unregister it using regsvr32 /u before creating the COM+
application.
Also once completed the dhcpobjs component can be called from IIS,
VBScript etc without any access denied or other authentication issues.
The COM+ application is given an "Identity" which it uses to
authenticate itself. The information for these steps has been modified
from the justftp.dll COM+ application creation steps located at
http://www.easterndigitalsoftware.co...alkthrough.htm .
IMPORTANT NOTE: For Windows 2003 only, be sure to follow the extra step
required for component access checking changes after step 18.
(This extra step is not necessary for Windows 2000.)

1. Create a directory called "DHCPOBJS" in "C:\Program Files\ComPlus
Applications".
i.e. "C:\Program Files\ComPlus Applications\DHCPOBJS". You could
use any directory, but this will help organize things.
2. Copy the dhcpobjs.dll file into this directory.
3. Start the Component Services GUI from the start menu: Start /
Programs / Administrative Tools / Component Services (Or, Control Panel
/ Administrative Tools / Component Services) or run dcomcnfg.
4. Expand "My Computer" as shown below. Select COM+ Applications and
right click. Select New / Application.

5. The COM+ App. install dialog pops up. Click Next.
6. Click "Create an empty application".

7. Enter the name DHCPOBJS and ensure "Server application" is selected
for the Activation Type. Click Next.

8. At the Identity dialog, select "This user" and enter a valid domain
or local computer account username and password. This account will be
used in Step 5 of the master steps at the beginning of this document. I
recommend something like "DHCPOBJS Administrator" and for it to be
a domain account.

A local administrative user account is recommended but I have found it
works without. (For Windows 2003 only, the "Network Service" account
can be used, however this account will only have "user" group rights).
Click Next. NOTE: At this point, the credentials entered are checked.
If you receive an error, the credentials are invalid for some reason.
9. No "roles" are modified. Simply click Next on the Add Application
Roles dialog.
NOTE: Windows 2000 does not prompt with these dialog boxes.
AGAIN NOTE: Windows 2000 does not prompt with these dialog boxes.
10. Click Next on the Add Users to Roles Dialog box.
11. Click Finish to create the empty COM+ application.
12. The empty DHCPOBJS 'package' should now be showing in the COM+
Applications tree. We will now add the DHCPOBJS component to this
application.
Expand the DHCPOBJS entry and select components.
13. Right click Components and select New Component.
14. The COM+ Component install dialog pops up. Click Next.
15. Click "Install new components".
16. Select DHCPOBJS.dll by browsing to the directory you put the DLL in
step 2. Double click it, (or select it and click "Open").
17. A Install new components dialog should open. Click Next to install
and register the DHCPOBJS component.
18. Click Finish to end the component install process for DHCPOBJS.
NOTE: The above procedure correctly registers DHCPOBJS in the registry.
There is no need to give the manual regsvr32 command.

IMPORTANT NOTE FOR Windows 2003:
Default security settings will restrict access to DHCPOBJS. Access
denied errors will occur unless this extra step is completed.

Perform this extra step on Windows 2003 installations of DHCPOBJS in
COM+.
Open Component Services, locate DHCPOBJS and right click on DHOBJS.
Select properties to open the property dialog.
Select the security Tab and uncheck the "Enforce access checks for this
application" checkbox in the Authorization section.
DHCPOBJS can now be called by IIS, scripts and applications without
"access denied" errors.
(There is no need to perform this step for Windows 2000.)
This completes the installation of DHCPOBJS as a managed COM+
application.

Convert the DHCPOBJS.DLL to a Common Language runtime assembly using
tlbimp

The Type Library Importer converts the type definitions found within a
COM type library into equivalent definitions in a common language
runtime assembly. More information on the Type Library Importer can we
found at http://msdn2.microsoft.com/en-us/lib...sx(VS.80).aspx
.. What that means it you have to make a wrapper around the dhcpobjs.dll
so that .net will recognize your COM+ application you just created. I
have not tried this in anything older than Microsoft Visual Studio
2005.
1. Find the tlbimp.exe file usually located at C:\Program
Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\tlb.
2. Run tlbimp.exe dhcpobjs.dll and it will create DhcpObjects.dll

Copy the DHCPOBJECTS.DLL you created in step 3 to the bin directory of
your web application (or add a reference).
This is pretty self-explanatory, but it creates a reference in your
web application.

Setup permissions on your DHCP Servers
In order to create objects you will be required to have the account
you used when setting up the COM+ application in step 2 to be in the
DHCP Administrators group on the Local machine. Putting that account in
the Domain Group DHCP Administrators will not be enough. It would give
that account permissions to authorize DHCP Servers in AD, but it will
not allow creation of things on the DHCP servers such as reservations
and scopes.
I would recommend creating an AD group similar to "DHCP Management
Administrators" and putting that group in the "DHCP
Administrators" local group found on each DHCP server. Then put your
account used to setup your COM+ application in step 2 in the DHCP
Management Administrators group. You may have to restart your IIS
application and your COM+ application after getting this all done
before it will let you create objects on the DHCP server. You will get
an error saying unable to create object (different from access denied)
if your permissions are not correct.

Start Coding
I don't have a lot of samples to give you but if you read over the
DHCP Objects documentation and look at the VBA examples you should be
able to get a pretty good idea of what is required.
Be sure to add the "using DhcpObjects;" at the top of your page.
To create a reservation:
DhcpObjects.Manager dhcpmgr = new DhcpObjects.Manager();
DhcpObjects.Server dhcpsrvr;
dhcpsrvr = dhcpmgr.Servers.Connect("1.1.1.1");
// You could also used a scope number such as [1] instead of an IP
address
lblRecordNameResult.Text = dhcpsrvr.Scopes["1.1.1.0"].Name.ToString();

Reservation DHCPReservation =
dhcpsrvr.Scopes["10.1.1.0"].Reservations.CreateNew();

DHCPReservation.Name = "TestReservation.yourdomain.com";
DHCPReservation.UniqueID = "001000000008";
DHCPReservation.Address = "1.1.1.3";
DHCPReservation.Comment = "This is the description";
DHCPReservation.Update();
dhcpsrvr.Disconnect();
To get the Name of a IP Reservation your could use:
strReservationResults =
dhcpsrvr.Scopes["1.1.1.0"].Reservations["1.1.1.3";].Name.ToString();
This should get your started. The objection model shows most of the
things you can do. Have fun!

Dec 8 '06 #1
1 11048
After I scan through the writing I would say let the IT Pro to do this and
developer sit back and relax.

chanmm

"Screenbert" <sc********@gmail.comwrote in message
news:11**********************@n67g2000cwd.googlegr oups.com...
After finding nothing anywhere in google I am posting this so everyone
can benefit by it. The formating is not pretty since I copied it from
my word document, but you should benefit by it.

Managing DHCP Servers using C#

They said it was impossible. It couldn't be done. But you can in fact
manage DHCP servers using C#. This includes creating and deleting
Scopes, SuperScopes, Reservations, Exclusions, Options and so forth.
Since the dll that is used was written several years ago by Microsoft,
you cannot manage things on the DNS tab when looking at a reservation
and few things such as that however it gives you main things you would
want to do. The DLL is also written to give you a snapshot view when
querying, which is fine for most C# applications.

Follow these 5 basic steps to get started:
1. Get the DHCPOBJS.DLL from the Windows 2000 Resource Kit
2. Create a COM+ Application for the DHCPOBJS.DLL
3. Convert the DHCPOBJS.DLL to a Common Language runtime assembly using
tlbimp
4. Copy the DHCPOBJECTS.DLL you created in step 3 to the bin directory
of your web application (or add a reference).
5. Setup permissions on your DHCP Servers
6. Start Coding

Get the DHCPOBJS.DLL from the Windows 2000 Resource Kit
Get out your trusty Windows 2000 Server Resource Kit books and break
out that CD in the back that contains all the utilities you thought you
would ever need. If that doesn't work try googling for "Windows
2000 Server Resource Kit" or "dhcpobjs.exe" or
"dhcpobjs.exe". Everything I've read in the newsgroups says that
this tool isn't supported by Microsoft, but then again that is there
standard disclaimer for all Windows 2000 resource kit tools.
Once you get the DHCPOBJS.EXE extract it. Along with the dll there is
a help file that gives a rough overview and some code usage in VBA. It
also includes the object model.

Create a COM+ Application for the DHCPOBJS.DLL

You could simply use regsvr32 to register the DLL, but since you want
to use it in IIS, it is necessary to place the dhcpobjs dll in a COM+
application so that you do not receive "access denied" errors from
your C# web application. If you have already registered this DLL for
any reason, unregister it using regsvr32 /u before creating the COM+
application.
Also once completed the dhcpobjs component can be called from IIS,
VBScript etc without any access denied or other authentication issues.
The COM+ application is given an "Identity" which it uses to
authenticate itself. The information for these steps has been modified
from the justftp.dll COM+ application creation steps located at
http://www.easterndigitalsoftware.co...alkthrough.htm .
IMPORTANT NOTE: For Windows 2003 only, be sure to follow the extra step
required for component access checking changes after step 18.
(This extra step is not necessary for Windows 2000.)

1. Create a directory called "DHCPOBJS" in "C:\Program Files\ComPlus
Applications".
i.e. "C:\Program Files\ComPlus Applications\DHCPOBJS". You could
use any directory, but this will help organize things.
2. Copy the dhcpobjs.dll file into this directory.
3. Start the Component Services GUI from the start menu: Start /
Programs / Administrative Tools / Component Services (Or, Control Panel
/ Administrative Tools / Component Services) or run dcomcnfg.
4. Expand "My Computer" as shown below. Select COM+ Applications and
right click. Select New / Application.

5. The COM+ App. install dialog pops up. Click Next.
6. Click "Create an empty application".

7. Enter the name DHCPOBJS and ensure "Server application" is selected
for the Activation Type. Click Next.

8. At the Identity dialog, select "This user" and enter a valid domain
or local computer account username and password. This account will be
used in Step 5 of the master steps at the beginning of this document. I
recommend something like "DHCPOBJS Administrator" and for it to be
a domain account.

A local administrative user account is recommended but I have found it
works without. (For Windows 2003 only, the "Network Service" account
can be used, however this account will only have "user" group rights).
Click Next. NOTE: At this point, the credentials entered are checked.
If you receive an error, the credentials are invalid for some reason.
9. No "roles" are modified. Simply click Next on the Add Application
Roles dialog.
NOTE: Windows 2000 does not prompt with these dialog boxes.
AGAIN NOTE: Windows 2000 does not prompt with these dialog boxes.
10. Click Next on the Add Users to Roles Dialog box.
11. Click Finish to create the empty COM+ application.
12. The empty DHCPOBJS 'package' should now be showing in the COM+
Applications tree. We will now add the DHCPOBJS component to this
application.
Expand the DHCPOBJS entry and select components.
13. Right click Components and select New Component.
14. The COM+ Component install dialog pops up. Click Next.
15. Click "Install new components".
16. Select DHCPOBJS.dll by browsing to the directory you put the DLL in
step 2. Double click it, (or select it and click "Open").
17. A Install new components dialog should open. Click Next to install
and register the DHCPOBJS component.
18. Click Finish to end the component install process for DHCPOBJS.
NOTE: The above procedure correctly registers DHCPOBJS in the registry.
There is no need to give the manual regsvr32 command.

IMPORTANT NOTE FOR Windows 2003:
Default security settings will restrict access to DHCPOBJS. Access
denied errors will occur unless this extra step is completed.

Perform this extra step on Windows 2003 installations of DHCPOBJS in
COM+.
Open Component Services, locate DHCPOBJS and right click on DHOBJS.
Select properties to open the property dialog.
Select the security Tab and uncheck the "Enforce access checks for this
application" checkbox in the Authorization section.
DHCPOBJS can now be called by IIS, scripts and applications without
"access denied" errors.
(There is no need to perform this step for Windows 2000.)
This completes the installation of DHCPOBJS as a managed COM+
application.

Convert the DHCPOBJS.DLL to a Common Language runtime assembly using
tlbimp

The Type Library Importer converts the type definitions found within a
COM type library into equivalent definitions in a common language
runtime assembly. More information on the Type Library Importer can we
found at http://msdn2.microsoft.com/en-us/lib...sx(VS.80).aspx
. What that means it you have to make a wrapper around the dhcpobjs.dll
so that .net will recognize your COM+ application you just created. I
have not tried this in anything older than Microsoft Visual Studio
2005.
1. Find the tlbimp.exe file usually located at C:\Program
Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\tlb.
2. Run tlbimp.exe dhcpobjs.dll and it will create DhcpObjects.dll

Copy the DHCPOBJECTS.DLL you created in step 3 to the bin directory of
your web application (or add a reference).
This is pretty self-explanatory, but it creates a reference in your
web application.

Setup permissions on your DHCP Servers
In order to create objects you will be required to have the account
you used when setting up the COM+ application in step 2 to be in the
DHCP Administrators group on the Local machine. Putting that account in
the Domain Group DHCP Administrators will not be enough. It would give
that account permissions to authorize DHCP Servers in AD, but it will
not allow creation of things on the DHCP servers such as reservations
and scopes.
I would recommend creating an AD group similar to "DHCP Management
Administrators" and putting that group in the "DHCP
Administrators" local group found on each DHCP server. Then put your
account used to setup your COM+ application in step 2 in the DHCP
Management Administrators group. You may have to restart your IIS
application and your COM+ application after getting this all done
before it will let you create objects on the DHCP server. You will get
an error saying unable to create object (different from access denied)
if your permissions are not correct.

Start Coding
I don't have a lot of samples to give you but if you read over the
DHCP Objects documentation and look at the VBA examples you should be
able to get a pretty good idea of what is required.
Be sure to add the "using DhcpObjects;" at the top of your page.
To create a reservation:
DhcpObjects.Manager dhcpmgr = new DhcpObjects.Manager();
DhcpObjects.Server dhcpsrvr;
dhcpsrvr = dhcpmgr.Servers.Connect("1.1.1.1");
// You could also used a scope number such as [1] instead of an IP
address
lblRecordNameResult.Text = dhcpsrvr.Scopes["1.1.1.0"].Name.ToString();

Reservation DHCPReservation =
dhcpsrvr.Scopes["10.1.1.0"].Reservations.CreateNew();

DHCPReservation.Name = "TestReservation.yourdomain.com";
DHCPReservation.UniqueID = "001000000008";
DHCPReservation.Address = "1.1.1.3";
DHCPReservation.Comment = "This is the description";
DHCPReservation.Update();
dhcpsrvr.Disconnect();
To get the Name of a IP Reservation your could use:
strReservationResults =
dhcpsrvr.Scopes["1.1.1.0"].Reservations["1.1.1.3";].Name.ToString();
This should get your started. The objection model shows most of the
things you can do. Have fun!
Dec 10 '06 #2

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

Similar topics

8
by: Richard D. Ford | last post by:
Does anyone know if VB has an exposed API that can be used to query DHCP for current address leases? I have seen threads that discuss DHCPObj.dll and have successfully used it. However, this only...
5
by: Steve | last post by:
Hi Folks, I'm trying to create a c# class to manage a DHCP subnet using the win api (dhcpsapi.dll). Currently I can't even instanciate my class, instead I receive the error:...
3
by: jmd.msdn | last post by:
Hello. Is it possible to obtain programmatically, with .net 2.0 + C# + Active Directory or P/Invoke, a list of the authorized current DHCP servers in a forest/domain ? And, if the above...
2
by: WhatHappend | last post by:
I have converted a .Net 1.0 application to .Net 2.0 and the web service invocations have delay of around 10seconds on each intial access. After the first access subsequent access are fast (After a...
5
by: Richard | last post by:
Hello, I'm working on an application to allow our network team to use a small application to make DHCP reservations on our Microsoft DHCP Server. The problem is you have to use P/Invoke to do...
0
by: GHUM | last post by:
Hello, I need to get a list of active leases on a windows dhcp server. Experts from Microsoft statet: """" A: Go to the Address leases of each scope in the DHCP snap-in and dump the leases...
0
by: screenbert | last post by:
Managing DHCP Servers using C# They said it was impossible. It couldn't be done. But you can in fact manage DHCP servers using C#. This includes creating and deleting Scopes, SuperScopes,...
7
by: ixeye | last post by:
Anybody else ever had a problem like this? Windows 2003 Domain with 180 machines, 21 of them with XP SP2 Laptop Wireless access to the domain, on three different WIFI AP. Randomly (and luckily...
7
by: =?Utf-8?B?VGhlTWFkSGF0dGVy?= | last post by:
Quick q: If I open a socket on my computer and eventualy the dhcp changes the address of my computer, what happens? Will it drop the connection? Thanks in advance for any help on the topic.
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.