Search in sources :

Example 66 with Win32Exception

use of com.sun.jna.platform.win32.Win32Exception in project jna by java-native-access.

the class Advapi32UtilTest method registrySetEmptyValue.

private static void registrySetEmptyValue(HKEY root, String keyPath, String name, final int valueType) {
    HKEYByReference phkKey = new HKEYByReference();
    int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, WinNT.KEY_READ | WinNT.KEY_WRITE, phkKey);
    if (rc != W32Errors.ERROR_SUCCESS) {
        throw new Win32Exception(rc);
    }
    try {
        char[] data = new char[0];
        rc = Advapi32.INSTANCE.RegSetValueEx(phkKey.getValue(), name, 0, valueType, data, 0);
        if (rc != W32Errors.ERROR_SUCCESS) {
            throw new Win32Exception(rc);
        }
    } finally {
        rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue());
        if (rc != W32Errors.ERROR_SUCCESS) {
            throw new Win32Exception(rc);
        }
    }
}
Also used : HKEYByReference(com.sun.jna.platform.win32.WinReg.HKEYByReference)

Example 67 with Win32Exception

use of com.sun.jna.platform.win32.Win32Exception in project jna by java-native-access.

the class WinspoolUtil method getPrinterInfo2.

private static PRINTER_INFO_2[] getPrinterInfo2(int flags) {
    IntByReference pcbNeeded = new IntByReference();
    IntByReference pcReturned = new IntByReference();
    Winspool.INSTANCE.EnumPrinters(flags, null, 2, null, 0, pcbNeeded, pcReturned);
    if (pcbNeeded.getValue() <= 0)
        return new PRINTER_INFO_2[0];
    PRINTER_INFO_2 pPrinterEnum = new PRINTER_INFO_2(pcbNeeded.getValue());
    if (!Winspool.INSTANCE.EnumPrinters(flags, null, 2, pPrinterEnum.getPointer(), pcbNeeded.getValue(), pcbNeeded, pcReturned))
        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
    pPrinterEnum.read();
    return (PRINTER_INFO_2[]) pPrinterEnum.toArray(pcReturned.getValue());
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) PRINTER_INFO_2(com.sun.jna.platform.win32.Winspool.PRINTER_INFO_2)

Example 68 with Win32Exception

use of com.sun.jna.platform.win32.Win32Exception in project jna by java-native-access.

the class WinspoolUtil method getPrinterInfo4.

public static PRINTER_INFO_4[] getPrinterInfo4() {
    IntByReference pcbNeeded = new IntByReference();
    IntByReference pcReturned = new IntByReference();
    Winspool.INSTANCE.EnumPrinters(Winspool.PRINTER_ENUM_LOCAL, null, 4, null, 0, pcbNeeded, pcReturned);
    if (pcbNeeded.getValue() <= 0) {
        return new PRINTER_INFO_4[0];
    }
    PRINTER_INFO_4 pPrinterEnum = new PRINTER_INFO_4(pcbNeeded.getValue());
    if (!Winspool.INSTANCE.EnumPrinters(Winspool.PRINTER_ENUM_LOCAL, null, 4, pPrinterEnum.getPointer(), pcbNeeded.getValue(), pcbNeeded, pcReturned)) {
        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
    }
    pPrinterEnum.read();
    return (PRINTER_INFO_4[]) pPrinterEnum.toArray(pcReturned.getValue());
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) PRINTER_INFO_4(com.sun.jna.platform.win32.Winspool.PRINTER_INFO_4)

Example 69 with Win32Exception

use of com.sun.jna.platform.win32.Win32Exception in project jna by java-native-access.

the class WinspoolUtil method getPrinterInfo2.

public static PRINTER_INFO_2 getPrinterInfo2(String printerName) {
    IntByReference pcbNeeded = new IntByReference();
    IntByReference pcReturned = new IntByReference();
    HANDLEByReference pHandle = new HANDLEByReference();
    if (!Winspool.INSTANCE.OpenPrinter(printerName, pHandle, null))
        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
    Win32Exception we = null;
    PRINTER_INFO_2 pinfo2 = null;
    try {
        Winspool.INSTANCE.GetPrinter(pHandle.getValue(), 2, null, 0, pcbNeeded);
        if (pcbNeeded.getValue() <= 0)
            return new PRINTER_INFO_2();
        pinfo2 = new PRINTER_INFO_2(pcbNeeded.getValue());
        if (!Winspool.INSTANCE.GetPrinter(pHandle.getValue(), 2, pinfo2.getPointer(), pcbNeeded.getValue(), pcReturned))
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        pinfo2.read();
    } catch (Win32Exception e) {
        we = e;
    } finally {
        if (!Winspool.INSTANCE.ClosePrinter(pHandle.getValue())) {
            Win32Exception ex = new Win32Exception(Kernel32.INSTANCE.GetLastError());
            if (we != null) {
                ex.addSuppressed(we);
            }
        }
    }
    if (we != null) {
        throw we;
    }
    return pinfo2;
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) PRINTER_INFO_2(com.sun.jna.platform.win32.Winspool.PRINTER_INFO_2) HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference)

Example 70 with Win32Exception

use of com.sun.jna.platform.win32.Win32Exception in project jna by java-native-access.

the class WinspoolUtil method getJobInfo1.

public static JOB_INFO_1[] getJobInfo1(HANDLEByReference phPrinter) {
    IntByReference pcbNeeded = new IntByReference();
    IntByReference pcReturned = new IntByReference();
    Winspool.INSTANCE.EnumJobs(phPrinter.getValue(), 0, 255, 1, null, 0, pcbNeeded, pcReturned);
    if (pcbNeeded.getValue() <= 0) {
        return new JOB_INFO_1[0];
    }
    JOB_INFO_1 pJobEnum = new JOB_INFO_1(pcbNeeded.getValue());
    if (!Winspool.INSTANCE.EnumJobs(phPrinter.getValue(), 0, 255, 1, pJobEnum.getPointer(), pcbNeeded.getValue(), pcbNeeded, pcReturned)) {
        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
    }
    pJobEnum.read();
    return (JOB_INFO_1[]) pJobEnum.toArray(pcReturned.getValue());
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) JOB_INFO_1(com.sun.jna.platform.win32.Winspool.JOB_INFO_1)

Aggregations

IntByReference (com.sun.jna.ptr.IntByReference)35 HKEYByReference (com.sun.jna.platform.win32.WinReg.HKEYByReference)18 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)17 Memory (com.sun.jna.Memory)15 PointerByReference (com.sun.jna.ptr.PointerByReference)11 ArrayList (java.util.ArrayList)11 EVT_HANDLE (com.sun.jna.platform.win32.Winevt.EVT_HANDLE)7 Pointer (com.sun.jna.Pointer)6 File (java.io.File)6 HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)5 Test (org.junit.Test)5 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)4 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)4 PSID (com.sun.jna.platform.win32.WinNT.PSID)4 Win32Exception (com.sun.jna.platform.win32.Win32Exception)3 HMODULE (com.sun.jna.platform.win32.WinDef.HMODULE)3 LOCALGROUP_INFO_1 (com.sun.jna.platform.win32.LMAccess.LOCALGROUP_INFO_1)2 LOCALGROUP_USERS_INFO_0 (com.sun.jna.platform.win32.LMAccess.LOCALGROUP_USERS_INFO_0)2 DATA_BLOB (com.sun.jna.platform.win32.WinCrypt.DATA_BLOB)2 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)2