Query DHCP via WMI via C#?

C

coconet

I would like to see a .NET Framework 3.5 or 2.0 example of how to
query the DHCP server using WMI to list all current clients and lease
begin and end datetimes. I want to make a simple web page that shows
this information.

Thanks.
 
J

Jeffrey Tan[MSFT]

Hi Coconet ,

As I know, DHCP doesn't support WMI interface for query, we'd better use
DHCP API to query these information. However, I did not find any C# or .Net
code for this task yet. Below is the detailed C++ code using DHCP API to
obtain these information, you have to perform p/invoke to use it in C#:

// sss.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include "stdafx.h"
#include <windows.h>
#include "Winsock2.h"
#include <dhcpsapi.h> /* Please link dhcpsapi.lib to the project. */

DWORD
DhcpDottedStringToIpAddress(
LPSTR String
)
/*++

Routine Description:
This functions converts a dotted decimal form ASCII string to a
Host order IP address.

Arguments:
String - The address to convert.

Return Value:
The corresponding IP address.

--*/
{
struct in_addr addr;

addr.s_addr = inet_addr( String );
return( ntohl(*(LPDWORD)&addr) );
}


LPSTR
DhcpIpAddressToDottedString(
DWORD IpAddress
)
/*++

Routine Description:

This functions converts a Host order IP address to a dotted decimal
form ASCII string.

Arguments:

IpAddress - Host order IP Address.

Return Value:

String for IP Address.

--*/
{
DWORD NetworkOrderIpAddress;

NetworkOrderIpAddress = htonl(IpAddress);
return(inet_ntoa( *(struct in_addr *)&NetworkOrderIpAddress));
}


int main(int argc, char *argv[])
{
DWORD dwMajor, dwMinor;
DWORD nRet;
LPDHCP_CLIENT_INFO_ARRAY clinfo;
DWORD subnet_ip;
char subnet_ip1[128];
DHCP_RESUME_HANDLE handle = NULL;
DWORD read = 0, total = 0;

if(argc <2)
{
printf (" Please input the DHCP server IP \n");
printf (" for example: sss.exe 192.168.1.1 \n");
return 0;
}

WCHAR GlobalServerIpAddressUnicodeString[256];

MultiByteToWideChar( CP_ACP, 0, argv[1],
strlen(argv[1])+1, GlobalServerIpAddressUnicodeString,

sizeof(GlobalServerIpAddressUnicodeString)/sizeof(GlobalServerIpAddressUnico
deString
[0]) );


nRet = DhcpGetVersion(GlobalServerIpAddressUnicodeString, &dwMajor,
&dwMinor);
if ( nRet == ERROR_SUCCESS )
{
}
else
{
printf("\nDhcpGetVersion() error = %d", nRet);
return 1;
}
/* The argv[1] must be Unicode string that specifies the IP address of the
DHCP
server. */

DWORD dwError = ERROR_MORE_DATA;
DWORD dwElementsRead = 0, dwElementsTotal = 0;
LPDHCP_IP_ARRAY pdhcpIpArray = NULL;
DWORD m_dwPreferredMax = 0xFFFFFFFF;
DHCP_RESUME_HANDLE m_dhcpResumeHandle = NULL;
SYSTEMTIME SystemTime;
FILETIME LocalTime;
//
// for this server, enumerate all of it's subnets
//
while (dwError == ERROR_MORE_DATA)
{
dwError = DhcpEnumSubnets(GlobalServerIpAddressUnicodeString,
&m_dhcpResumeHandle,
m_dwPreferredMax,
&pdhcpIpArray,
&dwElementsRead,
&dwElementsTotal);

if (dwElementsRead && dwElementsTotal && pdhcpIpArray)
{
//
// loop through all of the subnets that were returned
//
for (DWORD i = 0; i < pdhcpIpArray->NumElements; i++)
{

//printf("DHCP Subnet number = %d\n", pdhcpIpArray->NumElements);

strcpy (subnet_ip1, DhcpIpAddressToDottedString(pdhcpIpArray->Elements));
subnet_ip = pdhcpIpArray->Elements;
//printf("DHCP Subnet IP address = %s\n",
DhcpIpAddressToDottedString(subnet_ip));

nRet = DhcpEnumSubnetClients(GlobalServerIpAddressUnicodeString, subnet_ip,
&handle, 0xFFFFFFFF, &clinfo, &read, &total);
if ( (nRet == ERROR_MORE_DATA ) || (nRet == ERROR_SUCCESS) )
{
for (DWORD j=0; j< clinfo->NumElements; j++)
{
wprintf(L"clients name= %s", clinfo->Clients[j]->ClientName);
printf("\tIP= %s \t Subnet IP address=%s\n",
DhcpIpAddressToDottedString(clinfo->Clients[j]->ClientIpAddress),subnet_ip1)
;

printf("\tExpires = ");

FileTimeToLocalFileTime((FILETIME
*)(&clinfo->Clients[j]->ClientLeaseExpires),
&LocalTime) ;
FileTimeToSystemTime( &LocalTime, &SystemTime );

printf( "%02u/%02u/%02u %02u:%02u:%02u.\n",
SystemTime.wMonth,
SystemTime.wDay,
SystemTime.wYear,
SystemTime.wHour,
SystemTime.wMinute,
SystemTime.wSecond );

}
}
else
{
printf("\nDhcpEnumSubnetClients() error = %d", nRet);
}

} //for

} // if

} ///while
return 0;
}

Thanks and Merry Christmas!

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Coconet ,

Have you reviewed my reply to you? Does it make sense to you? If you still
need any help, please feel free to feedback, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top