com.sun.jna.platform.win32.WinError Java Examples

The following examples show how to use com.sun.jna.platform.win32.WinError. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConsumeWindowsEventLogTest.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testScheduleError() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
    evtSubscribe = new ConsumeWindowsEventLog(wEvtApi, kernel32);

    when(wEvtApi.EvtSubscribe(isNull(WinNT.HANDLE.class), isNull(WinNT.HANDLE.class), eq(ConsumeWindowsEventLog.DEFAULT_CHANNEL), eq(ConsumeWindowsEventLog.DEFAULT_XPATH),
            isNull(WinNT.HANDLE.class), isNull(WinDef.PVOID.class), isA(EventSubscribeXmlRenderingCallback.class),
            eq(WEvtApi.EvtSubscribeFlags.SUBSCRIBE_TO_FUTURE | WEvtApi.EvtSubscribeFlags.EVT_SUBSCRIBE_STRICT)))
            .thenReturn(null);

    when(kernel32.GetLastError()).thenReturn(WinError.ERROR_ACCESS_DENIED);

    testRunner = TestRunners.newTestRunner(evtSubscribe);

    testRunner.run(1);
    assertEquals(0, getCreatedSessions(testRunner).size());
    verify(wEvtApi, never()).EvtClose(any(WinNT.HANDLE.class));
}
 
Example #2
Source File: ConsumeWindowsEventLogTest.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testScheduleError() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
    evtSubscribe = new ConsumeWindowsEventLog(wEvtApi, kernel32);

    when(wEvtApi.EvtSubscribe(isNull(), isNull(), eq(ConsumeWindowsEventLog.DEFAULT_CHANNEL), eq(ConsumeWindowsEventLog.DEFAULT_XPATH),
            isNull(), isNull(), isA(EventSubscribeXmlRenderingCallback.class),
            eq(WEvtApi.EvtSubscribeFlags.SUBSCRIBE_TO_FUTURE | WEvtApi.EvtSubscribeFlags.EVT_SUBSCRIBE_STRICT)))
            .thenReturn(null);

    when(kernel32.GetLastError()).thenReturn(WinError.ERROR_ACCESS_DENIED);

    testRunner = TestRunners.newTestRunner(evtSubscribe);

    testRunner.run(1);
    assertEquals(0, getCreatedSessions(testRunner).size());
    verify(wEvtApi, never()).EvtClose(any(WinNT.HANDLE.class));
}
 
Example #3
Source File: WindowsNamedPipe.java    From buck with Apache License 2.0 6 votes vote down vote up
@Override
public void write(byte[] b, int off, int len) throws IOException {
  Pointer lpOverlapped = createOverlapped(writerWaitable).getPointer();
  boolean immediate =
      api.WriteFile(pipeHandle, ByteBuffer.wrap(b, off, len), len, null, lpOverlapped);
  if (!immediate && api.GetLastError() != WinError.ERROR_IO_PENDING) {
    throw new IOException("WriteFile() failed. WinError: " + api.GetLastError());
  }
  IntByReference written = new IntByReference();
  // wait = true, blocked until data is written
  if (!api.GetOverlappedResult(pipeHandle, lpOverlapped, written, true)) {
    throw new IOException("GetOverlappedResult() failed for write operation");
  }
  if (written.getValue() != len) {
    throw new IOException("WriteFile() wrote less bytes than requested");
  }
}
 
Example #4
Source File: ErrorLookup.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
public String getLastError() {
    int lastError = kernel32.GetLastError();
    return Arrays.stream(WinError.class.getDeclaredFields()).filter(field -> {
        try {
            return Modifier.isStatic(field.getModifiers())
                    && field.getType() == int.class
                    && field.getName().startsWith("ERROR")
                    && (int) field.get(null) == lastError;
        } catch (IllegalAccessException e) {
            return false;
        }
    }).map(field -> field.getName() + "(" + lastError + ")")
            .findFirst()
            .orElse(Integer.toString(lastError));
}
 
