(vb2005) I need your help for using "Rasapi32.dll"

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

    #1

    (vb2005) I need your help for using "Rasapi32.dll"

    Hello

    I know that I should get the information of windows internet connections by
    using "rasapi32.d ll" library, and I also have some sample codes, but I can't
    make them work. My exact need is to get the "UserName" of a connection. How
    is it possible?

    plz hlp
    thnk u


  • rowe_newsgroups

    #2
    Re: (vb2005) I need your help for using "Rasapi32. dll"

    On May 12, 6:53 am, "Javad" <j...@javad.com wrote:
    Hello
    >
    I know that I should get the information of windows internet connections by
    using "rasapi32.d ll" library, and I also have some sample codes, but I can't
    make them work. My exact need is to get the "UserName" of a connection. How
    is it possible?
    >
    plz hlp
    thnk u
    Post the sample code - perhaps one of us could get it to work.

    Thanks,

    Seth Rowe

    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: (vb2005) I need your help for using &quot;Rasapi32. dll&quot;

      "Javad" <javad@javad.co mschrieb:
      I know that I should get the information of windows internet connections
      by using "rasapi32.d ll" library, and I also have some sample codes, but I
      can't make them work. My exact need is to get the "UserName" of a
      connection. How is it possible?
      Maybe your declarations for the functions exported by "rasapi32.d ll" are
      wrong. I suggest to post a sample that is intended to work but which
      doesn't work in order to enable us to find the bugs it contains.

      --
      M S Herfried K. Wagner
      M V P <URL:http://dotnet.mvps.org/>
      V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

      Comment

      • Javad

        #4
        Re: (vb2005) I need your help for using &quot;Rasapi32. dll&quot;

        Here's some sample codes. How can I get the UserName of an internet
        connection? (vb 2005)

        Declare Function RasGetEntryDial Params Lib "rasapi32.d ll" Alias
        "RasGetEntryDia lParamsA" (ByVal lpcstr As String, ByRef lprasdialparams a As
        RASDIALPARAMS, ByRef lpbool As Long) As Long

        Declare Function RasDial Lib "rasapi32.d ll" Alias "RasDialA" (ByVal
        lprasdialextens ions As Long, ByVal lpcstr As String, ByRef lprasdialparams a
        As RASDIALPARAMS, ByVal dword As Long, ByVal lpvoid As Any, ByRef lphrasconn
        As Long) As Long

        Private Declare Function InternetDial Lib "wininet.dl l" Alias
        "InternetDi alA" (ByVal hwndParent As Long, ByVal lpszConnectoid As String,
        ByVal dwFlags As Long, ByVal lpdwConnection As Long, ByVal dwReserved As
        Long) As Long
        Private Declare Function InternetGoOnlin e Lib "wininet.dl l" Alias
        "InternetGoOnli neA" (ByVal lpszURL As String, ByVal hwndParent As Long,
        ByVal dwReserved As Long) As Long
        Private Declare Function InternetHangUp Lib "wininet.dl l" (ByVal
        dwConnection As Long, ByVal dwReserved As Long) As Long
        Private Declare Function InternetAutodia l Lib "wininet.dl l" (ByVal
        dwFlags As Long, ByVal hwndParent As Long) As Long
        Private Declare Function InternetAutodia lHangup Lib "wininet.dl l" (ByVal
        dwReserved As Long) As Long
        Private Declare Function InternetOpen Lib "wininet.dl l" Alias
        "InternetOp enA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal
        sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As
        Long
        Private Declare Function InternetConnect Lib "wininet.dl l" Alias
        "InternetConnec tA" (ByVal hInternetSessio n As Long, ByVal sServerName As
        String, ByVal nServerPort As Integer, ByVal sUserName As String, ByVal
        sPassword As String, ByVal lService As Long, ByVal lFlags As Long, ByVal
        lContext As Long) As Long
        Private Declare Function InternetGetLast ResponseInfo Lib "wininet.dl l"
        Alias "InternetGetLas tResponseInfoA" (ByVal lpdwError As Long, ByVal
        lpszBuffer As String, ByVal lpdwBufferLengt h As Long) As Boolean
        Private Declare Function InternetReadFil e Lib "wininet.dl l" (ByVal hFile
        As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, ByVal
        lNumberOfBytesR ead As Long) As Integer
        Private Declare Function InternetCloseHa ndle Lib "wininet.dl l" (ByVal
        hInet As Long) As Integer
        Private Declare Function InternetGetConn ectedStateEx Lib "wininet.dl l"
        Alias "InternetGetCon nectedStateExA" (ByVal lpdwFlags As Long, ByVal
        lpszConnectionN ame As Long, ByVal dwNameLen As Long, ByVal dwReserved As
        Long) As Long




        Declare Function RasHangUp Lib "rasapi32.d ll" Alias "RasHangUpA " (ByVal
        hRasConn As Long) As Long

        Declare Function RasGetErrorStri ng Lib "rasapi32.d ll" Alias
        "RasGetErrorStr ingA" (ByVal uErrorValue As Long, ByVal lpszErrorString As
        String, ByVal cBufSize As Long) As Long

        '---------------------------------
        '----------------------------------
        '-----------------------------------



        Comment

        • Javad

          #5
          Re: (vb2005) I need your help for using &quot;Rasapi32. dll&quot;

          I also have a Visual C 2003 (or maybe visual C#2003) sample project and also
          a vb6 sample code (not project) that are supposed to do all the things I
          need, such as getting username, password, dialing, hanging-up, etc. but
          don't know how to use their methods for obtaining my need. I only need to
          know the UserName of the active connection. I can give you their links if
          someone have the patience to extract the part of code I need, but at first
          please answer this my question:
          When I know there's a library such as "rasapi32.d ll" able to do what I need,
          How can I get its abilities to work? (I don't know any API programming).
          What kinds of programming skills should I have to get such libraries to
          work? Where should I start?

          Thank you

          "Javad" <javad@javad.co mwrote in message
          news:e%23YWiQIl HHA.4904@TK2MSF TNGP05.phx.gbl. ..
          Hello
          >
          I know that I should get the information of windows internet connections
          by using "rasapi32.d ll" library, and I also have some sample codes, but I
          can't make them work. My exact need is to get the "UserName" of a
          connection. How is it possible?
          >
          plz hlp
          thnk u
          >

          Comment

          • rowe_newsgroups

            #6
            Re: (vb2005) I need your help for using &quot;Rasapi32. dll&quot;

            On May 14, 3:51 am, "Javad" <j...@javad.com wrote:
            I also have a Visual C 2003 (or maybe visual C#2003) sample project and also
            a vb6 sample code (not project) that are supposed to do all the things I
            need, such as getting username, password, dialing, hanging-up, etc. but
            don't know how to use their methods for obtaining my need. I only need to
            know the UserName of the active connection. I can give you their links if
            someone have the patience to extract the part of code I need, but at first
            please answer this my question:
            When I know there's a library such as "rasapi32.d ll" able to do what I need,
            How can I get its abilities to work? (I don't know any API programming).
            What kinds of programming skills should I have to get such libraries to
            work? Where should I start?
            >
            Thank you
            >
            "Javad" <j...@javad.com wrote in message
            >
            news:e%23YWiQIl HHA.4904@TK2MSF TNGP05.phx.gbl. ..
            >
            Hello
            >
            I know that I should get the information of windows internet connections
            by using "rasapi32.d ll" library, and I also have some sample codes, but I
            can't make them work. My exact need is to get the "UserName" of a
            connection. How is it possible?
            >
            plz hlp
            thnk u
            When I know there's a library such as "rasapi32.d ll" able to do what I need,
            How can I get its abilities to work? (I don't know any API programming).
            What kinds of programming skills should I have to get such libraries to
            work? Where should I start?
            Some searching on www.msdn.com will tell you about what the method
            does, www.pinvoke.net will give you the proper declare/import
            statements, and then it is just a simple task of added them to your
            code and calling the methods like normal.

            P.S. I'll take a look at the code in your previous post later today.

            Thanks,

            Seth Rowe

            Comment

            • rowe_newsgroups

              #7
              Re: (vb2005) I need your help for using &quot;Rasapi32. dll&quot;

              On May 12, 10:34 pm, "Javad" <j...@javad.com wrote:
              Here's some sample codes. How can I get the UserName of an internet
              connection? (vb 2005)
              >
              Declare Function RasGetEntryDial Params Lib "rasapi32.d ll" Alias
              "RasGetEntryDia lParamsA" (ByVal lpcstr As String, ByRef lprasdialparams a As
              RASDIALPARAMS, ByRef lpbool As Long) As Long
              >
              Declare Function RasDial Lib "rasapi32.d ll" Alias "RasDialA" (ByVal
              lprasdialextens ions As Long, ByVal lpcstr As String, ByRef lprasdialparams a
              As RASDIALPARAMS, ByVal dword As Long, ByVal lpvoid As Any, ByRef lphrasconn
              As Long) As Long
              >
              Private Declare Function InternetDial Lib "wininet.dl l" Alias
              "InternetDi alA" (ByVal hwndParent As Long, ByVal lpszConnectoid As String,
              ByVal dwFlags As Long, ByVal lpdwConnection As Long, ByVal dwReserved As
              Long) As Long
              Private Declare Function InternetGoOnlin e Lib "wininet.dl l" Alias
              "InternetGoOnli neA" (ByVal lpszURL As String, ByVal hwndParent As Long,
              ByVal dwReserved As Long) As Long
              Private Declare Function InternetHangUp Lib "wininet.dl l" (ByVal
              dwConnection As Long, ByVal dwReserved As Long) As Long
              Private Declare Function InternetAutodia l Lib "wininet.dl l" (ByVal
              dwFlags As Long, ByVal hwndParent As Long) As Long
              Private Declare Function InternetAutodia lHangup Lib "wininet.dl l" (ByVal
              dwReserved As Long) As Long
              Private Declare Function InternetOpen Lib "wininet.dl l" Alias
              "InternetOp enA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal
              sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As
              Long
              Private Declare Function InternetConnect Lib "wininet.dl l" Alias
              "InternetConnec tA" (ByVal hInternetSessio n As Long, ByVal sServerName As
              String, ByVal nServerPort As Integer, ByVal sUserName As String, ByVal
              sPassword As String, ByVal lService As Long, ByVal lFlags As Long, ByVal
              lContext As Long) As Long
              Private Declare Function InternetGetLast ResponseInfo Lib "wininet.dl l"
              Alias "InternetGetLas tResponseInfoA" (ByVal lpdwError As Long, ByVal
              lpszBuffer As String, ByVal lpdwBufferLengt h As Long) As Boolean
              Private Declare Function InternetReadFil e Lib "wininet.dl l" (ByVal hFile
              As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, ByVal
              lNumberOfBytesR ead As Long) As Integer
              Private Declare Function InternetCloseHa ndle Lib "wininet.dl l" (ByVal
              hInet As Long) As Integer
              Private Declare Function InternetGetConn ectedStateEx Lib "wininet.dl l"
              Alias "InternetGetCon nectedStateExA" (ByVal lpdwFlags As Long, ByVal
              lpszConnectionN ame As Long, ByVal dwNameLen As Long, ByVal dwReserved As
              Long) As Long
              >
              Declare Function RasHangUp Lib "rasapi32.d ll" Alias "RasHangUpA " (ByVal
              hRasConn As Long) As Long
              >
              Declare Function RasGetErrorStri ng Lib "rasapi32.d ll" Alias
              "RasGetErrorStr ingA" (ByVal uErrorValue As Long, ByVal lpszErrorString As
              String, ByVal cBufSize As Long) As Long
              >
              '---------------------------------
              '----------------------------------
              '-----------------------------------
              Okay, your declarations seem to be from classic vb (with longs instead
              of integers) so I went through pinvoke.net and msdn and updated them
              for you. I have not checked any of these, as I don't have any code
              demonstrating how to use them - and I didn't feel like writing any.

              <StructLayout(L ayoutKind.Seque ntial)_
              Public Structure RASDIALPARAMS
              Public dwSize As Integer
              <MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=257) Public
              szEntryName As String
              <MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=129) Public
              szPhoneNumber As String
              <MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=129) Public
              szCallbackNumbe r As String
              <MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=257) Public
              szUserName As String
              <MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=257) Public
              szPassword As String
              <MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=16)P ublic
              szDomain As String
              Public dwSubEntry As UInt32
              Public dwCallbackId As IntPtr
              End Structure

              <StructLayout(L ayoutKind.Seque ntial)_
              Public Structure RASDIALEXTENSIO NS
              Public dwSize As Integer
              Public dwfOptions As UInt32
              Public hWndParent As IntPtr
              Public reserved As Integer
              Public reserved1 As Integer
              Public RasEapInfo As RASEAPINFO
              End Structure

              Public Structure RASEAPINFO
              Public dsSizeofEapInfo As Integer
              Public pbEapInfo As Byte
              End Structure

              <DllImport("ras api32.dll", CharSet:=CharSe t.Auto)_
              Public Shared Function RasGetEntryDial Params(ByVal lpszPhonebook
              As String, _
              <[In](), Out()ByRef
              lprasdialparams As RASDIALPARAMS, _
              <Out()ByRef
              lpfPassword As Boolean) As Integer
              End Function

              <DllImport("ras api32.dll", CharSet:=CharSe t.Auto)_
              Public Shared Function RasDial(<[In]()ByVal lpRasDialExtens ions
              As RASDIALEXTENSIO NS, _
              <[In]()ByVal lpszPhonebook As
              String, _
              <[In]()ByVal lpRasDialParams As
              RASDIALPARAMS, _
              ByVal dwNotifierType As UInt32, _
              ByVal lpvNotifier As [Delegate], _
              ByRef lphRasConn As IntPtr) As
              UInt32

              End Function

              <DllImport("win inet.dll")_
              Public Shared Function InternetDial(By Val hWndParent As IntPtr, _

              <MarshalAs(Unma nagedType.LPTSt r)ByVal pszEntryName As String, _
              ByVal dwFlags As Integer, _
              ByRef lpdwConnection As
              Integer, _
              ByVal dwReserved As Integer)
              As Integer
              End Function

              <DllImport("win inet.dll")_
              Public Shared Function
              InternetGoOnlin e(<MarshalAs(Un managedType.LPT Str)ByVal lpszURL As
              String, _
              ByVal hWndParent As
              IntPtr, _
              ByVal dwFlags As Integer)
              As Boolean
              End Function

              <DllImport("win inet.dll")_
              Public Shared Function InternetHangUp( ByRef dwConnection As
              Integer, _
              ByVal dwReserved As Integer)
              As Integer
              End Function

              <DllImport("win inet.dll")_
              Public Shared Function InternetAutoDia l(ByVal dwFlags As Integer,
              _
              ByVal hWndParent As
              IntPtr) As Boolean
              End Function

              <DllImport("win inet.dll")_
              Public Shared Function InternetAutoDia lHangup(ByVal dwReserved As
              Integer) As Boolean
              End Function

              <DllImport("win inet.dll")_
              Public Shared Function InternetOpen(By Val lpszAgent As String, _
              ByVal dwAccessType As Integer,
              _
              ByVal lpszProxyName As String,
              _
              ByVal lpszProxyBypass As
              String, _
              ByVal dwFlags As Integer) As
              IntPtr
              End Function

              <DllImport("win inet.dll")_
              Public Shared Function InternetConnect (ByVal hInternet As IntPtr,
              _
              ByVal lpszServerName As
              String, _
              ByVal nServerPort As Short,
              _
              ByVal lpszUserName As
              String, _
              ByVal lpszPassword As
              String, _
              ByVal dwService As Integer,
              _
              ByVal dwFlags As Integer, _
              ByVal dwContext As IntPtr)
              As IntPtr
              End Function

              <DllImport("win inet.dll")_
              Public Shared Function InternetGetLast ResponseInfo(By Ref errorCode
              As Integer, _
              ByVal buffer As
              System.Text.Str ingBuilder, _
              ByRef
              bufferLength As Integer) As <MarshalAs(Unma nagedType.Bool) Boolean
              End Function

              <DllImport("win inet.dll")_
              Public Shared Function InternetReadFil e(ByVal hFile As IntPtr, _
              ByRef lpBuffer As String,
              _
              ByVal
              dwNumberOfBytes ToRead As Integer, _
              ByRef
              lpdwNumberOfByt esRead As Integer) As Boolean
              End Function

              <DllImport("win inet.dll")_
              Public Shared Function InternetCloseHa ndle(ByVal hInternet As
              IntPtr) As <MarshalAs(Unma nagedType.Bool) Boolean
              End Function

              <DllImport("win inet.dll")_
              Public Shared Function InternetGetConn ectedStateEx(By Val lpdwFlags
              As Integer, _

              <MarshalAs(Unma nagedType.LPTSt r)ByVal lpszConnectionN ame As String,
              _
              ByVal dwNameLen
              As Integer, _
              ByVal
              dwReserved As Integer) As <MarshalAs(Unma nagedType.Bool) Boolean
              End Function

              <DllImport("ras api32.dll")_
              Public Shared Function RasHangUp(ByVal hRasConn As Integer) As
              Integer
              End Function

              <DllImport("ras api32.dll")_
              Public Shared Function RasGetErrorStri ng(ByVal uErrorValue As
              UInt32, _
              ByVal lpszErrorString As
              System.Text.Str ingBuilder, _
              <[In]()ByVal cBufSize
              As Integer) As UInt32
              End Function


              Try those and let me know how it works. If you need further help
              please share the code that makes use of these methods.

              Thanks,

              Seth Rowe

              Comment

              • Javad

                #8
                Re: (vb2005) I need your help for using &quot;Rasapi32. dll&quot;

                Thank you rowe for taking the time to modify the codes. I tried them, but
                couldn't get them to work. They had much errors.

                I searched MSDN and other resources. Now I know exactly where to get my
                needed parameter, but don't know how. I know that "Connection 's User
                Name" is a part of the string of "szEntryNam e" which is a parameter of
                "RasConn" type. and I also know that the values of the parameters of
                "RasConn" type can be obtained by calling "RasEnumConnect ions" function.

                Here's a function I could write (and doesn't work properly but) I think it
                shows the
                right way to get the connection's username. I hope someone here can correct
                it.

                Thank you

                '-How to get the UserName of the Active Connection--

                Public Structure RASCONN

                'http://msdn2.microsoft .com/en-us/library/aa376725.aspx

                Public dwSize As Int32

                Public hRasCon As Int32

                Public szEntryname() As String

                Public szDeviceType() As String

                Public szDeviceName() As String

                Public szPhoneBook() '(As ???)

                Public dwSubEntry ' (As ???)

                End Structure

                Private Declare Auto Function RasEnumConnecti ons Lib "rasapi32.d ll" Alias
                "RasEnumConnect ions" ( _

                ByRef iprasconn As RASCONN, _

                ByRef ipcb As Integer, _

                ByRef ipcconnections As Integer) As Integer

                'http://msdn2.microsoft .com/en-us/library/aa377284.aspx

                Public Function ConnectionUserN ame()

                Dim b1 As Integer

                Dim b1RasConn As RASCONN

                Dim b1IPcb, b1ipcconnection s As Integer

                b1 = RasEnumConnecti ons(b1RasConn, b1IPcb, b1ipcconnection s)

                Debug.Print(b1)

                'If everything goes OK, b1 must be "0"

                ConnectionUserN ame = b1RasConn.szEnt ryname

                End Function



                '-----------End of My Code--------------

                '-Here's also a sub I found, which calls "RasEnumConnect ions

                ' for another purpose, but it uses a different method to call it.

                'Maybe it helps:

                Private Const RAS_MaxEntryNam e As Integer = 256
                Private Const RAS_MaxDeviceTy pe As Integer = 16
                Private Const RAS_MaxDeviceNa me As Integer = 128

                Public Structure RASCONN
                Public dwSize As Integer
                Public hRasCon As IntPtr
                <MarshalAs(Unma nagedType.ByVal TStr, sizeconst:=RAS_ MaxEntryName + 1)_
                Public szEntryname As String
                <MarshalAs(Unma nagedType.ByVal TStr, sizeconst:=RAS_ MaxDeviceType + 1)_
                Public szDeviceType As String
                <MarshalAs(Unma nagedType.ByVal TStr, sizeconst:=RAS_ MaxDeviceName + 1)_
                Public szDeviceName As String
                End Structure

                Private Declare Auto Function RasEnumConnecti ons Lib "rasapi32.d ll" (ByVal _
                lpRasCon As IntPtr, ByRef lpcb As Integer, ByRef ByReflpcConnect ions As _
                Integer) As Integer

                Public Module SubMain

                Dim structtype As Type = GetType(RASCONN )
                Dim structsize As Integer = Marshal.SizeOf( GetType(RASCONN ))

                Dim bufsize As Integer = structsize
                Dim entrycount As Integer
                Dim entries() As RASCONN
                Dim bufptr As IntPtr = Marshal.AllocHG lobal(structsiz e)
                Marshal.WriteIn t32(bufptr, structsize)

                Dim retcode As Integer = RasEnumConnecti ons(bufptr, bufsize, entrycount)
                End Module


                Comment

                • rowe_newsgroups

                  #9
                  Re: (vb2005) I need your help for using &quot;Rasapi32. dll&quot;

                  On May 16, 6:07 am, "Javad" <j...@javad.com wrote:
                  Thank you rowe for taking the time to modify the codes. I tried them, but
                  couldn't get them to work. They had much errors.
                  >
                  I searched MSDN and other resources. Now I know exactly where to get my
                  needed parameter, but don't know how. I know that "Connection 's User
                  Name" is a part of the string of "szEntryNam e" which is a parameter of
                  "RasConn" type. and I also know that the values of the parameters of
                  "RasConn" type can be obtained by calling "RasEnumConnect ions" function.
                  >
                  Here's a function I could write (and doesn't work properly but) I think it
                  shows the
                  right way to get the connection's username. I hope someone here can correct
                  it.
                  >
                  Thank you
                  >
                  '-How to get the UserName of the Active Connection--
                  >
                  Public Structure RASCONN
                  >
                  'http://msdn2.microsoft .com/en-us/library/aa376725.aspx
                  >
                  Public dwSize As Int32
                  >
                  Public hRasCon As Int32
                  >
                  Public szEntryname() As String
                  >
                  Public szDeviceType() As String
                  >
                  Public szDeviceName() As String
                  >
                  Public szPhoneBook() '(As ???)
                  >
                  Public dwSubEntry ' (As ???)
                  >
                  End Structure
                  >
                  Private Declare Auto Function RasEnumConnecti ons Lib "rasapi32.d ll" Alias
                  "RasEnumConnect ions" ( _
                  >
                  ByRef iprasconn As RASCONN, _
                  >
                  ByRef ipcb As Integer, _
                  >
                  ByRef ipcconnections As Integer) As Integer
                  >
                  'http://msdn2.microsoft .com/en-us/library/aa377284.aspx
                  >
                  Public Function ConnectionUserN ame()
                  >
                  Dim b1 As Integer
                  >
                  Dim b1RasConn As RASCONN
                  >
                  Dim b1IPcb, b1ipcconnection s As Integer
                  >
                  b1 = RasEnumConnecti ons(b1RasConn, b1IPcb, b1ipcconnection s)
                  >
                  Debug.Print(b1)
                  >
                  'If everything goes OK, b1 must be "0"
                  >
                  ConnectionUserN ame = b1RasConn.szEnt ryname
                  >
                  End Function
                  >
                  '-----------End of My Code--------------
                  >
                  '-Here's also a sub I found, which calls "RasEnumConnect ions
                  >
                  ' for another purpose, but it uses a different method to call it.
                  >
                  'Maybe it helps:
                  >
                  Private Const RAS_MaxEntryNam e As Integer = 256
                  Private Const RAS_MaxDeviceTy pe As Integer = 16
                  Private Const RAS_MaxDeviceNa me As Integer = 128
                  >
                  Public Structure RASCONN
                  Public dwSize As Integer
                  Public hRasCon As IntPtr
                  <MarshalAs(Unma nagedType.ByVal TStr, sizeconst:=RAS_ MaxEntryName + 1)_
                  Public szEntryname As String
                  <MarshalAs(Unma nagedType.ByVal TStr, sizeconst:=RAS_ MaxDeviceType + 1)_
                  Public szDeviceType As String
                  <MarshalAs(Unma nagedType.ByVal TStr, sizeconst:=RAS_ MaxDeviceName + 1)_
                  Public szDeviceName As String
                  End Structure
                  >
                  Private Declare Auto Function RasEnumConnecti ons Lib "rasapi32.d ll" (ByVal _
                  lpRasCon As IntPtr, ByRef lpcb As Integer, ByRef ByReflpcConnect ions As _
                  Integer) As Integer
                  >
                  Public Module SubMain
                  >
                  Dim structtype As Type = GetType(RASCONN )
                  Dim structsize As Integer = Marshal.SizeOf( GetType(RASCONN ))
                  >
                  Dim bufsize As Integer = structsize
                  Dim entrycount As Integer
                  Dim entries() As RASCONN
                  Dim bufptr As IntPtr = Marshal.AllocHG lobal(structsiz e)
                  Marshal.WriteIn t32(bufptr, structsize)
                  >
                  Dim retcode As Integer = RasEnumConnecti ons(bufptr, bufsize, entrycount)
                  End Module
                  Hmm... I posted this yesterday but it seems it didn't make it through.
                  Anyways, I found the following article on codeproject that deals with
                  RasEnumConnecti ons. Using it I modified your code to at least return a
                  zero for the b1 variable. Unfortunately I don't use dial up
                  connections so I couldn't test the code further - I hope it helps you
                  out - let me know either way.



                  Public Structure RasConn
                  Public dwSize As Integer
                  Public hRasConn As Integer
                  Public szEntryName() As Byte
                  Public szDeviceType() As Byte
                  Public szDeviceName() As Byte
                  End Structure

                  <DllImport("ras api32.dll", SetLastError:=T rue,
                  CharSet:=CharSe t.Auto)_
                  Private Function RasEnumConnecti ons(<[In](), Out()ByRef rasconn
                  As RasConn, _
                  <[In](), Out()ByRef cb As
                  Integer, _
                  <Out()ByRef connections As
                  Integer) As UInt32
                  End Function

                  Public Function ConnectionUserN ame() As Byte()
                  Dim b1RasConn As RasConn = Nothing
                  Dim b1IPcb As Integer = &H53C
                  Dim b1ipcconnection s As Integer = 0

                  b1RasConn.dwSiz e = &H53C

                  Dim b1 As UInt32 = RasEnumConnecti ons(b1RasConn, b1IPcb,
                  b1ipcconnection s)
                  Debug.Print(b1. ToString())
                  'If everything goes OK, b1 must be "0"
                  Return b1RasConn.szEnt ryName
                  End Function

                  Thanks,

                  Seth Rowe

                  Comment

                  • Javad

                    #10
                    Re: (vb2005) I need your help for using &quot;Rasapi32. dll&quot;

                    Hello and thank you very much.
                    The code, has some "Errors" that I don't know how to solve. The errors are:
                    - Type "DllImport" is not defined
                    -Type "In" is not defined
                    -Type "out" is not defined.

                    I think the solution must be easy, but I don't have any idea how to solve
                    it.

                    Thank you

                    "rowe_newsgroup s" <rowe_email@yah oo.comwrote in message
                    news:1179443342 .077866.148330@ l77g2000hsb.goo glegroups.com.. .
                    On May 16, 6:07 am, "Javad" <j...@javad.com wrote:
                    >Thank you rowe for taking the time to modify the codes. I tried them, but
                    >couldn't get them to work. They had much errors.
                    >>
                    >I searched MSDN and other resources. Now I know exactly where to get my
                    >needed parameter, but don't know how. I know that "Connection 's User
                    >Name" is a part of the string of "szEntryNam e" which is a parameter of
                    >"RasConn" type. and I also know that the values of the parameters of
                    >"RasConn" type can be obtained by calling "RasEnumConnect ions" function.
                    >>
                    >Here's a function I could write (and doesn't work properly but) I think
                    >it
                    >shows the
                    >right way to get the connection's username. I hope someone here can
                    >correct
                    >it.
                    >>
                    >Thank you
                    >>
                    >'-How to get the UserName of the Active Connection--
                    >>
                    >Public Structure RASCONN
                    >>
                    >'http://msdn2.microsoft .com/en-us/library/aa376725.aspx
                    >>
                    >Public dwSize As Int32
                    >>
                    >Public hRasCon As Int32
                    >>
                    >Public szEntryname() As String
                    >>
                    >Public szDeviceType() As String
                    >>
                    >Public szDeviceName() As String
                    >>
                    >Public szPhoneBook() '(As ???)
                    >>
                    >Public dwSubEntry ' (As ???)
                    >>
                    >End Structure
                    >>
                    >Private Declare Auto Function RasEnumConnecti ons Lib "rasapi32.d ll" Alias
                    >"RasEnumConnec tions" ( _
                    >>
                    >ByRef iprasconn As RASCONN, _
                    >>
                    >ByRef ipcb As Integer, _
                    >>
                    >ByRef ipcconnections As Integer) As Integer
                    >>
                    >'http://msdn2.microsoft .com/en-us/library/aa377284.aspx
                    >>
                    >Public Function ConnectionUserN ame()
                    >>
                    >Dim b1 As Integer
                    >>
                    >Dim b1RasConn As RASCONN
                    >>
                    >Dim b1IPcb, b1ipcconnection s As Integer
                    >>
                    >b1 = RasEnumConnecti ons(b1RasConn, b1IPcb, b1ipcconnection s)
                    >>
                    >Debug.Print(b1 )
                    >>
                    >'If everything goes OK, b1 must be "0"
                    >>
                    >ConnectionUser Name = b1RasConn.szEnt ryname
                    >>
                    >End Function
                    >>
                    >'-----------End of My Code--------------
                    >>
                    >'-Here's also a sub I found, which calls "RasEnumConnect ions
                    >>
                    >' for another purpose, but it uses a different method to call it.
                    >>
                    >'Maybe it helps:
                    >>
                    >Private Const RAS_MaxEntryNam e As Integer = 256
                    >Private Const RAS_MaxDeviceTy pe As Integer = 16
                    >Private Const RAS_MaxDeviceNa me As Integer = 128
                    >>
                    >Public Structure RASCONN
                    >Public dwSize As Integer
                    >Public hRasCon As IntPtr
                    ><MarshalAs(Unm anagedType.ByVa lTStr, sizeconst:=RAS_ MaxEntryName + 1)_
                    >Public szEntryname As String
                    ><MarshalAs(Unm anagedType.ByVa lTStr, sizeconst:=RAS_ MaxDeviceType + 1)_
                    >Public szDeviceType As String
                    ><MarshalAs(Unm anagedType.ByVa lTStr, sizeconst:=RAS_ MaxDeviceName + 1)_
                    >Public szDeviceName As String
                    >End Structure
                    >>
                    >Private Declare Auto Function RasEnumConnecti ons Lib "rasapi32.d ll"
                    >(ByVal _
                    >lpRasCon As IntPtr, ByRef lpcb As Integer, ByRef ByReflpcConnect ions As _
                    >Integer) As Integer
                    >>
                    >Public Module SubMain
                    >>
                    >Dim structtype As Type = GetType(RASCONN )
                    >Dim structsize As Integer = Marshal.SizeOf( GetType(RASCONN ))
                    >>
                    >Dim bufsize As Integer = structsize
                    >Dim entrycount As Integer
                    >Dim entries() As RASCONN
                    >Dim bufptr As IntPtr = Marshal.AllocHG lobal(structsiz e)
                    >Marshal.WriteI nt32(bufptr, structsize)
                    >>
                    >Dim retcode As Integer = RasEnumConnecti ons(bufptr, bufsize, entrycount)
                    >End Module
                    >
                    Hmm... I posted this yesterday but it seems it didn't make it through.
                    Anyways, I found the following article on codeproject that deals with
                    RasEnumConnecti ons. Using it I modified your code to at least return a
                    zero for the b1 variable. Unfortunately I don't use dial up
                    connections so I couldn't test the code further - I hope it helps you
                    out - let me know either way.
                    >

                    >
                    Public Structure RasConn
                    Public dwSize As Integer
                    Public hRasConn As Integer
                    Public szEntryName() As Byte
                    Public szDeviceType() As Byte
                    Public szDeviceName() As Byte
                    End Structure
                    >
                    <DllImport("ras api32.dll", SetLastError:=T rue,
                    CharSet:=CharSe t.Auto)_
                    Private Function RasEnumConnecti ons(<[In](), Out()ByRef rasconn
                    As RasConn, _
                    <[In](), Out()ByRef cb As
                    Integer, _
                    <Out()ByRef connections As
                    Integer) As UInt32
                    End Function
                    >
                    Public Function ConnectionUserN ame() As Byte()
                    Dim b1RasConn As RasConn = Nothing
                    Dim b1IPcb As Integer = &H53C
                    Dim b1ipcconnection s As Integer = 0
                    >
                    b1RasConn.dwSiz e = &H53C
                    >
                    Dim b1 As UInt32 = RasEnumConnecti ons(b1RasConn, b1IPcb,
                    b1ipcconnection s)
                    Debug.Print(b1. ToString())
                    'If everything goes OK, b1 must be "0"
                    Return b1RasConn.szEnt ryName
                    End Function
                    >
                    Thanks,
                    >
                    Seth Rowe
                    >

                    Comment

                    • rowe_newsgroups

                      #11
                      Re: (vb2005) I need your help for using &quot;Rasapi32. dll&quot;

                      On May 19, 9:36 am, "Javad" <j...@javad.com wrote:
                      Hello and thank you very much.
                      The code, has some "Errors" that I don't know how to solve. The errors are:
                      - Type "DllImport" is not defined
                      -Type "In" is not defined
                      -Type "out" is not defined.
                      >
                      I think the solution must be easy, but I don't have any idea how to solve
                      it.
                      >
                      Thank you
                      >
                      "rowe_newsgroup s" <rowe_em...@yah oo.comwrote in message
                      >
                      news:1179443342 .077866.148330@ l77g2000hsb.goo glegroups.com.. .
                      >
                      On May 16, 6:07 am, "Javad" <j...@javad.com wrote:
                      Thank you rowe for taking the time to modify the codes. I tried them, but
                      couldn't get them to work. They had much errors.
                      >
                      I searched MSDN and other resources. Now I know exactly where to get my
                      needed parameter, but don't know how. I know that "Connection 's User
                      Name" is a part of the string of "szEntryNam e" which is a parameter of
                      "RasConn" type. and I also know that the values of the parameters of
                      "RasConn" type can be obtained by calling "RasEnumConnect ions" function.
                      >
                      Here's a function I could write (and doesn't work properly but) I think
                      it
                      shows the
                      right way to get the connection's username. I hope someone here can
                      correct
                      it.
                      >
                      Thank you
                      >
                      '-How to get the UserName of the Active Connection--
                      >
                      Public Structure RASCONN
                      >
                      'http://msdn2.microsoft .com/en-us/library/aa376725.aspx
                      >
                      Public dwSize As Int32
                      >
                      Public hRasCon As Int32
                      >
                      Public szEntryname() As String
                      >
                      Public szDeviceType() As String
                      >
                      Public szDeviceName() As String
                      >
                      Public szPhoneBook() '(As ???)
                      >
                      Public dwSubEntry ' (As ???)
                      >
                      End Structure
                      >
                      Private Declare Auto Function RasEnumConnecti ons Lib "rasapi32.d ll" Alias
                      "RasEnumConnect ions" ( _
                      >
                      ByRef iprasconn As RASCONN, _
                      >
                      ByRef ipcb As Integer, _
                      >
                      ByRef ipcconnections As Integer) As Integer
                      >
                      'http://msdn2.microsoft .com/en-us/library/aa377284.aspx
                      >
                      Public Function ConnectionUserN ame()
                      >
                      Dim b1 As Integer
                      >
                      Dim b1RasConn As RASCONN
                      >
                      Dim b1IPcb, b1ipcconnection s As Integer
                      >
                      b1 = RasEnumConnecti ons(b1RasConn, b1IPcb, b1ipcconnection s)
                      >
                      Debug.Print(b1)
                      >
                      'If everything goes OK, b1 must be "0"
                      >
                      ConnectionUserN ame = b1RasConn.szEnt ryname
                      >
                      End Function
                      >
                      '-----------End of My Code--------------
                      >
                      '-Here's also a sub I found, which calls "RasEnumConnect ions
                      >
                      ' for another purpose, but it uses a different method to call it.
                      >
                      'Maybe it helps:
                      >
                      Private Const RAS_MaxEntryNam e As Integer = 256
                      Private Const RAS_MaxDeviceTy pe As Integer = 16
                      Private Const RAS_MaxDeviceNa me As Integer = 128
                      >
                      Public Structure RASCONN
                      Public dwSize As Integer
                      Public hRasCon As IntPtr
                      <MarshalAs(Unma nagedType.ByVal TStr, sizeconst:=RAS_ MaxEntryName + 1)_
                      Public szEntryname As String
                      <MarshalAs(Unma nagedType.ByVal TStr, sizeconst:=RAS_ MaxDeviceType + 1)_
                      Public szDeviceType As String
                      <MarshalAs(Unma nagedType.ByVal TStr, sizeconst:=RAS_ MaxDeviceName + 1)_
                      Public szDeviceName As String
                      End Structure
                      >
                      Private Declare Auto Function RasEnumConnecti ons Lib "rasapi32.d ll"
                      (ByVal _
                      lpRasCon As IntPtr, ByRef lpcb As Integer, ByRef ByReflpcConnect ions As _
                      Integer) As Integer
                      >
                      Public Module SubMain
                      >
                      Dim structtype As Type = GetType(RASCONN )
                      Dim structsize As Integer = Marshal.SizeOf( GetType(RASCONN ))
                      >
                      Dim bufsize As Integer = structsize
                      Dim entrycount As Integer
                      Dim entries() As RASCONN
                      Dim bufptr As IntPtr = Marshal.AllocHG lobal(structsiz e)
                      Marshal.WriteIn t32(bufptr, structsize)
                      >
                      Dim retcode As Integer = RasEnumConnecti ons(bufptr, bufsize, entrycount)
                      End Module
                      >
                      Hmm... I posted this yesterday but it seems it didn't make it through.
                      Anyways, I found the following article on codeproject that deals with
                      RasEnumConnecti ons. Using it I modified your code to at least return a
                      zero for the b1 variable. Unfortunately I don't use dial up
                      connections so I couldn't test the code further - I hope it helps you
                      out - let me know either way.
                      >>
                      Public Structure RasConn
                      Public dwSize As Integer
                      Public hRasConn As Integer
                      Public szEntryName() As Byte
                      Public szDeviceType() As Byte
                      Public szDeviceName() As Byte
                      End Structure
                      >
                      <DllImport("ras api32.dll", SetLastError:=T rue,
                      CharSet:=CharSe t.Auto)_
                      Private Function RasEnumConnecti ons(<[In](), Out()ByRef rasconn
                      As RasConn, _
                      <[In](), Out()ByRef cb As
                      Integer, _
                      <Out()ByRef connections As
                      Integer) As UInt32
                      End Function
                      >
                      Public Function ConnectionUserN ame() As Byte()
                      Dim b1RasConn As RasConn = Nothing
                      Dim b1IPcb As Integer = &H53C
                      Dim b1ipcconnection s As Integer = 0
                      >
                      b1RasConn.dwSiz e = &H53C
                      >
                      Dim b1 As UInt32 = RasEnumConnecti ons(b1RasConn, b1IPcb,
                      b1ipcconnection s)
                      Debug.Print(b1. ToString())
                      'If everything goes OK, b1 must be "0"
                      Return b1RasConn.szEnt ryName
                      End Function
                      >
                      Thanks,
                      >
                      Seth Rowe
                      At the top of the page type:

                      Imports System.Runtime. Interop

                      And all the errors should go away.

                      Thanks,

                      Seth Rowe

                      Comment

                      • Javad

                        #12
                        Re: (vb2005) I need your help for using &quot;Rasapi32. dll&quot;

                        hello and thank you
                        I added:
                        System.Runtime. InteropServices

                        to the top of my Form1 Class and all the errors were gone, but a new error
                        raised:

                        "Error 1 'System.Runtime .InteropService s.DllImportAttr ibute' cannot be
                        applied to instance method."

                        ?????
                        :(
                        Thnk u



                        "rowe_newsgroup s" <rowe_email@yah oo.comwrote in message
                        news:1179829956 .816312.157680@ r3g2000prh.goog legroups.com...
                        On May 19, 9:36 am, "Javad" <j...@javad.com wrote:
                        >Hello and thank you very much.
                        >The code, has some "Errors" that I don't know how to solve. The errors
                        >are:
                        >- Type "DllImport" is not defined
                        >-Type "In" is not defined
                        >-Type "out" is not defined.
                        >>
                        >I think the solution must be easy, but I don't have any idea how to solve
                        >it.
                        >>
                        >Thank you
                        >>
                        >"rowe_newsgrou ps" <rowe_em...@yah oo.comwrote in message
                        >>
                        >news:117944334 2.077866.148330 @l77g2000hsb.go oglegroups.com. ..
                        >>
                        On May 16, 6:07 am, "Javad" <j...@javad.com wrote:
                        >Thank you rowe for taking the time to modify the codes. I tried them,
                        >but
                        >couldn't get them to work. They had much errors.
                        >>
                        >I searched MSDN and other resources. Now I know exactly where to get
                        >my
                        >needed parameter, but don't know how. I know that "Connection 's User
                        >Name" is a part of the string of "szEntryNam e" which is a parameter of
                        >"RasConn" type. and I also know that the values of the parameters of
                        >"RasConn" type can be obtained by calling "RasEnumConnect ions"
                        >function.
                        >>
                        >Here's a function I could write (and doesn't work properly but) I
                        >think
                        >it
                        >shows the
                        >right way to get the connection's username. I hope someone here can
                        >correct
                        >it.
                        >>
                        >Thank you
                        >>
                        >'-How to get the UserName of the Active Connection--
                        >>
                        >Public Structure RASCONN
                        >>
                        >'http://msdn2.microsoft .com/en-us/library/aa376725.aspx
                        >>
                        >Public dwSize As Int32
                        >>
                        >Public hRasCon As Int32
                        >>
                        >Public szEntryname() As String
                        >>
                        >Public szDeviceType() As String
                        >>
                        >Public szDeviceName() As String
                        >>
                        >Public szPhoneBook() '(As ???)
                        >>
                        >Public dwSubEntry ' (As ???)
                        >>
                        >End Structure
                        >>
                        >Private Declare Auto Function RasEnumConnecti ons Lib "rasapi32.d ll"
                        >Alias
                        >"RasEnumConnec tions" ( _
                        >>
                        >ByRef iprasconn As RASCONN, _
                        >>
                        >ByRef ipcb As Integer, _
                        >>
                        >ByRef ipcconnections As Integer) As Integer
                        >>
                        >'http://msdn2.microsoft .com/en-us/library/aa377284.aspx
                        >>
                        >Public Function ConnectionUserN ame()
                        >>
                        >Dim b1 As Integer
                        >>
                        >Dim b1RasConn As RASCONN
                        >>
                        >Dim b1IPcb, b1ipcconnection s As Integer
                        >>
                        >b1 = RasEnumConnecti ons(b1RasConn, b1IPcb, b1ipcconnection s)
                        >>
                        >Debug.Print(b1 )
                        >>
                        >'If everything goes OK, b1 must be "0"
                        >>
                        >ConnectionUser Name = b1RasConn.szEnt ryname
                        >>
                        >End Function
                        >>
                        >'-----------End of My Code--------------
                        >>
                        >'-Here's also a sub I found, which calls "RasEnumConnect ions
                        >>
                        >' for another purpose, but it uses a different method to call it.
                        >>
                        >'Maybe it helps:
                        >>
                        >Private Const RAS_MaxEntryNam e As Integer = 256
                        >Private Const RAS_MaxDeviceTy pe As Integer = 16
                        >Private Const RAS_MaxDeviceNa me As Integer = 128
                        >>
                        >Public Structure RASCONN
                        >Public dwSize As Integer
                        >Public hRasCon As IntPtr
                        ><MarshalAs(Unm anagedType.ByVa lTStr, sizeconst:=RAS_ MaxEntryName + 1)>
                        >_
                        >Public szEntryname As String
                        ><MarshalAs(Unm anagedType.ByVa lTStr, sizeconst:=RAS_ MaxDeviceType + 1)>
                        >_
                        >Public szDeviceType As String
                        ><MarshalAs(Unm anagedType.ByVa lTStr, sizeconst:=RAS_ MaxDeviceName + 1)>
                        >_
                        >Public szDeviceName As String
                        >End Structure
                        >>
                        >Private Declare Auto Function RasEnumConnecti ons Lib "rasapi32.d ll"
                        >(ByVal _
                        >lpRasCon As IntPtr, ByRef lpcb As Integer, ByRef ByReflpcConnect ions
                        >As _
                        >Integer) As Integer
                        >>
                        >Public Module SubMain
                        >>
                        >Dim structtype As Type = GetType(RASCONN )
                        >Dim structsize As Integer = Marshal.SizeOf( GetType(RASCONN ))
                        >>
                        >Dim bufsize As Integer = structsize
                        >Dim entrycount As Integer
                        >Dim entries() As RASCONN
                        >Dim bufptr As IntPtr = Marshal.AllocHG lobal(structsiz e)
                        >Marshal.WriteI nt32(bufptr, structsize)
                        >>
                        >Dim retcode As Integer = RasEnumConnecti ons(bufptr, bufsize,
                        >entrycount)
                        >End Module
                        >>
                        Hmm... I posted this yesterday but it seems it didn't make it through.
                        Anyways, I found the following article on codeproject that deals with
                        RasEnumConnecti ons. Using it I modified your code to at least return a
                        zero for the b1 variable. Unfortunately I don't use dial up
                        connections so I couldn't test the code further - I hope it helps you
                        out - let me know either way.
                        >>>>
                        Public Structure RasConn
                        Public dwSize As Integer
                        Public hRasConn As Integer
                        Public szEntryName() As Byte
                        Public szDeviceType() As Byte
                        Public szDeviceName() As Byte
                        End Structure
                        >>
                        <DllImport("ras api32.dll", SetLastError:=T rue,
                        CharSet:=CharSe t.Auto)_
                        Private Function RasEnumConnecti ons(<[In](), Out()ByRef rasconn
                        As RasConn, _
                        <[In](), Out()ByRef cb As
                        Integer, _
                        <Out()ByRef connections As
                        Integer) As UInt32
                        End Function
                        >>
                        Public Function ConnectionUserN ame() As Byte()
                        Dim b1RasConn As RasConn = Nothing
                        Dim b1IPcb As Integer = &H53C
                        Dim b1ipcconnection s As Integer = 0
                        >>
                        b1RasConn.dwSiz e = &H53C
                        >>
                        Dim b1 As UInt32 = RasEnumConnecti ons(b1RasConn, b1IPcb,
                        b1ipcconnection s)
                        Debug.Print(b1. ToString())
                        'If everything goes OK, b1 must be "0"
                        Return b1RasConn.szEnt ryName
                        End Function
                        >>
                        Thanks,
                        >>
                        Seth Rowe
                        >
                        At the top of the page type:
                        >
                        Imports System.Runtime. Interop
                        >
                        And all the errors should go away.
                        >
                        Thanks,
                        >
                        Seth Rowe
                        >

                        Comment

                        • rowe_newsgroups

                          #13
                          Re: (vb2005) I need your help for using &quot;Rasapi32. dll&quot;

                          On May 23, 3:24 pm, "Javad" <j...@javad.com wrote:
                          hello and thank you
                          I added:
                          System.Runtime. InteropServices
                          >
                          to the top of my Form1 Class and all the errors were gone, but a new error
                          raised:
                          >
                          "Error 1 'System.Runtime .InteropService s.DllImportAttr ibute' cannot be
                          applied to instance method."
                          >
                          ?????
                          :(
                          Thnk u
                          >
                          "rowe_newsgroup s" <rowe_em...@yah oo.comwrote in message
                          >
                          news:1179829956 .816312.157680@ r3g2000prh.goog legroups.com...
                          >
                          On May 19, 9:36 am, "Javad" <j...@javad.com wrote:
                          Hello and thank you very much.
                          The code, has some "Errors" that I don't know how to solve. The errors
                          are:
                          - Type "DllImport" is not defined
                          -Type "In" is not defined
                          -Type "out" is not defined.
                          >
                          I think the solution must be easy, but I don't have any idea how to solve
                          it.
                          >
                          Thank you
                          >
                          "rowe_newsgroup s" <rowe_em...@yah oo.comwrote in message
                          >
                          >news:117944334 2.077866.148330 @l77g2000hsb.go oglegroups.com. ..
                          >
                          On May 16, 6:07 am, "Javad" <j...@javad.com wrote:
                          Thank you rowe for taking the time to modify the codes. I tried them,
                          but
                          couldn't get them to work. They had much errors.
                          >
                          I searched MSDN and other resources. Now I know exactly where to get
                          my
                          needed parameter, but don't know how. I know that "Connection 's User
                          Name" is a part of the string of "szEntryNam e" which is a parameter of
                          "RasConn" type. and I also know that the values of the parameters of
                          "RasConn" type can be obtained by calling "RasEnumConnect ions"
                          function.
                          >
                          Here's a function I could write (and doesn't work properly but) I
                          think
                          it
                          shows the
                          right way to get the connection's username. I hope someone here can
                          correct
                          it.
                          >
                          Thank you
                          >
                          '-How to get the UserName of the Active Connection--
                          >
                          Public Structure RASCONN
                          >
                          'http://msdn2.microsoft .com/en-us/library/aa376725.aspx
                          >
                          Public dwSize As Int32
                          >
                          Public hRasCon As Int32
                          >
                          Public szEntryname() As String
                          >
                          Public szDeviceType() As String
                          >
                          Public szDeviceName() As String
                          >
                          Public szPhoneBook() '(As ???)
                          >
                          Public dwSubEntry ' (As ???)
                          >
                          End Structure
                          >
                          Private Declare Auto Function RasEnumConnecti ons Lib "rasapi32.d ll"
                          Alias
                          "RasEnumConnect ions" ( _
                          >
                          ByRef iprasconn As RASCONN, _
                          >
                          ByRef ipcb As Integer, _
                          >
                          ByRef ipcconnections As Integer) As Integer
                          >
                          'http://msdn2.microsoft .com/en-us/library/aa377284.aspx
                          >
                          Public Function ConnectionUserN ame()
                          >
                          Dim b1 As Integer
                          >
                          Dim b1RasConn As RASCONN
                          >
                          Dim b1IPcb, b1ipcconnection s As Integer
                          >
                          b1 = RasEnumConnecti ons(b1RasConn, b1IPcb, b1ipcconnection s)
                          >
                          Debug.Print(b1)
                          >
                          'If everything goes OK, b1 must be "0"
                          >
                          ConnectionUserN ame = b1RasConn.szEnt ryname
                          >
                          End Function
                          >
                          '-----------End of My Code--------------
                          >
                          '-Here's also a sub I found, which calls "RasEnumConnect ions
                          >
                          ' for another purpose, but it uses a different method to call it.
                          >
                          'Maybe it helps:
                          >
                          Private Const RAS_MaxEntryNam e As Integer = 256
                          Private Const RAS_MaxDeviceTy pe As Integer = 16
                          Private Const RAS_MaxDeviceNa me As Integer = 128
                          >
                          Public Structure RASCONN
                          Public dwSize As Integer
                          Public hRasCon As IntPtr
                          <MarshalAs(Unma nagedType.ByVal TStr, sizeconst:=RAS_ MaxEntryName + 1)>
                          _
                          Public szEntryname As String
                          <MarshalAs(Unma nagedType.ByVal TStr, sizeconst:=RAS_ MaxDeviceType + 1)>
                          _
                          Public szDeviceType As String
                          <MarshalAs(Unma nagedType.ByVal TStr, sizeconst:=RAS_ MaxDeviceName + 1)>
                          _
                          Public szDeviceName As String
                          End Structure
                          >
                          Private Declare Auto Function RasEnumConnecti ons Lib "rasapi32.d ll"
                          (ByVal _
                          lpRasCon As IntPtr, ByRef lpcb As Integer, ByRef ByReflpcConnect ions
                          As _
                          Integer) As Integer
                          >
                          Public Module SubMain
                          >
                          Dim structtype As Type = GetType(RASCONN )
                          Dim structsize As Integer = Marshal.SizeOf( GetType(RASCONN ))
                          >
                          Dim bufsize As Integer = structsize
                          Dim entrycount As Integer
                          Dim entries() As RASCONN
                          Dim bufptr As IntPtr = Marshal.AllocHG lobal(structsiz e)
                          Marshal.WriteIn t32(bufptr, structsize)
                          >
                          Dim retcode As Integer = RasEnumConnecti ons(bufptr, bufsize,
                          entrycount)
                          End Module
                          >
                          Hmm... I posted this yesterday but it seems it didn't make it through.
                          Anyways, I found the following article on codeproject that deals with
                          RasEnumConnecti ons. Using it I modified your code to at least return a
                          zero for the b1 variable. Unfortunately I don't use dial up
                          connections so I couldn't test the code further - I hope it helps you
                          out - let me know either way.
                          >>
                          Public Structure RasConn
                          Public dwSize As Integer
                          Public hRasConn As Integer
                          Public szEntryName() As Byte
                          Public szDeviceType() As Byte
                          Public szDeviceName() As Byte
                          End Structure
                          >
                          <DllImport("ras api32.dll", SetLastError:=T rue,
                          CharSet:=CharSe t.Auto)_
                          Private Function RasEnumConnecti ons(<[In](), Out()ByRef rasconn
                          As RasConn, _
                          <[In](), Out()ByRef cb As
                          Integer, _
                          <Out()ByRef connections As
                          Integer) As UInt32
                          End Function
                          >
                          Public Function ConnectionUserN ame() As Byte()
                          Dim b1RasConn As RasConn = Nothing
                          Dim b1IPcb As Integer = &H53C
                          Dim b1ipcconnection s As Integer = 0
                          >
                          b1RasConn.dwSiz e = &H53C
                          >
                          Dim b1 As UInt32 = RasEnumConnecti ons(b1RasConn, b1IPcb,
                          b1ipcconnection s)
                          Debug.Print(b1. ToString())
                          'If everything goes OK, b1 must be "0"
                          Return b1RasConn.szEnt ryName
                          End Function
                          >
                          Thanks,
                          >
                          Seth Rowe
                          >
                          At the top of the page type:
                          >
                          Imports System.Runtime. Interop
                          >
                          And all the errors should go away.
                          >
                          Thanks,
                          >
                          Seth Rowe
                          Sorry about that - I wrote the above in a module in which you can't
                          declare the function as shared. Change this:

                          <DllImport("ras api32.dll", SetLastError:=T rue,
                          CharSet:=CharSe t.Auto)_
                          Private Function RasEnumConnecti ons(<[In](), Out()ByRef rasconn,
                          As RasConn, _
                          <[In](),
                          Out()ByRef cb As Integer, _
                          <Out()>
                          ByRef connections As Integer) As UInt32
                          End Function

                          To this:

                          <DllImport("ras api32.dll", SetLastError:=T rue,
                          CharSet:=CharSe t.Auto)_
                          Private Shared Function RasEnumConnecti ons(<[In](), Out()ByRef
                          rasconn, As RasConn, _
                          <[In](),
                          Out()ByRef cb As Integer, _
                          <Out()>
                          ByRef connections As Integer) As UInt32
                          End Function

                          And you should be set (hopefully).

                          Thanks,

                          Seth Rowe

                          Comment

                          • Javad

                            #14
                            Re: (vb2005) I need your help for using &quot;Rasapi32. dll&quot;

                            Hello and thanx a lot for your patience to solve my prob.

                            Now, all the errors are gone, and b1 returns 0 when I am not connected to
                            the internet. But when I am connected to internet, an error raises on this
                            line:
                            Dim b1 As UInt32 = RasEnumConnecti ons(b1RasConn, b1IPcb, b1ipcconnection s)

                            The error is:


                            Attempted to read or write protected memory. This is often an indication
                            that other memory is corrupt.

                            Any Suggestions? Isn't there someone who have ever used "Ras" functions? I'm
                            surre the problem is easy to solve.

                            Thank you

                            This is the final code until now:
                            Imports System.Runtime. InteropServices

                            Public Class Form1

                            Public Structure RasConn

                            Public dwSize As Integer

                            Public hRasConn As Integer

                            Public szEntryName() As Byte

                            Public szDeviceType() As Byte

                            Public szDeviceName() As Byte

                            End Structure

                            <DllImport("ras api32.dll", SetLastError:=T rue, CharSet:=CharSe t.Auto)_

                            Private Shared Function RasEnumConnecti ons(<[In](), Out()ByRef rasconn As
                            RasConn, _

                            <[In](), Out()ByRef cb As Integer, _

                            <Out()ByRef connections As Integer) As UInt32

                            End Function



                            Public Function ConnectionUserN ame() As Byte()

                            Dim b1RasConn As RasConn = Nothing

                            Dim b1IPcb As Integer = &H53C

                            Dim b1ipcconnection s As Integer = 0

                            b1RasConn.dwSiz e = &H53C

                            Dim b1 As UInt32 = RasEnumConnecti ons(b1RasConn, b1IPcb, b1ipcconnection s)

                            Debug.Print(b1. ToString())

                            'If everything goes OK, b1 must be "0"

                            Return b1RasConn.szEnt ryName

                            End Function

                            Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
                            System.EventArg s) Handles Button1.Click

                            MsgBox(Connecti onUserName)

                            End Sub

                            End Class


                            Comment

                            Working...