Search in sources :

Example 66 with Kernel32

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);
}
Also used : HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 67 with Kernel32

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;
        }
    }
}
Also used : ArrayList(java.util.ArrayList) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 68 with Kernel32

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());
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) PRINTER_INFO_1(com.sun.jna.platform.win32.Winspool.PRINTER_INFO_1)

Example 69 with Kernel32

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;
}
Also used : SERVICE_STATUS_PROCESS(com.sun.jna.platform.win32.Winsvc.SERVICE_STATUS_PROCESS) IntByReference(com.sun.jna.ptr.IntByReference)

Example 70 with Kernel32

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());
    }
}
Also used : SERVICE_FAILURE_ACTIONS_FLAG(com.sun.jna.platform.win32.Winsvc.SERVICE_FAILURE_ACTIONS_FLAG)

Aggregations

HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)74 IntByReference (com.sun.jna.ptr.IntByReference)47 File (java.io.File)33 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)30 SC_HANDLE (com.sun.jna.platform.win32.Winsvc.SC_HANDLE)27 HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)26 Memory (com.sun.jna.Memory)25 PointerByReference (com.sun.jna.ptr.PointerByReference)14 Pointer (com.sun.jna.Pointer)12 EVT_HANDLE (com.sun.jna.platform.win32.Winevt.EVT_HANDLE)7 Test (org.junit.Test)7 SYSTEMTIME (com.sun.jna.platform.win32.WinBase.SYSTEMTIME)6 ArrayList (java.util.ArrayList)6 FILETIME (com.sun.jna.platform.win32.WinBase.FILETIME)5 HMODULE (com.sun.jna.platform.win32.WinDef.HMODULE)5 PSID (com.sun.jna.platform.win32.WinNT.PSID)5 TOKEN_PRIVILEGES (com.sun.jna.platform.win32.WinNT.TOKEN_PRIVILEGES)5 WString (com.sun.jna.WString)4 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)4 ShortByReference (com.sun.jna.ptr.ShortByReference)4