Advertisement
Guest User

ExternalModuleLocator

a guest
May 24th, 2017
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.28 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Runtime.InteropServices;
  3. using WinAPI.Kernel32;
  4.  
  5. namespace Injector
  6. {
  7.     public class ExternalModuleLocator
  8.     {
  9.         private readonly CreateToolhelp32SnapshotDelegate _createToolhelp32Snapshot;
  10.         private readonly Module32FirstDelegate _module32First;
  11.         private readonly Module32NextDelegate _module32Next;
  12.  
  13.         public ExternalModuleLocator(CreateToolhelp32SnapshotDelegate createToolhelp32Snapshot, Module32FirstDelegate module32First, Module32NextDelegate module32Next)
  14.         {
  15.             _createToolhelp32Snapshot = createToolhelp32Snapshot;
  16.             _module32First = module32First;
  17.             _module32Next = module32Next;
  18.         }
  19.        
  20.         public IEnumerable<Moduleentry32> EnumProcessModules(uint procId)
  21.         {
  22.             var snapshot = _createToolhelp32Snapshot(SnapshotFlags.Module | SnapshotFlags.Module32, procId);
  23.             var mod = new Moduleentry32() { dwSize = (uint)Marshal.SizeOf(typeof(Moduleentry32)) };
  24.             if (!_module32First(snapshot, ref mod))
  25.             {
  26.                 yield break;
  27.             }
  28.             do
  29.             {
  30.                 yield return mod;
  31.             }
  32.             while (_module32Next(snapshot, ref mod));
  33.         }
  34.  
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement