Click here to Skip to main content
15,886,362 members
Articles / Desktop Programming / Win32

Yet another DLL-wrapper for dynamic loading

Rate me:
Please Sign up or sign in to vote.
4.88/5 (11 votes)
25 Mar 2012LGPL314 min read 55.9K   1.2K   80  
A DLL-wrapper that requires no code changes when switching to dynamic loading and provides detailed error checking functionality.
/*++ BUILD Version: 0001    // Increment this if a change has global effects

Copyright (c) 1994-1999  Microsoft Corporation

Module Name:

    psapi.h

Abstract:

    Include file for APIs provided by PSAPI.DLL

Author:

    Richard Shupak   [richards]  06-Jan-1994

Revision History:

--*/


// DYNLINK: Outside the protection through ifndef!!!
#undef  DYNLINK_LIBNAME
#define DYNLINK_LIBNAME  psapi

#include <dynlink.h>
// DYNLINK: Declarations changed by
// DYNLINK: {.*}\nWINAPI\n{[^\(:b]*}:b*{\(([^;]*\n)*.*\)}; --> DYNLINK_DECLARE(\1,\nWINAPI,\n\2, \3)
// DYNLINK: in Visual studio regular expressions

// DYNLINK: allow multiple inclusions
// #ifndef _PSAPI_H_
// #define _PSAPI_H_

// #if _MSC_VER > 1000
// #pragma once
// #endif

