RasEnumConnections

Z

zurg

Hi!
I need really urgent help:
How to make RasEnumConnections work???
There were a few discusion about this function and RAS in general on this
forum so I'm sure you know the solvation...
On my computer this function returns 632
and I have no idea why...
It's really very important for me, help if you can...

My code looks like this:
Imports System.Runtime.InteropServices

Module rass

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, ByVal lpcb As Integer, ByVal ByReflpcConnections As
Integer) As Integer

Public Sub Main()

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 Sub

End Module
 
M

Mattias Sjögren

Private Declare Auto Function RasEnumConnections Lib "rasapi32.dll" (ByVal
lpRasCon As IntPtr, ByVal lpcb As Integer, ByVal ByReflpcConnections As
Integer) As Integer

The last two parameters should be passed ByRef.



Mattias
 
Z

zurg

Hi!
I've changed it - but still I get the retcode = 632 (should return 0 if
everything is right)
I've got no idea how to solve this problem...
Once again - help!

Now the code looks like this:
Imports System.Runtime.InteropServices

Module rasse

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 Sub Main()

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 Sub

End Module
 
A

Armin Zingler

zurg said:
Hi!
I've changed it - but still I get the retcode = 632 (should return 0
if everything is right)
I've got no idea how to solve this problem...
Once again - help!

Now the code looks like this:
Imports System.Runtime.InteropServices

Module rasse
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 Sub Main()
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 Sub
End Module


- Attach the structlayout attribute to structure rasconn
- Why do you use Marshal.AllocHGlobal instead of declare a variable of type
Rasconn and passing it BYREF to RasEnumConnections? (don't forget to set
dwsize)

Don't know if that's all, but have a try.
 
M

Mattias Sjögren

I've got no idea how to solve this problem...

Try adding this attribute to the RASCONN struct

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto, Pack:=4)>



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top