From: Anthony Lauzon Subject: [PATCH] kernel32: Implement K32EnumDeviceDrivers and K32GetDeviceDriverBaseNameA (pt. 2) Message-Id: Date: Thu, 21 Jun 2018 05:00:17 -0400 I noticed my submission yesterday was a little undercooked and since I have spent some more time on it, I thought I'd try to submit a (hopefully) proper patch to the list. This is a rough implementation of K32EnumDeviceDrivers and K32GetDeviceDriverBaseNameA.
I noticed my submission yesterday was a little undercooked and since I have spent some more time on it, I thought I'd try to submit a (hopefully) proper patch to the list.  This is a rough implementation of K32EnumDeviceDrivers and K32GetDeviceDriverBaseNameA.  
diff --git a/dlls/kernel32/file.c b/dlls/kernel32/file.c index 1e5b9fe3e7..fc3cb93052 100644 --- a/dlls/kernel32/file.c +++ b/dlls/kernel32/file.c @@ -2834,29 +2834,112 @@ HANDLE WINAPI ReOpenFile(HANDLE handle_original, DWORD access, DWORD sharing, DW /*********************************************************************** * K32EnumDeviceDrivers (KERNEL32.@) */ -BOOL WINAPI K32EnumDeviceDrivers(void **image_base, DWORD cb, DWORD *needed) +BOOL WINAPI K32EnumDeviceDrivers(LPVOID **image_base, DWORD cb, DWORD *needed) { - FIXME("(%p, %d, %p): stub\n", image_base, cb, needed); - - if (needed) - *needed = 0; + NTSTATUS status; + ULONG ReturnLength; + + ULONG SystemInformationLength = sizeof(SYSTEM_MODULE_INFORMATION); + SYSTEM_MODULE_INFORMATION* smi = HeapAlloc(GetProcessHeap(), + 0, + SystemInformationLength); + + status = NtQuerySystemInformation(SystemModuleInformation, + smi, + 0, + &ReturnLength); + + SystemInformationLength = ReturnLength; + smi = HeapReAlloc(GetProcessHeap(), + 0, + smi , + SystemInformationLength); + + status = NtQuerySystemInformation(SystemModuleInformation, + smi, + SystemInformationLength, + &ReturnLength); + + ULONG i; + /* Try to find which module matches the base address given */ + for (i = 0; i < smi->ModulesCount; ++i) + { + if (i >= cb/sizeof(LPVOID)) { + break; + } + image_base[i] = smi->Modules[i].ImageBaseAddress; + } + *needed += (smi->ModulesCount + 1) * sizeof(LPVOID); return TRUE; } -/*********************************************************************** - * K32GetDeviceDriverBaseNameA (KERNEL32.@) - */ -DWORD WINAPI K32GetDeviceDriverBaseNameA(void *image_base, LPSTR base_name, DWORD size) + +WORD WINAPI K32GetDeviceDriverBaseNameA(LPVOID image_base, + LPSTR base_name, + DWORD size) { - FIXME("(%p, %p, %d): stub\n", image_base, base_name, size); + NTSTATUS status; + ULONG ReturnLength; + + DWORD Len, LenWithNull; + + ULONG SystemInformationLength = sizeof(SYSTEM_MODULE_INFORMATION); + SYSTEM_MODULE_INFORMATION* smi = HeapAlloc(GetProcessHeap(), + 0, + SystemInformationLength); + + status = NtQuerySystemInformation(SystemModuleInformation, + smi, + 0, + &ReturnLength); + + SystemInformationLength = ReturnLength; + smi = HeapReAlloc(GetProcessHeap(), + 0, + smi, + SystemInformationLength); + + status = NtQuerySystemInformation(SystemModuleInformation, + smi, + SystemInformationLength, + &ReturnLength); + ULONG i; + /* Try to find which module matches the base address given */ + for (i = 0; i < smi->ModulesCount; ++i) + { + if (smi->Modules[i].ImageBaseAddress == image_base) + { + //memcpy(MatchingModulePtr, &smi->Modules[i], sizeof(SYSTEM_MODULE)); + LPSTR Name = &smi->Modules[i].Name[smi->Modules[i].NameOffset]; + Len = LenWithNull = strlen(Name) + 1; - if (base_name && size) + if (Len > size) + { + Len = size; + } + + memcpy(base_name, Name, Len); + + if (Len == LenWithNull) + { + --Len; + } + + return Len; + } + } + + if (base_name) { base_name[0] = '\0'; + } return 0; } + + + /*********************************************************************** * K32GetDeviceDriverBaseNameW (KERNEL32.@) */