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

  • Thread starter Thread starter Javad
  • Start date Start date
J

Javad

Hello

I know that I should get the information of windows internet connections by
using "rasapi32.dll" 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
 
Hello

I know that I should get the information of windows internet connections by
using "rasapi32.dll" 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
 
Javad said:
I know that I should get the information of windows internet connections
by using "rasapi32.dll" 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.dll" 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.
 
Here's some sample codes. How can I get the UserName of an internet
connection? (vb 2005)

Declare Function RasGetEntryDialParams Lib "rasapi32.dll" Alias
"RasGetEntryDialParamsA" (ByVal lpcstr As String, ByRef lprasdialparamsa As
RASDIALPARAMS, ByRef lpbool As Long) As Long

Declare Function RasDial Lib "rasapi32.dll" Alias "RasDialA" (ByVal
lprasdialextensions As Long, ByVal lpcstr As String, ByRef lprasdialparamsa
As RASDIALPARAMS, ByVal dword As Long, ByVal lpvoid As Any, ByRef lphrasconn
As Long) As Long

Private Declare Function InternetDial Lib "wininet.dll" Alias
"InternetDialA" (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 InternetGoOnline Lib "wininet.dll" Alias
"InternetGoOnlineA" (ByVal lpszURL As String, ByVal hwndParent As Long,
ByVal dwReserved As Long) As Long
Private Declare Function InternetHangUp Lib "wininet.dll" (ByVal
dwConnection As Long, ByVal dwReserved As Long) As Long
Private Declare Function InternetAutodial Lib "wininet.dll" (ByVal
dwFlags As Long, ByVal hwndParent As Long) As Long
Private Declare Function InternetAutodialHangup Lib "wininet.dll" (ByVal
dwReserved As Long) As Long
Private Declare Function InternetOpen Lib "wininet.dll" Alias
"InternetOpenA" (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.dll" Alias
"InternetConnectA" (ByVal hInternetSession 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 InternetGetLastResponseInfo Lib "wininet.dll"
Alias "InternetGetLastResponseInfoA" (ByVal lpdwError As Long, ByVal
lpszBuffer As String, ByVal lpdwBufferLength As Long) As Boolean
Private Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile
As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, ByVal
lNumberOfBytesRead As Long) As Integer
Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal
hInet As Long) As Integer
Private Declare Function InternetGetConnectedStateEx Lib "wininet.dll"
Alias "InternetGetConnectedStateExA" (ByVal lpdwFlags As Long, ByVal
lpszConnectionName As Long, ByVal dwNameLen As Long, ByVal dwReserved As
Long) As Long




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

Declare Function RasGetErrorString Lib "rasapi32.dll" Alias
"RasGetErrorStringA" (ByVal uErrorValue As Long, ByVal lpszErrorString As
String, ByVal cBufSize As Long) As Long

'---------------------------------
'----------------------------------
'-----------------------------------
 
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.dll" 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
 
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.dll" 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






When I know there's a library such as "rasapi32.dll" 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
 
Here's some sample codes. How can I get the UserName of an internet
connection? (vb 2005)

Declare Function RasGetEntryDialParams Lib "rasapi32.dll" Alias
"RasGetEntryDialParamsA" (ByVal lpcstr As String, ByRef lprasdialparamsa As
RASDIALPARAMS, ByRef lpbool As Long) As Long

Declare Function RasDial Lib "rasapi32.dll" Alias "RasDialA" (ByVal
lprasdialextensions As Long, ByVal lpcstr As String, ByRef lprasdialparamsa
As RASDIALPARAMS, ByVal dword As Long, ByVal lpvoid As Any, ByRef lphrasconn
As Long) As Long

Private Declare Function InternetDial Lib "wininet.dll" Alias
"InternetDialA" (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 InternetGoOnline Lib "wininet.dll" Alias
"InternetGoOnlineA" (ByVal lpszURL As String, ByVal hwndParent As Long,
ByVal dwReserved As Long) As Long
Private Declare Function InternetHangUp Lib "wininet.dll" (ByVal
dwConnection As Long, ByVal dwReserved As Long) As Long
Private Declare Function InternetAutodial Lib "wininet.dll" (ByVal
dwFlags As Long, ByVal hwndParent As Long) As Long
Private Declare Function InternetAutodialHangup Lib "wininet.dll" (ByVal
dwReserved As Long) As Long
Private Declare Function InternetOpen Lib "wininet.dll" Alias
"InternetOpenA" (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.dll" Alias
"InternetConnectA" (ByVal hInternetSession 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 InternetGetLastResponseInfo Lib "wininet.dll"
Alias "InternetGetLastResponseInfoA" (ByVal lpdwError As Long, ByVal
lpszBuffer As String, ByVal lpdwBufferLength As Long) As Boolean
Private Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile
As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, ByVal
lNumberOfBytesRead As Long) As Integer
Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal
hInet As Long) As Integer
Private Declare Function InternetGetConnectedStateEx Lib "wininet.dll"
Alias "InternetGetConnectedStateExA" (ByVal lpdwFlags As Long, ByVal
lpszConnectionName As Long, ByVal dwNameLen As Long, ByVal dwReserved As
Long) As Long

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

Declare Function RasGetErrorString Lib "rasapi32.dll" Alias
"RasGetErrorStringA" (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(LayoutKind.Sequential)> _
Public Structure RASDIALPARAMS
Public dwSize As Integer
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=257)> Public
szEntryName As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=129)> Public
szPhoneNumber As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=129)> Public
szCallbackNumber As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=257)> Public
szUserName As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=257)> Public
szPassword As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=16)> Public
szDomain As String
Public dwSubEntry As UInt32
Public dwCallbackId As IntPtr
End Structure

