Hey Guys,
I've got a problem in trying to get the DhcpDeleteClientInfo method in
the DHCP Server Management API working. I *really* think the issue
revolves around my DHCP_SEARCH_INFO_TYPE enum. Eventually what I want
to get done is a delete from the DHCP server by MAC Address. The code
below works for IPAddress, but not Name or MACAddress. both of those
attempts yield the same error: "87: the parameter is incorrect."
Have a look and tell me what you think:
[StructLayout(LayoutKind.Sequential)]
public struct DHCP_BINARY_DATA
{
public int DataLength;
public IntPtr Data;
}
public enum DHCP_SEARCH_INFO_TYPE:int
{
DhcpClientIpAddress =0,
DhcpClientHardwareAddress=1,
DhcpClientName=2
}
[StructLayout(LayoutKind.Sequential)]
public struct DHCP_SEARCH_INFO
{
public uint SearchType;
public UInt32 ClientIpAddress;
public DHCP_BINARY_DATA ClientMacAddress;
[MarshalAs(UnmanagedType.LPWStr)]public string ClientName;
};
[DllImport("dhcpsapi.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
public static extern int DhcpDeleteClientInfo(
string DHCPServer,
IntPtr searchInfo);
[DllImport("netapi32.dll", SetLastError = true)]
public extern static int NetApiBufferFree(IntPtr lpBuffer);
[DllImport("netapi32.dll", SetLastError = true)]
public extern static int NetApiBufferAllocate(int ByteCount,ref int
Buffer);
public static void DoDeletionByIP(string IPAddress)
{
string dhcpServer = "n.n.n.n";
DHCP_SEARCH_INFO SearchInfo = new DHCP_SEARCH_INFO();
SearchInfo.SearchType =
(int)DHCP_SEARCH_INFO_TYPE.DhcpClientIpAddress;
char [] dot = ".".ToCharArray();
char[] zero = "0".ToCharArray();
string [] Octets= IPAddress.Split(dot[0]);
string HexIP = "";
foreach(string Octet in Octets)
{
HexIP += Convert.ToString(Convert.ToInt16(Octet), 16).PadLeft(2,
zero[0]);
}
SearchInfo.ClientIpAddress = Convert.ToUInt32(HexIP, 16);
int bufptr = 0;
int StructSize;
StructSize = Marshal.SizeOf(SearchInfo);
NetApiBufferAllocate(StructSize,ref bufptr);
Marshal.StructureToPtr(SearchInfo, (IntPtr)bufptr, true);
int result = DhcpDeleteClientInfo(dhcpServer, (IntPtr)bufptr);
NetApiBufferFree((IntPtr)bufptr);
if(result == 0)
{
Console.WriteLine(IPAddress+": Deleted");
}
else
{
int code = 0;
unchecked
{
code = (int)result;
}
Win32Exception ex = new Win32Exception(code);
Console.WriteLine(ex.NativeErrorCode + ":" + ex.Message);
}
}
public static void DoDeletionByMAC(string MACAddress)
{
string dhcpServer = "n.n.n.n";
DHCP_SEARCH_INFO SearchInfo = new DHCP_SEARCH_INFO();
SearchInfo.SearchType =
(int)DHCP_SEARCH_INFO_TYPE.DhcpClientIpAddress;
DHCP_BINARY_DATA macAddress = new DHCP_BINARY_DATA();
int discarded = 0;
byte [] macArray = HexEncoding.GetBytes(MACAddress, out discarded);
int size=0;
for(int i=0;i<macArray.Length;i++)
{
size += Marshal.SizeOf(macArray[i]);
}
IntPtr pnt = Marshal.AllocHGlobal(size);
Marshal.Copy(macArray, 0, pnt, macArray.Length);
macAddress.Data= pnt;
macAddress.DataLength= size;
SearchInfo.ClientMacAddress = macAddress;
int bufptr = 0;
int StructSize;
StructSize = Marshal.SizeOf(SearchInfo);
NetApiBufferAllocate(StructSize,ref bufptr);
Marshal.StructureToPtr(SearchInfo, (IntPtr)bufptr, true);
int result = DhcpDeleteClientInfo(dhcpServer, (IntPtr)bufptr);
NetApiBufferFree((IntPtr)bufptr);
if(result == 0)
{
Console.WriteLine(MACAddress+": Deleted");
}
else
{
int code = 0;
unchecked
{
code = (int)result;
}
Win32Exception ex = new Win32Exception(code);
Console.WriteLine(ex.NativeErrorCode + ":" + ex.Message);
}
}
public static void DoDeletionByName(string Name)
{
string dhcpServer = "n.n.n.n";
DHCP_SEARCH_INFO SearchInfo = new DHCP_SEARCH_INFO();
SearchInfo.SearchType = (int)DHCP_SEARCH_INFO_TYPE.DhcpClientName;
SearchInfo.ClientName = Name;
int bufptr = 0;
int StructSize;
StructSize = Marshal.SizeOf(SearchInfo);
NetApiBufferAllocate(StructSize,ref bufptr);
Marshal.StructureToPtr(SearchInfo, (IntPtr)bufptr, true);
int result = DhcpDeleteClientInfo(dhcpServer, (IntPtr)bufptr);
NetApiBufferFree((IntPtr)bufptr);
if(result == 0)
{
Console.WriteLine(Name+": Deleted");
}
else
{
int code = 0;
unchecked
{
code = (int)result;
}
Win32Exception ex = new Win32Exception(code);
Console.WriteLine(ex.NativeErrorCode + ":" + ex.Message);
}
}
Any extra information would be greatly appreciated.
Thanks......