RASCREATE API call

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

VB.net 2000
Windows XP only

I have an app that (among other things) allows the user to create a new
"phonebook" entry. API is defined this way:

Declare Auto Function RasCreate Lib "Rasapi32.dll"_ Alias
"RasCreatePhonebookEntry" (ByVal hwnd As Integer,_
<MarshalAs(UnmanagedType.LPStr)> Byval lpszPhonebook As String) As Integer

(I use the same MarshalAs directive for the RASSETCREDENTIALS API and it
works perfectly.)

If I pass vbnull as the phonebook name like this:

APIResult = RasCreate(Me.Handle.ToInt32, VBNULL)

it works fine, but the entry is always created in the "All Users" phonebook.
I want the user to be able to select the private phonebook if he wants, so I
need to pass the phonebook name as a variable.

I use:
dim phonebook as string
dim APIResult as integer

phonebook =
Environment.GetFolderPath_(Environment.SpecialFolder.CommonApplicationData)_
& "\Microsoft\Network\Connections\Pbk\rasphone.pbk"

or:

phonebook =
Environment.GetFolderPath_(Environment.SpecialFolder.ApplicationData)_ &
"\Microsoft\Network\Connections\Pbk\rasphone.pbk"

depending on if the user wants the public or private phonebook. Then:

APIResult = RasCreate(Me.Handle.ToInt32, phonebook)

always returns "Error 621: The system could not open the phonebook file".

As I mentioned earlier, i use the exact same method to pass the phonebook to
RasSetCredentials and it works fine.

What am I missing?
 
Well I got it to work, but I'm as confused as ever. Maybe someone can explain
this.
I got it to work by leaving out the <marshalas..> directive so the delare is
just:

Declare Auto Function RasCreate Lib "Rasapi32.dll"_
Alias "RasCreatePhonebookEntry" (ByVal hwnd As Integer,_
Byval lpszPhonebook As String) As Integer

I'm confused because leaving out the <marshalas...> directive has caused an
error with every other RAS api i've used when passing a string variable. Can
anyone out there explain why RasCreatePhonebookEntry is different?
 
Doug,
I'm confused because leaving out the <marshalas...> directive has caused an
error with every other RAS api i've used when passing a string variable. Can
anyone out there explain why RasCreatePhonebookEntry is different?

The Auto modifier will cause the CLR to call the Unicode version of
the API on NT-based systems (which I assume you're running on), but
using MarshalAs(UnmanagedType.LPStr) will cause the string parameter
to marshal as an ANSI string. And passing an ANSI string to a function
expecting a Unicode string will of course not work. You could have
used MarshalAs(UnmanagedType.LPTStr), but leaving out the MarshalAs
attribute and relying on the default behavior works just as well.

BTW, I recommend changing the type of the hwnd parameter to IntPtr.



Mattias
 
So since this is strictly for our internal use and I don't care if it handles
unicode strings, would i be better off not using the Auto modifier and just
explicitly specifing Ansi?
 
NOW I get it!

On all my other API declarations, I had explicity listed the Ansi version in
the alias parameter (e.g. alias "RasSetCredentialsA") on this one I left off
the "A" in the alias. That's why I had to use the <MarshalAs...> directive on
the other calls to force an Ansi string.

Live and learn.
 
So since this is strictly for our internal use and I don't care if it handles
unicode strings, would i be better off not using the Auto modifier and just
explicitly specifing Ansi?

No, I definitely recommend using Auto.



Mattias
 
Back
Top