# IntPtr

***

**1. Initialize an IntPtr from an Int32**

```csharp
IntPtr ptr = new IntPtr(123);
```

**2. Initialize an IntPtr from a Pointer**

```csharp
int* p = &value;
IntPtr ptr = new IntPtr(p);
```

**3. Initialize an IntPtr from an IntPtr**

```csharp
IntPtr ptr1 = new IntPtr(123);
IntPtr ptr2 = new IntPtr(ptr1);
```

**4. Add an Int32 to an IntPtr**

```csharp
IntPtr ptr = new IntPtr(123);
ptr += 10; // ptr now points to 133
```

**5. Add an IntPtr to an IntPtr**

```csharp
IntPtr ptr1 = new IntPtr(123);
IntPtr ptr2 = new IntPtr(10);
ptr1 += ptr2; // ptr1 now points to 133
```

**6. Subtract an Int32 from an IntPtr**

```csharp
IntPtr ptr = new IntPtr(123);
ptr -= 10; // ptr now points to 113
```

**7. Subtract an IntPtr from an IntPtr**

```csharp
IntPtr ptr1 = new IntPtr(123);
IntPtr ptr2 = new IntPtr(10);
ptr1 -= ptr2; // ptr1 now points to 113
```

**8. Compare two IntPtrs**

```csharp
IntPtr ptr1 = new IntPtr(123);
IntPtr ptr2 = new IntPtr(123);
bool areEqual = ptr1 == ptr2; // true
```

**9. Dereference an IntPtr**

```csharp
IntPtr ptr = new IntPtr(123);
int value = *(int*)ptr; // value is now 123
```

**10. Use an IntPtr as an argument to a method**

```csharp
public void DoSomething(IntPtr ptr)
{
    // Do something with the pointer
}
```

**11. Return an IntPtr from a method**

```csharp
public IntPtr GetSomething()
{
    // Return a pointer to something
}
```

**12. Pass an IntPtr to a COM method**

```csharp
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
```

**13. Pass an IntPtr to a Win32 API function**

```csharp
[DllImport("kernel32.dll")]
public static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);
```

**14. Use an IntPtr to access a memory mapped file**

```csharp
public static IntPtr MapViewOfFile(IntPtr hFileMappingObject, uint dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, uint dwNumberOfBytesToMap);
```

**15. Use an IntPtr to access a device**

```csharp
public static IntPtr CreateFile(string lpFileName, uint dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);
```

**16. Use an IntPtr to access a socket**

```csharp
public static IntPtr socket(int af, int type, int protocol);
```

**17. Use an IntPtr to access a thread**

```csharp
public static IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);
```

**18. Use an IntPtr to access a window**

```csharp
public static IntPtr CreateWindowEx(uint dwExStyle, string lpClassName, string lpWindowName, uint dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam);
```

**19. Use an IntPtr to access a graphical device interface (GDI)**

```csharp
public static IntPtr CreateCompatibleDC(IntPtr hdc);
```

**20. Use an IntPtr to access a font**

```csharp
public static IntPtr CreateFont(int nHeight, int nWidth, int nEscapement, int nOrientation, int fnWeight, bool fItalic, bool fUnderline, bool fStrikeOut, int fnCharSet, int fnOutPrecision, int fnClipPrecision, int fnQuality, int fnPitchAndFamily, string lpszFace);
```

**21. Use an IntPtr to access a bitmap**

```csharp
public static IntPtr CreateBitmap(int nWidth, int nHeight, uint cPlanes, uint cBitsPerPixel, IntPtr lpvBits);
```

**22. Use an IntPtr to access a brush**

```csharp
public static IntPtr CreateSolidBrush(uint crColor);
```

**23. Use an IntPtr to access a palette**

```csharp
public static IntPtr CreatePalette(IntPtr lplgpl);
```

**24. Use an IntPtr to access a pen**

```csharp
public static IntPtr CreatePen(int fnPenStyle, int nWidth, uint crColor);
```

**25. Use an IntPtr to access a region**

```csharp
public static IntPtr CreateRectRgn(int nLeft, int nTop, int nRight, int nBottom);
```

**26. Use an IntPtr to access a DC brush**

```csharp
public static IntPtr SelectObject(IntPtr hdc, IntPtr hgbm);
```

**27. Use an IntPtr to access a DC font**

```csharp
public static IntPtr SelectObject(IntPtr hdc, IntPtr hfont);
```

**28. Use an IntPtr to access a DC palette**

```csharp
public static IntPtr SelectPalette(IntPtr hdc, IntPtr hpal, bool bForceBackground);
```

**29. Use an IntPtr to access a DC pen**

```csharp
public static IntPtr SelectObject(IntPtr hdc, IntPtr hpen);
```

**30. Use an IntPtr to access a DC region**

```csharp
public static IntPtr SelectObject(IntPtr hdc, IntPtr hrgn);
```

**31. Use an IntPtr to access a message**

```csharp
public static IntPtr GetMessage(IntPtr hWnd, IntPtr lpMsg, uint wMsgFilterMin, uint wMsgFilterMax);
```

**32. Use an IntPtr to access a paint struct**

```csharp
public static IntPtr BeginPaint(IntPtr hWnd, ref PAINTSTRUCT lpPaint);
```

**33. Use an IntPtr to access a window class**

```csharp
public static IntPtr GetClassLong(IntPtr hWnd, int nIndex);
```

**34. Use an IntPtr to access a window rect**

```csharp
public static IntPtr GetWindowRect(IntPtr hWnd);
```

**35. Use an IntPtr to access a client rect**

```csharp
public static IntPtr GetClientRect(IntPtr hWnd);
```

**36. Use an IntPtr to access a screen DC**

```csharp
public static IntPtr GetDC(IntPtr hWnd);
```

**37. Use an IntPtr to access a printer DC**

```csharp
public static IntPtr GetPrinterDC();
```

**38. Use an IntPtr to access a device context**

```csharp
public static IntPtr CreateDC(string lpszDriver, string lpszDevice, string lpszOutput, IntPtr lpInitData);
```

**39. Use an IntPtr to access a metafile DC**

```csharp
public static IntPtr CreateMetaFile(string lpszFilename);
```

**40. Use an IntPtr to access a bitmap DC**

```csharp
public static IntPtr CreateCompatibleDC(IntPtr hdc);
```

**41. Use an IntPtr to access a memory DC**

```csharp
public static IntPtr CreateCompatibleDC(IntPtr hdc);
```

**42. Use an IntPtr to access a printer DC context**

```csharp
public static IntPtr CreatePrinterDC(string lpszDriver, string lpszDevice, string lpszOutput, IntPtr lpInitData);
```

**43. Use an IntPtr to access a metafile playback DC**

```csharp
public static IntPtr CreateMetaFileDC(string lpszFilename);
```

**44. Use an IntPtr to access a clipboard**

```csharp
public static IntPtr OpenClipboard(IntPtr hWndNewOwner);
```

**45. Use an IntPtr to access a clipboard data object**

```csharp
public static IntPtr GetClipboardData(uint uFormat);
```

**46. Use an IntPtr to access a window**

```csharp
public static IntPtr GetForegroundWindow();
```

**47. Use an IntPtr to access a message queue**

```csharp
public static IntPtr GetMessageQueue();
```

**48. Use an IntPtr to access a thread**

```csharp
public static IntPtr GetCurrentThread();
```

**49. Use an IntPtr to access a process**

```csharp
public static IntPtr GetCurrentProcess();
```

**50. Use an IntPtr to access a module**

```csharp
public static IntPtr GetModuleHandle(string lpModuleName);
```
