Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C++/CLI -- address of operator

Posted on 2009-05-16
10
Medium Priority
?
930 Views
Last Modified: 2012-05-07
Consider this code in Managed Extensions for C++ (.Net 1.1)

int MyInt;

MyFunction(&MyInt);

That calls MyFunction with the address of MyInt, because MyFunction wants an int*.

Now, in .Net 2+, MyFunction wants an int^.

So ... how do I call it?  If I say
MyFunction(%MyInt);
I am told Error C3071: % can only be applied to an instance of a ref class or a value type.

I would have thought that int was a value type.

So again, how do I call MyFunction?

Thanks!
0
Comment
Question by:Daniel Wilson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 200 total points
ID: 24403499
Did you mean

MyFunction(&MyInt);

?

'%' is the modulus operator.
0
 
LVL 32

Author Comment

by:Daniel Wilson
ID: 24403580
yes, % is modulus.

And in real C++, it has no other meaning, to my knowledge.

But in C++/CLI it is similar to &.  See the "tracking reference" section here:
http://en.wikipedia.org/wiki/C%2B%2B/CLI
0
 
LVL 40

Assisted Solution

by:mrjoltcola
mrjoltcola earned 200 total points
ID: 24404088
I think you need:

MyFunction(^%MyInt);
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 9

Expert Comment

by:tculler
ID: 24404444
I think what the function wants is a handle to an Int32^. I may be wrong, but the following demonstration should be along the lines you're looking for:
System::Int32 someInt;
 
MyFunction(%someInt);

Open in new window

0
 
LVL 32

Author Comment

by:Daniel Wilson
ID: 24404498
MyFunction(^%MyInt);
does not compile.  In addition to C3071, I get:
C2100: Illegal indirection
C3192: Syntax Error: '^' is not a prefix operator. (Did you mean '*' ?)
0
 
LVL 32

Author Comment

by:Daniel Wilson
ID: 24404501
System::Int32 someInt;
 
MyFunction(%someInt);

This doesn't compile either.  I still get C3071.

Thanks to both of you for your ideas.  Do you have more I can try?

0
 
LVL 32

Author Comment

by:Daniel Wilson
ID: 24404542
Another overload out there for MyFunction is
static void MyFunction(System::IntPtr x);

I do find that the call MyFunction(MyInt); compiles.  Will I be sorry if I go with this?
0
 
LVL 9

Assisted Solution

by:tculler
tculler earned 800 total points
ID: 24404729
Well, have you tried just using the address-of operator? It may actually be looking for a pointer to a native int.

System::IntPtr would be ok; it's just a wrapper for a pointer to an int. That structure is usually used for things like managed handles. The .NET Framework rather dislikes pointers, so they created a special wrapper just for that reason. For example, in C#, to even work with pointers at all, you must compile with a /unsafe switch, on top of having the pointer-utilizing code within a block marked as unsafe.

Anyways, that call should work just fine. If the overload requests it and it compiles, I see no reason it should be the wrong thing.

By the way, int and Int32 are very different in C++, but yes, they are value types. I'm not sure why it doesn't let you use the % operator on it; that error message is incorrect in assuming int is not a value type.
0
 
LVL 32

Author Comment

by:Daniel Wilson
ID: 24411328
>>Well, have you tried just using the address-of operator?

& ?  Yes, I started there.  But I get C2665: 'MyFunction': none of the X overloads could convert all the argument types


Of course, MyFunction is not really mine.  I'm working against the Tao Framework which is written in C#.

One of the functions is this one, the 2nd parameter being my problem one:
        public static extern int ChoosePixelFormat(IntPtr deviceContext, ref PIXELFORMATDESCRIPTOR pixelFormatDescriptor);

Another is this, again the 2nd parameter being the one in question.        
        public static
        void glGenTextures(Int32 n, [Out] IntPtr textures)
0
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 0 total points
ID: 24411420
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question