473,652 Members | 2,979 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function Arguments - Reference to a Value type?

Another noob question for you all . . .

I have functions written in C++ that I want to call from C#. I need to
be able to pass a reference to a value type (like an int) so that the
function can change the value - just like passing a pointer to an int
in good old unmanaged C.

Here is the function in C++ (member of testclass) - this compiles to a
DLL without error in C++:

int test_function(i nt arg1, int^ arg2)
{
int result;

// arg1 is a VALUE type passed IN to the function. The
// function can use the value but not change it.

// arg2 is a REFERENCE to a value type, so the function
// should be able to use the value and change the value.

result = arg1 + *arg2;
*arg2 = 1234;

return( result );
}
I use the DLL from C# so I can call the function.
Here is what I am doing in C# (doesn't compile):

int status, val1, val2;

val1 = 11;
val2 = 12;
status = testclass.test_ function(val1, ref val2); //
Chokes on "ref arg2"

The error says - Argument '2': cannot convert from 'ref int' to
'System.ValueTy pe'.

I'm sure I'm doing something fundamentally wrong here. Can someone
here set me straight?

Thanks.

Nov 13 '06 #1
3 5429
Rich wrote:
I have functions written in C++ that I want to call from C#. I need to
be able to pass a reference to a value type (like an int) so that the
function can change the value - just like passing a pointer to an int
in good old unmanaged C.

Here is the function in C++ (member of testclass) - this compiles to a
DLL without error in C++:

int test_function(i nt arg1, int^ arg2)
{
int result;

// arg1 is a VALUE type passed IN to the function. The
// function can use the value but not change it.

// arg2 is a REFERENCE to a value type, so the function
// should be able to use the value and change the value.

result = arg1 + *arg2;
*arg2 = 1234;

return( result );
}
I use the DLL from C# so I can call the function.
Here is what I am doing in C# (doesn't compile):

int status, val1, val2;

val1 = 11;
val2 = 12;
status = testclass.test_ function(val1, ref val2); //
Chokes on "ref arg2"

The error says - Argument '2': cannot convert from 'ref int' to
'System.ValueTy pe'.

I'm sure I'm doing something fundamentally wrong here. Can someone
here set me straight?
If you want to pass this as a reference from C# to C++, the define your C++
method as follows:

int test_function(i nt arg1, int % arg2)
{
int result;

result = arg1 + arg2;
arg2 = 1234;

return (result);
}

^ tells managed C++ you want a handle to an object on the GC heap. % tells
managed C++ you want a tracking reference.
--
Tom Porterfield

Nov 13 '06 #2

Perfect - you hit the nail on the head! That solved the problem -
Thanks!

Nov 13 '06 #3
Rich wrote:
Perfect - you hit the nail on the head! That solved the problem -
Thanks!
Glad to help.
--
Tom Porterfield
Nov 13 '06 #4

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

Similar topics

9
4948
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my webserver runs that part of the script (see attached file, snippet.php), though, it doesn't go through. I don't get an error message or anything...it just returns a "1" (whereas it should return a "0") as far as I can tell. I have read the PHP...
1
10807
by: beavenour | last post by:
I am looking for some help creating a more universal function out of this lame attempt at javascript. I have little experience with javascript and want to be able to universalize this function. So how do I change this script so that I can call any field or any number of fields to be totaled? For example - calculate(myfield01, myfield02) at one total field and calculate(myfield06, myfield08, myfield11) at another total field instead of...
15
4201
by: chirs | last post by:
I am trying to understand a piece of code. In a javascrpit file, there is a function: function ItemStyle(){ var names=; addProps(this,arguments,names,true); }; In the html file, it calls the function as:
3
14927
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
2
7671
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would like mine to run immediately after it. So in the code below, what JS would i need to add to my "myfile.inc" page so that I could guarantee this behavior? <!-- main page --> <html> <head> <script type="text/javascript">
39
7633
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down indicate: a) That I don't know enough b) Passing arguments by ref is bad
10
2287
by: Robert Skidmore | last post by:
Take a look at this new JS function I made. It is really simple but very powerful. You can animate any stylesheet numeric value (top left width height have been tested), and works for both % and px values. Works in both ie and firefox. Parameters styleType = top | left | width | height toNumber = the new value of the style then you pass in as many ids as you would like.
1
1801
by: cognominal | last post by:
I am using fifrefox 1.5.0.2 and I have modified the javascript shell to deal with e4x. Below is a session. I would have expected <toto/>.nodeKind to be of type "function". Is this right? Is this conformant to the spec? Any comments?
28
4305
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
14
1544
by: Nickolay Ponomarev | last post by:
Hi, Why does http://www.jibbering.com/faq/ uses new Function constructor instead of function expressions (function(...) { ... }) when defining new functions? E.g. for LTrim and toFixed. Is the reason that you want to avoid accidental closures? Or does some obscure browser does not support function expressions? This just looks weird, given that http://www.jibbering.com/faq/#FAQ4_40 says to only use eval() (which is not really much...
0
8279
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8703
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8467
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8589
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7302
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5619
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4291
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1591
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.