473,320 Members | 2,073 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,320 software developers and data experts.

StrPtr Question

dev
Hello,
How can the following code be converted to VB.Net? I am mostly interested
in how to handle the StrPtr calls in the function.

Private Declare Function PathRelativePathToW Lib "shlwapi.dll" _
(ByVal pszPath As Long, _
ByVal pszFrom As Long, _
ByVal dwAttrFrom As Long, _
ByVal pszTo As Long, _
ByVal dwAttrTo As Long) As Boolean

Public Function GetRelativePath(ByVal sFromDirectory As String, _
ByVal sToFile As String) As String

Dim sRelativePath As String
Dim bResult As Boolean

Const MAX_PATH As Long = 260

sRelativePath = Space(MAX_PATH)

' Set "Attr" to vbDirectory for directories and 0 for files
bResult = PathRelativePathToW(StrPtr(sRelativePath),
StrPtr(sFromDirectory), _
vbDirectory, StrPtr(sToFile), 0)

If bResult Then
GetRelativePath = Left(sRelativePath, InStr(sRelativePath,
Chr(0)) -1)
End If
End Function

TIA,
Steve
Nov 20 '05 #1
16 5402
Steve,
How can the following code be converted to VB.Net? I am mostly interested in how to handle the StrPtr calls in the function. StrPtr is no more! As it is no longer needed!

I would recommend something like:

Declare Auto Function PathRelativePathTo Lib "shlwapi.dll" (ByVal
pszPath As System.Text.StringBuilder, ByVal pszFrom As String, ByVal
dwAttrFrom As FileAttributes, ByVal pszTo As String, ByVal dwAttrTo As
FileAttributes) As Boolean

Public Function GetRelativePath(ByVal sFromDirectory As String,
ByVal sToFile As String) As String
Const MAX_PATH As Integer = 260

Dim sRelativePath As New System.Text.StringBuilder(MAX_PATH)

Dim bResult As Boolean

' Set "Attr" to vbDirectory for directories and 0 for files
bResult = PathRelativePathTo(sRelativePath, sFromDirectory,
FileAttributes.Directory, sToFile, 0)

If bResult Then
Return sRelativePath.ToString()
Else
Return Nothing
End If
End Function

Passing the output string as a StringBuilder is safest as Strings themselves
are immutable. Just remember to initialize the capacity of the StringBuilder
to the maximum size of characters that may be returned.

Both String & Stringbuilder are correctly converted when defined in
parameters. String is good for input parameters, StringBuilder are good for
Output & Input/Output parameters.

Declaring dwAttrFrom & dwAttrTo as System.IO.FileAttributes simplifies the
usage of the parameters.

Declaring the function as Auto, ensures that the Unicode strings are
converted properly respective to the OS being run on. On NT based OS the
strings will stay Unicode & the W version of the function is called, on 95
based OS the strings will be converted to Ansi and the A version of the
function will be called!

BTW: Interesting function I needed it a few weeks ago! I'll have to file it
away in my grab bag of functions.

Hope this helps
Jay

"dev" <de****@nospam.com> wrote in message
news:ed**************@TK2MSFTNGP10.phx.gbl... Hello,
How can the following code be converted to VB.Net? I am mostly interested in how to handle the StrPtr calls in the function.

Private Declare Function PathRelativePathToW Lib "shlwapi.dll" _
(ByVal pszPath As Long, _
ByVal pszFrom As Long, _
ByVal dwAttrFrom As Long, _
ByVal pszTo As Long, _
ByVal dwAttrTo As Long) As Boolean

Public Function GetRelativePath(ByVal sFromDirectory As String, _
ByVal sToFile As String) As String

Dim sRelativePath As String
Dim bResult As Boolean

Const MAX_PATH As Long = 260

sRelativePath = Space(MAX_PATH)

' Set "Attr" to vbDirectory for directories and 0 for files
bResult = PathRelativePathToW(StrPtr(sRelativePath),
StrPtr(sFromDirectory), _
vbDirectory, StrPtr(sToFile), 0)

If bResult Then
GetRelativePath = Left(sRelativePath, InStr(sRelativePath,
Chr(0)) -1)
End If
End Function

TIA,
Steve

Nov 20 '05 #2
* "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> scripsit:
Passing the output string as a StringBuilder is safest as Strings themselves
are immutable. Just remember to initialize the capacity of the StringBuilder
to the maximum size of characters that may be returned.


