471,315 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,315 software developers and data experts.

passing references between C# and managed C++

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
Jan 30 '06 #1
8 1885
Change c++ to

void GimmeByRef(int __gc & reference);

?
"Lee Crabtree" <lc*******@goisi.com> wrote in message
news:Og**************@TK2MSFTNGP09.phx.gbl...
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

Jan 30 '06 #2
Lee Crabtree wrote:
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
Jan 30 '06 #3
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*.

"Lee Crabtree" <lc*******@goisi.com> wrote in message
news:OD**************@TK2MSFTNGP10.phx.gbl...
Lee Crabtree wrote:
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

Jan 30 '06 #4
Clive Dixon wrote:
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*.

"Lee Crabtree" <lc*******@goisi.com> wrote in message
news:OD**************@TK2MSFTNGP10.phx.gbl...
Lee Crabtree wrote:
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



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
Jan 30 '06 #5

"Lee Crabtree" <lc*******@goisi.com> wrote in message
news:Og**************@TK2MSFTNGP09.phx.gbl...
| 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.
Jan 30 '06 #6
Adding __gc works fine in VS2003 and it also works if you use the
/clr:oldSyntax 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);

?
"Lee Crabtree" <lc*******@goisi.com> wrote in message
news:Og**************@TK2MSFTNGP09.phx.gbl...
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


Mar 21 '06 #7
Use a "tracking reference", this will be emitted as a managed pointer in IL.

func(int% reference)

Willy.

"Matt Sawyer" <Ma********@discussions.microsoft.com> wrote in message
news:39**********************************@microsof t.com...
| Adding __gc works fine in VS2003 and it also works if you use the
| /clr:oldSyntax 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);
| >
| > ?
| >
| >
| > "Lee Crabtree" <lc*******@goisi.com> wrote in message
| > news:Og**************@TK2MSFTNGP09.phx.gbl...
| > > 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
| >
| >
| >
Mar 22 '06 #8
That did the trick, thank you!!!

"Willy Denoyette [MVP]" wrote:
Use a "tracking reference", this will be emitted as a managed pointer in IL.

func(int% reference)

Willy.

"Matt Sawyer" <Ma********@discussions.microsoft.com> wrote in message
news:39**********************************@microsof t.com...
| Adding __gc works fine in VS2003 and it also works if you use the
| /clr:oldSyntax 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);
| >
| > ?
| >
| >
| > "Lee Crabtree" <lc*******@goisi.com> wrote in message
| > news:Og**************@TK2MSFTNGP09.phx.gbl...
| > > 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
| >
| >
| >

Mar 22 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.