Пример #1
0
  /** Given the process handle, waits for its completion and returns the exit code. */
  public static int waitForExitProcess(Pointer hProcess) throws InterruptedException {
    while (true) {
      if (Thread.interrupted()) throw new InterruptedException();

      Kernel32.INSTANCE.WaitForSingleObject(hProcess, 1000);
      IntByReference exitCode = new IntByReference();
      exitCode.setValue(-1);
      Kernel32.INSTANCE.GetExitCodeProcess(hProcess, exitCode);

      int v = exitCode.getValue();
      if (v != Kernel32.STILL_ACTIVE) {
        return v;
      }
    }
  }
Пример #2
0
 public static final HWND createWindowEx(
     final int exStyle,
     final String className,
     final String windowName,
     final int style,
     final int x,
     final int y,
     final int width,
     final int height,
     final HWND parent,
     final HMENU menu,
     final HINSTANCE instance,
     final LPVOID param) {
   final HWND hWnd =
       User32.INSTANCE.CreateWindowEx(
           exStyle,
           new WString(className),
           windowName,
           style,
           x,
           y,
           width,
           height,
           parent,
           menu,
           instance,
           param);
   if (hWnd == null) throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
   return hWnd;
 }
Пример #3
0
 public void testGetUserAccount() {
   USER_INFO_1 userInfo = new USER_INFO_1();
   userInfo.usri1_name = "JNANetapi32TestUser";
   userInfo.usri1_password = "******";
   userInfo.usri1_priv = LMAccess.USER_PRIV_USER;
   // ignore test if not able to add user (need to be administrator to do this).
   if (LMErr.NERR_Success != Netapi32.INSTANCE.NetUserAdd(null, 1, userInfo, null)) {
     return;
   }
   try {
     HANDLEByReference phUser = new HANDLEByReference();
     try {
       assertTrue(
           Advapi32.INSTANCE.LogonUser(
               userInfo.usri1_name.toString(),
               null,
               userInfo.usri1_password.toString(),
               WinBase.LOGON32_LOGON_NETWORK,
               WinBase.LOGON32_PROVIDER_DEFAULT,
               phUser));
       Advapi32Util.Account account = Advapi32Util.getTokenAccount(phUser.getValue());
       assertTrue(account.name.length() > 0);
       assertEquals(userInfo.usri1_name.toString(), account.name);
     } finally {
       if (phUser.getValue() != WinBase.INVALID_HANDLE_VALUE) {
         Kernel32.INSTANCE.CloseHandle(phUser.getValue());
       }
     }
   } finally {
     assertEquals(
         LMErr.NERR_Success, Netapi32.INSTANCE.NetUserDel(null, userInfo.usri1_name.toString()));
   }
 }
Пример #4
0
 public static File getTempDir() {
   Memory buf = new Memory(1024);
   if (Kernel32.INSTANCE.GetTempPathW(1024, buf) != 0) {
     return new File(buf.getString(0, true));
   } else {
     return null;
   }
 }
Пример #5
0
  public void testVariantDate() {
    SYSTEMTIME lpSystemTime = new SYSTEMTIME();
    Kernel32.INSTANCE.GetLocalTime(lpSystemTime);

    DoubleByReference pvtime = new DoubleByReference();
    OleAuto.INSTANCE.SystemTimeToVariantTime(lpSystemTime, pvtime);

    VARIANT variantDate = new VARIANT(new DATE(pvtime.getValue()));
  }
Пример #6
0
 /**
  * @param target If relative, resolved against the location of the symlink. If absolute, it's
  *     absolute.
  * @throws UnsatisfiedLinkError If the function is not exported by kernel32. See
  *     http://msdn.microsoft.com/en-us/library/windows/desktop/aa363866(v=vs.85).aspx for
  *     compatibility info.
  */
 public static void createSymbolicLink(File symlink, String target, boolean dirLink)
     throws IOException {
   if (!Kernel32.INSTANCE.CreateSymbolicLinkW(
       new WString(symlink.getPath()),
       new WString(target),
       dirLink ? Kernel32.SYMBOLIC_LINK_FLAG_DIRECTORY : 0)) {
     throw new WinIOException("Failed to create a symlink " + symlink + " to " + target);
   }
 }
  protected static int getPid(Process p) throws Exception {

    if (Platform.isWindows()) {
      Field f = p.getClass().getDeclaredField("handle");
      f.setAccessible(true);
      int pid = Kernel32.INSTANCE.GetProcessId((Long) f.get(p));
      return pid;

    } else if (Platform.isLinux()) {
      Field f = p.getClass().getDeclaredField("pid");
      f.setAccessible(true);
      int pid = (Integer) f.get(p);
      return pid;

    } else {
      throw new Exception("Cannot currently process pid for " + System.getProperty("os.name"));
    }
  }
Пример #8
0
 public static int getWin32FileAttributes(File file) throws IOException {
   // allow lookup of paths longer than MAX_PATH
   // http://msdn.microsoft.com/en-us/library/aa365247(v=VS.85).aspx
   String canonicalPath = file.getCanonicalPath();
   String path;
   if (canonicalPath.length() < 260) {
     // path is short, use as-is
     path = canonicalPath;
   } else if (canonicalPath.startsWith("\\\\")) {
     // network share
     // \\server\share --> \\?\UNC\server\share
     path = "\\\\?\\UNC\\" + canonicalPath.substring(2);
   } else {
     // prefix, canonical path should be normalized and absolute so this should work.
     path = "\\\\?\\" + canonicalPath;
   }
   return Kernel32.INSTANCE.GetFileAttributesW(new WString(path));
 }
Пример #9
0
 public static final void destroyWindow(final HWND hWnd) {
   if (!User32.INSTANCE.DestroyWindow(hWnd))
     throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
 }
Пример #10
0
 public static final int registerWindowMessage(final String lpString) {
   final int messageId = User32.INSTANCE.RegisterWindowMessage(lpString);
   if (messageId == 0) throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
   return messageId;
 }