Solved

RasEnumConnections in VB.NET?

Posted on 2003-11-28
5
1,354 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
  • 3
5 Comments
 
LVL 15

Expert Comment

by:Raisor
Comment Utility
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
Comment Utility
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
Comment Utility
Did this help !?
0
 
LVL 12

Expert Comment

by:vascov
Comment Utility
zirich,

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

Thanks,

Vasco
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Adobe Customization Wizard XI issues 26 166
for loop with Set 4 45
Enhanced For loop vs ForEach Loop 4 54
Path to Python 9 37
Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
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.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

743 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