-/* $Id: modules.c,v 1.2 2003/06/01 14:59:01 chorns Exp $
+/* $Id$
*/
/*
* COPYRIGHT: See COPYING in the top level directory
* isolated in its own library to clear the confusion
* and improve reusability
*/
-
+#define WIN32_NO_STATUS
+#include <windows.h>
#define NTOS_MODE_USER
-#include <ntos.h>
+#include <ndk/ntndk.h>
#define NDEBUG
#include <debug.h>
-#include <epsapi.h>
+#include <epsapi/epsapi.h>
-NTSTATUS
-NTAPI
-PsaEnumerateProcessModules
-(
- IN HANDLE ProcessHandle,
- IN PPROCMOD_ENUM_ROUTINE Callback,
- IN OUT PVOID CallbackContext
-)
+NTSTATUS NTAPI
+PsaEnumerateProcessModules(IN HANDLE ProcessHandle,
+ IN PPROCMOD_ENUM_ROUTINE Callback,
+ IN OUT PVOID CallbackContext)
{
- register NTSTATUS nErrCode;
+ NTSTATUS Status;
- /* current process - use direct memory copy */
- if(ProcessHandle == NtCurrentProcess())
- {
- register PLIST_ENTRY pleListHead;
- register PLIST_ENTRY pleCurEntry;
+ /* current process - use direct memory copy */
+ /* FIXME - compare process id instead of a handle */
+ if(ProcessHandle == NtCurrentProcess())
+ {
+ PLIST_ENTRY ListHead, Current;
#if 0
- /* FIXME: activate this when GCC supports SEH */
- __try
- {
+ __try
+ {
#endif
- pleListHead = &(NtCurrentPeb()->Ldr->InLoadOrderModuleList);
- pleCurEntry = pleListHead->Flink;
+ ListHead = &(NtCurrentPeb()->Ldr->InLoadOrderModuleList);
+ Current = ListHead->Flink;
- while(pleCurEntry != pleListHead)
- {
- register PLDR_MODULE plmModule = CONTAINING_RECORD
- (
- pleCurEntry,
- LDR_MODULE,
- InLoadOrderModuleList
- );
+ while(Current != ListHead)
+ {
+ PLDR_DATA_TABLE_ENTRY LoaderModule = CONTAINING_RECORD(Current, LDR_DATA_TABLE_ENTRY, InLoadOrderLinks);
- /* return the current module to the callback */
- nErrCode = Callback(ProcessHandle, plmModule, CallbackContext);
+ /* return the current module to the callback */
+ Status = Callback(ProcessHandle, LoaderModule, CallbackContext);
- if(!NT_SUCCESS(nErrCode))
- /* failure */
- goto epm_Failure;
+ if(!NT_SUCCESS(Status))
+ {
+ goto Failure;
+ }
- pleCurEntry = plmModule->InLoadOrderModuleList.Flink;
- }
+ Current = LoaderModule->InLoadOrderLinks.Flink;
+ }
#if 0
- /* FIXME: activate this when GCC supports SEH */
+ }
+ __except(EXCEPTION_EXECUTE_HANDLER)
+ {
+ return GetExceptionCode();
+ }
+#endif
}
- __except(EXCEPTION_EXECUTE_HANDLER)
+ else
{
- return GetExceptionCode();
- }
-#endif
- }
- /* another process */
- else
- {
- PROCESS_BASIC_INFORMATION pbiInfo;
- PPEB_LDR_DATA ppldLdrData;
- LDR_MODULE lmModule;
- PLIST_ENTRY pleListHead;
- PLIST_ENTRY pleCurEntry;
+ PROCESS_BASIC_INFORMATION BasicInformation;
+ PPEB_LDR_DATA LoaderData;
+ LDR_DATA_TABLE_ENTRY LoaderModule;
+ PLIST_ENTRY ListHead, Current;
- /* query the process basic information (includes the PEB address) */
- nErrCode = NtQueryInformationProcess
- (
- ProcessHandle,
- ProcessBasicInformation,
- &pbiInfo,
- sizeof(pbiInfo),
- NULL
- );
+ /* query the process basic information (includes the PEB address) */
+ Status = NtQueryInformationProcess(ProcessHandle,
+ ProcessBasicInformation,
+ &BasicInformation,
+ sizeof(BasicInformation),
+ NULL);
- if(!NT_SUCCESS(nErrCode))
- {
- /* failure */
- DPRINT(FAILED_WITH_STATUS, "NtQueryInformationProcess", nErrCode);
- goto epm_Failure;
- }
+ if(!NT_SUCCESS(Status))
+ {
+ DPRINT(FAILED_WITH_STATUS, "NtQueryInformationProcess", Status);
+ goto Failure;
+ }
- /* get the address of the PE Loader data */
- nErrCode = NtReadVirtualMemory
- (
- ProcessHandle,
- &(pbiInfo.PebBaseAddress->Ldr),
- &ppldLdrData,
- sizeof(ppldLdrData),
- NULL
- );
+ /* get the address of the PE Loader data */
+ Status = NtReadVirtualMemory(ProcessHandle,
+ &(BasicInformation.PebBaseAddress->Ldr),
+ &LoaderData,
+ sizeof(LoaderData),
+ NULL);
- if(!NT_SUCCESS(nErrCode))
- {
- /* failure */
- DPRINT(FAILED_WITH_STATUS, "NtReadVirtualMemory", nErrCode);
- goto epm_Failure;
- }
+ if(!NT_SUCCESS(Status))
+ {
+ DPRINT(FAILED_WITH_STATUS, "NtReadVirtualMemory", Status);
+ goto Failure;
+ }
- /* head of the module list: the last element in the list will point to this */
- pleListHead = &ppldLdrData->InLoadOrderModuleList;
+ /* head of the module list: the last element in the list will point to this */
+ ListHead = &LoaderData->InLoadOrderModuleList;
- /* get the address of the first element in the list */
- nErrCode = NtReadVirtualMemory
- (
- ProcessHandle,
- &(ppldLdrData->InLoadOrderModuleList.Flink),
- &pleCurEntry,
- sizeof(pleCurEntry),
- NULL
- );
+ /* get the address of the first element in the list */
+ Status = NtReadVirtualMemory(ProcessHandle,
+ &(LoaderData->InLoadOrderModuleList.Flink),
+ &Current,
+ sizeof(Current),
+ NULL);
- while(pleCurEntry != pleListHead)
- {
- /* read the current module */
- nErrCode = NtReadVirtualMemory
- (
- ProcessHandle,
- CONTAINING_RECORD(pleCurEntry, LDR_MODULE, InLoadOrderModuleList),
- &lmModule,
- sizeof(lmModule),
- NULL
- );
+ while(Current != ListHead)
+ {
+ /* read the current module */
+ Status = NtReadVirtualMemory(ProcessHandle,
+ CONTAINING_RECORD(Current, LDR_DATA_TABLE_ENTRY, InLoadOrderLinks),
+ &LoaderModule,
+ sizeof(LoaderModule),
+ NULL);
- if(!NT_SUCCESS(nErrCode))
- {
- /* failure */
- DPRINT(FAILED_WITH_STATUS, "NtReadVirtualMemory", nErrCode);
- goto epm_Failure;
- }
+ if(!NT_SUCCESS(Status))
+ {
+ DPRINT(FAILED_WITH_STATUS, "NtReadVirtualMemory", Status);
+ goto Failure;
+ }
- /* return the current module to the callback */
- nErrCode = Callback(ProcessHandle, &lmModule, CallbackContext);
+ /* return the current module to the callback */
+ Status = Callback(ProcessHandle, &LoaderModule, CallbackContext);
- if(!NT_SUCCESS(nErrCode))
- /* failure */
- goto epm_Failure;
+ if(!NT_SUCCESS(Status))
+ {
+ goto Failure;
+ }
- /* address of the next module in the list */
- pleCurEntry = lmModule.InLoadOrderModuleList.Flink;
+ /* address of the next module in the list */
+ Current = LoaderModule.InLoadOrderLinks.Flink;
+ }
}
-
- }
- /* success */
- return (STATUS_SUCCESS);
+ return STATUS_SUCCESS;
-epm_Failure:
- /* failure */
- return (nErrCode);
+Failure:
+ return Status;
}
/* EOF */