473,399 Members | 3,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

RASCREATE API call

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.SpecialFold er.CommonApplicationData)_
& "\Microsoft\Network\Connections\Pbk\rasphone.p bk"

or:

phonebook =
Environment.GetFolderPath_(Environment.SpecialFold er.ApplicationData)_ &
"\Microsoft\Network\Connections\Pbk\rasphone.p bk"

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?
Nov 21 '05 #1
6 3459
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 Robertson" wrote:
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.SpecialFold er.CommonApplicationData)_
& "\Microsoft\Network\Connections\Pbk\rasphone.p bk"

or:

phonebook =
Environment.GetFolderPath_(Environment.SpecialFold er.ApplicationData)_ &
"\Microsoft\Network\Connections\Pbk\rasphone.p bk"

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?

Nov 21 '05 #2
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #3
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?

"Mattias Sjögren" wrote:
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 21 '05 #4
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.

"Doug Robertson" wrote:
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?

"Mattias Sjögren" wrote:
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 21 '05 #5
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #6
I just finished creating a RAS wrapper for the .NET framework with
(hopefully) all RAS functionality for outgoing connections. It can be found
at:
http://www.gotdotnet.com/Workspaces/...3-e5fdb0895b8e

If you find any bugs, please report them so they can be addressed.
Thanks

"Doug Robertson" wrote:
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.SpecialFold er.CommonApplicationData)_
& "\Microsoft\Network\Connections\Pbk\rasphone.p bk"

or:

phonebook =
Environment.GetFolderPath_(Environment.SpecialFold er.ApplicationData)_ &
"\Microsoft\Network\Connections\Pbk\rasphone.p bk"

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?

Nov 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Hubert Baumeister | last post by:
Fifth International Conference on eXtreme Programming and Agile Processes in Software Engineering XP2004 June 6-10, 2004, Garmisch-Partenkirchen, Germany http://www.xp2004.org/
1
by: Marwan | last post by:
Hello I am using asynchronous delegates to make a call to a COM ActiveX object, but even though the call occurs on a separate thread, my UI is still blocking. If i put the thread to sleep in my...
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
4
by: John | last post by:
Hi all, This really is quite an urgent matter. I have a page with multiple, dynamically-loaded user controls and when a user clicks on a button, the whole form is submitted. Now at this stage...
13
by: mitchellpal | last post by:
i am really having a hard time trying to differentiate the two..........i mean.....anyone got a better idea how each occurs?
13
by: shsingh | last post by:
I have a class A containing some map as data variables. I creat an object of class A on heap by allocatiing memory by using "malloc". This will return me the required memory but the object is not...
3
by: cberthu | last post by:
Hi all, Is it possible to have two connects in the same rexx script to different DB's? I have to get data form on DB (with specifics selects and filter out some values with RExx) and save the...
9
by: CryptiqueGuy | last post by:
Consider the variadic function with the following prototype: int foo(int num,...); Here 'num' specifies the number of arguments, and assume that all the arguments that should be passed to this...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.