VB.NET, RAS, RASDIAL AND STRUCTURES

B

Brian Corbett

I'm am trying to write a RAS Component that lists the Available RAS
Connections on the current machine, I have got as far as

getting the Connections using the RasEnumEntry API
Getting the Entry Properties for Each entry using RASGetEntryProperties
API
Getting the Dial Parameters using the RasGetEntryDialParams API

I am Getting unstuck when it comes to actually dial the Ras Connection. When
I actually Call the RASDIAL API I always Get Error 632 (Structure Size
Invalid) any Pointers that may be helpful to me would be appreciated.

Here is what I have :

*Ras Dial API Call*
<DllImport("RasApi32.dll", CharSet:=CharSet.Auto)> _
Public Function RasDial( _
ByRef lprasDialExtensions As RASDIALEXTENSIONS, _
ByVal lpszPhoneBook As String, _
ByRef lpRasDialParams As RASDIALPARAMS, _
ByVal dwNotifierType As Int32, _
ByVal hwndNotifier As System.Delegate, _
ByRef lphRasConn As IntPtr) As Int32
End Function

The threeStructures are Below:

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Public Structure RASDIALPARAMS
Public dwSize As Integer
<MarshalAs(UnmanagedType.ByValTStr,
sizeconst:=CInt(RASFieldSizeConstants.RAS_MaxEntryName + 1))> _
Public szEntryName As String
<MarshalAs(UnmanagedType.ByValTStr,
sizeconst:=CInt(RASFieldSizeConstants.RAS_MaxPhoneNumber + 1))> _
Public szPhoneNumber As String
<MarshalAs(UnmanagedType.ByValTStr,
sizeconst:=CInt(RASFieldSizeConstants.RAS_MaxCallbackNumber + 1))> _
Public szCallBackNumber As String
<MarshalAs(UnmanagedType.ByValTStr,
sizeconst:=CInt(RASFieldSizeConstants.UNLEN + 1))> _
Public szUsername As String
<MarshalAs(UnmanagedType.ByValTStr,
sizeconst:=CInt(RASFieldSizeConstants.PWLEN + 1))> _
Public szPassword As String
<MarshalAs(UnmanagedType.ByValTStr,
sizeconst:=CInt(RASFieldSizeConstants.DNLEN + 1))> _
Public szDomain As String
#If OSVER > 4 Then
Public dwSubEntry As Integer
Public dwCallBackID As Integer
#End If

'RASDIALEXTENSIONS
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Public Structure RASDIALEXTENSIONS
Public dwSize As Integer
Public dwfOptions As Integer
Public hwndParent As Integer
Public Reserved As Integer
Public Reserved1 As Integer
Public RasEapINfo As RasEapINfo
End Structure

'RASEAPINFO
<StructLayout(LayoutKind.Sequential, charset:=CharSet.Auto)> _
Public Structure RASEAPINFO
Public dwSizeofEAPInfo As Int32
Public pbEapInfo As Int32
End Structure

Here is the Code so far >

Public Function Connect() As Boolean
Dim x As Integer 'Return from API Calls
Dim hconn As IntPtr 'Connection Handle
Dim bln As Boolean 'Password Stored

mParams = New RASDIALPARAMS
With mParams
.dwSize = Marshal.SizeOf(GetType(RASDIALPARAMS))
.szEntryName = mEntryName
End With

x = RasGetEntryDialParams(mPhonebook, mParams, bln)
hconn = Nothing

x = RasDial(Nothing, Nothing, mParams, Nothing, Nothing, hconn)
End Function
 
F

Fergus Cooney

Hi Brian.

I know nothing about RAS but that didn't stop me investigating. :)

You left out the RASFieldSizeConstants so I got some values off the Net:

Enum RASFieldSizeConstants
RAS_MaxEntryName = 256
RAS_MaxPhoneNumber = 128
RAS_MaxCallbackNumber = 128
UNLEN = 256
PWLEN = 256
DNLEN = 12
End Enum

With those values and your structures, I obtained the following sizes,

Dim A1 As RasDial.RASEAPINFO
Dim A2 As RasDial.RASDIALEXTENSIONS
Dim A3 As RasDial.RASDIALPARAMS
Dim S As String = Marshal.SizeOf (A1) & ", " _
& Marshal.SizeOf (A2) & ", " _
& Marshal.SizeOf (A3)

RASEAPINFO 8
RASDIALEXTENSIONS 28
RASDIALPARAMS 2088

Also off the Net I got this (VB6):

Private Type RASDIALPARAMS
dwSize As Long ' 1052

Now this may or may not be the same version as your RASDIALPARAMS but
there is a big discrepancy. And I tracked that down to your Struct attribute.

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

