473,847 Members | 1,525 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.e xe" or
"dhcpobjs.e xe". 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\DH CPOBJS". 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.Man ager dhcpmgr = new DhcpObjects.Man ager();
DhcpObjects.Ser ver dhcpsrvr;
dhcpsrvr = dhcpmgr.Servers .Connect("1.1.1 .1");
// You could also used a scope number such as [1] instead of an IP
address
lblRecordNameRe sult.Text = dhcpsrvr.Scopes["1.1.1.0"].Name.ToString( );

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

DHCPReservation .Name = "TestReservatio n.yourdomain.co m";
DHCPReservation .UniqueID = "0010000000 08";
DHCPReservation .Address = "1.1.1.3";
DHCPReservation .Comment = "This is the description";
DHCPReservation .Update();
dhcpsrvr.Discon nect();
To get the Name of a IP Reservation your could use:
strReservationR esults =
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 11154
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********@gma il.comwrote in message
news:11******** **************@ n67g2000cwd.goo glegroups.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.e xe" or
"dhcpobjs.e xe". 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\DH CPOBJS". 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.Man ager dhcpmgr = new DhcpObjects.Man ager();
DhcpObjects.Ser ver dhcpsrvr;
dhcpsrvr = dhcpmgr.Servers .Connect("1.1.1 .1");
// You could also used a scope number such as [1] instead of an IP
address
lblRecordNameRe sult.Text = dhcpsrvr.Scopes["1.1.1.0"].Name.ToString( );

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

DHCPReservation .Name = "TestReservatio n.yourdomain.co m";
DHCPReservation .UniqueID = "0010000000 08";
DHCPReservation .Address = "1.1.1.3";
DHCPReservation .Comment = "This is the description";
DHCPReservation .Update();
dhcpsrvr.Discon nect();
To get the Name of a IP Reservation your could use:
strReservationR esults =
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
13765
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 exposed the server's configuration, not the current address leases. I was also able to track down an interface that is exposed to C, but 1.) already have a lengthy app written in VB and 2.) don't know enough about C to write my own dll to do...
5
6509
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: System.TypeLoadException. Does anybody have experience working with this API from C# or know of some documentation/examples? Thanks in advance. steve.
3
3315
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 question can be solved, is it also possible to manage the DHCP servers (list, add, remove scopes, ...) ? Thank you.
2
3055
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 whild of inactivity the next access will be slow agian). Changing the client to static IP address instead of DHCP makes the initial access about 1 second which is fine. Monitoring the network of the client computer the cleint is sending out 2...
5
7659
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 it, and from what I've found on the web and in this newsgroup is that it's not easy, however I believe it can/has been done. At the moment I'm just trying to find out information about an existing
0
2991
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 to a text file from the DHCP server snap-in. The text file gives you all the information for every active lease. You might also
0
12363
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, 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...
7
3637
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 infrequently) I am "losing" my dhcp on wireless machines where they will "drop" off the network suddenly and then not reconnect even rebooting, yet if I change to static IP they will reconnect fine (I am trying to stem a flow since I
7
3104
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.
0
9892
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9735
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10658
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10718
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10347
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7889
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7062
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5730
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5915
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.