By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,011 Members | 2,967 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,011 IT Pros & Developers. It's quick & easy.

Managing DHCP Servers using C#

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.