Click here to Skip to main content
Licence 
First Posted 26 Sep 2007
Views 24,500
Downloads 57
Bookmarked 9 times

Pointer Library for All .NET Languages (written in MSIL)

By | 17 Oct 2007 | Article
This pointer library is made to be used from for any .NET language.

Introduction

This library is designed to allow users of all .NET languages to be able to use pointers without trouble, just like those using C# or C++. Of course, it will be at the user's own risk.

Using the code

I was bothered by the fact that pointers (and related operations) can only be accessed in C# and C++, and that they only work on unmanaged types. I then decided to make a library which would overcome this, because:

  1. certain P/Invoke methods need it,
  2. because languages other than C++ and C# do not support pointers, and
  3. because the pointer structure is now strongly typed.

Just beware that using the Marshal class may be more accurate than this library, because the methods are for managed types (before marshaling). I noticed this only after I made the library. But, hey, if it works, use it!

Points of interest

The method ManagedSizeOf<T>() returns the same thing as saying sizeof(T) in C#, only that it is not restricted to unmanaged types - if T is a reference type, it returns the size of the reference (which on x86 computers seems to be 4 bytes). If T is a value type, it returns the size of that type, whether or not it is managed. The reason that I put the word Managed in front of it was to indicate that it returns the size of the type before marshaling, unlike Marshal.SizeOf().

The ManagedSizeOf(System.Type type) method uses Reflection to call the other ManagedSizeOf method, while substituting T with the type. Therefore, it is inherently slower than the previous method and should be used with care.

The GetAddressOf<T>(ref T obj) method returns the address of that variable, whether or not it is a reference type.

The Pointer<T> structure encapsulates a variable of type T* which can be indexed like normal pointers (like ptr[9]), as well as converted to System.IntPtr and other formats. If the language supports pointers (like in C#), then the pointer field can be directly used. Otherwise, indexing and other properties must be used, or it can be converted to a System.IntPtr.

Here is a bug I found, but do not know how to fix (any suggestions are appreciated): on one computer (on which I was not an Administrator), a test program was run. However, a VerificationException occurred before entering Main, because the runtime could not verify the code in UnsafeLibrary.dll. I only saw this error once.

This program may have bugs.. don't rely on it being correct! Tell me if you find any. I am still not sure whether the AddressOf method works correctly all the time, because I do not know whether it returns the address of the ref parameter or a copy of it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Turion



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionSourceCode PinmemberDanielGreen23:26 13 Dec '11  
GeneralThanks PinmemberTheraot7:08 20 Feb '09  
GeneralRe: Thanks PinmemberTheraot14:56 24 Feb '09  
GeneralGood job Pinmembersilverair2:34 16 Oct '07  
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 Smile | :)
GeneralRe: Good job PinmemberTurion6:54 17 Oct '07  
GeneralInteresting, but..... PinmemberMichael B. Hansen22:57 26 Sep '07  
GeneralRe: Interesting, but..... PinmemberTurion9:16 27 Sep '07  
GeneralRe: Interesting, but..... PinmemberMichael B. Hansen22:43 27 Sep '07  
AnswerRe: Interesting, but..... PinmemberTurion5:33 28 Sep '07  
GeneralKudos Pinmemberrcardare18:46 26 Sep '07  
GeneralWhy on earth PinmemberDancesWithBamboo15:46 26 Sep '07  
GeneralRe: Why on earth PinmemberTurion15:48 26 Sep '07  
GeneralRe: Why on earth PinmemberScott Dorman15:55 26 Sep '07  
GeneralRe: Why on earth PinmemberKy Nam19:13 26 Sep '07  
QuestionHow is this useful? PinmemberScott Dorman10:16 26 Sep '07  
AnswerRe: How is this useful? PinmemberTurion15:50 26 Sep '07  
GeneralRe: How is this useful? PinmemberScott Dorman15:53 26 Sep '07  
AnswerRe: How is this useful? PinmemberTurion9:13 27 Sep '07  
GeneralRe: How is this useful? PinmemberScott Dorman9:34 27 Sep '07  
GeneralRe: How is this useful? PinmemberTurion5:34 28 Sep '07  
GeneralDispose PinmemberPete O'Hanlon10:00 26 Sep '07  
GeneralRe: Dispose PinmemberScott Dorman10:15 26 Sep '07  
GeneralRe: Dispose PinmemberTurion15:52 26 Sep '07  
GeneralPeVerify PinmemberDaniel Grunwald9:49 26 Sep '07  
GeneralRe: PeVerify PinmemberTurion15:49 26 Sep '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 17 Oct 2007
Article Copyright 2007 by Turion
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid