usage of System.IntPtr

Posted on 2008-10-08
Last Modified: 2013-11-05

where would you use the System.IntPtr class?
The documentation writes "A platform-specific type that is used to represent a pointer or a handle."

Question by:xRalf
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 15

Expert Comment

ID: 22667764
Its a method of referring between different development languages, and for handles such as FileStream (it will hold the file handles).

Author Comment

ID: 22667902
Try to send some minimalistic code snippet that will do something useful...
LVL 15

Expert Comment

ID: 22669311
Snippet for print window on a picturebox.

private System.Windows.Forms.PictureBox pp;
pp = new PictureBox();
System.IntPtr hwnd = pp.Handle;
Bitmap bm = new Bitmap (pp.Size.Width, pp.Size.Height);
if (bm != null)
using (Graphics g = Graphics.FromImage (bm))
System.IntPtr bmDC = g.GetHdc ();
bool ok =true;
ok = PrintWindow (hwnd, bmDC,0);
g.ReleaseHdc (bmDC);
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.


Author Comment

ID: 22669547

I tried to compile the snippet, but it doesn't compile because function PrintWindow doesn't exist. I'd like to compile it to see why IntPtr is useful and what is it good for.
LVL 15

Assisted Solution

angus_young_acdc earned 100 total points
ID: 22669826
Here is an example to play a wave file from MS:
LVL 55

Accepted Solution

Jaime Olivares earned 150 total points
ID: 22669887
IntPtr represents a pointer, that obviuosly point to a non-managed resource (since managed environment don't use pointers but references). Also can represent an arbitrary 32-bit value like a resource handle (hWnd, hProcess, etc.)
So, IntPtr can be used when invoking a non-managed function like Platform (WinAPI) invoking.
Some related articles with real examples:

Featured Post

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question