473,320 Members | 1,853 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,320 software developers and data experts.

reference to handle / handle to reference

i'm looking for a possibility in C# to get a unique Int32 handle for an
object reference. This handle must later (within the same process of course)
be convertible back to an object reference (call it handle or pointer or
whatever...).
at the moment i'm working with a Hashtable Int32 to Object, but i want to
optimize this. I know it is possible to work with pointers in unsafe code
blocks, but as far as i know these pointer remain valid only within these
blocks, or when they are pinned. and i don't want to disturb the garbage
collector.
i mean, somehow a reference must be a number anyway, perhaps an index within
some garbage collector table, or am i wrong?

Jul 21 '05 #1
3 2055
As far as I know, there's no conversion between an object reference and an Int32, unless you pin objects using GCHandles and get their associated IntPtrs.

Also, if you're storing references in a Hashtable, then you are affecting the garbage collector by holding references to the objects, so they'll never get collected.

Even if references are stored internally as numbers in a GC table, there's no way for managed code to access it.

Hope that helps
-Chris

--------------------

i'm looking for a possibility in C# to get a unique Int32 handle for an
object reference. This handle must later (within the same process of course)
be convertible back to an object reference (call it handle or pointer or
whatever...).
at the moment i'm working with a Hashtable Int32 to Object, but i want to
optimize this. I know it is possible to work with pointers in unsafe code
blocks, but as far as i know these pointer remain valid only within these
blocks, or when they are pinned. and i don't want to disturb the garbage
collector.
i mean, somehow a reference must be a number anyway, perhaps an index within
some garbage collector table, or am i wrong?

--

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.

Jul 21 '05 #2
>unless you pin objects using GCHandles and get their associated IntPtrs.

You can cast a GCHandle to an IntPtr or void* (and back) even if it's
non-pinning.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jul 21 '05 #3
True, but then you're getting an IntPtr representation of the GCHandle, not the underlying object. Using GCHandle.AddrOfPinnedObject will give an IntPtr of the actual object.
--------------------
unless you pin objects using GCHandles and get their associated IntPtrs.


You can cast a GCHandle to an IntPtr or void* (and back) even if it's
non-pinning.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

--

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.

Jul 21 '05 #4

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

Similar topics

1
by: terminator800tlgm | last post by:
I don't understand the difference between ojbect handle and reference. For example, class Stack { private: CD cd_obj; // cd object DVD & dvd_ref;// dvd reference Why is it that dvd_ref can...
10
by: Tony Johansson | last post by:
Hello Experts!! This class template and main works perfectly fine. I have this class template called Handle that has a pointer declared as T* body; As you can see I have a reference counter in...
2
by: Steve Jorgensen | last post by:
When writing VB or VBA code that works with databases or other external libraries that cannot be trusted to automatically do the right thing when references to their objects are arbitrarily...
13
by: Bonj | last post by:
Specifically, is there any difference between usin void MyFunction(const HANDLE myhandle an void MyFunction(const HANDLE& myhandleref ?? is the latter likely to cause problems (such as I...
4
by: Don Kim | last post by:
I have the following code: using namespace System; ref class R { public: R() {
2
by: Giovanni Bassi | last post by:
Hello All, I have encountered a problem. I am using visual inheritance and my base form adds an event handler on Form Load using the AddHandler Keyword. The problem is that if the Event...
4
by: John Lafrowda | last post by:
Hi all, I'm trying to handle two projects in one solution (.net 2002, both projects are VB). The first project contains only some public constant definitions (for the moment) and compiles into...
3
by: Christian Lammel | last post by:
i'm looking for a possibility in C# to get a unique Int32 handle for an object reference. This handle must later (within the same process of course) be convertible back to an object reference (call...
1
by: PeterAPIIT | last post by:
What is template template arguments and template typename arguments ? The reason i write in policy based design is because this is the requirement of the assignment. My code so far: ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
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...

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.