Example #5
Source File: BackportWindowsNegotiateScheme.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
String getToken(
    final Sspi.CtxtHandle continueCtx,
    final Sspi.SecBufferDesc continueToken,
    final String targetName) {
    final IntByReference attr = new IntByReference();
    final ManagedSecBufferDesc token = new ManagedSecBufferDesc(
        Sspi.SECBUFFER_TOKEN, Sspi.MAX_TOKEN_SIZE);

    sspiContext = new Sspi.CtxtHandle();
    final int rc = Secur32.INSTANCE.InitializeSecurityContext(clientCred,
        continueCtx, targetName, Sspi.ISC_REQ_DELEGATE | Sspi.ISC_REQ_MUTUAL_AUTH, 0,
        Sspi.SECURITY_NATIVE_DREP, continueToken, 0, sspiContext, token,
        attr, null);
    switch(rc) {
        case WinError.SEC_I_CONTINUE_NEEDED:
            continueNeeded = true;
            break;
        case WinError.SEC_E_OK:
            dispose(); // Don't keep the context
            continueNeeded = false;
            break;
        default:
            dispose();
            throw new Win32Exception(rc);
    }
    return Base64.encodeBase64String(token.getBuffer(0).getBytes());
}
 
Example #6
Source File: ErrorLookup.java    From nifi with Apache License 2.0 5 votes vote down vote up
public String getLastError() {
    int lastError = kernel32.GetLastError();
    return Arrays.stream(WinError.class.getDeclaredFields()).filter(field -> {
        try {
            return Modifier.isStatic(field.getModifiers())
                    && field.getType() == int.class
                    && field.getName().startsWith("ERROR")
                    && (int) field.get(null) == lastError;
        } catch (IllegalAccessException e) {
            return false;
        }
    }).map(field -> field.getName() + "(" + lastError + ")")
            .findFirst()
            .orElse(Integer.toString(lastError));
}
 
Example #7
Source File: ErrorLookupTest.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testErrorLookupExists() {
    when(kernel32.GetLastError()).thenReturn(WinError.ERROR_INSUFFICIENT_BUFFER);
    assertEquals("ERROR_INSUFFICIENT_BUFFER(" + WinError.ERROR_INSUFFICIENT_BUFFER + ")", errorLookup.getLastError());
}
 
Example #8
Source File: FirstInstance.java    From jpexs-decompiler with GNU General Public License v3.0 4 votes vote down vote up
private static boolean isRunning() {
    if (Platform.isWindows()) {
        mutex = Kernel32.INSTANCE.CreateMutex(null, false, MUTEX_NAME);
        if (mutex == null) {
            return false;
        }
        int er = Kernel32.INSTANCE.GetLastError();
        if (er == WinError.ERROR_ALREADY_EXISTS) {
            return true;
        }

        new Thread("OtherInstanceCommunicator") {
            @Override
            public void run() {
                while (true) {
                    try (PipeInputStream pis = new PipeInputStream(PIPE_NAME, true)) {
                        ObjectInputStream ois = new ObjectInputStream(pis);
                        String app = ois.readUTF();
                        if (app.equals(PIPE_APP_CODE)) {
                            int major = ois.readInt();
                            int minor = ois.readInt();
                            int release = ois.readInt();
                            int build = ois.readInt();
                            int pipeMajor = ois.readInt();
                            int pipeMinor = ois.readInt();

                            if (pipeMajor == PIPE_MAJOR) {
                                String command = ois.readUTF();
                                switch (command) {
                                    case "open":
                                        int cnt = ois.readInt();
                                        String[] fileNames = new String[cnt];
                                        for (int i = 0; i < cnt; i++) {
                                            fileNames[i] = ois.readUTF();
                                        }

                                        View.execInEventDispatch(() -> {
                                            for (int i = 0; i < cnt; i++) {
                                                Main.openFile(fileNames[i], null);
                                            }
                                        });
                                    //no break - focus too
                                    case "focus":

                                        View.execInEventDispatch(new Runnable() {
                                            @Override
                                            public void run() {
                                                Window wnd = Main.getMainFrame().getWindow();
                                                wnd.setAlwaysOnTop(true);
                                                wnd.toFront();
                                                wnd.requestFocus();
                                                wnd.setAlwaysOnTop(false);
                                                wnd.repaint();
                                            }
                                        });
                                        break;
                                }
                            }

                        }
                    } catch (IOException ex) {
                        //ignore
                    }
                }
            }
        }.start();

    }
    return false;
}
 
Example #9
Source File: ErrorLookupTest.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testErrorLookupExists() {
    when(kernel32.GetLastError()).thenReturn(WinError.ERROR_INSUFFICIENT_BUFFER);
    assertEquals("ERROR_INSUFFICIENT_BUFFER(" + WinError.ERROR_INSUFFICIENT_BUFFER + ")", errorLookup.getLastError());
}