473,383 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Void pointer to managed object type

Hi,

I am trying to convert some unmanaged code (C++) to managed code
(using C++/CLI).

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.

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?

2) Also, is there any equivalent of double pointers **, in .NET
handles?

Thanks in advance,
Vijay.

May 19 '07 #1
3 14457
I received an answer in another forum:

You can't cast a void* to unmanaged memory to a managed object
reference. To turn this into managed memory, you'd have to use
Marshal.Copy() or Marshal.PtrToStructure(). That will of course only
work if you know the type of the data that the void* points to.

To handle double pointers, you'd declare the member or function
argument with the MarshalAs(UnmanagedType.LPArray) attribute. Or
LPStruct if it points to a structure or LPTStr if it points to a
string. In the case of an array, you'll also have to use the
SizeConst argument to tell the marshaler how long the array is.

Hans Passant.
On May 19, 4:52 pm, "vijay.gan...@gmail.com" <vijay.gan...@gmail.com>
wrote:
Hi,

I am trying to convert some unmanaged code (C++) to managed code
(using C++/CLI).

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.

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?

2) Also, is there any equivalent of double pointers **, in .NET
handles?

Thanks in advance,
Vijay.

May 20 '07 #2
vi**********@gmail.com wrote:
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
vector<void*or CPtrList (not 100% identical, though). List<Tis in
the System::Collections::Generic namespace.
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
May 22 '07 #3

"Tamas Demjen" <td*****@yahoo.comwrote in message
news:eP**************@TK2MSFTNGP06.phx.gbl...
vi**********@gmail.com wrote:
>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.
That's if you need to store a void* in a managed type. What it sounds like
the OP needs, is to return a managed handle in an untyped way, like void*.
You'd use System::Object^ for that, every managed handle can cast to and
from Object^. There still needs to be a distinction between managed and
unmanaged though, to keep the garbage collector informed. Specifically,
storing the address of a managed object in a void* will leave you with a
dangling pointer.
>
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
vector<void*or CPtrList (not 100% identical, though). List<Tis in the
System::Collections::Generic namespace.
>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 %
Sure there is: a ref class.

generic<typename To>
ref struct Handle
{
To Target;
};

triple indirection on string: Handle<Handle<Handle<System::String^>^>^>

mmm, syntax might be wrong, might need To^ Target and leave off the ^ when
using.... I always use templates, not generics, when in C++/CLI.
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

May 22 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: bob_jenkins | last post by:
{ const void *p; (void)memset((void *)p, ' ', (size_t)10); } Should this call to memset() be legal? Memset is of type void *memset(void *, unsigned char, size_t) Also, (void *) is the...
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
5
by: kernel.lover | last post by:
hello, I want to know if a fuction say malloc is declared as void *malloc() then whats the significance of void here. Does void * is used when function has the flexibility to return any type of...
9
by: Juggernaut | last post by:
I am trying to create a p_thread pthread_create(&threads, &attr, Teste, (void *)var); where var is a char variable. But this doesnt't work, I get this message: test.c:58: warning: cast to pointer...
56
by: maadhuu | last post by:
hello, this is a piece of code ,which is giving an error. #include<stdio.h> int main() { int a =10; void *p = &a; printf("%d ", *p ); //error....why should it //be an error ?can't the...
16
by: aegis | last post by:
Given the following: int a = 10; int *p; void *p1; unsigned char *p2; p = &a;
1
by: yaron | last post by:
Hi, my unmanaged method return void* pointer to my managed c++ wrapper class. How do i convert this pointer to a managed object so that a c# client could use it ? Thanks.
21
by: ashish.sadanandan | last post by:
Hi, I haven't done a lot of C++ programming (done a lot of it in C) and the reason why I'm considering switching now is also the question I'm posting here. I have some library functions that...
49
by: elmar | last post by:
Hi Clers, If I look at my ~200000 lines of C code programmed over the past 15 years, there is one annoying thing in this smart language, which somehow reduces the 'beauty' of the source code...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.