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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pravin

    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



  • Peter Q

    #2
    Re: Pass parameters by ref from c# to managed c++

    Your C++ code requires a pointer argument, 'ref' aren't exactly the same a
    pointer.

    You should try this:

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

    of course, turn on the unsafe option in ur compiler options


    "Pravin" <expertco@vsnl. com> wrote in message
    news:e8UVUtfgDH A.3968@tk2msftn gp13.phx.gbl...[color=blue]
    > 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[/color]
    objects[color=blue]
    > 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
    >
    >
    >[/color]


    Comment

    • Willy Denoyette [MVP]

      #3
      Re: Pass parameters by ref from c# to managed c++

      Should be:
      MC++:

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

      C# :
      foo(ref i);

      Willy.


      "Pravin" <expertco@vsnl. com> wrote in message news:e8UVUtfgDH A.3968@tk2msftn gp13.phx.gbl...[color=blue]
      > 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
      >
      >
      >[/color]


      Comment

      • Willy Denoyette [MVP]

        #4
        Re: Pass parameters by ref from c# to managed c++

        Should be:
        MC++:

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

        C# :
        foo(ref i);

        Willy.


        "Pravin" <expertco@vsnl. com> wrote in message news:e8UVUtfgDH A.3968@tk2msftn gp13.phx.gbl...[color=blue]
        > 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
        >
        >
        >[/color]


        Comment

        Working...