<StructLayout(LayoutKind.Sequential)> _
Public Structure RASDIALEXTENSIONS
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("rasapi32.dll", CharSet:=CharSet.Auto)> _
Public Shared Function RasGetEntryDialParams(ByVal lpszPhonebook
As String, _
<[In](), Out()> ByRef
lprasdialparams As RASDIALPARAMS, _
<Out()> ByRef
lpfPassword As Boolean) As Integer
End Function

<DllImport("rasapi32.dll", CharSet:=CharSet.Auto)> _
Public Shared Function RasDial(<[In]()> ByVal lpRasDialExtensions
As RASDIALEXTENSIONS, _
<[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("wininet.dll")> _
Public Shared Function InternetDial(ByVal hWndParent As IntPtr, _

<MarshalAs(UnmanagedType.LPTStr)> ByVal pszEntryName As String, _
ByVal dwFlags As Integer, _
ByRef lpdwConnection As
Integer, _
ByVal dwReserved As Integer)
As Integer
End Function

<DllImport("wininet.dll")> _
Public Shared Function
InternetGoOnline(<MarshalAs(UnmanagedType.LPTStr)> ByVal lpszURL As
String, _
ByVal hWndParent As
IntPtr, _
ByVal dwFlags As Integer)
As Boolean
End Function

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

<DllImport("wininet.dll")> _
Public Shared Function InternetAutoDial(ByVal dwFlags As Integer,
_
ByVal hWndParent As
IntPtr) As Boolean
End Function

<DllImport("wininet.dll")> _
Public Shared Function InternetAutoDialHangup(ByVal dwReserved As
Integer) As Boolean
End Function

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

<DllImport("wininet.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("wininet.dll")> _
Public Shared Function InternetGetLastResponseInfo(ByRef errorCode
As Integer, _
ByVal buffer As
System.Text.StringBuilder, _
ByRef
bufferLength As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function

<DllImport("wininet.dll")> _
Public Shared Function InternetReadFile(ByVal hFile As IntPtr, _
ByRef lpBuffer As String,
_
ByVal
dwNumberOfBytesToRead As Integer, _
ByRef
lpdwNumberOfBytesRead As Integer) As Boolean
End Function

<DllImport("wininet.dll")> _
Public Shared Function InternetCloseHandle(ByVal hInternet As
IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function

<DllImport("wininet.dll")> _
Public Shared Function InternetGetConnectedStateEx(ByVal lpdwFlags
As Integer, _

<MarshalAs(UnmanagedType.LPTStr)> ByVal lpszConnectionName As String,
_
ByVal dwNameLen
As Integer, _
ByVal
dwReserved As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function

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

<DllImport("rasapi32.dll")> _
Public Shared Function RasGetErrorString(ByVal uErrorValue As
UInt32, _
ByVal lpszErrorString As
System.Text.StringBuilder, _
<[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
 
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 "szEntryName" 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 "RasEnumConnections" 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 RasEnumConnections Lib "rasapi32.dll" Alias
"RasEnumConnections" ( _

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 ConnectionUserName()

Dim b1 As Integer

Dim b1RasConn As RASCONN

Dim b1IPcb, b1ipcconnections As Integer

b1 = RasEnumConnections(b1RasConn, b1IPcb, b1ipcconnections)

Debug.Print(b1)

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

ConnectionUserName = b1RasConn.szEntryname

End Function



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

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

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

'Maybe it helps:

Private Const RAS_MaxEntryName As Integer = 256
Private Const RAS_MaxDeviceType As Integer = 16
Private Const RAS_MaxDeviceName As Integer = 128

Public Structure RASCONN
Public dwSize As Integer
Public hRasCon As IntPtr
<MarshalAs(UnmanagedType.ByValTStr, sizeconst:=RAS_MaxEntryName + 1)> _
Public szEntryname As String
<MarshalAs(UnmanagedType.ByValTStr, sizeconst:=RAS_MaxDeviceType + 1)> _
Public szDeviceType As String
<MarshalAs(UnmanagedType.ByValTStr, sizeconst:=RAS_MaxDeviceName + 1)> _
Public szDeviceName As String
End Structure

Private Declare Auto Function RasEnumConnections Lib "rasapi32.dll" (ByVal _
lpRasCon As IntPtr, ByRef lpcb As Integer, ByRef ByReflpcConnections 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.AllocHGlobal(structsize)
Marshal.WriteInt32(bufptr, structsize)

Dim retcode As Integer = RasEnumConnections(bufptr, bufsize, entrycount)
End Module
 
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 "szEntryName" 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 "RasEnumConnections" 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 RasEnumConnections Lib "rasapi32.dll" Alias
"RasEnumConnections" ( _

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 ConnectionUserName()

Dim b1 As Integer

Dim b1RasConn As RASCONN

Dim b1IPcb, b1ipcconnections As Integer

b1 = RasEnumConnections(b1RasConn, b1IPcb, b1ipcconnections)

Debug.Print(b1)

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

ConnectionUserName = b1RasConn.szEntryname

End Function

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

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

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

'Maybe it helps:

Private Const RAS_MaxEntryName As Integer = 256
Private Const RAS_MaxDeviceType As Integer = 16
Private Const RAS_MaxDeviceName As Integer = 128

Public Structure RASCONN
Public dwSize As Integer
Public hRasCon As IntPtr
<MarshalAs(UnmanagedType.ByValTStr, sizeconst:=RAS_MaxEntryName + 1)> _
Public szEntryname As String
<MarshalAs(UnmanagedType.ByValTStr, sizeconst:=RAS_MaxDeviceType + 1)> _
Public szDeviceType As String
<MarshalAs(UnmanagedType.ByValTStr, sizeconst:=RAS_MaxDeviceName + 1)> _
Public szDeviceName As String
End Structure

Private Declare Auto Function RasEnumConnections Lib "rasapi32.dll" (ByVal _
lpRasCon As IntPtr, ByRef lpcb As Integer, ByRef ByReflpcConnections 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.AllocHGlobal(structsize)
Marshal.WriteInt32(bufptr, structsize)

Dim retcode As Integer = RasEnumConnections(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
RasEnumConnections. 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.

http://www.codeproject.com/internet/rasconn_632.asp

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("rasapi32.dll", SetLastError:=True,
CharSet:=CharSet.Auto)> _
Private Function RasEnumConnections(<[In](), Out()> ByRef rasconn
As RasConn, _
<[In](), Out()> ByRef cb As
Integer, _
<Out()> ByRef connections As
Integer) As UInt32
End Function

Public Function ConnectionUserName() As Byte()
Dim b1RasConn As RasConn = Nothing
Dim b1IPcb As Integer = &H53C
Dim b1ipcconnections As Integer = 0

b1RasConn.dwSize = &H53C

Dim b1 As UInt32 = RasEnumConnections(b1RasConn, b1IPcb,
b1ipcconnections)
Debug.Print(b1.ToString())
'If everything goes OK, b1 must be "0"
Return b1RasConn.szEntryName
End Function

Thanks,

Seth Rowe
 
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_newsgroups said:
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 "szEntryName" 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 "RasEnumConnections" 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 RasEnumConnections Lib "rasapi32.dll" Alias
"RasEnumConnections" ( _

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 ConnectionUserName()

Dim b1 As Integer

Dim b1RasConn As RASCONN

Dim b1IPcb, b1ipcconnections As Integer

b1 = RasEnumConnections(b1RasConn, b1IPcb, b1ipcconnections)

Debug.Print(b1)

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

ConnectionUserName = b1RasConn.szEntryname

End Function

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

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

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

'Maybe it helps:

Private Const RAS_MaxEntryName As Integer = 256
Private Const RAS_MaxDeviceType As Integer = 16
Private Const RAS_MaxDeviceName As Integer = 128

Public Structure RASCONN
Public dwSize As Integer
Public hRasCon As IntPtr
<MarshalAs(UnmanagedType.ByValTStr, sizeconst:=RAS_MaxEntryName + 1)> _
Public szEntryname As String
<MarshalAs(UnmanagedType.ByValTStr, sizeconst:=RAS_MaxDeviceType + 1)> _
Public szDeviceType As String
<MarshalAs(UnmanagedType.ByValTStr, sizeconst:=RAS_MaxDeviceName + 1)> _
Public szDeviceName As String
End Structure

Private Declare Auto Function RasEnumConnections Lib "rasapi32.dll"
(ByVal _
lpRasCon As IntPtr, ByRef lpcb As Integer, ByRef ByReflpcConnections 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.AllocHGlobal(structsize)
Marshal.WriteInt32(bufptr, structsize)

Dim retcode As Integer = RasEnumConnections(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
RasEnumConnections. 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.

http://www.codeproject.com/internet/rasconn_632.asp

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("rasapi32.dll", SetLastError:=True,
CharSet:=CharSet.Auto)> _
Private Function RasEnumConnections(<[In](), Out()> ByRef rasconn
As RasConn, _
<[In](), Out()> ByRef cb As
Integer, _
<Out()> ByRef connections As
Integer) As UInt32
End Function

Public Function ConnectionUserName() As Byte()
Dim b1RasConn As RasConn = Nothing
Dim b1IPcb As Integer = &H53C
Dim b1ipcconnections As Integer = 0

b1RasConn.dwSize = &H53C

Dim b1 As UInt32 = RasEnumConnections(b1RasConn, b1IPcb,
b1ipcconnections)
Debug.Print(b1.ToString())
'If everything goes OK, b1 must be "0"
Return b1RasConn.szEntryName
End Function

Thanks,

Seth Rowe
 
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


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
RasEnumConnections. 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("rasapi32.dll", SetLastError:=True,
CharSet:=CharSet.Auto)> _
Private Function RasEnumConnections(<[In](), Out()> ByRef rasconn
As RasConn, _
<[In](), Out()> ByRef cb As
Integer, _
<Out()> ByRef connections As
Integer) As UInt32
End Function
Public Function ConnectionUserName() As Byte()
Dim b1RasConn As RasConn = Nothing
Dim b1IPcb As Integer = &H53C
Dim b1ipcconnections As Integer = 0
b1RasConn.dwSize = &H53C
Dim b1 As UInt32 = RasEnumConnections(b1RasConn, b1IPcb,
b1ipcconnections)
Debug.Print(b1.ToString())
'If everything goes OK, b1 must be "0"
Return b1RasConn.szEntryName
End Function

Seth Rowe

At the top of the page type:

Imports System.Runtime.Interop

And all the errors should go away.

Thanks,

Seth Rowe
 
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.InteropServices.DllImportAttribute' cannot be
applied to instance method."

?????
:(
Thnk u



rowe_newsgroups said:
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


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 "szEntryName" 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 "RasEnumConnections"
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

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 RasEnumConnections Lib "rasapi32.dll"
Alias
"RasEnumConnections" ( _
ByRef iprasconn As RASCONN, _
ByRef ipcb As Integer, _
ByRef ipcconnections As Integer) As Integer

Public Function ConnectionUserName()
Dim b1 As Integer
Dim b1RasConn As RASCONN
Dim b1IPcb, b1ipcconnections As Integer
b1 = RasEnumConnections(b1RasConn, b1IPcb, b1ipcconnections)

'If everything goes OK, b1 must be "0"
ConnectionUserName = b1RasConn.szEntryname
End Function
'-----------End of My Code--------------
'-Here's also a sub I found, which calls "RasEnumConnections
' for another purpose, but it uses a different method to call it.
'Maybe it helps:
Private Const RAS_MaxEntryName As Integer = 256
Private Const RAS_MaxDeviceType As Integer = 16
Private Const RAS_MaxDeviceName As Integer = 128
Public Structure RASCONN
Public dwSize As Integer
Public hRasCon As IntPtr
<MarshalAs(UnmanagedType.ByValTStr, sizeconst:=RAS_MaxEntryName + 1)>
_
Public szEntryname As String
<MarshalAs(UnmanagedType.ByValTStr, sizeconst:=RAS_MaxDeviceType + 1)>
_
Public szDeviceType As String
<MarshalAs(UnmanagedType.ByValTStr, sizeconst:=RAS_MaxDeviceName + 1)>
_
Public szDeviceName As String
End Structure
Private Declare Auto Function RasEnumConnections Lib "rasapi32.dll"
(ByVal _
lpRasCon As IntPtr, ByRef lpcb As Integer, ByRef ByReflpcConnections
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.AllocHGlobal(structsize)
Marshal.WriteInt32(bufptr, structsize)
Dim retcode As Integer = RasEnumConnections(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
RasEnumConnections. 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("rasapi32.dll", SetLastError:=True,
CharSet:=CharSet.Auto)> _
Private Function RasEnumConnections(<[In](), Out()> ByRef rasconn
As RasConn, _
<[In](), Out()> ByRef cb As
Integer, _
<Out()> ByRef connections As
Integer) As UInt32
End Function
Public Function ConnectionUserName() As Byte()
Dim b1RasConn As RasConn = Nothing
Dim b1IPcb As Integer = &H53C
Dim b1ipcconnections As Integer = 0
b1RasConn.dwSize = &H53C
Dim b1 As UInt32 = RasEnumConnections(b1RasConn, b1IPcb,
b1ipcconnections)
Debug.Print(b1.ToString())
'If everything goes OK, b1 must be "0"
Return b1RasConn.szEntryName
End Function

Seth Rowe

At the top of the page type:

Imports System.Runtime.Interop

And all the errors should go away.

Thanks,

Seth Rowe
 
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.InteropServices.DllImportAttribute' cannot be
applied to instance method."

?????
:(
Thnk u


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

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 "szEntryName" 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 "RasEnumConnections"
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 RasEnumConnections Lib "rasapi32.dll"
Alias
"RasEnumConnections" ( _
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 ConnectionUserName()
Dim b1 As Integer
Dim b1RasConn As RASCONN
Dim b1IPcb, b1ipcconnections As Integer
b1 = RasEnumConnections(b1RasConn, b1IPcb, b1ipcconnections)
Debug.Print(b1)
'If everything goes OK, b1 must be "0"
ConnectionUserName = b1RasConn.szEntryname
End Function
'-----------End of My Code--------------
'-Here's also a sub I found, which calls "RasEnumConnections
' for another purpose, but it uses a different method to call it.
'Maybe it helps:
Private Const RAS_MaxEntryName As Integer = 256
Private Const RAS_MaxDeviceType As Integer = 16
Private Const RAS_MaxDeviceName As Integer = 128
Public Structure RASCONN
Public dwSize As Integer
Public hRasCon As IntPtr
<MarshalAs(UnmanagedType.ByValTStr, sizeconst:=RAS_MaxEntryName + 1)>
_
Public szEntryname As String
<MarshalAs(UnmanagedType.ByValTStr, sizeconst:=RAS_MaxDeviceType + 1)>
_
Public szDeviceType As String
<MarshalAs(UnmanagedType.ByValTStr, sizeconst:=RAS_MaxDeviceName + 1)>
_
Public szDeviceName As String
End Structure
Private Declare Auto Function RasEnumConnections Lib "rasapi32.dll"
(ByVal _
lpRasCon As IntPtr, ByRef lpcb As Integer, ByRef ByReflpcConnections
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.AllocHGlobal(structsize)
Marshal.WriteInt32(bufptr, structsize)
Dim retcode As Integer = RasEnumConnections(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
RasEnumConnections. 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.
http://www.codeproject.com/internet/rasconn_632.asp
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("rasapi32.dll", SetLastError:=True,
CharSet:=CharSet.Auto)> _
Private Function RasEnumConnections(<[In](), Out()> ByRef rasconn
As RasConn, _
<[In](), Out()> ByRef cb As
Integer, _
<Out()> ByRef connections As
Integer) As UInt32
End Function
Public Function ConnectionUserName() As Byte()
Dim b1RasConn As RasConn = Nothing
Dim b1IPcb As Integer = &H53C
Dim b1ipcconnections As Integer = 0
b1RasConn.dwSize = &H53C
Dim b1 As UInt32 = RasEnumConnections(b1RasConn, b1IPcb,
b1ipcconnections)
Debug.Print(b1.ToString())
'If everything goes OK, b1 must be "0"
Return b1RasConn.szEntryName
End Function
Thanks,
Seth Rowe
At the top of the page type:
Imports System.Runtime.Interop
And all the errors should go away.

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("rasapi32.dll", SetLastError:=True,
CharSet:=CharSet.Auto)> _
Private Function RasEnumConnections(<[In](), Out()> ByRef rasconn,
As RasConn, _
<[In](),
Out()> ByRef cb As Integer, _
<Out()>
ByRef connections As Integer) As UInt32
End Function

To this:

<DllImport("rasapi32.dll", SetLastError:=True,
CharSet:=CharSet.Auto)> _
Private Shared Function RasEnumConnections(<[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
 
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 = RasEnumConnections(b1RasConn, b1IPcb, b1ipcconnections)

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("rasapi32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _

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

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

<Out()> ByRef connections As Integer) As UInt32

End Function



Public Function ConnectionUserName() As Byte()

Dim b1RasConn As RasConn = Nothing

Dim b1IPcb As Integer = &H53C

Dim b1ipcconnections As Integer = 0

b1RasConn.dwSize = &H53C

Dim b1 As UInt32 = RasEnumConnections(b1RasConn, b1IPcb, b1ipcconnections)

Debug.Print(b1.ToString())

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

Return b1RasConn.szEntryName

End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

MsgBox(ConnectionUserName)

End Sub

End Class
 
Back
Top