Win32

Balloon Tooltips

Balloon Tooltips

In the Windows XP login screen, the password text box will warn you with a balloon tooltip if you accidentally turn Caps Lock on. The balloon tooltip appears to be a Windows tooltip common control with the TTS_BALLOON style. To replicate this functionality, I decided to write a function called ShowMsgBalloon() which, given a control and the various balloon tooltip parameters, creates and shows the balloon tooltip below the control.
STL Objects and Win32 Module Boundaries

STL Objects and Win32 Module Boundaries

Let’s say you have the following function: void AppendChar(std::string& s, char ch) { s += ch; } What happens if this function is exported as an ordinal function from a DLL (not an inlined piece of code inside a header) and you call it from an EXE? It works most of the time. When it doesn’t, it corrupts your heap and causes a spectacular mess. In Windows you must free memory with the same allocator that allocated it.
Custom-Drawn Win32 Tooltips

Custom-Drawn Win32 Tooltips

Like many common controls, the tooltip control supports custom drawing for maximum flexibility. This is a quick tutorial on how to use the tooltip custom draw facility. First, start with the following scratch program (which is a slightly modified version of Raymond Chen’s scratch program): #define STRICT #include <windows.h>#include <windowsx.h>#include <commctrl.h>#include <tchar.h> #define WND_CLASS_NAME TEXT("Scratch") HINSTANCE g_hinst; BOOL OnCreate(HWND hwnd, LPCREATESTRUCT lpcs) { return TRUE; } void OnDestroy(HWND hwnd) { PostQuitMessage(0); } LRESULT CALLBACK WndProc(HWND hwnd, UINT uiMsg, WPARAM wParam, LPARAM lParam) { switch (uiMsg) { HANDLE_MSG(hwnd, WM_CREATE, OnCreate); HANDLE_MSG(hwnd, WM_DESTROY, OnDestroy); } return DefWindowProc(hwnd, uiMsg, wParam, lParam); } BOOL RegisterWindowClass() { WNDCLASS wc; ATOM atom; wc.
Win32: Getting LOGFONT from HFONT

Win32: Getting LOGFONT from HFONT

To convert a HFONT to a LOGFONT, use the GDI function GetObject(), as in: LOGFONT lf; int ret = GetObject(hfont, sizeof(lf), &lf); // Be sure to check the return value of GetObject The code is trivial but the function took me forever to find.
Vista Does Not Virtualize Creation Of Shell Links

Vista Does Not Virtualize Creation Of Shell Links

Windows Vista developers beware: Vista does not perform file virtualization on the creation of shell links. Consider the following code: // Creates a shell link (a.k.a. shortcut) located at swzLinkFile that points to // szTargetFile with a description of szDescription. BOOL CreateLink(LPCTSTR szTargetFile, LPCTSTR szDescription, LPCOLESTR swzLinkFile) { BOOL bRet = FALSE; IShellLink* psl; HRESULT hr = ::CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**) &psl); if (SUCCEEDED(hr)) { IPersistFile* ppf; hr = psl->QueryInterface(IID_IPersistFile, (void**) &ppf); if (SUCCEEDED(hr)) { hr = psl->SetPath(szTargetFile); if (SUCCEEDED(hr)) { hr = psl->SetDescription(szDescription); if (SUCCEEDED(hr)) { hr = ppf->Save(swzLinkFile, TRUE); if (SUCCEEDED(hr)) { bSuccess = TRUE; } } } ppf->Release(); } psl->Release(); } return bSuccess; } // NOTE: Hardcoding C:\WINDOWS and C:\Program Files is a bad practice.
Debugging Crashes in Windows Applications: The Null Pointer Dereference

Debugging Crashes in Windows Applications: The Null Pointer Dereference

Windows C++ developers remain all too familiar with the standard Windows crash dialog. This post is an attempt to teach developers how to understand the data the crash dialog reports to diagnose difficult issues. A basic understanding of assembly language is assumed; for more background on these topics please read Matt Pietrek’s “Under The Hood” articles in the Microsoft Systems Journal February 1998 and June 1998 issues. To begin with, let’s write an application that crashes with a null pointer dereference:
Delta Compression Application Programming Interface

Delta Compression Application Programming Interface

Apparently Microsoft has an API for just about everything. Today I read about Microsoft’s Delta Compression Application Programming Interface, an API for creating and applying binary diffs. This API looks ideal for building an application’s incremental online update facility.
Win32 Shell Lightweight Utility API (shlwapi.dll)

Win32 Shell Lightweight Utility API (shlwapi.dll)

The Win32 shell lightweight utility API (shlwapi.dll) is a cornucopia of useful functions. It appears to be Microsoft’s “dumping ground” for functions without a better home. (I believe Microsoft internal DLL ownership also played a part.) Had I known about shlwapi years ago, it would have saved me considerable programming effort. Particularly useful are SHCreateStreamOnFile, the path family of functions (e.g. PathCombine), and the registry family of functions (e.g. SHRegGetPath).
Use RAII

Use RAII

This is covered by any halfway-decent C++ book, but I believe it deserves reiteration: Use the RAII idiom. I don’t think I could explain RAII any better than HackCraft does in The RAII Programming Idiom. Let me demonstrate how to use RAII with a semi-contrived example. Here’s the pre-RAII code: HMODULE hm = LoadLibrary(_T("user32.dll")); if (hm != NULL) { FARPROC proc = GetProcAddress(hm, "MessageBoxW"); if (proc != NULL) { typedef int (WINAPI *FnMessageBoxW)(HWND, LPCWSTR, LPCWSTR, UINT);FnMessageBoxW fnMessageBoxW = (FnMessageBoxW) proc; fnMessageBoxW(NULL, L"Hello World!
Beware File Open/Save Dialogs: They May Change The Current Directory

Beware File Open/Save Dialogs: They May Change The Current Directory

On some Windows operating systems (primarily Windows 95, 98, and ME), GetOpenFileName() and GetSaveFileName() (and wrappers of these functions such as MFC’s CFileDialog) will permanently change the process’s current working directory unless the OFN_NOCHANGEDIR option is specified. As you can imagine, this can easily break your application if you ever rely on the current working directory being set to a particular value (such as if you open files using relative paths).