Following program to retrieve all the process information from my computer. The same application when it is called through a dll, it fails to retrieve system process information.
// TestSysInternals.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include "ntdll.h"
#include <Winternl.h>
#define VISTA_FILETYPE 25
#define XP_FILETYPE 28
static PNtQuerySystemInformation NtQuerySystemInformation_dynamic = NULL;
static DWORD curPid = 0;
static int nFileHandleType;
#define STATUS_INFO_LENGTH_MISMATCH 0xC0000004
int _tmain(int argc, _TCHAR* argv[])
{
NtQuerySystemInformation_dynamic = (PNtQuerySystemInformation)GetProcAddress(GetModuleHandle(TEXT("ntdll.dll")), ("NtQuerySystemInformation"));
// (PNtQuerySystemInformation)GetProcAddress(GetModuleHandle(_T("ntdll.dll")), _("NtQuerySystemInformation"));
nFileHandleType = XP_FILETYPE;
NTSTATUS status;
DWORD size = sizeof(SYSTEM_HANDLE_INFORMATION);
PSYSTEM_HANDLE_INFORMATION pSysHandleInformation = (PSYSTEM_HANDLE_INFORMATION)malloc(size);
DWORD needed = 0;
int nfound = 0;
while (!NT_SUCCESS(status = NtQuerySystemInformation_dynamic(SystemHandleInformation, pSysHandleInformation, size, &needed)))
{
if (status != STATUS_INFO_LENGTH_MISMATCH
|| needed == 0)
{
//DBGLOG("==>Failed Status=%l(%#X) Needed=%lu", status, status, needed);
goto CLEAN;// some other error
}
// The previously supplied buffer wasn't enough.
size = needed + 1024;
pSysHandleInformation = (PSYSTEM_HANDLE_INFORMATION)realloc(pSysHandleInformation, size);
}
DWORD i;
for (i = 0; i < pSysHandleInformation->Count; i++)
{
DWORD handlePid = pSysHandleInformation->Handles[i].ProcessID;
}
CLEAN:
free(pSysHandleInformation);
return nfound;
return 0;
}
Header file
#ifndef NT_DLL_H_INCLUDED
#define NT_DLL_H_INCLUDED
#include <Winternl.h>
#include <Windows.h>
typedef DWORD(WINAPI *PNtQuerySystemInformation)(DWORD, VOID*, DWORD, ULONG*);
typedef struct _SYSTEM_HANDLE
{
DWORD ProcessID;
BYTE HandleType;
BYTE HandleFlags;
WORD HandleNumber;
DWORD KernelAddress;
DWORD Flags;
} SYSTEM_HANDLE, *PSYSTEM_HANDLE;
typedef struct _SYSTEM_HANDLE_INFORMATION
{
DWORD Count;
SYSTEM_HANDLE Handles[1];
} SYSTEM_HANDLE_INFORMATION, *PSYSTEM_HANDLE_INFORMATION;
#define NT_SUCCESS_K(Status) ((NTSTATUS)(Status) >= 0)
#define SystemHandleInformation (DWORD)0x10
#endif
When checkd the pSysHandleInformation->Handle
s array through debug watch, I could see following information.
As per the picture, it gives real process id for processID value. However, when I put the same code inside a dll, the processID value get really large values that really does not exist in my pc, following is the screenshot of the process id values that i get when I run the same code through dll. I have no idea why application behave differently when the same code is run through dll. Highly appreciate any thought on this wired behavior.