com.github.jjYBdx4IL.utils.jna.windows.ProcessList Maven / Gradle / Ivy
The newest version!
/*
* Copyright © 2017 jjYBdx4IL (https://github.com/jjYBdx4IL)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.jjYBdx4IL.utils.jna.windows;
import com.sun.jna.Native;
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.Tlhelp32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinNT;
import com.sun.jna.win32.W32APIOptions;
import java.util.ArrayList;
import java.util.List;
//CHECKSTYLE:OFF
/**
*
* @author jjYBdx4IL
*/
public class ProcessList {
public static class Process {
public int id;
public String exe;
public Process(int id, String exe) {
this.id = id;
this.exe = exe;
}
@Override
public String toString() {
return "Process{" + "id=" + id + ", exe=" + exe + '}';
}
}
public static List get() {
List result = new ArrayList<>();
Kernel32 kernel32 = (Kernel32) Native.loadLibrary(Kernel32.class, W32APIOptions.UNICODE_OPTIONS);
Tlhelp32.PROCESSENTRY32.ByReference processEntry = new Tlhelp32.PROCESSENTRY32.ByReference();
WinNT.HANDLE snapshot = kernel32.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS, new WinDef.DWORD(0));
try {
while (kernel32.Process32Next(snapshot, processEntry)) {
result.add(new Process(processEntry.th32ProcessID.intValue(), Native.toString(processEntry.szExeFile)));
}
} finally {
kernel32.CloseHandle(snapshot);
}
return result;
}
}