473,654 Members | 3,042 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14490
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.PtrToSt ructure(). 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(Unman agedType.LPArra y) 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...@g mail.com" <vijay.gan...@g mail.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**********@gm ail.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::Collect ions::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 TestTrackingPoi nter()
{
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******** ******@TK2MSFTN GP06.phx.gbl...
vi**********@gm ail.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::Collect ions::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<typenam e To>
ref struct Handle
{
To Target;
};

triple indirection on string: Handle<Handle<H andle<System::S tring^>^>^>

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 TestTrackingPoi nter()
{
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
8222
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 generic pointer type. My real question is, is (void *) such a generic pointer type that it
188
17316
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
5
2826
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 value by casting that functions with appropriate data type?
9
5288
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 from integer of different size. Now I thought that when it was a void I could pass anything? Thing is it works when I use an int, but in this case I wanted to use a char. It wouldnt be hard to work around it, but it annoys me because I've heard...
56
3316
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 compiler make out because //of %d that the pointer is supposed to refer to an integer ?? or is explicit type casting required ??
16
2502
by: aegis | last post by:
Given the following: int a = 10; int *p; void *p1; unsigned char *p2; p = &a;
1
2410
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
2312
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 return to me a void* to data that has been specifed by the user. There are also functions that return an enum which contains information about the data type of this data. So what I've done in the past is made a switch case statement based on the...
49
2775
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 ;-): char *cp; void *vp; void **vpp;
0
8375
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8290
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8815
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8707
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8593
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7306
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.