Mapping int out to managed C++ (now failing?)

G

Guest

Hello,

In the MC++ FAQ, we can find how to map a C# "out int" argument to managed
C++. The following solution used to work, in previous version of VS, but
fails with VC# and VC++ Express:

// TestOutInt.h

#pragma once

using namespace System;
using namespace System::Runtime::InteropServices;

namespace TestOutInt {

public ref class Class1
{
public:
void SomeFunc( [Out]Int32 * arg)
{
*arg = 2;
}
};
}

// C# code
using System;
using System.Collections.Generic;
using System.Text;
using TestOutInt;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int test;
Class1 c = new Class1();
c.SomeFunc(out test);
Console.WriteLine(test.ToString());
}
}
}

It splits out the error: Argument '1': cannot convert from 'out int' to 'int*'

As mentioned the equivalent code used to work in previous version.
What am I misssing here?

-daniel
 
G

Guest

Have you tried a 'managed pointer' (I'm not sure if that's the correct term...)
e.g.,
void SomeFunc( [Out]Int32 % arg)
{
arg = 2;
}

--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#
 
R

Ray

danielp said:
Hello,

In the MC++ FAQ, we can find how to map a C# "out int" argument to managed
C++. The following solution used to work, in previous version of VS, but
fails with VC# and VC++ Express:
<snip>

Hi Daniel,

Instead of this:
void SomeFunc( [Out]Int32 * arg)
{
*arg = 2;
}

you should do this:

void SomeFunc([Out] Int32% arg)
{
arg = 123456;
}

(Note, not Int32^%, because Int32 is a ValueType)

Then you'll C# code will work perfectly.

Cheers
Ray
 
G

Guest

you should do this:
void SomeFunc([Out] Int32% arg)
{
arg = 123456;
}

(Note, not Int32^%, because Int32 is a ValueType)

Then you'll C# code will work perfectly.
Thanks guys,

I guess I'll have to study the new syntax more carefully.

-daniel
 

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