Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygendrivers.c
Go to the documentation of this file.
00001 /* $Id: drivers.c 52985 2011-07-28 16:21:48Z akhaldi $ 00002 */ 00003 /* 00004 * COPYRIGHT: See COPYING in the top level directory 00005 * LICENSE: See LGPL.txt in the top level directory 00006 * PROJECT: ReactOS system libraries 00007 * FILE: reactos/lib/epsapi/enum/drivers.c 00008 * PURPOSE: Enumerate system modules 00009 * PROGRAMMER: KJK::Hyperion <noog@libero.it> 00010 * UPDATE HISTORY: 00011 * 02/04/2003: Created 00012 * 12/04/2003: internal PSAPI renamed EPSAPI (Extended PSAPI) and 00013 * isolated in its own library to clear the confusion 00014 * and improve reusability 00015 */ 00016 00017 #include "precomp.h" 00018 00019 #define NDEBUG 00020 #include <debug.h> 00021 00022 NTSTATUS NTAPI 00023 PsaEnumerateSystemModules(IN PSYSMOD_ENUM_ROUTINE Callback, 00024 IN OUT PVOID CallbackContext) 00025 { 00026 PRTL_PROCESS_MODULES psmModules; 00027 NTSTATUS Status = STATUS_SUCCESS; 00028 00029 #if 0 00030 __try 00031 { 00032 #else 00033 do 00034 { 00035 #endif 00036 /* capture the system modules */ 00037 Status = PsaCaptureSystemModules(&psmModules); 00038 00039 if(!NT_SUCCESS(Status)) 00040 { 00041 break; 00042 } 00043 00044 /* walk the system modules */ 00045 Status = PsaWalkSystemModules(psmModules, Callback, CallbackContext); 00046 #if 0 00047 } 00048 __finally 00049 { 00050 #else 00051 } while(0); 00052 #endif 00053 /* free the capture */ 00054 PsaFreeCapture(psmModules); 00055 #if 0 00056 } 00057 #endif 00058 00059 return Status; 00060 } 00061 00062 NTSTATUS NTAPI 00063 PsaCaptureSystemModules(OUT PRTL_PROCESS_MODULES *SystemModules) 00064 { 00065 SIZE_T nSize = 0; 00066 PRTL_PROCESS_MODULES psmModules = NULL; 00067 NTSTATUS Status; 00068 00069 #if 0 00070 __try 00071 { 00072 #else 00073 do 00074 { 00075 #endif 00076 /* initial probe. We just get the count of system modules */ 00077 Status = NtQuerySystemInformation(SystemModuleInformation, 00078 &nSize, 00079 sizeof(nSize), 00080 NULL); 00081 00082 if(!NT_SUCCESS(Status) && (Status != STATUS_INFO_LENGTH_MISMATCH)) 00083 { 00084 DPRINT(FAILED_WITH_STATUS, "NtQuerySystemInformation", Status); 00085 break; 00086 } 00087 00088 /* RATIONALE: the loading of a system module is a rare occurrence. To 00089 minimize memory operations that could be expensive, or fragment the 00090 pool/heap, we try to determine the buffer size in advance, knowing that 00091 the number of elements is unlikely to change */ 00092 nSize = sizeof(RTL_PROCESS_MODULES) + 00093 (nSize * sizeof(RTL_PROCESS_MODULES)); 00094 00095 psmModules = NULL; 00096 00097 do 00098 { 00099 PVOID pTmp; 00100 00101 /* free the buffer, and reallocate it to the new size. RATIONALE: since we 00102 ignore the buffer's content at this point, there's no point in a realloc, 00103 that could end up copying a large chunk of data we'd discard anyway */ 00104 PsaiFree(psmModules); 00105 pTmp = PsaiMalloc(nSize); 00106 00107 if(pTmp == NULL) 00108 { 00109 Status = STATUS_NO_MEMORY; 00110 DPRINT(FAILED_WITH_STATUS, "PsaiMalloc", Status); 00111 break; 00112 } 00113 00114 psmModules = pTmp; 00115 00116 /* query the information */ 00117 Status = NtQuerySystemInformation(SystemModuleInformation, 00118 psmModules, 00119 nSize, 00120 NULL); 00121 00122 /* double the buffer for the next loop */ 00123 nSize *= 2; 00124 } while(Status == STATUS_INFO_LENGTH_MISMATCH); 00125 00126 if(!NT_SUCCESS(Status)) 00127 { 00128 DPRINT(FAILED_WITH_STATUS, "NtQuerySystemInformation", Status); 00129 break; 00130 } 00131 00132 *SystemModules = psmModules; 00133 00134 Status = STATUS_SUCCESS; 00135 #if 0 00136 } 00137 __finally 00138 { 00139 #else 00140 } while(0); 00141 #endif 00142 /* in case of failure, free the buffer */ 00143 if(!NT_SUCCESS(Status)) 00144 { 00145 PsaiFree(psmModules); 00146 } 00147 #if 0 00148 } 00149 #endif 00150 00151 return Status; 00152 } 00153 00154 NTSTATUS NTAPI 00155 PsaWalkSystemModules(IN PRTL_PROCESS_MODULES SystemModules, 00156 IN PSYSMOD_ENUM_ROUTINE Callback, 00157 IN OUT PVOID CallbackContext) 00158 { 00159 ULONG i; 00160 NTSTATUS Status; 00161 00162 /* repeat until all modules have been returned */ 00163 for(i = 0; i < SystemModules->NumberOfModules; i++) 00164 { 00165 /* return current module to the callback */ 00166 Status = Callback(&(SystemModules->Modules[i]), CallbackContext); 00167 00168 if(!NT_SUCCESS(Status)) 00169 { 00170 return Status; 00171 } 00172 } 00173 00174 return STATUS_SUCCESS; 00175 } 00176 00177 PRTL_PROCESS_MODULE_INFORMATION FASTCALL 00178 PsaWalkFirstSystemModule(IN PRTL_PROCESS_MODULES SystemModules) 00179 { 00180 return &(SystemModules->Modules[0]); 00181 } 00182 00183 PRTL_PROCESS_MODULE_INFORMATION FASTCALL 00184 PsaWalkNextSystemModule(IN PRTL_PROCESS_MODULES CurrentSystemModule) 00185 { 00186 return (PRTL_PROCESS_MODULE_INFORMATION)((ULONG_PTR)CurrentSystemModule + 00187 (FIELD_OFFSET(RTL_PROCESS_MODULES, Modules[1]) - 00188 FIELD_OFFSET(RTL_PROCESS_MODULES, Modules[0]))); 00189 } 00190 00191 /* EOF */ Generated on Thu May 24 2012 04:36:41 for ReactOS by
1.7.6.1
|