.NETGURU
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...
 
    
Dot Net Guruias 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.

Dot Net Guruias

--
Dot Net Guruias 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




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification