I have a requirement to retrieve all modules of a 64bit process in a 32bit WOW process in Windows, EnumProcessModules would fail as described:
If this
Use Windows Management Instrumentation (WMI). Example (Delphi):
function GetProcessCount(const aFileName: string): Integer;
var
lValue: LongWord;
lWMIService: OleVariant;
lWMIItems: OleVariant;
lWMIItem: OleVariant;
lWMIEnum: IEnumVariant;
begin
Result := -1;
lWMIService := GetWMIObject('winmgmts:\\.\root\CIMV2'); { Do not localize. }
if (TVarData(lWMIService).VType = varDispatch) and (TVarData(lWMIService).VDispatch <> nil) then
begin
Result := 0;
lWMIItems := lWMIService.ExecQuery(Format('SELECT * FROM Win32_Process WHERE Name=''%s''', [ExtractFileName(aFileName)])); { Do not localize. }
lWMIEnum := IUnknown(lWMIItems._NewEnum) as IEnumVariant;
while lWMIEnum.Next(1, lWMIItem, lValue) = 0 do
begin
Inc(Result);
end;
end;
end;