RasEnumConnections in VB.NET?

'*******this code works in vb6

Const RAS95_MaxEntryName = 256
Const RAS95_MaxDeviceType = 16
Const RAS95_MaxDeviceName = 32

Public Declare Function RasEnumConnections Lib "rasapi32.dll" Alias "RasEnumConnectionsA" (lpRasCon As Any, lpCb As Long, lpcConnections As Long) As Long

Type RASCONN95
    dwSize As Long
    hRasCon As Long
    szEntryName(RAS95_MaxEntryName) As Byte
    szDeviceType(RAS95_MaxDeviceType) As Byte
    szDeviceName(RAS95_MaxDeviceName) As Byte
End Type

Private Function CheckInternetConnection() As Boolean

    Dim TRasCon(255) As RASCONN95
    Dim lg As Long
    Dim lpcon As Long
    Dim retval As Long
   
    TRasCon(0).dwSize = 412
    lg = 256 * TRasCon(0).dwSize
    retval = RasEnumConnections(TRasCon(0), lg, lpcon)
    .
    .
    .
    .
   
End Function

'****** In VB.Net I Upgraded this code (with the Upgrade Wizard) and I added some code but
'****** the code is invalid can someone help me with this job.


'****** Code Upgraded In VB.net
    Const RAS95_MaxEntryName = 256
    Const RAS95_MaxDeviceType = 16
    Const RAS95_MaxDeviceName = 32
    Public Structure RASCONN95
        Dim dwSize As Integer
        Dim hRasCon As Integer
        <VBFixedArray(RAS95_MaxEntryName)> Dim szEntryName() As Byte
        <VBFixedArray(RAS95_MaxDeviceType)> Dim szDeviceType() As Byte
        <VBFixedArray(RAS95_MaxDeviceName)> Dim szDeviceName() As Byte

        Public Sub Initialize()
            ReDim szEntryName(RAS95_MaxEntryName)
            ReDim szDeviceType(RAS95_MaxDeviceType)
            ReDim szDeviceName(RAS95_MaxDeviceName)
        End Sub
    End Structure

    Public Declare Function RasEnumConnections Lib "rasapi32.dll" Alias "RasEnumConnectionsA" (ByRef lpRasCon As RASCONN95, ByRef lpCb As Integer, ByRef lpcConnections As Integer) As Integer

    Private Function CheckInternetConnection() As Boolean
        'On Error GoTo ErrHandler
        'UPGRADE_WARNING: Array TRasCon may need to have individual elements initialized. Click for more: 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="vbup1042"'
        Dim TRasCon(255) As RASCONN95
        Dim i As Integer
        Dim lg As Integer
        Dim lpcon As Integer
        Dim retval As Integer


        For i = 0 To 255
            TRasCon(i).Initialize()
        Next i
        TRasCon(0).dwSize = Len(TRasCon(0))
        lg = 256 * TRasCon(0).dwSize
        'UPGRADE_WARNING: Couldn't resolve default property of object TRasCon(). Click for more: 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="vbup1037"'
        retval = RasEnumConnections(TRasCon(0), lg, lpcon)
      .
      .
      .
      .

    End Function
zirich_2002Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ralf KlattEmployee in Civil ServiceCommented:
Hi,

What I would suggest is NOT UPGRADING!

Step1: Take your old VB6 source code and if it's not already a dll then compile it as DLL with appropriate entry and exit functions.

Step2: When that is done you register the dll using "regsvr32.exe"

Step3: Next create a packet in the COM+ Application sector of your component service manager.

Step4: When that is done you import your registered dll into the in Step3 created packet.

Step5: The last step is to import the dll (COM Component) into your .NET project and add an "Imports" statement to the module where you want to use your RASEnumConnections ... you'll be able to use your old functions as if they'd be a part of .NET.

I'm using a lot of "old" modules that way because once you're used to perform the mentioned steps you'll need less than 5 minutes to get the "old" stuff working ... migration or updates of source code can take from hours to days (even weeks!) ...

Best regards, Raisor
0
vascovCommented:
Hi,

Assuming you do down the COM Interop path, please not that you do not need to put your component in a COM+ Application. You should only do that if you're planning of making use of COM+ Services, otherwise it just adds overhead. All you need is to register you COM dll, using regsvr32.exe

Still, if you want to use VB.NET and therefore don't have dependencies on COM components, here's a simple sample i've built that i believe does what you want.




Imports System.Runtime.InteropServices

Module Module1

    Const RAS95_MaxEntryName = 256
    Const RAS95_MaxDeviceType = 16
    Const RAS95_MaxDeviceName = 32

    Const RAS95_StructSize = 412

    <StructLayout(LayoutKind.Sequential, Size:=RAS95_StructSize)> _
    Public Structure RASCONN95
        Public dwSize As Integer
        Public hRasCon As Integer
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=RAS95_MaxEntryName)> Dim szEntryName As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=RAS95_MaxDeviceType)> Dim szDeviceType As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=RAS95_MaxDeviceName)> Dim szDeviceName As String

        Public Sub New(ByVal size As Integer)
            dwSize = size
        End Sub
    End Structure 'OSVersionInfo

    Public Declare Ansi Function RasEnumConnections Lib "rasapi32.dll" Alias _
        "RasEnumConnectionsA" (<[In](), Out()> ByVal lpRasCon As RASCONN95(), ByRef lpCb As Integer, ByRef lpcConnections As Integer) As Integer

    Private Function GetRASConnections() As RASCONN95()

        Dim lg As Integer
        Dim lpcon As Integer
        Dim retval As Integer

        Dim TRasCon As RASCONN95() = {New RASCONN95(RAS95_StructSize), New RASCONN95(RAS95_StructSize)}

        lpcon = 0
        lg = RAS95_StructSize * TRasCon.Length

        retval = RasEnumConnections(TRasCon, lg, lpcon)

        If (retval = 0) Then
            Console.WriteLine("yupi ")
        End If

        Return TRasCon
    End Function



    Sub Main()
        GetRASConnections()
    End Sub

End Module



HTH
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vascovCommented:
Did this help !?
0
vascovCommented:
zirich,

I provided a working solution, so i think i should be awarded points.

Thanks,

Vasco
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.