Announcement

Collapse
No announcement yet.

RasEnumEntries on Vista

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • RasEnumEntries on Vista

    This code worked perfectly for XP and now returns an error 632 (ERROR_INVALID_SIZE)
    whenever it is run on Vista (home basic, home premium, and Business).
    Code:
    ' this is the version 5(NT) UDT
    Type RASENTRYNAME_V5
       dwSize As Long
       szEntryName As Asciiz * (%RAS_MaxEntryName + 1 )
       Padding1 As Asciiz * 3
       dwFlags As Long
       szPhonebookPath As Asciiz * %MAX_PATH + 1
       Padding2 As Asciiz * 3
    End Type
    
    Declare Function RasEnumEntries Lib "rasapi32.dll" Alias "RasEnumEntriesA" _
          (  zReserved As Asciiz, zPhoneBook As Asciiz, lpRasEntry As Any, _ 'RASENTRYNAME, _
             lpCb As Long, lpcEntries As Long ) As Long
    
    Function GetRASConnections() Export As String
    
        Dim bsize???, count???, lret???, i&, tmp???, sTemp$, os&
        Dim b(3) As Byte
        Dim ver As OSVERSIONINFO
    
        'Get OS version
        Ver.dwOSVersionInfoSize = SizeOf(OSVERSIONINFO)
        GetVersionEx Ver
        os& = Ver.dwMajorVersion
    
        'Set default structure size
        tmp = SizeOf(RASENTRYNAME)
    
        'if NT, change structure size
        If os& >= 5  Then
        	 tmp = SizeOf(RASENTRYNAME_V5)
    		End If
    
       'Set the structure size to the beginning of our buffer
        MoveMemory ByVal VarPtr(b(0)), ByVal VarPtr(tmp), 4
    
        'Call rasentries to see how many we have
        lret = RasEnumEntries(ByVal 0, ByVal 0, ByVal VarPtr(b(0)),  BSize, count)
    Also note that bsize and count are returned as zero. This would lead me to believe that the
    variable tmp is not the correct size. So then I tried this:
    [code]
    Function GetRASConnections() Export As String

    Dim bsize???, count???, lret???, i&, tmp???, sTemp$, os&
    Dim b(3) As Byte
    Dim ver As OSVERSIONINFO

    local x As long
    x = 0
    'loop through starting at 1
    While %True
    incr x
    tmp = x
    'Set the structure size to the beginning of our buffer
    MoveMemory ByVal VarPtr(b(0)), ByVal VarPtr(tmp), 4
    'Call rasentries to see how many we have
    lret = RasEnumEntries(ByVal 0, ByVal 0, ByVal VarPtr(b(0)), BSize, count)
    If lret <> 632 Or bsize <> 0 Or Count <> 0 Then
    msgBox "hit at " + str$(x)'this message box is never hit
    End If
    Wend
    [\code]
    The above while loop runs infinitely. That would lead me to believe that something else is causing this error.
    However, MS documentation does not seem to give any other causes.

    So, does anybody have RasEnumEntries working on Vista? If so, how? Is there something wrong with the above code?
    Any thoughts, suggestions, and prayers would be appreciated.

    ------------------
    Bill Scharf
    Bill Scharf

  • #2
    The buffer was not correctly specified, try this code:

    Code:
    #Compile Exe
    #Dim All
     
    %USEMACROS = 1
    #Include "win32api.inc"
    #Include "ras32.inc"
     
    Type RASENTRYNAME_V5 ' This is the version 5(NT) UDT
       dwSize As Dword
       szEntryName As Asciiz * (%RAS_MaxEntryName + 1 )
       Padding1 As Asciiz * 3
       dwFlags As Long
       szPhonebookPath As Asciiz * %MAX_PATH + 1
       Padding2 As Asciiz * 3
    End Type
     
    
    Function GetRASConnections() Export As String
     
        Dim lRet As Dword
        Dim dwSize As Dword
        Dim dwEntries As Dword
        Dim ver As OSVERSIONINFO
        Dim hRas As Dword
     
        ' Get OS version
        Ver.dwOSVersionInfoSize = SizeOf(OSVERSIONINFO)
        GetVersionEx Ver
     
        If (Ver.dwMajorVersion => 5) Then
           ' Windows 2000/XP/Vista...
           Dim tRas5(0) As RASENTRYNAME_V5
           tRas5(0).dwSize = SizeOf(RASENTRYNAME_V5)
           hRas = VarPtr(tRas5(0))
           dwSize = SizeOf(RASENTRYNAME_V5)
        Else
           ' All earlier versions...
           Dim tRas(0) As RASENTRYNAME
           tRas(0).dwSize = SizeOf(RASENTRYNAME)
           hRas = VarPtr(tRas(0))
           dwSize = SizeOf(RASENTRYNAME)
        End If
     
        ' Call rasentries to see how many we have
        lRet = RasEnumEntries(ByVal %NULL, ByVal %NULL, ByVal hRas, dwSize, dwEntries)
        
        ' Must return at least one entry...
        If (lRet <> 0) Or (dwEntries = 0) Then Exit Function
     
        If (Ver.dwMajorVersion => 5) Then
           ' Windows 2000/XP/Vista...
           ReDim tRas5(0 To dwEntries-1) As RASENTRYNAME_V5
           tRas5(0).dwSize = SizeOf(RASENTRYNAME_V5)
           hRas = VarPtr(tRas5(0))
           dwSize = SizeOf(RASENTRYNAME_V5)
        Else
           ' All earlier versions...
           ReDim tRas(0 To dwEntries-1) As RASENTRYNAME
           tRas(0).dwSize = SizeOf(RASENTRYNAME)
           hRas = VarPtr(tRas(0))
           dwSize = SizeOf(RASENTRYNAME)
        End If
        
        ' Get the entries into the array...
        lRet = RasEnumEntries(ByVal %NULL, ByVal %NULL, ByVal hRas, dwSize, dwEntries)
        ' Failed to get entries...
        If (lRet <> 0) Then Exit Function
        
        ' ##### Test code: display the entries...
        Local i As Long
        If (Ver.dwMajorVersion => 5) Then
           ' Windows 2000/XP/Vista...
           For i = 0 To UBound(tRas5)
               ? tRas5(i).szEntryName
           Next i
        Else
           ' All earlier versions...
           For i = 0 To UBound(tRas)
               ? tRas(i).szEntryName
           Next i
        End If
     
    End Function
     
    
    Function PBMain
      GetRASConnections
    End Function
    ------------------
    kgpsoftware.com - Downloads
    kgpsoftware.com - Development and Consulting
    kgpsoftware.com | Slam DBMS | PrpT Control | Other Downloads | Contact Me

    Comment


    • #3
      Thank you very much.
      That works nicely for Vista Business and I will test for prior OS's but I bet it works for them also.
      I very much appreciate your help.

      ------------------
      Bill Scharf
      Bill Scharf

      Comment


      • #4
        Two small changes were needed to get this to work with more than 1 connection:
        Code:
        ...
            If (Ver.dwMajorVersion => 5) Then
               ' Windows 2000/XP/Vista...
               ReDim tRas5(0 To dwEntries-1) As RASENTRYNAME_V5
               tRas5(0).dwSize = SizeOf(RASENTRYNAME_V5) 
               hRas = VarPtr(tRas5(0))
               'dwSize = SizeOf(RASENTRYNAME_V5)
            Else
               ' All earlier versions...
               ReDim tRas(0 To dwEntries-1) As RASENTRYNAME
               tRas(0).dwSize = SizeOf(RASENTRYNAME)
               hRas = VarPtr(tRas(0))
               'dwSize = SizeOf(RASENTRYNAME)
            End If
        ...
        Dwsize is passed back from the first call of RASENUMENTRIES, so it
        does not have to be set again. So the two lines where dwSize is set for the second time are not needed.
        Again, Kev's code is great and a big help! thank you!

        ------------------
        Bill Scharf

        [This message has been edited by Bill Scharf (edited July 27, 2007).]
        Bill Scharf

        Comment


        • #5
          Bill,

          Yup, Sorry for that oversight. I don't have any entries in the remote phonebook so I was blind with the results. Glad it works though

          ------------------
          kgpsoftware.com - Downloads
          kgpsoftware.com - Development and Consulting
          kgpsoftware.com | Slam DBMS | PrpT Control | Other Downloads | Contact Me

          Comment

          Working...
          X