When using VB.NET's 'Declare', it's not necessary to declare the
parameter as 'StringBuilder'. VB.NET will do the marshalling
automatically.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #3
Herfried,
My answer is based on what Adam Nathan wrote in ".NET and COM - The Complete
Interoperability Guide" from SAMS press. Plus other MSDN Magazine articles.

Yes VB.NET will marshal the string correctly for you.

However! Remember that a System.String is immutable, the API that the OP
gave requires a buffer passed in, to put the output string in. Passing a
System.String for this buffer breaks the "immutable" intent of the
System.String class. So although it may appear to work you just modified an
immutable object, which may lead to subtle runtime bugs elsewhere.

Based on the above book and other articles in MSDN Magazine on P/Invoke
using StringBuilder for output strings is the safest & easiest method to
use.

So although you can send in a System.String to an output parameter of an API
call, I would seriously not recommend it as you are running a high risk of
corrupting the runtime. Of course using any API call you are running a risk
of corrupting the runtime time, why elevate that risk?

Hope this helps
Jay

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:uw****************@TK2MSFTNGP11.phx.gbl...
* "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> scripsit:
Passing the output string as a StringBuilder is safest as Strings themselves are immutable. Just remember to initialize the capacity of the StringBuilder to the maximum size of characters that may be returned.


When using VB.NET's 'Declare', it's not necessary to declare the
parameter as 'StringBuilder'. VB.NET will do the marshalling
automatically.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>

Nov 20 '05 #4
* "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> scripsit:
My answer is based on what Adam Nathan wrote in ".NET and COM - The Complete
Interoperability Guide" from SAMS press. Plus other MSDN Magazine articles.

Yes VB.NET will marshal the string correctly for you.

However! Remember that a System.String is immutable, the API that the OP
gave requires a buffer passed in, to put the output string in. Passing a
System.String for this buffer breaks the "immutable" intent of the
System.String class. So although it may appear to work you just modified an
immutable object, which may lead to subtle runtime bugs elsewhere.
ACK. But AFAIR it won't cause problems (except maybe performance
problems) when passing the string.
Based on the above book and other articles in MSDN Magazine on P/Invoke
using StringBuilder for output strings is the safest & easiest method to
use.
When using 'DllImport': Yes. When using 'Declare' it's not necessary
and IMHO not the "preferred" way. When using VB.NET's 'Declare', VB.NET
will automatically create the '<MarshalAs(UnmanagedType.VBByRefStr)>':

<http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemRuntimeInteropServicesUnmanagedTypeClas sTopic.asp>
So although you can send in a System.String to an output parameter of an API
call, I would seriously not recommend it as you are running a high risk of
corrupting the runtime. Of course using any API call you are running a risk
of corrupting the runtime time, why elevate that risk?


As mentioned above, I don't think that there is a risk in this case when
passing the string directly.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #5
Herfried,
Interesting!

I missed the section in the book on the VBByRefStr, thanks for pointing
VBByRefStr out as it is a handy MarshalAs parameter to know!

Note: Adam's book states the following about
MarshalAs(UnmanagedType.VBByRefStr), after explaining how it works in VB.NET
& how to use it in C#:

"This is not recommended, however. Even in Visual Basic .NET,
you should use StringBuilder rather then String if backwards
compatibility with existing source code isn't important"

Then he follows that with:

"TIP: You should use StringBuider to represent a buffer in
Visual Basic .NET, just like you would in C#.
Using String types for this case is simply a second
option for backward compatibility."

I'm sure when I read the above section 6 months ago the above info is what
stuck with me, and VBByRefStr slipped away. ;-)

I'm not certain what "backward compatibility" he is referring to, I would
think VB6. I wish he would dig deeper into why it is not recommended! Maybe
I just have not got to that section of the book yet ;-)

Looking closer at Adam's book, he indicates that you should use the
InAttribute & OutAttribute (from System.Runtime.InteropServices) to help
minimize the amount of copying going on, as StringBuilder & String will
actually copy the buffer for the call, then copy the buffer back. I suspect
VBByRefStr is doing its own amount of copying...

I normally try to include InAttribute & OutAttribute in my Declares &
DllImports.
When using 'DllImport': Yes. When using 'Declare' it's not necessary
and IMHO not the "preferred" way. I'll let you, now that I know its safe!

However! I will continue to recommend StringBuilder even with Declare. As to
me it is more obvious what is happening (strings are immutable) and it
allows converting back & forth from DllImport easier.
As mentioned above, I don't think that there is a risk in this case when
passing the string directly. In light of "MarshalAs(UnmanagedType.VBByRefStr)" I agree, there is no
greater risk then normal API calls.

Thanks for the info
Jay
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:ut***************@TK2MSFTNGP09.phx.gbl... * "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> scripsit:
My answer is based on what Adam Nathan wrote in ".NET and COM - The Complete Interoperability Guide" from SAMS press. Plus other MSDN Magazine articles.
Yes VB.NET will marshal the string correctly for you.

However! Remember that a System.String is immutable, the API that the OP
gave requires a buffer passed in, to put the output string in. Passing a
System.String for this buffer breaks the "immutable" intent of the
System.String class. So although it may appear to work you just modified an immutable object, which may lead to subtle runtime bugs elsewhere.
ACK. But AFAIR it won't cause problems (except maybe performance
problems) when passing the string.
Based on the above book and other articles in MSDN Magazine on P/Invoke
using StringBuilder for output strings is the safest & easiest method to
use.


When using 'DllImport': Yes. When using 'Declare' it's not necessary
and IMHO not the "preferred" way. When using VB.NET's 'Declare', VB.NET
will automatically create the '<MarshalAs(UnmanagedType.VBByRefStr)>':

<http://msdn.microsoft.com/library/en...mRuntimeIntero
pServicesUnmanagedTypeClassTopic.asp>
So although you can send in a System.String to an output parameter of an API call, I would seriously not recommend it as you are running a high risk of corrupting the runtime. Of course using any API call you are running a risk of corrupting the runtime time, why elevate that risk?


As mentioned above, I don't think that there is a risk in this case when
passing the string directly.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>

Nov 20 '05 #6
dev
Thanks, I had Googled StrPtr and most of the answers stated the the way to
handle StrPtr in VB.Net was specific to how StrPtr was being used in the
original function., VB6 in this case.

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:ut***************@TK2MSFTNGP09.phx.gbl...
* "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> scripsit:
My answer is based on what Adam Nathan wrote in ".NET and COM - The Complete Interoperability Guide" from SAMS press. Plus other MSDN Magazine articles.
Yes VB.NET will marshal the string correctly for you.

However! Remember that a System.String is immutable, the API that the OP
gave requires a buffer passed in, to put the output string in. Passing a
System.String for this buffer breaks the "immutable" intent of the
System.String class. So although it may appear to work you just modified an immutable object, which may lead to subtle runtime bugs elsewhere.
ACK. But AFAIR it won't cause problems (except maybe performance
problems) when passing the string.
Based on the above book and other articles in MSDN Magazine on P/Invoke
using StringBuilder for output strings is the safest & easiest method to
use.


When using 'DllImport': Yes. When using 'Declare' it's not necessary
and IMHO not the "preferred" way. When using VB.NET's 'Declare', VB.NET
will automatically create the '<MarshalAs(UnmanagedType.VBByRefStr)>':

<http://msdn.microsoft.com/library/en...mRuntimeIntero
pServicesUnmanagedTypeClassTopic.asp>
So although you can send in a System.String to an output parameter of an API call, I would seriously not recommend it as you are running a high risk of corrupting the runtime. Of course using any API call you are running a risk of corrupting the runtime time, why elevate that risk?


As mentioned above, I don't think that there is a risk in this case when
passing the string directly.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>

Nov 20 '05 #7
Dev,
Did you see my earlier post? I gave you a complete working example on how to
call the API. As StrPtr does not exist in VB.NET!

You need to use one of the following Declare statements:

Declare Auto Function PathRelativePathTo Lib "shlwapi.dll" (ByVal
pszPath As System.Text.StringBuilder, ByVal pszFrom As String, ByVal
dwAttrFrom As FileAttributes, ByVal pszTo As String, ByVal dwAttrTo As
FileAttributes) As Boolean

Declare Auto Function PathRelativePathTo Lib "shlwapi.dll" (ByVal
pszPath As String, ByVal pszFrom As String, ByVal
dwAttrFrom As FileAttributes, ByVal pszTo As String, ByVal dwAttrTo As
FileAttributes) As Boolean

The second one maybe easier for VB6 developers to follow, however I prefer
the first one. Both should do the job equally well for you!

Hope this helps
Jay
"dev" <de****@nospam.com> wrote in message
news:uZ******************@TK2MSFTNGP11.phx.gbl...
Thanks, I had Googled StrPtr and most of the answers stated the the way to
handle StrPtr in VB.Net was specific to how StrPtr was being used in the
original function., VB6 in this case.

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:ut***************@TK2MSFTNGP09.phx.gbl...
* "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> scripsit:
My answer is based on what Adam Nathan wrote in ".NET and COM - The Complete Interoperability Guide" from SAMS press. Plus other MSDN Magazine articles.
Yes VB.NET will marshal the string correctly for you.

However! Remember that a System.String is immutable, the API that the OP gave requires a buffer passed in, to put the output string in. Passing a System.String for this buffer breaks the "immutable" intent of the
System.String class. So although it may appear to work you just modified
an
immutable object, which may lead to subtle runtime bugs elsewhere.
ACK. But AFAIR it won't cause problems (except maybe performance
problems) when passing the string.
Based on the above book and other articles in MSDN Magazine on
P/Invoke using StringBuilder for output strings is the safest & easiest method to use.


When using 'DllImport': Yes. When using 'Declare' it's not necessary
and IMHO not the "preferred" way. When using VB.NET's 'Declare', VB.NET
will automatically create the '<MarshalAs(UnmanagedType.VBByRefStr)>':

<http://msdn.microsoft.com/library/en...mRuntimeIntero pServicesUnmanagedTypeClassTopic.asp>
So although you can send in a System.String to an output parameter of
an API call, I would seriously not recommend it as you are running a high
risk
of corrupting the runtime. Of course using any API call you are running a risk of corrupting the runtime time, why elevate that risk?


As mentioned above, I don't think that there is a risk in this case when
passing the string directly.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>


Nov 20 '05 #8
Cor
Hi Jay B.
That is the Jay B we know.
:-)
Cor

Nov 20 '05 #9
Herfried,
I believe I figured out why StringBuilder is recommended! ;-)

<MarshalAs(UnmanagedType.VBByRefStr)> can only be used with "InOut"
parameters, if you start adding InAttribute or OutAttribute to the declare
statement you need to give a MarshalAs with the 'regular' string types
<MarshalAs(UnmanagedType.LPTStr)> for example, other wise you get an
exception from the Marshaller

If you are not concerned with the InAttribute or OutAttribute then the
"default" behavior of VBByRefStr is reasonable for the Declare statement. As
it mimics what VB6 would do.

As I stated in my other post I normally include InAttribute & OutAttribute
on my Declare statements...

Hope this helps
Jay

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:ut***************@TK2MSFTNGP09.phx.gbl...
* "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> scripsit:
My answer is based on what Adam Nathan wrote in ".NET and COM - The Complete Interoperability Guide" from SAMS press. Plus other MSDN Magazine articles.
Yes VB.NET will marshal the string correctly for you.

However! Remember that a System.String is immutable, the API that the OP
gave requires a buffer passed in, to put the output string in. Passing a
System.String for this buffer breaks the "immutable" intent of the
System.String class. So although it may appear to work you just modified an immutable object, which may lead to subtle runtime bugs elsewhere.
ACK. But AFAIR it won't cause problems (except maybe performance
problems) when passing the string.
Based on the above book and other articles in MSDN Magazine on P/Invoke
using StringBuilder for output strings is the safest & easiest method to
use.


When using 'DllImport': Yes. When using 'Declare' it's not necessary
and IMHO not the "preferred" way. When using VB.NET's 'Declare', VB.NET
will automatically create the '<MarshalAs(UnmanagedType.VBByRefStr)>':

<http://msdn.microsoft.com/library/en...mRuntimeIntero
pServicesUnmanagedTypeClassTopic.asp>
So although you can send in a System.String to an output parameter of an API call, I would seriously not recommend it as you are running a high risk of corrupting the runtime. Of course using any API call you are running a risk of corrupting the runtime time, why elevate that risk?


As mentioned above, I don't think that there is a risk in this case when
passing the string directly.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>

Nov 20 '05 #10
dev
Yes, I saw it and I thank you for your example. My major point of interest
in my post was how to handle the non-existant StrPtr function in VB.Net. I
thought maybe there was another way to get a pointer to a string in VB.Net.
I am new to the VB.Net paradigm, but have programmed VB since VB3. I going
to try your example right now. Thanks again!

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ut**************@TK2MSFTNGP11.phx.gbl...
Dev,
Did you see my earlier post? I gave you a complete working example on how to call the API. As StrPtr does not exist in VB.NET!

