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
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