Reputation: 67
This is so basic it should be easy to find. In my searches all I get is more complex solutions. Converting strings, marshaling, pinning objects. How do you simply convert from a c++/CLI int^ pointer to a native int* in C++/CLI.
The body of my function is
void Open(int ^Hndl)
{
void Unmanaged_Open(Hndl); // How do you pass the pointer to this
}
where void Unmanaged_Open(int *handle);
Upvotes: 2
Views: 2133
Reputation: 283733
Here is how you implement an output parameter in C++/CLI, like C#'s void func(out int x)
. Note that there is no int^
.
void Open([OutAttribute] int% retval)
{
int result;
if (!UnmanagedOpen(&result))
throw gcnew Exception("Open failed!");
retval = result;
}
Note that it is probably even better to simply return the value. Out parameters most appear in native functions when the return value is used for error checking. You can either use exceptions in .NET for error-checking, like so:
int Open()
{
int result;
if (!UnmanagedOpen(&result))
throw gcnew Exception("Open failed!");
return result;
}
or if failure is expected (untrusted input, for example), implement the TryXYZ pattern (described on MSDN):
bool TryOpen([OutAttribute] int% retval)
{
retval = 0;
int result;
if (!UnmanagedOpen(&result)) return false;
retval = result;
return true;
}
Upvotes: 3