You need to use one of the following Declare statements:

Declare Auto Function PathRelativePathTo Lib "shlwapi.dll" (ByVal
pszPath As System.Text.StringBuilder, ByVal pszFrom As String, ByVal
dwAttrFrom As FileAttributes, ByVal pszTo As String, ByVal dwAttrTo As
FileAttributes) As Boolean

Declare Auto Function PathRelativePathTo Lib "shlwapi.dll" (ByVal
pszPath As String, ByVal pszFrom As String, ByVal
dwAttrFrom As FileAttributes, ByVal pszTo As String, ByVal dwAttrTo As
FileAttributes) As Boolean

The second one maybe easier for VB6 developers to follow, however I prefer
the first one. Both should do the job equally well for you!

Hope this helps
Jay
"dev" <de****@nospam.com> wrote in message
news:uZ******************@TK2MSFTNGP11.phx.gbl...
Thanks, I had Googled StrPtr and most of the answers stated the the way to
handle StrPtr in VB.Net was specific to how StrPtr was being used in the
original function., VB6 in this case.

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:ut***************@TK2MSFTNGP09.phx.gbl...
* "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> scripsit:
> My answer is based on what Adam Nathan wrote in ".NET and COM - The Complete
> Interoperability Guide" from SAMS press. Plus other MSDN Magazine

articles.
>
> Yes VB.NET will marshal the string correctly for you.
>
> However! Remember that a System.String is immutable, the API that the OP > gave requires a buffer passed in, to put the output string in.
Passing
a > System.String for this buffer breaks the "immutable" intent of the
> System.String class. So although it may appear to work you just modified
an
> immutable object, which may lead to subtle runtime bugs elsewhere.

ACK. But AFAIR it won't cause problems (except maybe performance
problems) when passing the string.

> Based on the above book and other articles in MSDN Magazine on P/Invoke > using StringBuilder for output strings is the safest & easiest
method
to > use.

When using 'DllImport': Yes. When using 'Declare' it's not necessary
and IMHO not the "preferred" way. When using VB.NET's 'Declare',
VB.NET will automatically create the '<MarshalAs(UnmanagedType.VBByRefStr)>':

<http://msdn.microsoft.com/library/en...mRuntimeIntero pServicesUnmanagedTypeClassTopic.asp>

> So although you can send in a System.String to an output parameter of an
API
> call, I would seriously not recommend it as you are running a high risk
of
> corrupting the runtime. Of course using any API call you are running

a risk
> of corrupting the runtime time, why elevate that risk?

As mentioned above, I don't think that there is a risk in this case

when passing the string directly.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>



Nov 20 '05 #11
dev
The example works as required! Thanks.

"dev" <de****@nospam.com> wrote in message
news:OI*************@TK2MSFTNGP09.phx.gbl...
Yes, I saw it and I thank you for your example. My major point of interest in my post was how to handle the non-existant StrPtr function in VB.Net. I thought maybe there was another way to get a pointer to a string in VB.Net. I am new to the VB.Net paradigm, but have programmed VB since VB3. I going to try your example right now. Thanks again!

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ut**************@TK2MSFTNGP11.phx.gbl...
Dev,
Did you see my earlier post? I gave you a complete working example on how
to
call the API. As StrPtr does not exist in VB.NET!

You need to use one of the following Declare statements:

Declare Auto Function PathRelativePathTo Lib "shlwapi.dll" (ByVal pszPath As System.Text.StringBuilder, ByVal pszFrom As String, ByVal
dwAttrFrom As FileAttributes, ByVal pszTo As String, ByVal dwAttrTo As
FileAttributes) As Boolean

Declare Auto Function PathRelativePathTo Lib "shlwapi.dll" (ByVal pszPath As String, ByVal pszFrom As String, ByVal
dwAttrFrom As FileAttributes, ByVal pszTo As String, ByVal dwAttrTo As
FileAttributes) As Boolean

The second one maybe easier for VB6 developers to follow, however I prefer the first one. Both should do the job equally well for you!

