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)