1) One of the functions used returns a void* which I need to cast into
a handle of a managed object. Can somebody please tell me how.
Typically you can solve that problem with the IntPtr type. Of course C#
won't be able to interpret the meaning of IntPtr, but it can store its
value, and when you pass it back to a C++ function, you can get the
native pointer out of it. This is how handles (GDI or file handles) are
implemented in .NET -- they are simply stored as IntPtr members.
Note that the value of an IntPtr is not managed, it just contains a
navite pointer address. The .NET framework doesn't know what to do with
that address, and doesn't keep track of native objects where those
IntPtrs point to. IntPtr is just as unsafe as a void*, because it
contains no type information, and it may point to a dead object.
Actually, the function that returns a void* is a part of a MFC class -
CPtrList, used in my unmanaged code. I was searching of its equivalent
in .NET, but couldn't find it. Is there any equivalent?
Perhaps you could use List<IntPtr>, which is roughly analogous to
2) Also, is there any equivalent of double pointers **, in .NET
handles?
The short answer is no, but depending on what you are doing, there are
alternative solutions to that problem.
There's no such type as a handle to a handle, and you can't call the %
operator to obtain a handle to a handle:
MyRefClass^ c = gcnew MyRefClass;
MyRefClass^^ p = %c; // error!!!
You can, however, generate a tracking reference to a handle. Here are
two examples:
bool Allocate(Object^% obj)
{
obj = gcnew String("hello");
return true;
}
void Swap(Object^% one, Object^% two)
{
Object^ temp = one;
one = two;
two = temp;
}
In a similar way, you can get a tracking pointer to a handle too:
ref class C
{
};
void TestTrackingPointer()
{
C^ c = gcnew C;
interior_ptr<C^> p = &c;
}
There are restrictions regarding tracking references and pointers. For
example, they can't be declared as class members. They must be either
function arguments or automatic (local) variables. When the garbage
collector moves an object around in the memory, it updates all the
tracking references and pointers to it. Tracking pointers can't be used
in verifiable assemblies, because they can be freely incremented, and
therefore they can point outside of the object's memory bounds. On the
other hand, they're very fast. Unlike array indexing, interior_ptr is
not bounds checked, and it is excellent for fast image processing routines.
And finally, if you want to pass the address of a managed object to an
unmanaged function, you can pin it using the pin_ptr keyword. While an
object is pinned, the garbage collection won't move it around in the
memory. That's yet another way of obtaining a pointer to managed memory.
Tom