Hope this helps
Jay
"dev" <de****@nospam.com> wrote in message
news:uZ******************@TK2MSFTNGP11.phx.gbl...
Thanks, I had Googled StrPtr and most of the answers stated the the way
to handle StrPtr in VB.Net was specific to how StrPtr was being used in
the original function., VB6 in this case.

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:ut***************@TK2MSFTNGP09.phx.gbl...
> * "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> scripsit:
> > My answer is based on what Adam Nathan wrote in ".NET and COM - The Complete
> > Interoperability Guide" from SAMS press. Plus other MSDN Magazine
articles.
> >
> > Yes VB.NET will marshal the string correctly for you.
> >
> > However! Remember that a System.String is immutable, the API that
the
OP
> > gave requires a buffer passed in, to put the output string in.

Passing
a
> > System.String for this buffer breaks the "immutable" intent of the
> > System.String class. So although it may appear to work you just

modified
an
> > immutable object, which may lead to subtle runtime bugs elsewhere.
>
> ACK. But AFAIR it won't cause problems (except maybe performance
> problems) when passing the string.
>
> > Based on the above book and other articles in MSDN Magazine on

P/Invoke
> > using StringBuilder for output strings is the safest & easiest

method
to
> > use.
>
> When using 'DllImport': Yes. When using 'Declare' it's not necessary > and IMHO not the "preferred" way. When using VB.NET's 'Declare',

VB.NET > will automatically create the '<MarshalAs(UnmanagedType.VBByRefStr)>': >
>

<http://msdn.microsoft.com/library/en...mRuntimeIntero
pServicesUnmanagedTypeClassTopic.asp>
>
> > So although you can send in a System.String to an output parameter of
an
API
> > call, I would seriously not recommend it as you are running a high

risk
of
> > corrupting the runtime. Of course using any API call you are

running a risk
> > of corrupting the runtime time, why elevate that risk?
>
> As mentioned above, I don't think that there is a risk in this case when > passing the string directly.
>
> --
> Herfried K. Wagner [MVP]
> <http://www.mvps.org/dotnet>



Nov 20 '05 #12
Dev,
Unfortunately that is on a case by case basis, as you stated.

In this case where you are passing the string to an API, you simply define
the API to accept a string, as I showed, and the Framework does all the work
for you. As Herfried & I discussed in our digression from your specific
question. VB.NET actually helps you a little over the normal framework
functions and allows you to use String for output parameters as well.

I have not really used StrPtr in VB6, so I don't know other what cases it is
used there. However if you give specific examples of StrPtr in VB6 I or
someone else will show you how to do the same thing in VB.NET. And as
Hefried & I have showed, there is probably more then one way to do it! ;-)

Hope this helps
Jay

"dev" <de****@nospam.com> wrote in message
news:OI*************@TK2MSFTNGP09.phx.gbl...
Yes, I saw it and I thank you for your example. My major point of interest in my post was how to handle the non-existant StrPtr function in VB.Net. I thought maybe there was another way to get a pointer to a string in VB.Net. I am new to the VB.Net paradigm, but have programmed VB since VB3. I going to try your example right now. Thanks again!

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ut**************@TK2MSFTNGP11.phx.gbl...
Dev,
Did you see my earlier post? I gave you a complete working example on how
to
call the API. As StrPtr does not exist in VB.NET!

You need to use one of the following Declare statements:

Declare Auto Function PathRelativePathTo Lib "shlwapi.dll" (ByVal pszPath As System.Text.StringBuilder, ByVal pszFrom As String, ByVal
dwAttrFrom As FileAttributes, ByVal pszTo As String, ByVal dwAttrTo As
FileAttributes) As Boolean

Declare Auto Function PathRelativePathTo Lib "shlwapi.dll" (ByVal pszPath As String, ByVal pszFrom As String, ByVal
dwAttrFrom As FileAttributes, ByVal pszTo As String, ByVal dwAttrTo As
FileAttributes) As Boolean

The second one maybe easier for VB6 developers to follow, however I prefer the first one. Both should do the job equally well for you!

Hope this helps
Jay
"dev" <de****@nospam.com> wrote in message
news:uZ******************@TK2MSFTNGP11.phx.gbl...
Thanks, I had Googled StrPtr and most of the answers stated the the way
to handle StrPtr in VB.Net was specific to how StrPtr was being used in
the original function., VB6 in this case.

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:ut***************@TK2MSFTNGP09.phx.gbl...
> * "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> scripsit:
> > My answer is based on what Adam Nathan wrote in ".NET and COM - The Complete
> > Interoperability Guide" from SAMS press. Plus other MSDN Magazine
articles.
> >
> > Yes VB.NET will marshal the string correctly for you.
> >
> > However! Remember that a System.String is immutable, the API that
the
OP
> > gave requires a buffer passed in, to put the output string in.

