passing references between C# and managed C++

L

Lee Crabtree

I'm trying to pass a variable by reference from a C# program to a
managed C++ wrapper. Something like this:

------------C++--------------
void GimmeByRef(int &reference);
-----------------------------

------------C#---------------
GimmeByRef(ref referenceVariable);
-----------------------------

However, I keep getting errors relating to not being able to convert
'ref uint' to 'uint*'. What am I doing wrong?

Lee Crabtree
 
L

Lee Crabtree

Lee said:
I'm trying to pass a variable by reference from a C# program to a
managed C++ wrapper. Something like this:

------------C++--------------
void GimmeByRef(int &reference);
-----------------------------

------------C#---------------
GimmeByRef(ref referenceVariable);
-----------------------------

However, I keep getting errors relating to not being able to convert
'ref uint' to 'uint*'. What am I doing wrong?

Lee Crabtree

Well, trying an IntPtr didn't do any good. Converting a 'ref
System.IntPtr' to an 'int*' doesn't work any better than before.

I think the problem is somehow related to the fact that 'ref' in C#
doesn't equate to '&' in C++. What's the managed C++ equivalent of the
'ref' keyword?

Lee Crabtree
 
C

Clive Dixon

As per my previous post I think you need 'int __gc&' to make it a managed
reference. 'int&' is an unmanaged reference which is basically an unmanaged
pointer - hence the compiler complaint about int*.
 
L

Lee Crabtree

Clive said:
As per my previous post I think you need 'int __gc&' to make it a managed
reference. 'int&' is an unmanaged reference which is basically an unmanaged
pointer - hence the compiler complaint about int*.

Yeah, we must have posted pretty close together...Thunderbird didn't
show a new post. You, sir, were correct, and I salute you for it.

Lee Crabtree
 
W

Willy Denoyette [MVP]

| I'm trying to pass a variable by reference from a C# program to a
| managed C++ wrapper. Something like this:
|
| ------------C++--------------
| void GimmeByRef(int &reference);
| -----------------------------
|
| ------------C#---------------
| GimmeByRef(ref referenceVariable);
| -----------------------------
|
| However, I keep getting errors relating to not being able to convert
| 'ref uint' to 'uint*'. What am I doing wrong?
|
| Lee Crabtree



void GimmeByRef(int __gc &reference)
or
void GimmeByRef(int __gc *reference)

another option is to pass a pointer in an unsafe context.

Willy.
 
G

Guest

Adding __gc works fine in VS2003 and it also works if you use the
/clr:blush:ldSyntax compiler option in VS2005. However, how do you do it using
the new syntax in VS2005 (with just /clr as the compiler option)?
 
W

Willy Denoyette [MVP]

Use a "tracking reference", this will be emitted as a managed pointer in IL.

func(int% reference)

Willy.

| Adding __gc works fine in VS2003 and it also works if you use the
| /clr:blush:ldSyntax compiler option in VS2005. However, how do you do it using
| the new syntax in VS2005 (with just /clr as the compiler option)?
|
|
| "Clive Dixon" wrote:
|
| > Change c++ to
| >
| > void GimmeByRef(int __gc & reference);
| >
| > ?
| >
| >
| > | > > I'm trying to pass a variable by reference from a C# program to a
managed
| > > C++ wrapper. Something like this:
| > >
| > > ------------C++--------------
| > > void GimmeByRef(int &reference);
| > > -----------------------------
| > >
| > > ------------C#---------------
| > > GimmeByRef(ref referenceVariable);
| > > -----------------------------
| > >
| > > However, I keep getting errors relating to not being able to convert
'ref
| > > uint' to 'uint*'. What am I doing wrong?
| > >
| > > Lee Crabtree
| >
| >
| >
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top