Having CharSet.Auto leaves it up to the compiler. It's choosing Unicode
instead of Ansi. Changing it to CharSet.Ansi brings the size to 1040 but this
is still short.

I tried intellisense with A3 to see what fields it had and dwEntry and
dwCallBackID were missing. I'm on Win2000. Maybe OSVER isn't the correct name
to use. I don't know.

Anyway, I commented out the #If and the size came up to 1048. I noticed in
the code that dwSize was being set to the size of the structure plus 6. Which
brings us to 1052.

I've no idea what the correct value should be. I was looking at:


http://pub13.ezboard.com/fvisualbasicexplorervbtips.showMessage?topicID=355.to
pic
and
http://users.chariot.net.au/~akia/rasgetentrydialparams.htm

It'll probably make more sense to you.

The reason I've spelled out my labours is partly to keep track as I've
been doing it on and off for a while. The other part is to give you a flavour
of the sort of hunting around type things that you can use when solving
problems of this nature. Things to put in your toolkit as it were. :)

One final note. If you don't get any joy and it <still> complains about
the size, add an array of bytes to the end of your structure (to prevent
memory overwrites). Then create a loop with error trapping that will simply
try a whole range of sizes until it comes back without an error. At least then
you'll know what size you're aiming for.

Best of luck,
Fergus
 
C

Cor

Hi Fergus,
I know nothing and than an explanation about RAS if you are the natural born
hacker.
:)
Cor
 
B

Brian

Fergus, Firstly thanks for your reply. It has shed some light on some of
the problems that i have had.

I am still unable to get the correct structure size to pass into the
RasDial API. I have been using the Structure Information from the
Platform SDK on MS Web site

RASDIALPARAMS >
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/rras/rr
as/rasdialparams_str.asp

RASGetDIALEntryParams API>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/rras/rr
as/rasgetentrydialparams.asp

RASDIAL API
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/rras/rr
as/rasdial.asp

It says in the RASDIAL API Spec that to get the RASDIALPARAMS Structure
correctly - set the dwsize property to
"MARSHAL.SIZEOF(GETTYPE(RASDIALPARMS))" well thats how I interpreted it
;) and then pass this into the RASDIAL API. Which in theory should make
the structure the correct size. it doesn't. I had a similar problem with
another part of the APP which was RASENUMENTRYS and I was getting the
632 error then also - but as it turned out the structure size was being
affected by the incorrect (BYREF/BYVAL) declaration. I am at a loss as
to what is wrong. I have a working example of this in c# and when I step
through the code making notes of the sizes and values it is !exactly!
the same as in my VB.NET Implementation of the code. The only thing I
think it could be is the API and Structure Declarations themselves.
Don't know.

Brian Corbett
 
F

Fergus Cooney

Hi Brian,

I am a bit confused. You say the C# sizes and values match the VB ones exactly. This is <after> having changed CharSet.Auto to
Ansi? And what sizes/values are these?

Could you post the C# and your latest VB? Preferably in the form of classes/modules (zipped) that I can plug into a project. If
it's not too big, an entire project would be handy.

Did you check out the references that I gave? These show VB code that omits the '+ 1's that the MSDN doc shows, yet I believe
they show working code.

How did you get on with the idea of trying a range of sizes until it stops complaining?

Regards,
Fergus
 
B

Brian

Fergus, Thanks very much for your help. Fortunately I have found an
example that show me what I need to do in VB.NET so I can see where I am
going wrong - and I'm a long way off ;) - I have posted two links below
one is the c# example that I was using - and 1 to the vb.net example -
These may be of use to you sometime.

c#
http://www.codeproject.com/useritems/rasdemo.asp?target=ras

vb.net
http://groups.google.com/groups?q=ras+dotnet+vb&hl=en&lr=&ie=utf-8&oe=ut
f-8&safe=off&scoring=d&selm=ba47440d.0305151934.6d9d8ed8%40posting.googl
e.com&rnum=5http://groups.google.com/groups?selm=An87uzi%23BHA.1532%40cp
msftngxa08

Thanks again for you time and help
Brian Corbett
 
F

Fergus Cooney

Hi Brian,

Thanks for the links. That's a lot of Marshally code there, shudder. Glad someone else has done it.:)

Good luck with your App.

Regards,
Fergus
 
B

Black Ninja

I never finished this software due to time limitaitons. But when you
enumerate entries and stuff, pass a array of structures by reference
and the class function will fill it in. Then you can do For Each
statement to go through the array and know which entries you want to
work with,etc.

Let me know if you need help. Like I said, time permitting I will
actually make this thing a little more understandable than it is now.

I have implemented most of the features I needed using VB6. One day, I
will use the .NET stuff since it is better. It just doesnt have enough
documentation.
 

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