Solved

RasEnumConnections in VB.NET?

Posted on 2003-11-28
5
1,419 Views
Last Modified: 2012-05-04
'*******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
0
Comment
Question by:zirich_2002
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 15

Expert Comment

by:Raisor
ID: 9845247
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
 
LVL 12

Accepted Solution

by:
vascov earned 125 total points
ID: 9854139
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
 
LVL 12

Expert Comment

by:vascov
ID: 9889598
Did this help !?
0
 
LVL 12

Expert Comment

by:vascov
ID: 10315872
zirich,

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

Thanks,

Vasco
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET String Settings and Temp Folder Question 3 89
asp Google Map 2 91
Oracle programming for starter 14 74
Code planning methods/tools? 5 55
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…

737 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