Translating code from VB6

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • zurg

    Translating code from VB6

    Hi!
    I translated code from VB6 (worked there) to VB.Net and not it doesn't
    work...
    It has problems with RasEnumConnecti ons function...
    Probably I have to add initialize but how should it look like?
    I tried

    tInfo.Initializ e

    but still not working....


    Please help!!!

    That's code from VB6:
    -----------------------------------
    Option Explicit

    Public Declare Function RasEnumConnecti ons Lib "rasapi32.d ll" Alias
    "RasEnumConnect ionsA" (ByRef lpRasCon As RASCONN, ByRef lpcb As Long, ByRef
    lpNumConnection s As Long) As Long
    Public Declare Function RasHangUp Lib "rasapi32.d ll" Alias "RasHangUpA "
    (ByVal hRasConn As Long) As Long

    Public Type RASCONN
    dwSize As Long
    hRasConn As Long
    szEntryName(256 ) As Byte
    szDeviceType(16 ) As Byte
    szDeviceName(12 9) As Byte
    End Type

    Sub Main()
    Dim tInfo(10) As RASCONN
    Dim lSize As Long
    Dim lTotal As Long

    tInfo(0).dwSize = Len(tInfo(0))
    lSize = tInfo(0).dwSize * 10

    If RasEnumConnecti ons(tInfo(0), lSize, lTotal) <> 0 Then
    MsgBox ("Error during enumeration atempt")
    Else
    Dim iCurrent As Long
    MsgBox (Str(lTotal) + " active RAS connections were found")

    For iCurrent = 0 To lTotal - 1
    If RasHangUp(tInfo (iCurrent).hRas Conn) Then
    MsgBox ("Failed to diconnect one of the Ras connections")
    Else
    MsgBox ("One of the RAS connection was disconnected")
    End If
    Next
    End If
    End Sub
    --------------------------
    And after translation to VB.Net

    Option Strict Off

    Option Explicit On

    Module Module1


    'UPGRADE_WARNIN G: Structure RASCONN may require marshalling attributes to be
    passed as an argument in this Declare statement. Click for more:
    'ms-help://MS.VSCC/commoner/redir/redirect.htm?ke yword="vbup1050 "'

    Public Declare Function RasEnumConnecti ons Lib "rasapi32.d ll" Alias
    "RasEnumConnect ionsA"(ByRef lpRasCon As RASCONN, ByRef lpcb As Integer,
    ByRef lpNumConnection s As Integer) As Integer

    Public Declare Function RasHangUp Lib "rasapi32.d ll" Alias
    "RasHangUpA"(By Val hRasConn As Integer) As Integer


    Public Structure RASCONN

    Dim dwSize As Integer

    Dim hRasConn As Integer

    <VBFixedArray(2 56)> Dim szEntryName() As Byte

    <VBFixedArray(1 6)> Dim szDeviceType() As Byte

    <VBFixedArray(1 29)> Dim szDeviceName() As Byte


    'UPGRADE_TODO: "Initialize " must be called to initialize instances of this
    structure. Click for more:
    'ms-help://MS.VSCC/commoner/redir/redirect.htm?ke yword="vbup1026 "'

    Public Sub Initialize()

    ReDim szEntryName(256 )

    ReDim szDeviceType(16 )

    ReDim szDeviceName(12 9)

    End Sub

    End Structure


    Public Sub Main()

    'UPGRADE_WARNIN G: Array tInfo may need to have individual elements
    initialized. Click for more:
    'ms-help://MS.VSCC/commoner/redir/redirect.htm?ke yword="vbup1042 "'

    Dim tInfo(10) As RASCONN

    Dim lSize As Integer

    Dim lTotal As Integer

    Dim iCurrent As Integer


    tInfo(0).dwSize = Len(tInfo(0))

    lSize = tInfo(0).dwSize * 10


    If RasEnumConnecti ons(tInfo(0), lSize, lTotal) <> 0 Then

    MsgBox("Error during enumeration atempt")

    Else

    MsgBox (Str(lTotal) + " active RAS connections were found")
    For iCurrent = 0 To lTotal - 1

    If RasHangUp(tInfo (iCurrent).hRas Conn) Then

    MsgBox ("Failed to diconnect one of the
    Ras connections")
    Else

    MsgBox ("One of the RAS connection was
    disconnected")
    End If

    Next

    End If

    End Sub

    End Module


  • Ken Tucker [MVP]

    #2
    Re: Translating code from VB6

    Hi,

    How to initialize tInfo.
    Dim x As Integer

    For x = 0 To 10

    tInfo(x).Initia lize()

    Next

    Ken

    --------------------------------

    "zurg" <zurg@wp.pl> wrote in message
    news:OUGBY7pqDH A.2012@TK2MSFTN GP12.phx.gbl...[color=blue]
    > Hi!
    > I translated code from VB6 (worked there) to VB.Net and not it doesn't
    > work...
    > It has problems with RasEnumConnecti ons function...
    > Probably I have to add initialize but how should it look like?
    > I tried
    >
    > tInfo.Initializ e
    >
    > but still not working....
    >
    >
    > Please help!!!
    >
    > That's code from VB6:
    > -----------------------------------
    > Option Explicit
    >
    > Public Declare Function RasEnumConnecti ons Lib "rasapi32.d ll" Alias
    > "RasEnumConnect ionsA" (ByRef lpRasCon As RASCONN, ByRef lpcb As Long,[/color]
    ByRef[color=blue]
    > lpNumConnection s As Long) As Long
    > Public Declare Function RasHangUp Lib "rasapi32.d ll" Alias "RasHangUpA "
    > (ByVal hRasConn As Long) As Long
    >
    > Public Type RASCONN
    > dwSize As Long
    > hRasConn As Long
    > szEntryName(256 ) As Byte
    > szDeviceType(16 ) As Byte
    > szDeviceName(12 9) As Byte
    > End Type
    >
    > Sub Main()
    > Dim tInfo(10) As RASCONN
    > Dim lSize As Long
    > Dim lTotal As Long
    >
    > tInfo(0).dwSize = Len(tInfo(0))
    > lSize = tInfo(0).dwSize * 10
    >
    > If RasEnumConnecti ons(tInfo(0), lSize, lTotal) <> 0 Then
    > MsgBox ("Error during enumeration atempt")
    > Else
    > Dim iCurrent As Long
    > MsgBox (Str(lTotal) + " active RAS connections were found")
    >
    > For iCurrent = 0 To lTotal - 1
    > If RasHangUp(tInfo (iCurrent).hRas Conn) Then
    > MsgBox ("Failed to diconnect one of the Ras connections")
    > Else
    > MsgBox ("One of the RAS connection was disconnected")
    > End If
    > Next
    > End If
    > End Sub
    > --------------------------
    > And after translation to VB.Net
    >
    > Option Strict Off
    >
    > Option Explicit On
    >
    > Module Module1
    >
    >
    > 'UPGRADE_WARNIN G: Structure RASCONN may require marshalling attributes to[/color]
    be[color=blue]
    > passed as an argument in this Declare statement. Click for more:
    > 'ms-help://MS.VSCC/commoner/redir/redirect.htm?ke yword="vbup1050 "'
    >
    > Public Declare Function RasEnumConnecti ons Lib "rasapi32.d ll" Alias
    > "RasEnumConnect ionsA"(ByRef lpRasCon As RASCONN, ByRef lpcb As Integer,
    > ByRef lpNumConnection s As Integer) As Integer
    >
    > Public Declare Function RasHangUp Lib "rasapi32.d ll" Alias
    > "RasHangUpA"(By Val hRasConn As Integer) As Integer
    >
    >
    > Public Structure RASCONN
    >
    > Dim dwSize As Integer
    >
    > Dim hRasConn As Integer
    >
    > <VBFixedArray(2 56)> Dim szEntryName() As Byte
    >
    > <VBFixedArray(1 6)> Dim szDeviceType() As Byte
    >
    > <VBFixedArray(1 29)> Dim szDeviceName() As Byte
    >
    >
    > 'UPGRADE_TODO: "Initialize " must be called to initialize instances of this
    > structure. Click for more:
    > 'ms-help://MS.VSCC/commoner/redir/redirect.htm?ke yword="vbup1026 "'
    >
    > Public Sub Initialize()
    >
    > ReDim szEntryName(256 )
    >
    > ReDim szDeviceType(16 )
    >
    > ReDim szDeviceName(12 9)
    >
    > End Sub
    >
    > End Structure
    >
    >
    > Public Sub Main()
    >
    > 'UPGRADE_WARNIN G: Array tInfo may need to have individual elements
    > initialized. Click for more:
    > 'ms-help://MS.VSCC/commoner/redir/redirect.htm?ke yword="vbup1042 "'
    >
    > Dim tInfo(10) As RASCONN
    >
    > Dim lSize As Integer
    >
    > Dim lTotal As Integer
    >
    > Dim iCurrent As Integer
    >
    >
    > tInfo(0).dwSize = Len(tInfo(0))
    >
    > lSize = tInfo(0).dwSize * 10
    >
    >
    > If RasEnumConnecti ons(tInfo(0), lSize, lTotal) <> 0 Then
    >
    > MsgBox("Error during enumeration atempt")
    >
    > Else
    >
    > MsgBox (Str(lTotal) + " active RAS connections were found")
    > For iCurrent = 0 To lTotal - 1
    >
    > If RasHangUp(tInfo (iCurrent).hRas Conn) Then
    >
    > MsgBox ("Failed to diconnect one of[/color]
    the[color=blue]
    > Ras connections")
    > Else
    >
    > MsgBox ("One of the RAS connection[/color]
    was[color=blue]
    > disconnected")
    > End If
    >
    > Next
    >
    > End If
    >
    > End Sub
    >
    > End Module
    >
    >[/color]


    Comment

    • zurg

      #3
      Re: Translating code from VB6

      Thanks, that helped...

      But I get an error when it has to run RasEnumConnecti ons function:
      An unhandled exception of type 'System.TypeLoa dException' occurred in
      RASGuard.exe

      Additional information: Can not marshal field szEntryName of type RASCONN:
      This type can not be marshaled as a structure field.

      I have no idea what to do...
      Once again - help!
      zurg


      U¿ytkownik "Ken Tucker [MVP]" <vb2ae@bellsout h.net> napisa³ w wiadomo¶ci
      news:uEXjWYqqDH A.1096@TK2MSFTN GP11.phx.gbl...[color=blue]
      > Hi,
      >
      > How to initialize tInfo.
      > Dim x As Integer
      >
      > For x = 0 To 10
      >
      > tInfo(x).Initia lize()
      >
      > Next
      >
      > Ken
      >
      > --------------------------------
      >
      > "zurg" <zurg@wp.pl> wrote in message
      > news:OUGBY7pqDH A.2012@TK2MSFTN GP12.phx.gbl...[color=green]
      > > Hi!
      > > I translated code from VB6 (worked there) to VB.Net and not it doesn't
      > > work...
      > > It has problems with RasEnumConnecti ons function...
      > > Probably I have to add initialize but how should it look like?
      > > I tried
      > >
      > > tInfo.Initializ e
      > >
      > > but still not working....
      > >
      > >
      > > Please help!!!
      > >
      > > That's code from VB6:
      > > -----------------------------------
      > > Option Explicit
      > >
      > > Public Declare Function RasEnumConnecti ons Lib "rasapi32.d ll" Alias
      > > "RasEnumConnect ionsA" (ByRef lpRasCon As RASCONN, ByRef lpcb As Long,[/color]
      > ByRef[color=green]
      > > lpNumConnection s As Long) As Long
      > > Public Declare Function RasHangUp Lib "rasapi32.d ll" Alias "RasHangUpA "
      > > (ByVal hRasConn As Long) As Long
      > >
      > > Public Type RASCONN
      > > dwSize As Long
      > > hRasConn As Long
      > > szEntryName(256 ) As Byte
      > > szDeviceType(16 ) As Byte
      > > szDeviceName(12 9) As Byte
      > > End Type
      > >
      > > Sub Main()
      > > Dim tInfo(10) As RASCONN
      > > Dim lSize As Long
      > > Dim lTotal As Long
      > >
      > > tInfo(0).dwSize = Len(tInfo(0))
      > > lSize = tInfo(0).dwSize * 10
      > >
      > > If RasEnumConnecti ons(tInfo(0), lSize, lTotal) <> 0 Then
      > > MsgBox ("Error during enumeration atempt")
      > > Else
      > > Dim iCurrent As Long
      > > MsgBox (Str(lTotal) + " active RAS connections were found")
      > >
      > > For iCurrent = 0 To lTotal - 1
      > > If RasHangUp(tInfo (iCurrent).hRas Conn) Then
      > > MsgBox ("Failed to diconnect one of the Ras[/color][/color]
      connections")[color=blue][color=green]
      > > Else
      > > MsgBox ("One of the RAS connection was disconnected")
      > > End If
      > > Next
      > > End If
      > > End Sub
      > > --------------------------
      > > And after translation to VB.Net
      > >
      > > Option Strict Off
      > >
      > > Option Explicit On
      > >
      > > Module Module1
      > >
      > >
      > > 'UPGRADE_WARNIN G: Structure RASCONN may require marshalling attributes[/color][/color]
      to[color=blue]
      > be[color=green]
      > > passed as an argument in this Declare statement. Click for more:
      > > 'ms-help://MS.VSCC/commoner/redir/redirect.htm?ke yword="vbup1050 "'
      > >
      > > Public Declare Function RasEnumConnecti ons Lib "rasapi32.d ll" Alias
      > > "RasEnumConnect ionsA"(ByRef lpRasCon As RASCONN, ByRef lpcb As Integer,
      > > ByRef lpNumConnection s As Integer) As Integer
      > >
      > > Public Declare Function RasHangUp Lib "rasapi32.d ll" Alias
      > > "RasHangUpA"(By Val hRasConn As Integer) As Integer
      > >
      > >
      > > Public Structure RASCONN
      > >
      > > Dim dwSize As Integer
      > >
      > > Dim hRasConn As Integer
      > >
      > > <VBFixedArray(2 56)> Dim szEntryName() As Byte
      > >
      > > <VBFixedArray(1 6)> Dim szDeviceType() As Byte
      > >
      > > <VBFixedArray(1 29)> Dim szDeviceName() As Byte
      > >
      > >
      > > 'UPGRADE_TODO: "Initialize " must be called to initialize instances of[/color][/color]
      this[color=blue][color=green]
      > > structure. Click for more:
      > > 'ms-help://MS.VSCC/commoner/redir/redirect.htm?ke yword="vbup1026 "'
      > >
      > > Public Sub Initialize()
      > >
      > > ReDim szEntryName(256 )
      > >
      > > ReDim szDeviceType(16 )
      > >
      > > ReDim szDeviceName(12 9)
      > >
      > > End Sub
      > >
      > > End Structure
      > >
      > >
      > > Public Sub Main()
      > >
      > > 'UPGRADE_WARNIN G: Array tInfo may need to have individual elements
      > > initialized. Click for more:
      > > 'ms-help://MS.VSCC/commoner/redir/redirect.htm?ke yword="vbup1042 "'
      > >
      > > Dim tInfo(10) As RASCONN
      > >
      > > Dim lSize As Integer
      > >
      > > Dim lTotal As Integer
      > >
      > > Dim iCurrent As Integer
      > >
      > >
      > > tInfo(0).dwSize = Len(tInfo(0))
      > >
      > > lSize = tInfo(0).dwSize * 10
      > >
      > >
      > > If RasEnumConnecti ons(tInfo(0), lSize, lTotal) <> 0 Then
      > >
      > > MsgBox("Error during enumeration atempt")
      > >
      > > Else
      > >
      > > MsgBox (Str(lTotal) + " active RAS connections were found")
      > > For iCurrent = 0 To lTotal - 1
      > >
      > > If RasHangUp(tInfo (iCurrent).hRas Conn) Then
      > >
      > > MsgBox ("Failed to diconnect one of[/color]
      > the[color=green]
      > > Ras connections")
      > > Else
      > >
      > > MsgBox ("One of the RAS connection[/color]
      > was[color=green]
      > > disconnected")
      > > End If
      > >
      > > Next
      > >
      > > End If
      > >
      > > End Sub
      > >
      > > End Module
      > >
      > >[/color]
      >
      >[/color]


      Comment

      Working...