2012-05-14 3 views
0

시스템 프로세스가로드 한 모듈의 파일 이름을 가져오고 싶습니다.시스템 프로세스 모듈 파일 이름 가져 오기

I 따라서 제) (

System.Diagnostics.Process.EnterDebugMode 실행;

디버그 모드로 들어가서 SEDebugPrivilege를 가져옵니다. 내가 뭘 잘못

그러나 Module32First 기능이 항상 실패

...

?

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Runtime.InteropServices; 

{ 
    public static class Toolhelp32 
    { 
     public const uint TH32CS_SNAPHEAPLIST = 0x00000001; 
     public const uint TH32CS_SNAPPROCESS = 0x00000002; 
     public const uint TH32CS_SNAPTHREAD = 0x00000004; 
     public const uint TH32CS_SNAPMODULE = 0x00000008; 
     public const uint TH32CS_SNAPMODULE32 = 0x00000010; 
     public const uint TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST | 
               TH32CS_SNAPPROCESS | 
               TH32CS_SNAPTHREAD | 
               TH32CS_SNAPMODULE); 
     public const uint TH32CS_INHERIT = 0x80000000; 

     public const uint HF32_DEFAULT = 1; 
     public const uint HF32_SHARED = 2; 

     public const uint LF32_FIXED = 0x00000001; 
     public const uint LF32_FREE = 0x00000002; 
     public const uint LF32_MOVEABLE = 0x00000004; 

     public struct HEAPLIST32 
     { 
      public uint dwSize; 
      public uint th32ProcessID; 
      public uint th32HeapID; 
      public uint dwFlags; 
     } 

     public struct HEAPENTRY32 
     { 
      public uint dwSize; 
      public IntPtr hHandle; 
      public uint dwAddress; 
      public uint dwBlockSize; 
      public uint dwFlags; 
      public uint dwLockCount; 
      public uint dwResvd; 
      public uint th32ProcessID; 
      public uint th32HeapID; 
     } 


     public struct PROCESSENTRY32W 
     { 
      public uint dwSize; 
      public uint cntUsage; 
      public uint th32ProcessID; 
      public UIntPtr th32DefaultHeapID; 
      public uint th32ModuleID; 
      public uint cntThreads; 
      public uint th32ParentProcessID; 
      public int pcPriClassBase; 
      public uint dwFlags; 
      public string szExeFile; 
     } 

     public struct PROCESSENTRY32 
     { 
      public uint dwSize; 
      public uint cntUsage; 
      public uint th32ProcessID; 
      public uint th32DefaultHeapID; 
      public uint th32ModuleID; 
      public uint cntThreads; 
      public uint th32ParentProcessID; 
      public int pcPriClassBase; 
      public uint dwFlags; 
      public string szExeFile; 
     } 

     public struct THREADENTRY32 
     { 
      public uint dwSize; 
      public uint cntUsage; 
      public uint th32ThreadID; 
      public uint th32OwnerProcessID; 
      public int tpBasePri; 
      public int tpDeltaPri; 
      public uint dwFlags; 
     } 

     public struct MODULEENTRY32W 
     { 
      public uint dwSize; 
      public uint th32ModuleID; 
      public uint th32ProcessID; 
      public uint GlblcntUsage; 
      public uint ProccntUsage; 
      public IntPtr modBaseAddr; 
      public uint modBaseSize; 
      public IntPtr hModule; 
      [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] 
      public string szModule; 
      [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] 
      public string szExePath; 
     } 

     public struct MODULEENTRY32 
     { 
      public uint dwSize; 
      public uint th32ModuleID; 
      public uint th32ProcessID; 
      public uint GlblcntUsage; 
      public uint ProccntUsage; 
      public IntPtr modBaseAddr; 
      public uint modBaseSize; 
      public IntPtr hModule; 
      public string szModule; 
      public string szExePath; 
     } 

     [DllImport("kernel32.dll")] 
     public static extern IntPtr CreateToolhelp32Snapshot(uint dwFlags, uint th32ProcessID); 

     [DllImport("kernel32.dll", SetLastError = true)] 
     [return: MarshalAs(UnmanagedType.Bool)] 
     internal static extern bool CloseHandle(IntPtr hSnapshot); 

     [DllImport("kernel32.dll")] 
     public static extern bool Heap32ListFirst(IntPtr hSnapshot, ref HEAPLIST32 lphl); 

     [DllImport("kernel32.dll")] 
     public static extern bool Heap32ListNext(IntPtr hSnapshot, ref HEAPLIST32 lphl); 

     [DllImport("kernel32.dll")] 
     public static extern bool Heap32First(IntPtr hSnapshot, ref HEAPENTRY32 lphe, 
      uint th32ProcessID, uint th32HeapID); 

     [DllImport("kernel32.dll")] 
     public static extern bool Heap32Next(IntPtr hSnapshot, ref HEAPENTRY32 lphe); 

     [DllImport("kernel32.dll")] 
     public static extern bool Toolhelp32ReadProcessMemory(uint th32ProcessID, 
      IntPtr lpBaseAddress, IntPtr lpBuffer, uint cbRead, IntPtr lpNumberOfBytesRead); 

     [DllImport("kernel32.dll")] 
     public static extern bool Process32FirstW(IntPtr hSnapshot, ref PROCESSENTRY32W lppe); 

     [DllImport("kernel32.dll")] 
     public static extern bool Process32NextW(IntPtr hSnapshot, ref PROCESSENTRY32W lppe); 

     [DllImport("kernel32.dll")] 
     public static extern bool Process32First(IntPtr hSnapshot, ref PROCESSENTRY32 lppe); 

     [DllImport("kernel32.dll")] 
     public static extern bool Process32Next(IntPtr hSnapshot, ref PROCESSENTRY32 lppe); 

     [DllImport("kernel32.dll")] 
     public static extern bool Thread32First(IntPtr hSnapshot, ref THREADENTRY32 lpte); 

     [DllImport("kernel32.dll")] 
     public static extern bool Thread32Next(IntPtr hSnapshot, ref THREADENTRY32 lpte); 

     [DllImport("kernel32.dll")] 
     public static extern bool Module32FirstW(IntPtr hSnapshot, ref MODULEENTRY32W lpme); 

     [DllImport("kernel32.dll")] 
     public static extern bool Module32NextW(IntPtr hSnapshot, ref MODULEENTRY32W lpme); 

     [DllImport("kernel32.dll")] 
     public static extern bool Module32First(IntPtr hSnapshot, ref MODULEENTRY32W lpme); 

     [DllImport("kernel32.dll")] 
     public static extern bool Module32Next(IntPtr hSnapshot, ref MODULEENTRY32W lpme); 
    } 
} 

