Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches

◆ GetModules() [2/2]

static ProcessModuleCollection System.Diagnostics.NtProcessManager.GetModules ( int processId,
bool firstModuleOnly )
inlinestaticprivate

Definition at line 444 of file NtProcessManager.cs.

445 {
446 if (processId == SystemProcessID || processId == 0)
447 {
448 throw new Win32Exception(-2147467259, System.SR.EnumProcessModuleFailed);
449 }
451 try
452 {
453 safeProcessHandle = ProcessManager.OpenProcess(processId, 1040, throwIfExited: true);
454 if (!global::Interop.Kernel32.EnumProcessModules(safeProcessHandle, null, 0, out var needed))
455 {
456 if (!global::Interop.Kernel32.IsWow64Process(global::Interop.Kernel32.GetCurrentProcess(), out var Wow64Process))
457 {
458 throw new Win32Exception();
459 }
460 if (!global::Interop.Kernel32.IsWow64Process(safeProcessHandle, out var Wow64Process2))
461 {
462 throw new Win32Exception();
463 }
465 {
467 }
469 }
470 int num = needed / IntPtr.Size;
471 IntPtr[] array = new IntPtr[num];
472 while (true)
473 {
474 int num2 = needed;
476 if (num2 == needed)
477 {
478 break;
479 }
480 if (needed > num2 && needed / IntPtr.Size > num)
481 {
482 num = needed / IntPtr.Size;
483 array = new IntPtr[num];
484 }
485 }
486 ProcessModuleCollection processModuleCollection = new ProcessModuleCollection(firstModuleOnly ? 1 : num);
487 char[] array2 = ArrayPool<char>.Shared.Rent(260);
488 try
489 {
490 for (int i = 0; i < num && (i <= 0 || !firstModuleOnly); i++)
491 {
493 if (!global::Interop.Kernel32.GetModuleInformation(safeProcessHandle, moduleHandle, out var ntModuleInfo))
494 {
496 continue;
497 }
498 ProcessModule processModule = new ProcessModule
499 {
500 ModuleMemorySize = ntModuleInfo.SizeOfImage,
501 EntryPointAddress = ntModuleInfo.EntryPoint,
502 BaseAddress = ntModuleInfo.BaseOfDll
503 };
504 int num3 = 0;
505 while ((num3 = global::Interop.Kernel32.GetModuleBaseName(safeProcessHandle, moduleHandle, array2, array2.Length)) == array2.Length)
506 {
507 char[] array3 = array2;
508 array2 = ArrayPool<char>.Shared.Rent(num3 * 2);
509 ArrayPool<char>.Shared.Return(array3);
510 }
511 if (num3 == 0)
512 {
513 processModule.Dispose();
515 continue;
516 }
517 processModule.ModuleName = new string(array2, 0, num3);
518 while ((num3 = global::Interop.Kernel32.GetModuleFileNameEx(safeProcessHandle, moduleHandle, array2, array2.Length)) == array2.Length)
519 {
520 char[] array4 = array2;
521 array2 = ArrayPool<char>.Shared.Rent(num3 * 2);
522 ArrayPool<char>.Shared.Return(array4);
523 }
524 if (num3 == 0)
525 {
526 processModule.Dispose();
528 continue;
529 }
530 ReadOnlySpan<char> span = array2.AsSpan(0, num3);
531 if (span.StartsWith("\\\\?\\"))
532 {
533 span = span.Slice("\\\\?\\".Length);
534 }
535 processModule.FileName = span.ToString();
537 }
538 }
539 finally
540 {
541 ArrayPool<char>.Shared.Return(array2);
542 }
544 }
545 finally
546 {
547 if (!safeProcessHandle.IsInvalid)
548 {
549 safeProcessHandle.Dispose();
550 }
551 }
552 }
static readonly SafeProcessHandle InvalidHandle
void Add(TKey key, TValue value)
static void EnumProcessModulesUntilSuccess(SafeProcessHandle processHandle, IntPtr[] modules, int size, out int needed)
static string EnumProcessModuleFailedDueToWow
Definition SR.cs:54
static string EnumProcessModuleFailed
Definition SR.cs:52
Definition SR.cs:7

References System.Collections.Generic.Dictionary< TKey, TValue >.Add(), System.array, System.SR.EnumProcessModuleFailed, System.SR.EnumProcessModuleFailedDueToWow, System.Diagnostics.NtProcessManager.EnumProcessModulesUntilSuccess(), System.Diagnostics.NtProcessManager.HandleLastWin32Error(), Microsoft.Win32.SafeHandles.SafeProcessHandle.InvalidHandle, System.Diagnostics.ProcessManager.OpenProcess(), System.IntPtr.Size, and System.Diagnostics.NtProcessManager.SystemProcessID.