Passing
a
> > System.String for this buffer breaks the "immutable" intent of the
> > System.String class. So although it may appear to work you just

modified
an
> > immutable object, which may lead to subtle runtime bugs elsewhere.
>
> ACK. But AFAIR it won't cause problems (except maybe performance
> problems) when passing the string.
>
> > Based on the above book and other articles in MSDN Magazine on

P/Invoke
> > using StringBuilder for output strings is the safest & easiest

method
to
> > use.
>
> When using 'DllImport': Yes. When using 'Declare' it's not necessary > and IMHO not the "preferred" way. When using VB.NET's 'Declare',

VB.NET > will automatically create the '<MarshalAs(UnmanagedType.VBByRefStr)> ': >
>

<http://msdn.microsoft.com/library/en...mRuntimeIntero
pServicesUnmanagedTypeClassTopic.asp>
>
> > So although you can send in a System.String to an output parameter of
an
API
> > call, I would seriously not recommend it as you are running a high

risk
of
> > corrupting the runtime. Of course using any API call you are

running a risk
> > of corrupting the runtime time, why elevate that risk?
>
> As mentioned above, I don't think that there is a risk in this case when > passing the string directly.
>
> --
> Herfried K. Wagner [MVP]
> <http://www.mvps.org/dotnet>



Nov 20 '05 #13
* "dev" <de****@nospam.com> scripsit:
Thanks, I had Googled StrPtr and most of the answers stated the the way to
handle StrPtr in VB.Net was specific to how StrPtr was being used in the
original function., VB6 in this case.


Forget the 'StrPtr'.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #14
Jay,

* "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> scripsit:
I missed the section in the book on the VBByRefStr, thanks for pointing
VBByRefStr out as it is a handy MarshalAs parameter to know!
;-)
Note: Adam's book states the following about
MarshalAs(UnmanagedType.VBByRefStr), after explaining how it works in VB.NET
& how to use it in C#:

"This is not recommended, however. Even in Visual Basic .NET,
you should use StringBuilder rather then String if backwards
compatibility with existing source code isn't important"
Interesting.
Then he follows that with:

"TIP: You should use StringBuider to represent a buffer in
Visual Basic .NET, just like you would in C#.
Using String types for this case is simply a second
option for backward compatibility."
Maybe 'Declare' shouldn't be used too because if's only a 2nd option for
backward compatibility? I don't think so. It's valid VB.NET syntax, if
it worked in VB6, why shouldn't VB.NET be able to cope with strings too?
I'm sure when I read the above section 6 months ago the above info is what
stuck with me, and VBByRefStr slipped away. ;-)

I'm not certain what "backward compatibility" he is referring to, I would
think VB6. I wish he would dig deeper into why it is not recommended! Maybe
I just have not got to that section of the book yet ;-)
Would be really interesting to know. If you find out why, please let
me/us know.
actually copy the buffer for the call, then copy the buffer back. I suspect
VBByRefStr is doing its own amount of copying...


ACK. That's what I suspect too.
When using 'DllImport': Yes. When using 'Declare' it's not necessary
and IMHO not the "preferred" way.


I'll let you, now that I know its safe!


I would use a 'StringBuilder' too, but I only wanted to note that
'String' "should" work too in this case.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #15
dev
I never used any of the *Ptr functions in any version of VB since these
functions are undocumented and not guaranteed to be in the next version of
VB, as in the case of VB.Net. The only reason I asked in this case was
because I need to display a relative path to the user. The documentation I
found for PathRelativePathW included the StrPtr function for the call, which
I posted. So after Googling, I decided to ask as the opinion was the
translation from previous versions of VB to .Net was on a case by case
basis. I wanted ideas for this specific case, which were provided and I am
happy.

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:bs************@ID-208219.news.uni-berlin.de...
* "dev" <de****@nospam.com> scripsit:
Thanks, I had Googled StrPtr and most of the answers stated the the way to handle StrPtr in VB.Net was specific to how StrPtr was being used in the
original function., VB6 in this case.


Forget the 'StrPtr'.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>

Nov 20 '05 #16
dev
This was the only case I was interested in. If you ae interested, you can
Google Groups on 'StrPtr .Net +vb' and you will see the same posts I saw.
They included a hodgepodge of opinions, as always. Some used
GCHandle.Alloc and some other methods.

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:eD**************@TK2MSFTNGP09.phx.gbl...
Dev,
Unfortunately that is on a case by case basis, as you stated.

In this case where you are passing the string to an API, you simply define
the API to accept a string, as I showed, and the Framework does all the work for you. As Herfried & I discussed in our digression from your specific
question. VB.NET actually helps you a little over the normal framework
functions and allows you to use String for output parameters as well.

I have not really used StrPtr in VB6, so I don't know other what cases it is used there. However if you give specific examples of StrPtr in VB6 I or
someone else will show you how to do the same thing in VB.NET. And as
Hefried & I have showed, there is probably more then one way to do it! ;-)

Hope this helps
Jay

"dev" <de****@nospam.com> wrote in message
news:OI*************@TK2MSFTNGP09.phx.gbl...
Yes, I saw it and I thank you for your example. My major point of interest
in my post was how to handle the non-existant StrPtr function in VB.Net.

I
thought maybe there was another way to get a pointer to a string in

VB.Net.
I am new to the VB.Net paradigm, but have programmed VB since VB3. I

going
to try your example right now. Thanks again!

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ut**************@TK2MSFTNGP11.phx.gbl...
Dev,
Did you see my earlier post? I gave you a complete working example on how
to
call the API. As StrPtr does not exist in VB.NET!

You need to use one of the following Declare statements:

Declare Auto Function PathRelativePathTo Lib "shlwapi.dll"

(ByVal pszPath As System.Text.StringBuilder, ByVal pszFrom As String, ByVal
dwAttrFrom As FileAttributes, ByVal pszTo As String, ByVal dwAttrTo As
FileAttributes) As Boolean

Declare Auto Function PathRelativePathTo Lib "shlwapi.dll" (ByVal pszPath As String, ByVal pszFrom As String, ByVal
dwAttrFrom As FileAttributes, ByVal pszTo As String, ByVal dwAttrTo As
FileAttributes) As Boolean

The second one maybe easier for VB6 developers to follow, however I prefer the first one. Both should do the job equally well for you!

Hope this helps
Jay
"dev" <de****@nospam.com> wrote in message
news:uZ******************@TK2MSFTNGP11.phx.gbl...
> Thanks, I had Googled StrPtr and most of the answers stated the the way
to
> handle StrPtr in VB.Net was specific to how StrPtr was being used in

the > original function., VB6 in this case.
>
> "Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message > news:ut***************@TK2MSFTNGP09.phx.gbl...
> > * "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> scripsit: > > > My answer is based on what Adam Nathan wrote in ".NET and COM - The > Complete
> > > Interoperability Guide" from SAMS press. Plus other MSDN Magazine > articles.
> > >
> > > Yes VB.NET will marshal the string correctly for you.
> > >
> > > However! Remember that a System.String is immutable, the API that the
OP
> > > gave requires a buffer passed in, to put the output string in.

Passing
a
> > > System.String for this buffer breaks the "immutable" intent of
the > > > System.String class. So although it may appear to work you just
modified
> an
> > > immutable object, which may lead to subtle runtime bugs elsewhere. > >
> > ACK. But AFAIR it won't cause problems (except maybe performance
> > problems) when passing the string.
> >
> > > Based on the above book and other articles in MSDN Magazine on
P/Invoke
> > > using StringBuilder for output strings is the safest & easiest

method
to
> > > use.
> >
> > When using 'DllImport': Yes. When using 'Declare' it's not necessary > > and IMHO not the "preferred" way. When using VB.NET's 'Declare',

VB.NET
> > will automatically create the '<MarshalAs(UnmanagedType.VBByRefStr)> ':
> >
> >
>

<http://msdn.microsoft.com/library/en...mRuntimeIntero > pServicesUnmanagedTypeClassTopic.asp>
> >
> > > So although you can send in a System.String to an output parameter of
an
> API
> > > call, I would seriously not recommend it as you are running a
high risk
> of
> > > corrupting the runtime. Of course using any API call you are

running
a
> risk
> > > of corrupting the runtime time, why elevate that risk?
> >
> > As mentioned above, I don't think that there is a risk in this

case when
> > passing the string directly.
> >
> > --
> > Herfried K. Wagner [MVP]
> > <http://www.mvps.org/dotnet>
>
>



Nov 20 '05 #17

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

Similar topics

3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
10
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a...
11
by: Tim | last post by:
I've searched my help files, and all I can find is that the StrPtr() function is not supported. Is there a suitable replacement in VB.Net?
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
2
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.