class GetProcessModuleFilenamesHelper 
    { 
     const int INVALID_HANDLE_VALUE = -1; 
     const int ERROR_BAD_LENGTH = 0x18; 
     [DllImport("kernel32.dll", CharSet = CharSet.Auto)] 
     public static extern int GetModuleFileName(HandleRef hModule, StringBuilder buffer, int length); 

     public static List<Toolhelp32.MODULEENTRY32W> GetProcessModuleFilenames(int ProcId) 
     { 
      List<Toolhelp32.MODULEENTRY32W> lst = new List<Toolhelp32.MODULEENTRY32W>(); 

      IntPtr hModuleSnapshot = new IntPtr(ERROR_BAD_LENGTH); 
      while (hModuleSnapshot == new IntPtr(ERROR_BAD_LENGTH)) 
      { 
       hModuleSnapshot = Toolhelp32.CreateToolhelp32Snapshot(Toolhelp32.TH32CS_SNAPMODULE32|Toolhelp32.TH32CS_SNAPMODULE, (uint)ProcId); 
       System.Windows.Forms.Application.DoEvents(); 
      } 

      if (hModuleSnapshot != new IntPtr(INVALID_HANDLE_VALUE)) 
      { 
       Toolhelp32.MODULEENTRY32W ModuleList = new Toolhelp32.MODULEENTRY32W(); 

       ModuleList.dwSize = (uint)Marshal.SizeOf(ModuleList); 

       if (Toolhelp32.Module32First(hModuleSnapshot, ref ModuleList)) 
       { 
        do 
        { 

         Toolhelp32.MODULEENTRY32W ModuleEntry = new Toolhelp32.MODULEENTRY32W(); 
         ModuleEntry.dwSize = (uint)Marshal.SizeOf(ModuleEntry);                  

         /* 
         if (Toolhelp32.Heap32First(hHeapSnapshot, ref HeapEntry, HeapList.th32ProcessID, HeapList.th32HeapID)) 
         { 
          do 
          { 
           MemUsage += HeapEntry.dwBlockSize; 
          } while (Toolhelp32.Heap32Next(hHeapSnapshot, ref HeapEntry)); 
         } 
         */ 

         /* 
         StringBuilder buffer = new StringBuilder(260); 
         GetModuleFileName(ModuleEntry.hModule, buffer, buffer.Capacity); 
         string str = Path.GetFullPath(buffer.ToString()); 
         str=str.Substring(0, str.LastIndexOf('\\')); 
         */ 
         lst.Add(ModuleEntry); 

        } while (Toolhelp32.Module32Next(hModuleSnapshot, ref ModuleList)); 


       } 

       int lasterr = Marshal.GetLastWin32Error(); 
       //System.Windows.Forms.MessageBox.Show(Marshal.GetLastWin32Error().ToString()); 

       Toolhelp32.CloseHandle(hModuleSnapshot); 
      } 

      return lst; 
     } 
    } 
+0

GetLastError에서 어떤 오류가 발생합니까? btw. 왜 visual-C++ 태그입니까? –

+0

ProcId에 대해 무엇을 전달합니까? –

+0

오류가 발생했습니다. List에 ModuleList를 추가해야합니다. ModuleEntry가 필요하지 않습니다. 나는 아직도 시스템 프로세스에 의해 잠긴 모듈을 얻지 못한다. – alexandertr

답변

0

나는 오류가 발생했습니다. List에 ModuleList를 추가해야합니다.