 |
|
|
Very good job, this is exactly what I was loocking for! While exploring UnsafeLibrary's abilites:
public Form1() { InitializeComponent();
EventHandler clickHandler = new EventHandler(Form1_Click);
this.Click += clickHandler; Pointer unsafeLibClickPtr = Unsafe.GetAddressOf(ref clickHandler); IntPtr marshaledClickPtr = Marshal.GetFunctionPointerForDelegate(clickHandler); Delegate marshaledClickDlg = Marshal.GetDelegateForFunctionPointer(marshaledClickPtr, typeof(EventHandler)); Delegate unsafeLibClickDlc = Marshal.GetDelegateForFunctionPointer(unsafeLibClickPtr.ToIntPtr(), typeof(EventHandler));
EventHandler derefUnsafeLibEventHandler = unsafeLibClickPtr.Dereference(); if (true == object.ReferenceEquals(clickHandler, derefUnsafeLibEventHandler)) { System.Diagnostics.Debug.WriteLine("Objects are equal"); } if (marshaledClickPtr != unsafeLibClickPtr.ToIntPtr()) { System.Diagnostics.Debug.WriteLine("Pointers are not equal"); }
object o = new object(); GCHandle objectHandle = GCHandle.Alloc(o); IntPtr objectPtr = GCHandle.ToIntPtr(objectHandle); Pointer<object> unsafeLibObjectPtr = Unsafe.GetAddressOf(ref o);
object derefUnsafeLibObject = unsafeLibObjectPtr.Dereference(); if (true == object.ReferenceEquals(o, derefUnsafeLibObject)) { System.Diagnostics.Debug.WriteLine("Objects are equal"); } if(objectPtr != unsafeLibObjectPtr.ToIntPtr()) { System.Diagnostics.Debug.WriteLine("Pointers are not equal"); }
GC.KeepAlive(o); objectHandle.Free(); }
void Form1_Click(object sender, EventArgs e) { // Do nothing... }
I've noticed that the Pointer<>.ToIntPtr()-method returned different values as expected (e.g. here: (objectPtr != unsafeLibObjectPtr.ToIntPtr())). Is it a bug or did I do something wrong?
However, good job
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Thanks! At last someone liked it!
That's because it returns the address to the reference, not the object. I have no idea how to fix it.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
It is an interesting concept - but I have a hard time of figuring out whether or not this is actually smart or not.
Could you perhaps extend the article / source code with some examples ?
Best regards,
/Michael
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Well, they are mostly useful for Platform Invoke methods which require pointer inputs. Although I guess the Marshaler could pass things by reference, I have a hard time figuring out whether the type will be passed by pointer or not (maybe I'm not experienced). Furthermore, some predefined types need to be passed by pointer, so making a new type is hard or pointless. Another point (although I don't know if it will really bring a performance benefit): It might be useful for arrays if you don't want array bound checking to be performed automatically.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Yes, I know it is primarely for Platform Invoke methods....
But I still could use an example or two - so I can compare it with the traditional way of calling such methods.
/Michael
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
|
would you want to do this? The single greatest benefit to .net is that it manages memory allocation and pointers in a safe manner for us. If you want to access pointers directly use C++ and enjoy the Blue Screens of Death that go along with dereferencing the null pointer and walking off the end of your array into OS land.
DancesWithBamboo
|
| Sign In·View Thread·PermaLink | 1.00/5 (2 votes) |
|
|
|
 |
|
|
 |
|
|
While that's true, there are lots of other ways to work with those functions from managed code that don't also require you to use pointers. Even if you did need to use pointers, you can always use the unsafe keyword and compiler switch.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
The title of this article : Pointer Library for All .NET Languages
In VB.NET , no pointer , no unsafe code
Hi Turion , I like your idea
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
It's certainly an interesting concept, but how is it actually useful? What are the real-world problems this is intended to solve?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
I still don't see how this is really useful. If you're dealing with unmanaged code in .NET you generally don't need to deal with pointers directly and can use the .NET data marshalling capabilities or you do want to use pointers and use the unsafe keyword and compiler switch.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Uh... Could you show me how to use VB.NET's /unsafe switch? Thanks. The point was that now ANY .NET language can use pointers.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Ok...that's a valid point. VB doesn't support unsafe code like C# does. It would also help if the article contained a bit more substance by providing some examples of how it could be used and what the real benefits gained are. One of the key concepts behind the .NET Framework is that you don't need to deal with pointers directly anymore (while not entirely true, it is true 99.9% of the time), so I still maintain that this is useful only in limited/specific situations (which should be talked about in the article as well).
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Yeah, the point was not to Always use it - just be able to use it in some cases. I'll put some examples soon.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I'm unwilling to test this class because of it's inherit lack of safety, but I do have to wonder what happens when you try to Dispose of items that you've converted. How does Garbage Collection cope with this?
Second point - remove the unnecessary sections from the top of the article.
Deja View - the feeling that you've seen this post before.
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
Pete O`Hanlon wrote: How does Garbage Collection cope with this?
My guess is that it doesn't deal with it. The class doesn't appear to implement IDisposable or even a Dispose method.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
It doesn't need to handle anything, because all it creates is a Pointer struct, nothing else. The GetAddressOf returns the address to the item you pass it - it creates nothing else. Use Reflector to look at the code in C#. (But of course the code won't recompile in C#...)
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
The .NET Framework SDK contains a tool called PeVerify.exe which you can use to verify if .NET assemblies are correct. The output of compilers should always verify (unless you run into a compiler bug), but if you use ILAsm, you should use the tool to ensure you did not produce invalid IL sequences. For your assembly, it complains: [MD]: Error: TypeDef that is not an Interface and not the Object class extends Nil token. [token:0x02000002]
When I compile a static class in C#, it "extends [mscorlib]System.Object", but your class "Unsafe" seems to derive from nothing - this is not allowed in .NET.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |