Jump to content
Setting up an AI agent? ReverseEngineering.wiki skill (works from the command line, no configuration): https://reverseengineering.wiki/skill/rewiki/SKILL.md · setup for every client, including MCP: https://reverseengineering.wiki/llms.txt · REWiki:Connect

Library:MFC

From ReverseEngineering.wiki
Library
Vendor Microsoft
Platforms Platform:Windows
Versions mfc40, mfc42, mfc70-mfc140
License proprietary (redistributable DLLs)
On this wiki Facts · Code · Notes · Topics

Microsoft Foundation Classes, the C++ GUI framework of classic Visual C++ programs: linked as mfcNN.dll (imports by ordinal only) or statically, recognisable by CWinApp/CWnd RTTI names, AFX_MSGMAP message maps and CRuntimeClass records.

Identification

  • Dynamic build: the import table lists mfcNN.dll (mfc40, mfc42/mfc42u, mfc70, mfc71, mfc80, mfc90, mfc100, mfc110, mfc120, mfc140; a u suffix is the Unicode build, d the debug build) and almost every import is by ordinal, so the names must come from a per-version ordinal map (an MFC export list, or the Microsoft import library that was used). The runtime is msvcrNN.dll/msvcp/vcruntime of the same Visual C++ generation, which dates the compiler. Unverified: the exact set of DLL names and their mapping to Visual Studio releases (mfc42 = VC6, 70 = 2002, 71 = 2003, 80 = 2005, 90 = 2008, 100 = 2010, 110 = 2012, 120 = 2013, 140 = 2015 to 2022).
  • Static build (no mfc DLL): the MFC code is in the executable. Use the MSVC RTTI type names (Technique:Recovering C++ class metadata): .?AVCWinApp@@, .?AVCWnd@@, .?AVCCmdTarget@@, .?AVCObject@@, .?AVCFrameWnd@@, .?AVCDocument@@, .?AVCView@@. The program's own classes (.?AVCMyApp@@ deriving CWinApp) mark the application layer. FLIRT signatures for MFC exist, see Technique:Library function recognition.
  • Strings: MFC registers window classes whose names start with Afx: followed by hex fields and uses names such as AfxWnd, AfxFrameOrView, AfxControlBar, AfxMDIFrame. Unverified: exact formats differ per version.
  • Entry: the CRT startup calls AfxWinMain (inside MFC), which calls the virtual CWinApp::InitInstance of the global application object (theApp). Find theApp as the one global object whose vtable derives CWinApp.

Message maps

Every class derived from CCmdTarget that handles messages has a message map (documented by Microsoft in TN006: DECLARE_MESSAGE_MAP adds a private array _messageEntries of AFX_MSGMAP_ENTRY, a protected AFX_MSGMAP messageMap that points to it, and a protected virtual GetMessageMap that returns the address of messageMap). The layouts below are not in a header this wiki read; they come from knowledge of the MFC headers and are marked Unverified until checked on a binary.

  • AFX_MSGMAP: two pointers. In DLL builds and since VC7: const AFX_MSGMAP* (*pfnGetBaseMap)() (a function returning the base class map) then const AFX_MSGMAP_ENTRY* lpEntries. In old static builds (VC6 and before): const AFX_MSGMAP* pBaseMap directly. Unverified.
  • AFX_MSGMAP_ENTRY (24 bytes on x86, 32 on x64): UINT nMessage (WM_xxx), UINT nCode (notification code, 0 for plain messages, CN_COMMAND 0 for commands), UINT nID, UINT nLastID (the range end; equal to nID for a single command), UINT_PTR nSig (an AfxSig_xxx number selecting how the handler is called), AFX_PMSG pfn (pointer to the handler or its thunk). The table ends with an all-zero entry ({0, 0, 0, 0, AfxSig_end, 0}). Unverified.
  • Examples: ON_COMMAND(id, fn) is {WM_COMMAND (0x111), 0, id, id, AfxSig_vv, fn}; ON_WM_PAINT() is {WM_PAINT (0x0F), 0, 0, 0, AfxSig_vv, OnPaint}; ON_MESSAGE(msg, fn) is {msg, 0, 0, 0, AfxSig_lwl, fn}; ON_UPDATE_COMMAND_UI(id, fn) uses nCode 0xFFFF (CN_UPDATE_COMMAND_UI). Unverified.

Walking the maps to find handlers

  1. Find the CCmdTarget-derived vtable of a class (RTTI) and its GetMessageMap slot (the same slot in every class; a tiny function that returns a data address), or search .data for a pair of pointers whose second points to an array ending in six zero fields.
  2. Follow lpEntries and read entries until the zero terminator; each non-zero pfn is a handler. With the message number 0x111 and the nID you get the WM_COMMAND handlers; link them to menu and dialog ids (Format:Windows menu resource (RT_MENU), Format:Windows dialog template resource (RT_DIALOG)).
  3. Follow the base map to the parent class to see inherited handlers (stop at CWnd/CCmdTarget, whose maps are inside MFC).
  4. On MSVC, pfn is a pointer to member function; for a class with virtual-only or single inheritance it is the plain code address (or a thunk that jumps to it).

Message handlers wrapped in ON_WM_* have very specific AfxSig values, which tell the C++ signature of the handler (for example AfxSig_vwww for a (UINT, UINT, UINT) handler), a free type recovery.

CRuntimeClass

DECLARE_DYNAMIC/IMPLEMENT_DYNAMIC (and the _DYNCREATE, _SERIAL variants) give a class a static CRuntimeClass record, reachable through GetRuntimeClass() (a virtual) and RUNTIME_CLASS(X). Layout (x86), Unverified: LPCSTR m_lpszClassName (ASCII class name, +0), int m_nObjectSize (+4, sizeof the class, a ready-made size for the structure), UINT m_wSchema (+8, 0xFFFF for classes that are not serializable), CObject* (*m_pfnCreateObject)() (+0xC, NULL if not dynamically creatable), the base class record (CRuntimeClass* m_pBaseClass, or a getter function in DLL builds, +0x10), CRuntimeClass* m_pNextClass (+0x14, linked at startup) and, since VC7, const AFX_CLASSINIT* m_pClassInit.

The ASCII names (CMyDoc, CMainFrame) survive in stripped builds even without RTTI; search for pointers to them in .data.

Archives (documented in TN002): object tags in a serialized stream are 0 (null pointer), 0xFFFF (a new class description follows: schema number plus ASCII class name, written by CRuntimeClass::Store) and 0x8000 (an already seen class); a CRuntimeClass with schema mismatch raises CArchiveException unless VERSIONABLE_SCHEMA is used. This identifies MFC document files (CObject::Serialize).

Code & repositories

  • No open-source MFC. Layouts here are re-described from Microsoft documentation (MicrosoftDocs/cpp-docs TN002 and TN006) and knowledge of the headers; do not paste header text.
  • FLIRT signatures for MFC: see Technique:Library function recognition.

Evidence & references

  • MicrosoftDocs/cpp-docs docs/mfc/tn006-message-maps.md and docs/mfc/tn002-persistent-object-data-format.md (read for the facts attributed to Microsoft above).
  • All structure layouts marked Unverified: test on a binary with symbols (a PDB for an MFC program, or a debug build).