RasEnumEntries - incorrect struct size ??

G

Guest

Hi
I'm trying to enum all the RAS entries but continually get error 632 (invalid structure size). I know there are different sizes of the structure depending on version and I've tried with and without the Win2000 additional struct members...
The sizes should be either 264 or 532 depending on version - In VB6 it works fine with 264 - but not VB.Ne
I think it may be something to do with the way I'm writing the value into the dwSize member of the first structure (i.e. Marshal.WriteInt32(bufptr, structsize)

Anyone provide any hints ??? - Thanks .. Ke

I'm using the foloowing declaration

<StructLayout(LayoutKind.Sequential, Pack:=4, CharSet:=CharSet.Ansi)>
Public Structure RASENTRYNAM
Public dwSize As Intege
<MarshalAs(UnmanagedType.ByValTStr, sizeconst:=257)> Public szEntryname As Strin
'Public dwFlags As Intege
'<MarshalAs(UnmanagedType.ByValTStr, sizeconst:=261)> Public szPhonebook As Strin
End Structur

Public Declare Auto Function RasEnumEntries Lib "rasapi32.dll" (
ByVal lpcsReserved As String,
ByRef lpcsPhoneBook As String,
ByRef lpRASentries As IntPtr,
ByRef lpSize As Integer,
ByRef lpCount As Integer) As Intege

and the following in my code..

Private Function Connect() As Boolea

Dim structtype As Type = GetType(RASENTRYNAME
Dim structsize As Integer = Marshal.SizeOf(structtype
Dim maxcount As Integer = 25
Dim bufsize As Integer = maxcount * structsiz
Dim bufptr As IntPt
Dim realcount As Integer =
Dim retcode As Intege
Dim TRasCon As RASENTRYNAME(
Dim bResult As Boolean = Fals

bufptr = Marshal.AllocHGlobal(bufsize
Marshal.WriteInt32(bufptr, structsize

retcode = RasEnumEntries(vbNullString, vbNullString, bufptr, bufsize, realcount

If (retcode = 0) And (realcount > 0) The
ReDim TRasCon(realcount - 1
Dim i As Intege
Dim runptr As IntPtr = bufpt

For i = 0 To (realcount - 1
TRasCon(i) = CType(Marshal.PtrToStructure(runptr, structtype), RASENTRYNAME
If (TRasCon(i).szEntryname = sEntryName) The
MsgBox(TRasCon(i).szEntryname
End I
runptr = New IntPtr(runptr.ToInt32 + structsize
Nex
End I

Marshal.FreeHGlobal(bufptr

End Function
 
B

Bill McCarthy

Hi Ken,

Lots of issues there <g> You appear to be using an ansi aligned struct with
what is most probably a wide character api call (you declared the api as using
Auto .
also, you used to have to use pointer to structure for arrays etc, way back when
;) That has since been fixed.
Below is some code that works on .NET 1.1. If you are using an earlier version
of .NET, you cna probably piece together my sample from the developer discussion
archives circa December 2000 (wow, the time sure flies)
http://discuss.develop.com/archives/wa.exe?A2=ind0012A&L=DOTNET&T=0&F=&I=-3&S=&P=11213

The code below is for .NET 1.1



Sub Main()

Dim entries() As RASENTRYNAME = GetRasEntries()

For i As Int32 = 0 To entries.Length - 1
Console.WriteLine("{0}, {1}", entries(i).EntryName,
entries(i).PhonebookPath)
Next

Console.ReadLine()
End Sub



Public Const RAS_MaxEntryName As Int32 = 257
Public Const MAX_PATH As Int32 = 260

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
Public Structure RASENTRYNAME
Public Size As Int32
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=RAS_MaxEntryName + 1)> _
Public EntryName As String
Public Flags As Int32
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MAX_PATH + 1)> _
Public PhonebookPath As String
End Structure


Public Declare Auto Function RasEnumEntries Lib "rasapi32.dll" ( _
ByVal Reserved As String, _
ByVal Phonebook As String, _
<[In](), Out()> ByVal RasEntryNames() As
RASENTRYNAME, _
ByRef CountOfBytes As Int32, _
ByRef CountOfEntries As Int32 _
) As Int32









Public Function GetRasEntries() As RASENTRYNAME()
Return GetRasEntries(Nothing)
End Function


Public Function GetRasEntries(ByVal Phonebook As String) As RASENTRYNAME()
Return GetRasEntries(Phonebook, 1)
End Function


Private Function GetRasEntries(ByVal phonebook As String, ByVal count As
Int32) As RASENTRYNAME()
Dim entries() As RASENTRYNAME
Dim size As Int32 = Marshal.SizeOf(GetType(RASENTRYNAME))
Dim rtn As Int32
Dim CountOfBytes As Int32
Dim CountOfEntries As Int32 = count

ReDim entries(CountOfEntries - 1)
entries(0).Size = size
CountOfBytes = size * CountOfEntries
rtn = RasEnumEntries(Nothing, phonebook, entries, CountOfBytes,
CountOfEntries)

If CountOfEntries > count Then
Return GetRasEntries(phonebook, CountOfEntries)
Else
Return entries
End If
End Function







Ken Hughes said:
Hi,
I'm trying to enum all the RAS entries but continually get error 632 (invalid
structure size). I know there are different sizes of the structure depending on
version and I've tried with and without the Win2000 additional struct
members....
The sizes should be either 264 or 532 depending on version - In VB6 it works fine with 264 - but not VB.Net
I think it may be something to do with the way I'm writing the value into the
dwSize member of the first structure (i.e. Marshal.WriteInt32(bufptr,
structsize) )
 
G

Guest

Thanks Bill

Difficult getting to grips with the marshalling of data..
You don't have anything for RASDial do you - looks similar to the RasEnumEntries but could do without the trial and error..

Thanks .. ke
 
B

Bill McCarthy

Hi Ken,

Not handy, no. Think of it this way, but working through it, you'll learn more
;)



Kenn Hughes said:
Thanks Bill,

Difficult getting to grips with the marshalling of data...
You don't have anything for RASDial do you - looks similar to the
RasEnumEntries but could do without the trial and error...
 

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