all groups > dotnet clr > june 2005 >
You're in the

dotnet clr

group:

Marshalling LPSTR


Marshalling LPSTR Henning Krause [MVP - Exhange]
6/20/2005 12:00:00 AM
dotnet clr:
Hello,

I have a COM interface which has the following method:
HRESULT Item(
[in] DWORD dwIndex,
[in] DWORD dwWhichName,
[in] DWORD cchLength,
[out,size_is(cchLength)] LPSTR pszName
);

When I create a Wrapper-Dll with midl.exe and tlbimp.exe, this method is
converted to:

public void Item ( System.UInt32 dwIndex , System.UInt32 dwWhichName ,
System.UInt32 cchLength , System.String pszName )

pszName is supposed to be a buffer, so obviously, this won't work. I've
already decompiled the dll I generated with ildasm, the result is this:

..method public hidebysig newslot abstract virtual
instance void Item([in] unsigned int32 dwIndex,
[in] unsigned int32 dwWhichName,
[in] unsigned int32 cchLength,
[out] string marshal( lpstr) pszName)
runtime managed internalcall

My question is: How do I need to modify this so that pszName becomes a
buffer that can be filled by the called method?

Some time ago I read something about using a StringBuilder... but I lost the
link.

Any ideas?

Greetings,
Henning Krause [MVP - Exchange]
==========================
Visit my website: http://www.infinitec.de
Try my free Exchange Explorer: Mistaya
(http://www.infinitec.de/software/mistaya.aspx)

Re: Marshalling LPSTR Valery Pryamikov
6/20/2005 8:58:02 PM
Henning,
this is not exactly answer to your question, but just an explanation of
reason that tlbimp did not handle this interface well: the reason is that
typelibrary neither stores nor uses size_is attribute. size_is attribute is
only available for proxi/stub marshalling.
However, if I remember it correctly, there was sample code in Adam Nathan's
book [*] that shown how to handle size_is in .Net. I don't have that book
handy right now, so I can't check, sorry.

[*]
http://www.amazon.com/exec/obidos/tg/detail/-/067232170X/ref=pd_sxp_f/104-7106506-9751163?v=glance&s=books

-Valery.
http://www.harper.no/valery

"Henning Krause [MVP - Exhange]" <newsgroup.no@spam.infinitec.de> wrote in
message news:O3IuYpadFHA.3808@TK2MSFTNGP14.phx.gbl...
[quoted text, click to view]
Re: Marshalling LPSTR Netveloper
6/21/2005 8:57:21 AM
Henning,

I believe you chould just change the datatype of the pszName parameter
to a StringBuilder instead of a string.

PS. You misspelled "Exchange" in your MVP declaration :-)

HTH

"Henning Krause [MVP - Exhange]" <newsgroup.no@spam.infinitec.de> skrev i
meddelandet news:O3IuYpadFHA.3808@TK2MSFTNGP14.phx.gbl...
[quoted text, click to view]

Re: Marshalling LPSTR [SOLVED] Henning Krause [MVP - Exchange]
6/26/2005 12:00:00 AM
Hello,

if anyone has a similar problem, here is a possible solution:

..method public hidebysignewslot abstract virtual instance void Item(
[in] unsigned int32 dwIndex,
[in] unsigned int32 dwWhichName,
[in] unsigned int32 chLength,
class [mscorlib]System.Text.StringBuilder marshal( lpstr) pszName)
runtime managed internalcall

Greetings,
Henning Krause [MVP - Exchange]
==========================
Visit my website: http://www.infinitec.de
Try my free Exchange Explorer: Mistaya
(http://www.infinitec.de/software/mistaya.aspx)


[quoted text, click to view]

Re: Marshalling LPSTR [SOLVED] Ben Rush
6/29/2005 7:40:54 PM
Hello Henning,

Another option you have is the MarshalAs attribute and SizeConst.You can do
the following:

void Googliebah[MarshalAs(UnmanagedType.LPArray,SizeConst=10)] ref
System.Byte[] var);

This is a snippet I wrote into the post, so it may not be perfect, but you
get the main idea - I've used the SizeConst attribute before.

--
Ben Rush
http://www.littlebigendian.com



"Henning Krause [MVP - Exchange]" <newsgroup.no@spam.infinitec.de> wrote in
message news:%23pIUfkleFHA.2244@TK2MSFTNGP15.phx.gbl...
[quoted text, click to view]

AddThis Social Bookmark Button