Posted on 2003-10-30
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:

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)
'  DHCP_CONST WCHAR* ServerIpAddress,
'  DHCP_RESUME_HANDLE* ResumeHandle,
'  DWORD PreferredMaximum,
'  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; _


   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
   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)
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
Question by:Pber
  • 2
LVL 16

Expert Comment

ID: 9651301
LVL 26

Author Comment

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

Accepted Solution

Richie_Simonetti earned 125 total points
ID: 9681927

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel VBA combine two working workbooks 8 53
VBA error replacing data 6 39
VB 6 error 5 in windows 10 but not in XP 7 55
Export Data to Different .csv Files 26 103
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

815 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