5,702,067 members and growing! (16,293 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » General     Advanced

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

By Turion

This pointer library is made so that any .NET language could use
MSIL, VC8.0, C# 2.0, C# 3.0, VB 8.0, VB 9.0, C++, C#, VB, Windows, .NET, .NET 3.0, .NET 2.0VS2005, VS2008, Visual Studio, Dev

Posted: 26 Sep 2007
Updated: 17 Oct 2007
Views: 9,134
Bookmarked: 6 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
21 votes for this Article.
Popularity: 1.99 Rating: 1.51 out of 5
15 votes, 71.4%
1
2 votes, 9.5%
2
1 vote, 4.8%
3
2 votes, 9.5%
4
1 vote, 4.8%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

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 (a) certain P/Invoke methods need it, (b) because languages other than C++ and C# do not support pointers, and (c) 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 called put the work Managed in front of it was to indicated 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, the 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 occured 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



Location: United States United States

Other popular .NET Framework articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 22 of 22 (Total in Forum: 22) (Refresh)FirstPrevNext
GeneralGood jobmembersilverair3:34 16 Oct '07  
GeneralRe: Good jobmemberTurion7:54 17 Oct '07  
GeneralInteresting, but.....memberMichael B. Hansen23:57 26 Sep '07  
GeneralRe: Interesting, but.....memberTurion10:16 27 Sep '07  
GeneralRe: Interesting, but.....memberMichael B. Hansen23:43 27 Sep '07  
AnswerRe: Interesting, but.....memberTurion6:33 28 Sep '07  
GeneralKudosmemberrcardare19:46 26 Sep '07  
GeneralWhy on earthmemberDancesWithBamboo16:46 26 Sep '07  
GeneralRe: Why on earthmemberTurion16:48 26 Sep '07  
GeneralRe: Why on earthmemberScott Dorman16:55 26 Sep '07  
GeneralRe: Why on earthmemberKy Nam20:13 26 Sep '07  
GeneralHow is this useful?memberScott Dorman11:16 26 Sep '07  
GeneralRe: How is this useful?memberTurion16:50 26 Sep '07  
GeneralRe: How is this useful?memberScott Dorman16:53 26 Sep '07  
AnswerRe: How is this useful?memberTurion10:13 27 Sep '07  
GeneralRe: How is this useful?memberScott Dorman10:34 27 Sep '07  
GeneralRe: How is this useful?memberTurion6:34 28 Sep '07  
GeneralDisposememberPete O'Hanlon11:00 26 Sep '07  
GeneralRe: DisposememberScott Dorman11:15 26 Sep '07  
GeneralRe: DisposememberTurion16:52 26 Sep '07  
GeneralPeVerifymemberDaniel Grunwald10:49 26 Sep '07  
GeneralRe: PeVerifymemberTurion16:49 26 Sep '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 17 Oct 2007
Editor:
Copyright 2007 by Turion
Everything else Copyright © CodeProject, 1999-2008
Web11 | Advertise on the Code Project