Solved

DHCP API

Posted on 2003-10-30
4
805 Views
Last Modified: 2011-09-20
I'm trying to do some DHCP enumerations in VB6.  I'm trying to do a DhcpEnumSubnets call as per MSDN:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dhcp/dhcp/dhcpenumsubnets.asp

I'm apparently having problems porting this over to VB.  Currently with the code below I get bad DLL calling convention.
Any ideas where I'm going wrong?


Here's my code:

Public Const NERR_Success = 0&
Public Const NERR_Access_Denied = 5&
Public Const NERR_MoreData = 234&

Public Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" _
        (pTo As Any, _
         uFrom As Any, _
         ByVal lSize As Long)
     
'DWORD DHCP_API_FUNCTION DhcpEnumSubnets(
'  DHCP_CONST WCHAR* ServerIpAddress,
'  DHCP_RESUME_HANDLE* ResumeHandle,
'  DWORD PreferredMaximum,
'  LPDHCP_IP_ARRAY* EnumInfo,
'  DWORD* ElementsRead,
'  DWORD* ElementsTotal
');

Declare Function DhcpEnumSubnets Lib "dhcpsapi" _
      (ServerIP As Any, lpResumeHandle As Long, _
      ByVal prefmaxlen As Long, Enuminfo As Long, _
      EntriesRead As Long, TotalEntries As Long)

'typedef struct _DHCP_IP_ARRAY { _
'DWORD NumElements; _
'LPDHCP_IP_ADDRESS Elements;
'} DHCP_IP_ARRAY, *LPDHCP_IP_ARRAY;

'typedef DWORD DHCP_IP_ADDRESS, *PDHCP_IP_ADDRESS, *LPDHCP_IP_ADDRESS;

Type DHCP_IP_ARRAY
   NumElements As Long
   IPaddress As Long
End Type

Public Function DHCPEnum(Server As String)
   Dim yServer() As Byte, lRetCode As Long
   Dim nRead As Long, nTotal As Long
   Dim nret As Long, nResume As Long
   Dim prefmaxlen As Long
   Dim i As Long, x As Long
   Dim lDHCPInfo As Long
   Dim lDHCPInfoPtr As Long
   Dim DHCPInfo As DHCP_IP_ARRAY
     
   yServer = MakeServerName(ByVal Server)
   prefmaxlen = 65536
   nResume = 0

   nret = NERR_MoreData
   Do While (nret = NERR_MoreData)

      nret = DhcpEnumSubnets(yServer(0), nResume, prefmaxlen, _
                      lDHCPInfo, nRead, nTotal)

      If (nret <> NERR_Success And _
       nret <> NERR_MoreData) Then
         'NetError nret
         Debug.Print "Error:" & nret
       
         Exit Do
      End If

      lDHCPInfoPtr = lDHCPInfo
   
      Do While x <= nRead
         CopyMem DHCPInfo, ByVal lDHCPInfoPtr, Len(DHCPInfo)
         Debug.Print DHCPInfo.NumElements
         Debug.Print DHCPInfo.IPaddress
       
         lDHCPInfoPtr = lDHCPInfoPtr + Len(DHCPInfo)
      Loop
   Loop
   
End Function


Public Function MakeServerName(ByVal ServerName As String)
    Dim yServer() As Byte
       
    If ServerName <> "" Then
        If InStr(1, ServerName, "\\") = 0 Then
            ServerName = "\\" & ServerName
        End If
    End If
   
    yServer = ServerName & vbNullChar
    MakeServerName = yServer

End Function
0
Comment
Question by:Pber
  • 2
4 Comments
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9651301
listening...
0
 
LVL 26

Author Comment

by:Pber
ID: 9681729
Do you know what I'm doing wrong converting the API for VB?
0
 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 125 total points
ID: 9681927
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
vb6 programatically bring window to front 8 63
VBA error replacing data 6 36
VBA Shell can't Find Word document 11 74
vbModal 12 31
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now