Home All Groups Group Topic Archive Search About

How to script the creation of a client in DHCP

Author
16 Nov 2006 9:38 AM
samuel.lambert
Hello,

I'm trying to create a program for automaticaly create a client in the
dhcp.

I can enumerate the subnets and clients but I can't create client...
error 1783

I 'm usind the Dhcpsapi dll with the DhcpCreateClientInfo function.

Can some one have an example please ?

Thks
Sam

My code : VB 2005


Declare Unicode Function DhcpCreateClientInfo Lib "Dhcpsapi" (ByVal
ServerIpAddress As String, ByRef ClientInfo As IntPtr) As Integer

Private Structure DHCP_CLIENT_INFO

        Dim ClientIpAddress As Int32
        Dim SubnetMask As Int32
        Dim ClientHardwareAddress As DHCP_BINARY_DATA

        <MarshalAs(UnmanagedType.LPWStr)> _
            Dim ClientName As String

        <MarshalAs(UnmanagedType.LPWStr)> _
        Dim ClientComment As String

        Dim ClientLeaseExpires As DHCP_DATE_TIME
        Dim OwnerHost As DHCP_HOST_INFO

    End Structure

  <StructLayout(LayoutKind.Sequential)> _
    Private Structure DHCP_BINARY_DATA
        Dim DataLength As Int32
        Dim Data As IntPtr
    End Structure

    <StructLayout(LayoutKind.Sequential)> _
   Private Structure Date_Time

        <MarshalAs(UnmanagedType.U4)> _
        Dim dwLowDateTime As UInt32

        <MarshalAs(UnmanagedType.U4)> _
        Dim dwHighDateTime As UInt32

    End Structure

    <StructLayout(LayoutKind.Sequential)> _
    Public Structure DHCP_DATE_TIME
        Dim dwLowDateTime As Integer
        Dim dwHighDateTime As Integer
    End Structure

Private Structure DHCP_HOST_INFO

        Dim IpAddress As Int32

        <MarshalAs(UnmanagedType.LPWStr)> _
        Dim NetBiosName As String

        <MarshalAs(UnmanagedType.LPWStr)> _
        Dim HostName As String

    End Structure
'************************************************************************

Sub Main()


        Dim new_dhcp_client As DHCP_CLIENT_INFO
        Dim new_dhcp_client_mac As DHCP_BINARY_DATA
        Dim mac_memory As MAC_INFO

        mac_memory.MAC5 = &H0
        mac_memory.MAC4 = &HB
        mac_memory.MAC3 = &H5D
        mac_memory.MAC2 = &H9A
        mac_memory.MAC1 = &H5C
        mac_memory.MAC0 = &H3A

        Dim macArray(6) As Byte

        macArray(0) = mac_memory.MAC0
        macArray(1) = mac_memory.MAC1
        macArray(2) = mac_memory.MAC2
        macArray(3) = mac_memory.MAC3
        macArray(4) = mac_memory.MAC4
        macArray(5) = mac_memory.MAC5


        Dim pnt As IntPtr = Marshal.AllocHGlobal(6)
        Marshal.Copy(macArray, 0, pnt, 6)
        new_dhcp_client_mac.Data = pnt
        new_dhcp_client_mac.DataLength = 6






        new_dhcp_client.ClientComment = "TEST BY SAM"
        new_dhcp_client.ClientName = "TESTBYSAM"
        new_dhcp_client.OwnerHost.HostName = "dhcpservername"
        new_dhcp_client.OwnerHost.IpAddress = -1633184056
        new_dhcp_client.OwnerHost.NetBiosName = "dhcpservername"
        new_dhcp_client.SubnetMask = -256
        new_dhcp_client.ClientHardwareAddress = new_dhcp_client_mac
        new_dhcp_client.ClientIpAddress = -1633184058
        Dim xx As New IntPtr
         xx = Marshal.AllocHGlobal(Marshal.SizeOf(new_dhcp_client))
        Marshal.StructureToPtr(new_dhcp_client, xx, False)



        Dim errortt As Integer = 0
        errortt = DhcpCreateClientInfo("ipservername", xx)

end sub

AddThis Social Bookmark Button