#ifdef __cplusplus
extern "C" {
#endif

// DYNLINK: protect parts not inside DECLARE_MACROS
#ifndef _PSAPI_H_

#define LIST_MODULES_DEFAULT 0x0  // This is the default one app would get without any flag.
#define LIST_MODULES_32BIT   0x01  // list 32bit modules in the target process.
#define LIST_MODULES_64BIT   0x02  // list all 64bit modules. 32bit exe will be stripped off.

// list all the modules
#define LIST_MODULES_ALL   (LIST_MODULES_32BIT | LIST_MODULES_64BIT)

// DYNLINK: end protection
#endif

DYNLINK_DECLARE(BOOL,
WINAPI,
EnumProcesses, (
    __out_bcount(cb) DWORD * lpidProcess,
    __in DWORD cb,
    __out LPDWORD lpcbNeeded
    ))

DYNLINK_DECLARE(BOOL,
WINAPI,
EnumProcessModules, (
    __in  HANDLE hProcess,
    __out_bcount(cb) HMODULE *lphModule,
    __in  DWORD cb,
    __out LPDWORD lpcbNeeded
    ))

DYNLINK_DECLARE(BOOL,
WINAPI,
EnumProcessModulesEx, (
    __in  HANDLE hProcess,
    __out_bcount(cb)  HMODULE *lphModule,
    __in  DWORD cb,
    __out  LPDWORD lpcbNeeded,
    __in  DWORD dwFilterFlag
    ))

DYNLINK_DECLARE(DWORD,
WINAPI,
GetModuleBaseNameA, (
    __in HANDLE hProcess,
    __in_opt HMODULE hModule,
    __out_ecount(nSize) LPSTR lpBaseName,
    __in DWORD nSize
    ))

DYNLINK_DECLARE(DWORD,
WINAPI,
GetModuleBaseNameW, (
    __in HANDLE hProcess,
    __in_opt HMODULE hModule,
    __out_ecount(nSize) LPWSTR lpBaseName,
    __in DWORD nSize
    ))

// DYNLINK: protect parts not inside DECLARE_MACROS
#ifndef _PSAPI_H_

#ifdef UNICODE
#define GetModuleBaseName  GetModuleBaseNameW
#else
#define GetModuleBaseName  GetModuleBaseNameA
#endif // !UNICODE

#endif

DYNLINK_DECLARE(DWORD,
WINAPI,
GetModuleFileNameExA, (
    __in HANDLE hProcess,
    __in_opt HMODULE hModule,
    __out_ecount(nSize) LPSTR lpFilename,
    __in DWORD nSize
    ))

DYNLINK_DECLARE(DWORD,
WINAPI,
GetModuleFileNameExW, (
    __in HANDLE hProcess,
    __in_opt HMODULE hModule,
    __out_ecount(nSize) LPWSTR lpFilename,
    __in DWORD nSize
    ))

// DYNLINK: protect parts not inside DECLARE_MACROS
#ifndef _PSAPI_H_

#ifdef UNICODE
#define GetModuleFileNameEx  GetModuleFileNameExW
#else
#define GetModuleFileNameEx  GetModuleFileNameExA
#endif // !UNICODE

typedef struct _MODULEINFO {
    LPVOID lpBaseOfDll;
    DWORD SizeOfImage;
    LPVOID EntryPoint;
} MODULEINFO, *LPMODULEINFO;

#endif

DYNLINK_DECLARE(BOOL,
WINAPI,
GetModuleInformation, (
    __in HANDLE hProcess,
    __in HMODULE hModule,
    __out LPMODULEINFO lpmodinfo,
    __in DWORD cb
    ))


DYNLINK_DECLARE(BOOL,
WINAPI,
EmptyWorkingSet, (
    __in HANDLE hProcess
    ))

//
// Working set information structures. All non-specified bits are reserved.
//

#if _MSC_VER >= 1200
#pragma warning(push)
#endif
#pragma warning(disable:4201)   // unnamed struct
#pragma warning(disable:4214)   // bit fields other than int

// DYNLINK: protect parts not inside DECLARE_MACROS
#ifndef _PSAPI_H_

typedef union _PSAPI_WORKING_SET_BLOCK {
    ULONG_PTR Flags;
    struct {
        ULONG_PTR Protection : 5;
        ULONG_PTR ShareCount : 3;
        ULONG_PTR Shared : 1;
        ULONG_PTR Reserved : 3;
#if defined(_WIN64)
        ULONG_PTR VirtualPage : 52;
#else
        ULONG_PTR VirtualPage : 20;
#endif
    };
} PSAPI_WORKING_SET_BLOCK, *PPSAPI_WORKING_SET_BLOCK;

typedef struct _PSAPI_WORKING_SET_INFORMATION {
    ULONG_PTR NumberOfEntries;
    PSAPI_WORKING_SET_BLOCK WorkingSetInfo[1];
} PSAPI_WORKING_SET_INFORMATION, *PPSAPI_WORKING_SET_INFORMATION;

typedef union _PSAPI_WORKING_SET_EX_BLOCK {
    ULONG_PTR Flags;
    struct {
        ULONG_PTR Valid : 1;        // The following fields are valid only if this bit is set
        ULONG_PTR ShareCount : 3;
        ULONG_PTR Win32Protection : 11;
        ULONG_PTR Shared : 1;
        ULONG_PTR Node : 6;
        ULONG_PTR Locked : 1;
        ULONG_PTR LargePage : 1;
    };
} PSAPI_WORKING_SET_EX_BLOCK, *PPSAPI_WORKING_SET_EX_BLOCK;

typedef struct _PSAPI_WORKING_SET_EX_INFORMATION {
    PVOID VirtualAddress;
    PSAPI_WORKING_SET_EX_BLOCK VirtualAttributes;
} PSAPI_WORKING_SET_EX_INFORMATION, *PPSAPI_WORKING_SET_EX_INFORMATION;

#if _MSC_VER >= 1200
#pragma warning(pop)
#else
#pragma warning(default:4214)
#pragma warning(default:4201)
#endif

#endif 

DYNLINK_DECLARE(BOOL,
WINAPI,
QueryWorkingSet, (
    __in HANDLE hProcess,
    __out_bcount(cb) PVOID pv,
    __in DWORD cb
    ))

DYNLINK_DECLARE(BOOL,
WINAPI,
QueryWorkingSetEx, (
    __in HANDLE hProcess,
    __out_bcount(cb) PVOID pv,
    __in DWORD cb
    ))

DYNLINK_DECLARE(BOOL,
WINAPI,
InitializeProcessForWsWatch, (
    __in HANDLE hProcess
    ))

// DYNLINK: protect parts not inside DECLARE_MACROS
#ifndef _PSAPI_H_

typedef struct _PSAPI_WS_WATCH_INFORMATION {
    LPVOID FaultingPc;
    LPVOID FaultingVa;
} PSAPI_WS_WATCH_INFORMATION, *PPSAPI_WS_WATCH_INFORMATION;

typedef struct _PSAPI_WS_WATCH_INFORMATION_EX {
    PSAPI_WS_WATCH_INFORMATION BasicInfo;
    ULONG_PTR FaultingThreadId;
    ULONG_PTR Flags;    // Reserved
} PSAPI_WS_WATCH_INFORMATION_EX, *PPSAPI_WS_WATCH_INFORMATION_EX;

#endif

DYNLINK_DECLARE(BOOL,
WINAPI,
GetWsChanges, (
    __in HANDLE hProcess,
    __out_bcount(cb) PPSAPI_WS_WATCH_INFORMATION lpWatchInfo,
    __in DWORD cb
    ))

DYNLINK_DECLARE(BOOL,
WINAPI,
GetWsChangesEx, (
    __in HANDLE hProcess,
    __out_bcount_part(*cb, *cb) PPSAPI_WS_WATCH_INFORMATION_EX lpWatchInfoEx,
    __inout PDWORD cb
    ))

DYNLINK_DECLARE(DWORD,
WINAPI,
GetMappedFileNameW, (
    __in HANDLE hProcess,
    __in LPVOID lpv,
    __out_ecount(nSize) LPWSTR lpFilename,
    __in DWORD nSize
    ))

DYNLINK_DECLARE(DWORD,
WINAPI,
GetMappedFileNameA, (
    __in HANDLE hProcess,
    __in LPVOID lpv,
    __out_ecount(nSize) LPSTR lpFilename,
    __in DWORD nSize
    ))

// DYNLINK: protect parts not inside DECLARE_MACROS
#ifndef _PSAPI_H_

#ifdef UNICODE
#define GetMappedFileName  GetMappedFileNameW
#else
#define GetMappedFileName  GetMappedFileNameA
#endif // !UNICODE

#endif

DYNLINK_DECLARE(BOOL,
WINAPI,
EnumDeviceDrivers, (
    __out_bcount(cb) LPVOID *lpImageBase,
    __in DWORD cb,
    __out LPDWORD lpcbNeeded
    ))


DYNLINK_DECLARE(DWORD,
WINAPI,
GetDeviceDriverBaseNameA, (
    __in LPVOID ImageBase,
    __out_ecount(nSize) LPSTR lpFilename,
    __in DWORD nSize
    ))

DYNLINK_DECLARE(DWORD,
WINAPI,
GetDeviceDriverBaseNameW, (
    __in LPVOID ImageBase,
    __out_ecount(nSize) LPWSTR lpBaseName,
    __in DWORD nSize
    ))

// DYNLINK: protect parts not inside DECLARE_MACROS
#ifndef _PSAPI_H_

#ifdef UNICODE
#define GetDeviceDriverBaseName  GetDeviceDriverBaseNameW
#else
#define GetDeviceDriverBaseName  GetDeviceDriverBaseNameA
#endif // !UNICODE

#endif

DYNLINK_DECLARE(DWORD,
WINAPI,
GetDeviceDriverFileNameA, (
    __in LPVOID ImageBase,
    __out_ecount(nSize) LPSTR lpFilename,
    __in DWORD nSize
    ))

DYNLINK_DECLARE(DWORD,
WINAPI,
GetDeviceDriverFileNameW, (
    __in LPVOID ImageBase,
    __out_ecount(nSize) LPWSTR lpFilename,
    __in DWORD nSize
    ))

// DYNLINK: protect parts not inside DECLARE_MACROS
#ifndef _PSAPI_H_

#ifdef UNICODE
#define GetDeviceDriverFileName  GetDeviceDriverFileNameW
#else
#define GetDeviceDriverFileName  GetDeviceDriverFileNameA
#endif // !UNICODE

// Structure for GetProcessMemoryInfo()

typedef struct _PROCESS_MEMORY_COUNTERS {
    DWORD cb;
    DWORD PageFaultCount;
    SIZE_T PeakWorkingSetSize;
    SIZE_T WorkingSetSize;
    SIZE_T QuotaPeakPagedPoolUsage;
    SIZE_T QuotaPagedPoolUsage;
    SIZE_T QuotaPeakNonPagedPoolUsage;
    SIZE_T QuotaNonPagedPoolUsage;
    SIZE_T PagefileUsage;
    SIZE_T PeakPagefileUsage;
} PROCESS_MEMORY_COUNTERS;
typedef PROCESS_MEMORY_COUNTERS *PPROCESS_MEMORY_COUNTERS;

#if (_WIN32_WINNT >= 0x0501)

typedef struct _PROCESS_MEMORY_COUNTERS_EX {
    DWORD cb;
    DWORD PageFaultCount;
    SIZE_T PeakWorkingSetSize;
    SIZE_T WorkingSetSize;
    SIZE_T QuotaPeakPagedPoolUsage;
    SIZE_T QuotaPagedPoolUsage;
    SIZE_T QuotaPeakNonPagedPoolUsage;
    SIZE_T QuotaNonPagedPoolUsage;
    SIZE_T PagefileUsage;
    SIZE_T PeakPagefileUsage;
    SIZE_T PrivateUsage;
} PROCESS_MEMORY_COUNTERS_EX;
typedef PROCESS_MEMORY_COUNTERS_EX *PPROCESS_MEMORY_COUNTERS_EX;

#endif

#endif

DYNLINK_DECLARE(BOOL,
WINAPI,
GetProcessMemoryInfo, (
    HANDLE Process,
    PPROCESS_MEMORY_COUNTERS ppsmemCounters,
    DWORD cb
    ))

// DYNLINK: protect parts not inside DECLARE_MACROS
#ifndef _PSAPI_H_

typedef struct _PERFORMANCE_INFORMATION {
    DWORD cb;
    SIZE_T CommitTotal;
    SIZE_T CommitLimit;
    SIZE_T CommitPeak;
    SIZE_T PhysicalTotal;
    SIZE_T PhysicalAvailable;
    SIZE_T SystemCache;
    SIZE_T KernelTotal;
    SIZE_T KernelPaged;
    SIZE_T KernelNonpaged;
    SIZE_T PageSize;
    DWORD HandleCount;
    DWORD ProcessCount;
    DWORD ThreadCount;
} PERFORMANCE_INFORMATION, *PPERFORMANCE_INFORMATION, PERFORMACE_INFORMATION, *PPERFORMACE_INFORMATION;

#endif

DYNLINK_DECLARE(BOOL,
WINAPI,
GetPerformanceInfo, (
    PPERFORMANCE_INFORMATION pPerformanceInformation,
    DWORD cb
    ))

// DYNLINK: protect parts not inside DECLARE_MACROS
#ifndef _PSAPI_H_

typedef struct _ENUM_PAGE_FILE_INFORMATION {
    DWORD cb;
    DWORD Reserved;
    SIZE_T TotalSize;
    SIZE_T TotalInUse;
    SIZE_T PeakUsage;
} ENUM_PAGE_FILE_INFORMATION, *PENUM_PAGE_FILE_INFORMATION;

typedef BOOL (*PENUM_PAGE_FILE_CALLBACKW) (LPVOID pContext, PENUM_PAGE_FILE_INFORMATION pPageFileInfo, LPCWSTR lpFilename);

typedef BOOL (*PENUM_PAGE_FILE_CALLBACKA) (LPVOID pContext, PENUM_PAGE_FILE_INFORMATION pPageFileInfo, LPCSTR lpFilename);

#endif

DYNLINK_DECLARE(BOOL,
WINAPI,
EnumPageFilesW, (
    PENUM_PAGE_FILE_CALLBACKW pCallBackRoutine,
    LPVOID pContext
    ))

DYNLINK_DECLARE(BOOL,
WINAPI,
EnumPageFilesA, (
    PENUM_PAGE_FILE_CALLBACKA pCallBackRoutine,
    LPVOID pContext
    ))

// DYNLINK: protect parts not inside DECLARE_MACROS
#ifndef _PSAPI_H_

#ifdef UNICODE
#define PENUM_PAGE_FILE_CALLBACK PENUM_PAGE_FILE_CALLBACKW
#define EnumPageFiles EnumPageFilesW
#else
#define PENUM_PAGE_FILE_CALLBACK PENUM_PAGE_FILE_CALLBACKA
#define EnumPageFiles EnumPageFilesA
#endif // !UNICODE

#endif

DYNLINK_DECLARE(DWORD,
WINAPI,
GetProcessImageFileNameA, (
    __in HANDLE hProcess,
    __out_ecount(nSize) LPSTR lpImageFileName,
    __in DWORD nSize
    ))

DYNLINK_DECLARE(DWORD,
WINAPI,
GetProcessImageFileNameW, (
    __in HANDLE hProcess,
    __out_ecount(nSize) LPWSTR lpImageFileName,
    __in DWORD nSize
    ))

// DYNLINK: protect parts not inside DECLARE_MACROS 
// DYNLINK: in the last block define the protecting symbol so these parts are
// DYNLINK: not included any more
#ifndef _PSAPI_H_
#define _PSAPI_H_

#ifdef UNICODE
#define GetProcessImageFileName  GetProcessImageFileNameW
#else
#define GetProcessImageFileName  GetProcessImageFileNameA
#endif // !UNICODE

#endif

// These are functions not available in the dll, to test stubbing
DYNLINK_DECLARE(DWORD,
WINAPI,
GetProcessImageFileNameW1, (
    __in HANDLE hProcess,
    __out_ecount(nSize) LPWSTR lpImageFileName,
    __in DWORD nSize
    ))

DYNLINK_DECLARE(int,
	WINAPI,
	GetProcessImageFileNameW2, (
	__in HANDLE hProcess,
	__out_ecount(nSize) LPWSTR lpImageFileName,
	__in DWORD nSize
	))

DYNLINK_DECLARE_EX(
DWORD, 
WINAPI,
GetProcessImageFileNameW3, (
    __in HANDLE hProcess,
    __out_ecount(nSize) LPWSTR lpImageFileName,
    __in DWORD nSize
    ), (DWORD)-2);

#ifdef __cplusplus
}
#endif

// DYNLINK: former protection removed
//#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions