Search:
Namespaces
Discussions
.NET v1.1
Feedback
Ints versus Shorts in C# Apollo Wrapper
Messages
Related Types
This message was discovered on
microsoft.public.dotnet.framework.interop
.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...
Dan Noel (VIP)
I am working on project that involves numerous calls to a COM C++ program
that declares numerous parameters and return types as 16 bit shorts.
When we automatically generated a C# wrapper using tlibimp those short
parameters show up as int16 declarations.
When writing our C# wrapper, a colleague insists that we can use (ints) for
these 16 bit values in all of our wrapper code ... I am somewhat skeptical.
So far, we have not tested all the code, but apparently the marshaller is
able to translate from C++ shorts to ints.
My colleague wants ints on the C# end of the world because, he does not want
the C# programmer to need to worry about short declarations. The code is
easier to work with when everything takes an int. I agree with him on this
point, but I worry that we will get into trouble at some point with this type
of coding practice. Perhaps, we are just lucky that we have not had a problem
up to this point by using ints for our C# wrappers for our C++ shorts.
I would greatly appreciate some commentary from some C# interop gurus on
this point.
How do you declare your C# code which references C++ 16 bit integer values ?
Will we eventually run into problems by declaring our C++ shorts as C# ints ?
Reply to this message...
Robert Jordan
Hi Dan,
[Original message clipped]
AFAIK, that kind of "retyping" will only work, when you apply
the appropiate marshalling attributes:
C++:
short method(short arg);
C#
[return: MarshallAs(
UnmanagedType
.I2)]
int method(
[MarshallAs(
UnmanagedType
.I2)] int arg
);
bye
Rob
Reply to this message...
Robert Jordan
[Original message clipped]
MarshalAs ;-)
bye
Rob
Reply to this message...
Dan Noel (VIP)
Thanks for the response
Is there any chance this type of Marshaling happens implicitly ?
I am trying to figure out why we have not seen any problems doing this type
of conversion.
Dan
"Robert Jordan" wrote:
[Original message clipped]
Reply to this message...
Mattias Sjögren
Robert,
[Original message clipped]
You can't change the way an int is marshaled with MarshalAs, it's a
blittable type that will always be copied as 32 bites of data (for
performance reasons, among other things). If you try it you get a
MarshalDirectiveException
.
Mattias
--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
|
http://www.dotnetinterop.com
Please reply only to the newsgroup.
Reply to this message...
System.Runtime.InteropServices.MarshalDirectiveException
System.Runtime.InteropServices.UnmanagedType
Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us
-
Terms of Use
-
Privacy Policy
-
www.dotnet247.com