선언:
C#
[DllImport("kernel32")]
public static extern IntPtr CreateToolhelp32Snapshot(Int32 dwFlags, Int32 th32ProcessID);
VB.NET
<DllImport("kernel32")> _
Public Shared Function CreateToolhelp32Snapshot(dwFlags As Int32, th32ProcessID As Int32) As IntPtr
End Function
사용 예제:
C#
using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
namespace ApiReference {
public class ApiReference {
[DllImport("kernel32")]
public static extern IntPtr CreateToolhelp32Snapshot(Int32 dwFlags, Int32 th32ProcessID);
[DllImport("kernel32")]
public static extern void CloseHandle(IntPtr hObject);
[DllImport("kernel32")]
public static extern Int32 Process32First(IntPtr hSnapshot, ref PROCESSENTRY32 pe32);
[DllImport("kernel32")]
public static extern Int32 Process32Next(IntPtr hSnapshot, ref PROCESSENTRY32 pe32);
public const Int32 MAX_PATH = 260;
public const Int32 TH32CS_SNAPPROCESS = 2;
[StructLayout(LayoutKind.Sequential)]
public struct PROCESSENTRY32 {
public Int32 dwSize;
public Int32 cntUsage;
public Int32 th32ProcessID;
public IntPtr th32DefaultHeapID;
public Int32 th32ModuleID;
public Int32 cntThreads;
public Int32 th32ParentProcessID;
public Int32 pcPriClassBase;
public Int32 dwFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)]
public String szExeFile;
public static Int32 Size {
get { return Marshal.SizeOf(typeof(PROCESSENTRY32)); }
}
}
public static void Main(String[] args) {
Console.WriteLine("프로세스 목록을 가져오는 중입니다!");
List<PROCESSENTRY32> processes = EnumProcesses();
if ( processes != null ) {
foreach ( PROCESSENTRY32 pe32 in processes ) {
Console.WriteLine("{0,-5}\t{1,-5}\t{2}", pe32.th32ProcessID, pe32.th32ParentProcessID, pe32.szExeFile);
}
} else
Console.WriteLine("목록 정보 가져오기 실패!");
Console.ReadKey(true);
}
public static List<PROCESSENTRY32> EnumProcesses() {
IntPtr hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if ( hSnapshot == IntPtr.Zero )
return null;
PROCESSENTRY32 pe32 = new PROCESSENTRY32();
pe32.dwSize = PROCESSENTRY32.Size;
if ( Process32First(hSnapshot, ref pe32) == 0 ) {
CloseHandle(hSnapshot);
return null;
}
List<PROCESSENTRY32> lstProcesses = new List<PROCESSENTRY32>();
do {
lstProcesses.Add(pe32);
} while ( Process32Next(hSnapshot, ref pe32) != 0 );
CloseHandle(hSnapshot);
return lstProcesses;
}
}
}
VB.NET
Imports System
Imports System.Runtime.InteropServices
Imports System.Collections.Generic
Namespace ApiReference
Public Class ApiReference
<DllImport("kernel32")> _
Public Shared Function CreateToolhelp32Snapshot(dwFlags As Int32, th32ProcessID As Int32) As IntPtr
End Function
<DllImport("kernel32")> _
Public Shared Sub CloseHandle(hObject As IntPtr)
End Sub
<DllImport("kernel32")> _
Public Shared Function Process32First(hSnapshot As IntPtr, ByRef pe32 As PROCESSENTRY32) As Int32
End Function
<DllImport("kernel32")> _
Public Shared Function Process32Next(hSnapshot As IntPtr, ByRef pe32 As PROCESSENTRY32) As Int32
End Function
Public Const MAX_PATH As Int32 = 260
Public Const TH32CS_SNAPPROCESS As Int32 = 2
<StructLayout(LayoutKind.Sequential)> _
Public Structure PROCESSENTRY32
Public dwSize As Int32
Public cntUsage As Int32
Public th32ProcessID As Int32
Public th32DefaultHeapID As IntPtr
Public th32ModuleID As Int32
Public cntThreads As Int32
Public th32ParentProcessID As Int32
Public pcPriClassBase As Int32
Public dwFlags As Int32
<MarshalAs(UnmanagedType.ByValTStr, SizeConst := MAX_PATH)> _
Public szExeFile As [String]
Public Shared ReadOnly Property Size() As Int32
Get
Return Marshal.SizeOf(GetType(PROCESSENTRY32))
End Get
End Property
End Structure
Public Shared Sub Main(args As [String]())
Console.WriteLine("프로세스 목록을 가져오는 중입니다!")
Dim processes As List(Of PROCESSENTRY32) = EnumProcesses()
If processes IsNot Nothing Then
For Each pe32 As PROCESSENTRY32 In processes
Console.WriteLine("{0,-5}" & vbTab & "{1,-5}" & vbTab & "{2}", pe32.th32ProcessID, pe32.th32ParentProcessID, pe32.szExeFile)
Next
Else
Console.WriteLine("목록 정보 가져오기 실패!")
End If
Console.ReadKey(True)
End Sub
Public Shared Function EnumProcesses() As List(Of PROCESSENTRY32)
Dim hSnapshot As IntPtr = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If hSnapshot = IntPtr.Zero Then
Return Nothing
End If
Dim pe32 As New PROCESSENTRY32()
pe32.dwSize = PROCESSENTRY32.Size
If Process32First(hSnapshot, pe32) = 0 Then
CloseHandle(hSnapshot)
Return Nothing
End If
Dim lstProcesses As New List(Of PROCESSENTRY32)()
Do
lstProcesses.Add(pe32)
Loop While Process32Next(hSnapshot, pe32) <> 0
CloseHandle(hSnapshot)
Return lstProcesses
End Function
End Class
End Namespace
예제 실행 결과:
매개 변수 설명:
dwFlags - 가져올 스냅샷 정보를 입력합니다.
th32ProcessID - 스냅샷을 가져올 프로세스의 식별자를 입력합니다.
API 설명:
지정된 프로세스 혹은 시스템의 지정된 정보의 스냅샷을 생성합니다.
참고:
CreateToolhelp32Snapshot (MSDN)
비고:
TH32CS_SNAPHEAPLIST, TH32CS_SNAPMODULE, TH32CS_SNAPMODULE32, TH32CS_SNAPALL 이 4개의
옵션을 입력한 경우에만 th32ProcessID 매개 변수가 유효합니다. 그 외의 경우엔 th32ProcessID 매개 변수에
대개 0을 입력합니다.
또한, TH32CS_SNAPMODULE32 옵션의 경우 64비트 윈도우에서만 사용이 가능합니다.
'API Reference' 카테고리의 다른 글
10. WindowFromPoint (0) | 2014.09.19 |
---|---|
9. Process32First, Process32Next (0) | 2014.09.18 |
7. CloseHandle (0) | 2014.09.11 |
6. IsWow64Process (0) | 2014.09.10 |
5. TerminateProcess (0) | 2014.09.09 |