473,396 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Pass parameters by ref from c# to managed c++

I have a function as follows in managed c++.

public foo( object *objectValue)
{
// pseudo code below, as I don't have exact code at this very moment.
if( type of objectValue is int)
{
objectValue = objectValue * 2;
}
elseif(type of objectValue is short)
{
objectValue = objectValue * 3;
}
// This function does some more processing after this.
}

I wish to call above function from C#.

To say it briefly, from C# I wish to pass 'int' and 'short' type of objects
to a function in managed c++. Managed c++ code will alter the value. The
changed value should be available to me in C# code.

The problem: In C# if I call the above as:
int i = 10;
foo(ref i);
The above code gives me compilation error. If I do boxing, unboxing in c#
code to pass parameter, then the changed value is not reflected back in C#
function.

I request to please guide, how to achieve it? Thanks.

--
Regards,
Pravin Joshi

Nov 15 '05 #1
3 1875
Your C++ code requires a pointer argument, 'ref' aren't exactly the same a
pointer.

You should try this:

unsafe public integerManipulation()
{
int i = 10;
foo(&i);
}

of course, turn on the unsafe option in ur compiler options
"Pravin" <ex******@vsnl.com> wrote in message
news:e8**************@tk2msftngp13.phx.gbl...
I have a function as follows in managed c++.

public foo( object *objectValue)
{
// pseudo code below, as I don't have exact code at this very moment.
if( type of objectValue is int)
{
objectValue = objectValue * 2;
}
elseif(type of objectValue is short)
{
objectValue = objectValue * 3;
}
// This function does some more processing after this.
}

I wish to call above function from C#.

To say it briefly, from C# I wish to pass 'int' and 'short' type of objects to a function in managed c++. Managed c++ code will alter the value. The
changed value should be available to me in C# code.

The problem: In C# if I call the above as:
int i = 10;
foo(ref i);
The above code gives me compilation error. If I do boxing, unboxing in c#
code to pass parameter, then the changed value is not reflected back in C#
function.

I request to please guide, how to achieve it? Thanks.

--
Regards,
Pravin Joshi

Nov 15 '05 #2
Should be:
MC++:

.....
public:
foo( Object **objectValue) {
*objectValue = ....
....
}

C# :
foo(ref i);

Willy.
"Pravin" <ex******@vsnl.com> wrote in message news:e8**************@tk2msftngp13.phx.gbl...
I have a function as follows in managed c++.

public foo( object *objectValue)
{
// pseudo code below, as I don't have exact code at this very moment.
if( type of objectValue is int)
{
objectValue = objectValue * 2;
}
elseif(type of objectValue is short)
{
objectValue = objectValue * 3;
}
// This function does some more processing after this.
}

I wish to call above function from C#.

To say it briefly, from C# I wish to pass 'int' and 'short' type of objects
to a function in managed c++. Managed c++ code will alter the value. The
changed value should be available to me in C# code.

The problem: In C# if I call the above as:
int i = 10;
foo(ref i);
The above code gives me compilation error. If I do boxing, unboxing in c#
code to pass parameter, then the changed value is not reflected back in C#
function.

I request to please guide, how to achieve it? Thanks.

--
Regards,
Pravin Joshi

Nov 15 '05 #3
Should be:
MC++:

.....
public:
foo( Object **objectValue) {
*objectValue = ....
....
}

C# :
foo(ref i);

Willy.
"Pravin" <ex******@vsnl.com> wrote in message news:e8**************@tk2msftngp13.phx.gbl...
I have a function as follows in managed c++.

public foo( object *objectValue)
{
// pseudo code below, as I don't have exact code at this very moment.
if( type of objectValue is int)
{
objectValue = objectValue * 2;
}
elseif(type of objectValue is short)
{
objectValue = objectValue * 3;
}
// This function does some more processing after this.
}

I wish to call above function from C#.

To say it briefly, from C# I wish to pass 'int' and 'short' type of objects
to a function in managed c++. Managed c++ code will alter the value. The
changed value should be available to me in C# code.

The problem: In C# if I call the above as:
int i = 10;
foo(ref i);
The above code gives me compilation error. If I do boxing, unboxing in c#
code to pass parameter, then the changed value is not reflected back in C#
function.

I request to please guide, how to achieve it? Thanks.

--
Regards,
Pravin Joshi

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Brian F | last post by:
Exactly what the subject says. I have an ASP page that I want my C# windows application to open in Internet Explorer, and if possible have it send along a list of values from a list box. Thank you.
3
by: J | last post by:
I'm at a loss on how to accomplish one item with C# entirely in managed code -- I'd like to make a call to a Win32 function, one of its parameters is a structure that contains a pointer to one or...
16
by: Ekim | last post by:
hello, I'm allocating a byte-Array in C# with byte byteArray = new byte; Now I want to pass this byte-Array to a managed C++-function by reference, so that I'm able to change the content of the...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.