Click to See Complete Forum and Search --> : is there something in managed C++ that can be called with "out" in C#?


petru66
September 10th, 2002, 03:14 AM
I have to write a managed C++ class library that wraps some C++ code. Some of the methods of this class must have parameters sent by reference (or pointers, for that matter: the point is, I need methods which modify their parameters).

This dll will be used in a C# project, which has a reference to it. Intuitively, the arguments of the mentioned methods' calls must have attribute "out". However, I was not able to find the proper combination of attributes in managed C++ and C#.

I will post here the code - it is very short, and I hope clear enough.

1. I created a new managed C++ class library project, called TestOutParam; in the header file I put:

#pragma once
using namespace System;
using namespace System::Runtime::InteropServices;
namespace TestOutParam
{
public __gc class Class1
{
public:
Class1(){}
int simpleMethod(int anInt,
[System::Runtime::InteropServices::OutAttribute] int* outInt)
{
*outInt = -1;
return anInt-10;
}
};
}

Note the attribute of the second parameter of simpleMethod - it does not help, though. Also, the int* can be changed with int&
(and the first line of code to "outInt = -1;").

2. I created a C# project, as a console application, called CSOutParam. The project has only one class, and a reference to project TestOutParam. The code of the class is:

using System;
using System.Runtime.InteropServices;
using TestOutParam;
namespace CSOutParam
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
int a = 1;
int b;
b = (new TestOutParam.Class1()).simpleMethod(10, out a);
System.Console.WriteLine("{0}, {0}, {0}\n", 10, b, a);
}
}
}

The managed C++ project compiled without any problem.
The C# project produced two errors:
---
error CS1502: The best overloaded method match for 'TestOutParam.Class1.simpleMethod(int, int*)' has some invalid arguments
error CS1503: Argument '2': cannot convert from 'ref int' to 'int*'
---

The same error occur if int& is used instead of int*.

Does anyone have an idea what is going on here?

Regards,
Petru

petru66
September 11th, 2002, 11:25 AM
To whoever browses this thread: I found the answer quite fast,
in one of Google forums. Please see:

answer found in Google (http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&threadm=10987e1f.0206260704.4fc18a48%40posting.google.com&rnum=1&prev=/groups%3Fq%3Dout%2Bparameter%2Breference%2BManaged%2BC%252B%252B%2BC%2523%26hl%3Den%26lr%3D%26ie%3DUTF-8%26selm%3D10987e1f.0206260704.4fc18a48%2540posting.google.com%26rnum%3D1)

Petru: