Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / June 2005

Tip: Looking for answers? Try searching our database.

Marshalling LPSTR

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Henning Krause [MVP - Exhange] - 20 Jun 2005 16:11 GMT
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)
Valery Pryamikov - 20 Jun 2005 19:58 GMT
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-710650
6-9751163?v=glance&s=books


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

> Hello,
>
[quoted text clipped - 36 lines]
> Try my free Exchange Explorer: Mistaya
> (http://www.infinitec.de/software/mistaya.aspx)
Netveloper - 21 Jun 2005 07:57 GMT
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

> Hello,
>
[quoted text clipped - 36 lines]
> Try my free Exchange Explorer: Mistaya
> (http://www.infinitec.de/software/mistaya.aspx)
Henning Krause [MVP - Exchange] - 26 Jun 2005 15:12 GMT
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)

> Henning,
>
[quoted text clipped - 45 lines]
>> Try my free Exchange Explorer: Mistaya
>> (http://www.infinitec.de/software/mistaya.aspx)
Ben Rush - 30 Jun 2005 01:40 GMT
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.

Signature

Ben Rush
http://www.littlebigendian.com

> Hello,
>
[quoted text clipped - 63 lines]
>>> Try my free Exchange Explorer: Mistaya
>>> (http://www.infinitec.de/software/mistaya.aspx)

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.