Easier Dynamic Win32 API

When Win32 API first came out, it wasn't sprawled out like it is now.  As it expanded (exploded) in spurts across versions, architectures, and service packs, Windows developers had to deal increasingly with the headaches from the need to take advantage of latest features without crippling their software with older Windows.

While one could now use deferred library loading creatively to address this problem, I've always handled it with manual LoadLibrary and GetProcAddress calls.  Well, I finally got tired of doing this so I tried the handy LateLoad, a small MFC-style event framework-like macros that creates a C++ class for each library and a C++ method for each procedure.  Very nice.

Here is the LateLoad definitions for a set of Win32 functions one needs to enumerating running processes and modules across Win32 versions.

LATELOAD_BEGIN_CLASS(CPsApi, PSAPI.DLL, FALSE, FALSE)
  LATELOAD_FUNC_3(FALSE, BOOL, STDAPICALLTYPE,
      EnumProcesses,
DWORD*, DWORD, DWORD*)
 
LATELOAD_FUNC_4(FALSE, BOOL, STDAPICALLTYPE,
      EnumProcessModules,
HANDLE, HMODULE*, DWORD, LPDWORD)
 
LATELOAD_FUNC_4(0, DWORD, STDAPICALLTYPE, GetModuleBaseNameA,
     
HANDLE, HMODULE, LPTSTR, DWORD)
 
LATELOAD_FUNC_4(0, DWORD, STDAPICALLTYPE, GetModuleFileNameExA,
     
HANDLE, HMODULE, LPTSTR, DWORD)
LATELOAD_END_CLASS()

LATELOAD_BEGIN_CLASS(CVdmDbg, VDMDBG.DLL, FALSE, FALSE)
 
LATELOAD_FUNC_3(0, INT, STDAPICALLTYPE, VDMEnumTaskWOWEx,
     
DWORD, TASKENUMPROCEX, LPARAM)
LATELOAD_END_CLASS()

LATELOAD_BEGIN_CLASS(CKernel32, Kernel32.DLL, FALSE, FALSE)
 
LATELOAD_FUNC_2(NULL, HANDLE, STDAPICALLTYPE,
      CreateToolhelp32Snapshot,
DWORD, DWORD)
 
LATELOAD_FUNC_2(FALSE, BOOL, STDAPICALLTYPE,
      Process32First,
HANDLE, LPPROCESSENTRY32)
 
LATELOAD_FUNC_2(FALSE, BOOL, STDAPICALLTYPE,
      Process32Next,
HANDLE, LPPROCESSENTRY32)
LATELOAD_END_CLASS()

Advertisements