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
  • 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);
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.


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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now