Good Day
and Happy New Year to all of you!

I was wondering if we should trust the auto-marshalling done by the CLR or
not ?

i mean this works very fine :
'***
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Private Shared Function GetPrivateProfileString( _
ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As StringBuilder, _
ByVal nSize As Int32, _
ByVal lpFileName As String) As Int32
'
End Function
'***

is there any advantage to hardcode the marshaling type like this (except for
the fact that we got self-documented code) ?
'***
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Private Shared Function GetPrivateProfileString( _
<MarshalAs(UnmanagedType.LPTStr)> ByVal lpAppName As String, _
<MarshalAs(UnmanagedType.LPTStr)> ByVal lpKeyName As String, _
<MarshalAs(UnmanagedType.LPTStr)> ByVal lpDefault As String, _
<MarshalAs(UnmanagedType.LPTStr)> ByVal lpReturnedString As
StringBuilder, _
<MarshalAs(UnmanagedType.U2)> ByVal nSize As Int32, _
<MarshalAs(UnmanagedType.LPTStr)> ByVal lpFileName As String) As Int32
'
End Function
'***

also is there any garantee that the defaults auto-marshalling made by the
CLR changes over the next versions and makes our actual
non-explicitly-marshalled API declaration crash in the future ?


thanks a lot !

--
Best Regards
Yanick

Re: MarshalAsAttribute() by Mattias

Mattias
Wed Jan 05 13:22:59 CST 2005


>I was wondering if we should trust the auto-marshalling done by the CLR or
>not ?

Absolutely, it works.


>is there any advantage to hardcode the marshaling type like this (except for
>the fact that we got self-documented code) ?

No, it just bloats your executable. But some people like to do it to
be explicit.


> <MarshalAs(UnmanagedType.U2)> ByVal nSize As Int32, _

If you're going to include explicit MarshalAs attributes, make sure
you do it correctly. U2 doesn't make sense on an Int32 parameter.


>also is there any garantee that the defaults auto-marshalling made by the
>CLR changes over the next versions and makes our actual
>non-explicitly-marshalled API declaration crash in the future ?

That would break an awful lot of code so it seems highly unlikely.



Mattias

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

Re: MarshalAsAttribute() by Zoury

Zoury
Wed Jan 05 13:38:13 CST 2005

Hi Mattias! :O)

> If you're going to include explicit MarshalAs attributes, make sure
> you do it correctly. U2 doesn't make sense on an Int32 parameter.

hehe! your right, that's a brain bug alright.


> >also is there any garantee that the defaults auto-marshalling made by the
> >CLR changes over the next versions and makes our actual
> >non-explicitly-marshalled API declaration crash in the future ?
>
> That would break an awful lot of code so it seems highly unlikely.

Yeah but... Microsoft seems quite gifted when it comes to breaking
compatibility from impair to pair SPs (or even versions). <g>


Thanks a lot for your input.

--
Best Regards
Yanick



Re: MarshalAsAttribute() by Michael

Michael
Fri Jan 07 04:10:24 CST 2005

> >is there any advantage to hardcode the marshaling type like this (except
for
> >the fact that we got self-documented code) ?
>
> No, it just bloats your executable. But some people like to do it to
> be explicit.

Well, there are cases where you are required to marshal manually...



Re: MarshalAsAttribute() by Mattias

Mattias
Fri Jan 07 20:12:45 CST 2005

Michael,

>Well, there are cases where you are required to marshal manually...

Sure, but the question here was about situations where you just
explicitly state the default behavior. And that's of course never
required.



Mattias

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

Re: MarshalAsAttribute() by Michael

Michael
Mon Jan 10 00:42:20 CST 2005

Indeed, there you are right. I can't imagine any situation where you must or
even should do so.

"Mattias Sjögren" <mattias.dont.want.spam@mvps.org> schrieb im Newsbeitrag
news:#xJa0cS9EHA.3336@TK2MSFTNGP11.phx.gbl...
> Michael,
>
> >Well, there are cases where you are required to marshal manually...
>
> Sure, but the question here was about situations where you just
> explicitly state the default behavior. And that's of course never
> required.
>
>
>
> Mattias
>
> --
> Mattias Sjögren [MVP] mattias @ mvps.org
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.