Sourcecode and Code Snippets


3 minute read  • 

Presented here are a collection of sourcecode snippets and mini-projects. All downloads are plain Win32 C and C++, and compile under Microsoft Visual Studio 6.0.

Code Snippets

A collection of C and C++ programming snippets organised into categories of functionality.

C/C++ Programming Snippets

Win32 Sourcecode projects

Sourcecode downloads which implement full applications or utilities. First published around 2001-2005.

Global Titlebar HookInsert buttons into window titlebars on a global level (system-wide). Handles XP Themes if available. Requires NT/2000/XP.
WinCap3Capture hidden and obscured windows - proof of concept demo application. Windows XP only - will not work under Win95/98/ME. Might work under NT,2000 but untested. Click here and here for old versions.
ReportViewForce Report-style view in all Open and Save common dialogs.
Directory TreeDisplay a directory hierarchy using ASCII graphics (ala the “tree” DOS command).
Process TreeDisplay a process hierarchy like SysInternal’s ProcessExplorer.
SetResControl multiple display resolutions and colour depths with this useful tool.

Win32 Sourcecode snippets

A collection of useful functions which I’ve accumulated over the years.

AttachDesktopAttach to a specified desktop
RegHelperRegistry utilities which mimic the GetPrivateProfile_xxx APIs
EnableDebugEnable SE_DEBUG_NAME and other system privileges (useful for opening system processes)
C++ Socket LibraryC++ class wrappers around the winsock library - makes socket programming much easier!
Trace macro“printf” style wrapper around the OutputDebugString API
DragFileFilename Drag+Drop the Windows 3.1 way
Alpha-blended windowsSimple functions to provide alpha-blended window support.
StylesHelper functions to set window and dialog item styles, using SetWindowLong.
ForceSetForegroundWindowSetForegroundWindow replacement (that works!)
EnableDialogThemeEnable XP themes on dialog boxes
GetVersionStringHowto retrieve version strings (e.g. File description) from an executable’s version resources.

Windows NT sourcecode

The following sourcecode snippets are specific to the Windows NT family of operating systems - Windows NT, 2000, XP and 2003 Server. Many of the downloads are heavily influenced by USENET threads, in particular postings by Gary Nebbett.

SetConsolePaletteModify the Win32 Console’s palette dynamically at runtime.
CreateTokenCreate an access token for local user accounts without knowing the password - requires SeCreateTokenPrivilege level. You also require a custom ntdll.h if you don’t already have one. Gary Nebbet original author.
QuerySystemInfoWrapper around the ZwQuerySystemInformation native system service.
UsernameToSidConvert a username to SID
KMemRead kernel memory from usermode (kmemcpy function). Gary Nebbet original author.
InjectThreadCreateRemoteThread replacement, can inject into an already-running thread. View the example usage.
BreakPointHardware breakpoint and single-step debugging routines.

Windows NT Device Driver snippets

Windows NT kernel-mode snippets.

IsrSpinlockHowto acquire a spinlock at any IRQL level - implemented as a custom spinlock
InterlockedExecuteAcquire exclusive access to all CPUs and execute a single function with full system lock. Please send feedback on this technique if you find it useful!