First contact with C++/CLI
-
Background: I'm trying to build a managed wrapper around a C library, using C++/CLI to build a DLL that will be referenced by a C# program. This is my first look to C++/CLI, so even little things seem really hard. Now, my first problem. The C library has lots of parameters passed by pointers, e.g.:
void H5get_libversion(int * major, int * minor, int * release);
What I'd like to have is an equivalent method in C++/CLI, something that in C# would be:void GetLibVersion(ref int major, ref int minor, ref int release);
The only way I found to achieve this is doing something like:void HGlobals::GetLibVersion(unsigned int % major, unsigned int % minor, unsigned int % release)
{
unsigned int _major, _minor, _release;H5get\_libversion(&\_major, &\_minor, &\_release); major = \_major; minor = \_minor; release = \_release;
}
Do I really have to do this manual "duplication"? Isn't there a better (shorter) way? Thanks in advance
Luca The Price of Freedom is Eternal Vigilance. -- Wing Commander IV En Það Besta Sem Guð Hefur Skapað, Er Nýr Dagur. (But the best thing God has created, is a New Day.) -- Sigur Ròs - Viðrar vel til loftárása
-
Background: I'm trying to build a managed wrapper around a C library, using C++/CLI to build a DLL that will be referenced by a C# program. This is my first look to C++/CLI, so even little things seem really hard. Now, my first problem. The C library has lots of parameters passed by pointers, e.g.:
void H5get_libversion(int * major, int * minor, int * release);
What I'd like to have is an equivalent method in C++/CLI, something that in C# would be:void GetLibVersion(ref int major, ref int minor, ref int release);
The only way I found to achieve this is doing something like:void HGlobals::GetLibVersion(unsigned int % major, unsigned int % minor, unsigned int % release)
{
unsigned int _major, _minor, _release;H5get\_libversion(&\_major, &\_minor, &\_release); major = \_major; minor = \_minor; release = \_release;
}
Do I really have to do this manual "duplication"? Isn't there a better (shorter) way? Thanks in advance
Luca The Price of Freedom is Eternal Vigilance. -- Wing Commander IV En Það Besta Sem Guð Hefur Skapað, Er Nýr Dagur. (But the best thing God has created, is a New Day.) -- Sigur Ròs - Viðrar vel til loftárása
Maybe try pinning those moveable managed "pointers" (tracking references)
void NativeFunc(int *a, int *b)
{
*a = 10;
*b = 20;
}public ref class TestRefClass
{
public:
TestRefClass() {}
void TestMethod(Int32 %a, Int32 %b)
{
pin_ptr<Int32> pa = &a;
pin_ptr<Int32> pb = &b;
NativeFunc(pa, pb);
}
};int _tmain()
{
int a = 3;
int b = 4;
TestRefClass obj;
obj.TestMethod(a, b);return 0;
}Mark
Mark Salsbery Microsoft MVP - Visual C++ :java: