use of com.sun.jna.platform.win32.Kernel32 in project jna by java-native-access.
the class AbstractWin32TestSupport method killProcessByName.
public static void killProcessByName(String filename) {
HANDLE hSnapShot = Kernel32.INSTANCE.CreateToolhelp32Snapshot(TH32CS_SNAPALL, null);
Tlhelp32.PROCESSENTRY32 process = new Tlhelp32.PROCESSENTRY32();
boolean hRes = Kernel32.INSTANCE.Process32First(hSnapShot, process);
while (hRes) {
String imageName = Native.toString(process.szExeFile);
if (imageName.equalsIgnoreCase(filename)) {
HANDLE hProcess = Kernel32.INSTANCE.OpenProcess(Kernel32.PROCESS_TERMINATE, false, process.th32ProcessID.intValue());
if (hProcess != null) {
Kernel32.INSTANCE.TerminateProcess(hProcess, 9);
Kernel32.INSTANCE.CloseHandle(hProcess);
}
}
hRes = Kernel32.INSTANCE.Process32Next(hSnapShot, process);
}
Kernel32.INSTANCE.CloseHandle(hSnapShot);
}
use of com.sun.jna.platform.win32.Kernel32 in project jna by java-native-access.
the class Kernel32Util method getModules.
/**
* Returns all the executable modules for a given process ID.<br>
*
* @param processID
* The process ID to get executable modules for
* @return All the modules in the process.
*/
public static List<Tlhelp32.MODULEENTRY32W> getModules(int processID) {
HANDLE snapshot = Kernel32.INSTANCE.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPMODULE, new DWORD(processID));
if (snapshot == null) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
Win32Exception we = null;
try {
Tlhelp32.MODULEENTRY32W first = new Tlhelp32.MODULEENTRY32W();
if (!Kernel32.INSTANCE.Module32FirstW(snapshot, first)) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
List<Tlhelp32.MODULEENTRY32W> modules = new ArrayList<Tlhelp32.MODULEENTRY32W>();
modules.add(first);
Tlhelp32.MODULEENTRY32W next = new Tlhelp32.MODULEENTRY32W();
while (Kernel32.INSTANCE.Module32NextW(snapshot, next)) {
modules.add(next);
next = new Tlhelp32.MODULEENTRY32W();
}
int lastError = Kernel32.INSTANCE.GetLastError();
// or if something went wrong.
if (lastError != W32Errors.ERROR_SUCCESS && lastError != W32Errors.ERROR_NO_MORE_FILES) {
throw new Win32Exception(lastError);
}
return modules;
} catch (Win32Exception e) {
we = e;
// re-throw so finally block is executed
throw we;
} finally {
try {
closeHandle(snapshot);
} catch (Win32Exception e) {
if (we == null) {
we = e;
} else {
we.addSuppressed(e);
}
}
if (we != null) {
throw we;
}
}
}
use of com.sun.jna.platform.win32.Kernel32 in project jna by java-native-access.
the class WinspoolUtil method getPrinterInfo1.
public static PRINTER_INFO_1[] getPrinterInfo1() {
IntByReference pcbNeeded = new IntByReference();
IntByReference pcReturned = new IntByReference();
Winspool.INSTANCE.EnumPrinters(Winspool.PRINTER_ENUM_LOCAL, null, 1, null, 0, pcbNeeded, pcReturned);
if (pcbNeeded.getValue() <= 0) {
return new PRINTER_INFO_1[0];
}
PRINTER_INFO_1 pPrinterEnum = new PRINTER_INFO_1(pcbNeeded.getValue());
if (!Winspool.INSTANCE.EnumPrinters(Winspool.PRINTER_ENUM_LOCAL, null, 1, pPrinterEnum.getPointer(), pcbNeeded.getValue(), pcbNeeded, pcReturned)) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
pPrinterEnum.read();
return (PRINTER_INFO_1[]) pPrinterEnum.toArray(pcReturned.getValue());
}
use of com.sun.jna.platform.win32.Kernel32 in project jna by java-native-access.
the class W32Service method queryStatus.
/**
* Retrieves the current status of the specified service based on the specified information level.
* @return
* Service status information
*/
public SERVICE_STATUS_PROCESS queryStatus() {
IntByReference size = new IntByReference();
Advapi32.INSTANCE.QueryServiceStatusEx(_handle, SC_STATUS_TYPE.SC_STATUS_PROCESS_INFO, null, 0, size);
SERVICE_STATUS_PROCESS status = new SERVICE_STATUS_PROCESS(size.getValue());
if (!Advapi32.INSTANCE.QueryServiceStatusEx(_handle, SC_STATUS_TYPE.SC_STATUS_PROCESS_INFO, status, status.size(), size)) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
return status;
}
use of com.sun.jna.platform.win32.Kernel32 in project jna by java-native-access.
the class W32Service method setFailureActionsFlag.
/**
* Set the failure action flag of the specified service. Corresponds to
* <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms681988.aspx">ChangeServiceConfig2</a>
* with parameter dwInfoLevel set to SERVICE_CONFIG_FAILURE_ACTIONS_FLAG.
*/
public void setFailureActionsFlag(boolean flagValue) {
SERVICE_FAILURE_ACTIONS_FLAG flag = new SERVICE_FAILURE_ACTIONS_FLAG();
flag.fFailureActionsOnNonCrashFailures = flagValue ? 1 : 0;
if (!Advapi32.INSTANCE.ChangeServiceConfig2(_handle, Winsvc.SERVICE_CONFIG_FAILURE_ACTIONS_FLAG, flag)) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
}
Aggregations