Beispiel #1
0
 /**
  * Retrieve the PassThru device firmware version, DLL version, and the J2534 implementation
  * version.
  *
  * @param deviceId - of PassThru device
  * @return an instance of <b>Version</b>
  * @see Version
  */
 public Version readVersion(int deviceId) {
   ByteBuffer pFirmwareVersion = ByteBuffer.allocate(80);
   ByteBuffer pDllVersion = ByteBuffer.allocate(80);
   ByteBuffer pApiVersion = ByteBuffer.allocate(80);
   NativeLong ret =
       lib.PassThruReadVersion(
           new NativeLong(deviceId), pFirmwareVersion, pDllVersion, pApiVersion);
   if (ret.intValue() != Status.NOERROR.getValue())
     handleError("PassThruReadVersion", ret.intValue());
   return new Version(
       Native.toString(pFirmwareVersion.array()),
       Native.toString(pDllVersion.array()),
       Native.toString(pApiVersion.array()));
 }
Beispiel #2
0
    public WindowInfo(HWND hWnd, boolean isChild) {
      byte[] buffer = new byte[1024];
      User32.instance.GetWindowTextA(hWnd, buffer, buffer.length);
      title = Native.toString(buffer);

      char[] buffer2 = new char[1026];
      User32.instance.GetClassName(hWnd, buffer2, 1026);
      className = Native.toString(buffer2);

      rect = new RECT();
      User32.instance.GetWindowRect(hWnd, rect);

      this.isChild = isChild;
    }
  /**
   * Uses JNA to retrieve the module file name of a program and parses it as a string which is then
   * passed to the InterruptionLogic class.
   */
  private void getProgramIdentifier() {

    PSAPI psapi = (PSAPI) Native.loadLibrary("psapi", PSAPI.class);

    HWND focusedWindow = User32.INSTANCE.GetForegroundWindow();
    byte[] name = new byte[1024];

    IntByReference pid = new IntByReference();
    User32.INSTANCE.GetWindowThreadProcessId(focusedWindow, pid);

    System.out.println("pid = " + pid.getValue());

    HANDLE process = Kernel32.INSTANCE.OpenProcess(0x0400 | 0x0010, false, pid.getValue());

    // Initally I was using the 'GetWindowModuleFileName' which provided
    // erronous behaviour. 'technomage' on Stackoverflow pointed out that
    // the method that should be used is 'GetModuleFileNameEx' -
    // http://stackoverflow.com/questions/15693210/getmodulefilename-for-window-in-focus-jna-windows-os

    psapi.GetModuleFileNameExA(process, null, name, 1024);
    String nameString = Native.toString(name);

    System.out.println(nameString);

    interruptDec.interruptNow(nameString);
  }
  public static void main(String[] args) {
    // get the taskbar's window handle
    HWND shellTrayHwnd = User32.instance.FindWindow(User32.SHELL_TRAY_WND, null);

    // use it to minimize all windows
    User32.instance.SendMessageA(shellTrayHwnd, User32.WM_COMMAND, User32.MIN_ALL, 0);

    // Test
    // System.out.println(shellTrayHwnd);
    // System.out.println(User32.WM_COMMAND);

    // sleep for 3 seconds
    try {
      Thread.sleep(3000);
    } catch (InterruptedException e) {
    }

    // then restore previously minimized windows
    User32.instance.SendMessageA(shellTrayHwnd, User32.WM_COMMAND, User32.MIN_ALL_UNDO, 0);
    byte[] windowText = new byte[512];
    PointerType hwnd = User32.INSTANCE.GetForegroundWindow();
    // then you can call it!
    User32.INSTANCE.GetWindowTextA(hwnd, windowText, 512);
    System.out.println(Native.toString(windowText));
    // System.out.println(User32.INSTANCE.GetForegroundWindow());

  }
Beispiel #5
0
 public void testSHGetFolderPath() {
   char[] pszPath = new char[WinDef.MAX_PATH];
   assertEquals(
       W32Errors.S_OK,
       Shell32.INSTANCE.SHGetFolderPath(
           null, ShlObj.CSIDL_PROGRAM_FILES, null, ShlObj.SHGFP_TYPE_CURRENT, pszPath));
   assertTrue(Native.toString(pszPath).length() > 0);
 }
Beispiel #6
0
 /**
  * Retrieve the text description for the most recent non-zero error and throw an exception.
  *
  * @param operation - string containing the name of the method for which the error occurred
  * @param status - the method's numeric error value
  * @exception J2534Exception on various non-zero return status
  * @see J2534_v0404
  */
 private static void handleError(String operation, int status) {
   ByteBuffer error = ByteBuffer.allocate(255);
   lib.PassThruGetLastError(error);
   String errString =
       String.format(
           "%s error [%d:%s], %s",
           operation, status, Status.get(status), Native.toString(error.array()));
   throw new J2534Exception(errString);
 }
  @Nullable
  public static String toStringViaUTF8(ID cfString) {
    if (cfString.intValue() == 0) return null;

    int lengthInChars = myFoundationLibrary.CFStringGetLength(cfString);
    int potentialLengthInBytes = 3 * lengthInChars + 1; // UTF8 fully escaped 16 bit chars, plus nul

    byte[] buffer = new byte[potentialLengthInBytes];
    byte ok =
        myFoundationLibrary.CFStringGetCString(
            cfString, buffer, buffer.length, FoundationLibrary.kCFStringEncodingUTF8);
    if (ok == 0) throw new RuntimeException("Could not convert string");
    return Native.toString(buffer);
  }
Beispiel #8
0
  @Override
  public boolean resolvAlertDialog(String message) {
    Util.infoPrintln(
        LogLevel.LOG_DEBUG, "WindowsBrowser#resolveAlertDialg: CALLED with: [" + message + "]");
    boolean ret = true;

    try {
      /* alert dialog を探す */
      StructBrowserInfo bInfo = searchWindows(null, browserAlertWindowText, Const.PREFIX_SEARCH);
      List<NativeLong> hWnds = bInfo.getWindowHandles();

      for (NativeLong hWnd : hWnds) {
        /* 得られた window handle の Window Text を表示してみる */
        printWindowTextByHandle("Got Window", hWnd, LogLevel.LOG_DEBUG);

        /* message が一致するものを探す */
        StructBrowserInfo cInfo = searchWindows(hWnd, message, Const.PREFIX_SEARCH);
        List<NativeLong> hChldWnds = cInfo.getWindowHandles();

        /* 得られたchild window の window text を表示してみる(debug) */
        printWindowTextByHandle("\tChild Window Text", hChldWnds, LogLevel.LOG_DEBUG);

        if (hChldWnds.size() != 0) {
          Util.infoPrintln(
              LogLevel.LOG_DEBUG, "WindowsBrowser#resolveAlertDialog: alert found! : " + message);
        } else {
          Util.infoPrintln(
              LogLevel.LOG_DEBUG, "WindowsBrowser#resolveAlertDialog: Not alert! : " + message);
          hWnds.remove(hWnd);
          if (hWnds.size() == 0) {
            break;
          }
        }
      }

      NativeLong alert = null;
      if (hWnds.size() == 1) {
        Util.infoPrintln(
            LogLevel.LOG_DEBUG, "WindowsBrowser#resolveAlertDialog: alert is unique! : " + message);
        alert = hWnds.get(0);

        /* OK button を探す */
        StructBrowserInfo oInfo = searchWindows(alert, browserAlertOkText, Const.COMPLETE_SEARCH);
        List<NativeLong> hCand = oInfo.getWindowHandles();
        if (hCand.size() == 1) {
          NativeLong hOk = hCand.get(0);

          Util.infoPrintln(LogLevel.LOG_DEBUG, "*** button found: " + hOk);

          ret = pushButtonByHandle(alert, hOk, bInfo.getWindowThread(alert), 1000);

        } else if (hCand.size() > 1) {
          Util.infoPrintln(LogLevel.LOG_ERR, "multiple OK buttons found:" + hCand.size());
        } else {
          Util.infoPrintln(LogLevel.LOG_ERR, "no OK button found.");
        }
      } else if (hWnds.size() > 1) {
        Util.infoPrintln(
            LogLevel.LOG_ERR, "multiple alerts found: " + hWnds.size() + " : " + message);
      } else {
        Util.infoPrintln(LogLevel.LOG_ERR, "no such alert: " + message);
      }
    } catch (LastErrorException e) {
      Util.infoPrintln(LogLevel.LOG_CRIT, e);

      byte[] lpMsgBuf = new byte[2048];

      kernel32.FormatMessageA(
          WinConst.FORMAT_MESSAGE_FROM_SYSTEM | WinConst.FORMAT_MESSAGE_IGNORE_INSERTS,
          null,
          e.getErrorCode(),
          WinConst.MAKELANGID(WinConst.LANG_NEUTRAL, WinConst.SUBLANG_DEFAULT), // 既定の言語
          lpMsgBuf,
          2048,
          null);

      String msgBuf = Native.toString(lpMsgBuf, "MS932");

      Util.infoPrintln(
          LogLevel.LOG_ERR,
          "IEBrowser#resolveAlertDialog: system error : code : "
              + e.getErrorCode()
              + " : "
              + msgBuf);

    } catch (IOException e) {
      Util.infoPrintln(LogLevel.LOG_CRIT, e);
    } catch (ClassCastException e) {
      Util.infoPrintln(LogLevel.LOG_CRIT, e);
    } catch (ClassNotFoundException e) {
      Util.infoPrintln(LogLevel.LOG_CRIT, e);
    }

    return ret;
  }
Beispiel #9
0
  @Override
  public boolean resolvFileDownloadDialog(
      String filename,
      boolean isBrowserFileDownloadConfirm,
      boolean isBrowserFileDownloadDialog,
      boolean isBrowserFileDownloadOverrideConfirm,
      boolean isBrowserFileDownloadCompleteConfirm) {
    Util.infoPrintln(
        LogLevel.LOG_DEBUG,
        "WindowsBrowser#resolvFileDownloadDialog: CALLED with: [" + filename + "]");
    boolean ret = true;

    /* FileDialog を探す */
    try {
      if (isBrowserFileDownloadConfirm) {
        // ダウンロード確認ダイアログを処理する
        ret =
            processDialogWindow(
                browserFileDownloadConfirmText,
                Const.PREFIX_SEARCH,
                browserFileDownloadConfirmButton,
                Const.COMPLETE_SEARCH);
      }

      if (ret && isBrowserFileDownloadDialog) {
        // File Dialog を処理する
        ret =
            processDialogWindow(
                browserFileDownloadDialogText,
                Const.PREFIX_SEARCH,
                browserFileDownloadDialogButton,
                Const.COMPLETE_SEARCH);
      }

      if (ret && isBrowserFileDownloadOverrideConfirm) {
        // 上書き確認ダイアログを処理する
        ret =
            processDialogWindow(
                browserFileDownloadOverrideConfirmText,
                Const.PREFIX_SEARCH,
                browserFileDownloadOverrideConfirmButton,
                Const.COMPLETE_SEARCH);
      }

      if (ret && isBrowserFileDownloadCompleteConfirm) {
        // 完了確認ダイアログを処理する
        ret =
            processDialogWindow(
                browserFileDownloadCompleteConfirmText,
                Const.PREFIX_SEARCH,
                browserFileDownloadCompleteConfirmButton,
                Const.COMPLETE_SEARCH);
      }

    } catch (ClassCastException e) {
      Util.infoPrintln(LogLevel.LOG_CRIT, e);
    } catch (IOException e) {
      Util.infoPrintln(LogLevel.LOG_CRIT, e);
    } catch (ClassNotFoundException e) {
      Util.infoPrintln(LogLevel.LOG_CRIT, e);
    } catch (LastErrorException e) {
      Util.infoPrintln(LogLevel.LOG_CRIT, e);

      byte[] lpMsgBuf = new byte[2048];

      kernel32.FormatMessageA(
          WinConst.FORMAT_MESSAGE_FROM_SYSTEM | WinConst.FORMAT_MESSAGE_IGNORE_INSERTS,
          null,
          e.getErrorCode(),
          WinConst.MAKELANGID(WinConst.LANG_NEUTRAL, WinConst.SUBLANG_DEFAULT), // 既定の言語
          lpMsgBuf,
          2048,
          null);

      String msgBuf = Native.toString(lpMsgBuf, "MS932");

      Util.infoPrintln(
          LogLevel.LOG_ERR,
          "IEBrowser#resolvFileDownloadDialog: system error : code : "
              + e.getErrorCode()
              + " : "
              + msgBuf);

      ret = false;
    }

    return ret;
  }
Beispiel #10
0
  /**
   * Downloads an image and saves it somewhere.
   *
   * @param directoryItem The item you want to download
   * @param destination A path in the filesystem where you want to save the file. Can also be null
   *     or a directory. In case of null the temp directory will be used, in case of a directory the
   *     file name of the item will be used.
   * @param appendFileExtension Adds the extension of the photo onto File to ensure that supplied
   *     File name extension matches the image being downloaded from the camera. This is especially
   *     important if the camera is set to RAW+JPEG where the order of the images is not consistent.
   * @return Either null, or the location the file was ultimately saved to on success.
   */
  public static File download(
      final EdsDirectoryItemRef directoryItem,
      File destination,
      final boolean appendFileExtension) {
    EdsError err = EdsError.EDS_ERR_OK;
    final EdsStreamRef.ByReference stream = new EdsStreamRef.ByReference();
    final EdsDirectoryItemInfo dirItemInfo = new EdsDirectoryItemInfo();

    boolean success = false;

    // final long timeStart = System.currentTimeMillis();

    try {
      err =
          CanonUtils.toEdsError(
              CanonCamera.EDSDK.EdsGetDirectoryItemInfo(directoryItem, dirItemInfo));
      if (err == EdsError.EDS_ERR_OK) {
        if (destination == null) {
          destination = new File(System.getProperty("java.io.tmpdir"));
        }
        if (destination.isDirectory()) {
          destination = new File(destination, Native.toString(dirItemInfo.szFileName));
        } else if (appendFileExtension) {
          final String sourceFileName = Native.toString(dirItemInfo.szFileName);
          final int i = sourceFileName.lastIndexOf(".");
          if (i > 0) {
            final String extension = sourceFileName.substring(i);
            if (!destination.getName().toLowerCase().endsWith(extension)) {
              destination = new File(destination.getPath() + extension);
            }
          }
        }

        if (destination.getParentFile() != null) {
          destination.getParentFile().mkdirs();
        }

        /*
         * System.out.println( "Downloading image " +
         * Native.toString( dirItemInfo.szFileName ) +
         * " to " + destination.getCanonicalPath() );
         */

        // TODO: see if using an EdsCreateMemoryStream would be faster and whether the image could
        // be read directly without saving to file first - see:
        // http://stackoverflow.com/questions/1083446/canon-edsdk-memorystream-image
        err =
            CanonUtils.toEdsError(
                CanonCamera.EDSDK.EdsCreateFileStream(
                    Native.toByteArray(destination.getCanonicalPath()),
                    EdsFileCreateDisposition.kEdsFileCreateDisposition_CreateAlways.value(),
                    EdsAccess.kEdsAccess_ReadWrite.value(),
                    stream));
      }

      if (err == EdsError.EDS_ERR_OK) {
        err =
            CanonUtils.toEdsError(
                CanonCamera.EDSDK.EdsDownload(directoryItem, dirItemInfo.size, stream.getValue()));
      }

      if (err == EdsError.EDS_ERR_OK) {
        /*
         * System.out.println( "Image downloaded in " +
         * ( System.currentTimeMillis() - timeStart ) +
         * " ms" );
         */

        err = CanonUtils.toEdsError(CanonCamera.EDSDK.EdsDownloadComplete(directoryItem));

        success = true;
      }

      if (stream != null) {
        CanonCamera.EDSDK.EdsRelease(stream.getValue());
      }
    } catch (final Exception e) {
      e.printStackTrace();
    }

    return success ? destination : null;
  }
Beispiel #11
0
 /**
  * Returns the class of the window, such as "SWT_Window0" for any SWT application. See AutoIt
  * Window Info Tool
  */
 public String getClassName() {
   char[] buffer = new char[2048];
   User32.INSTANCE.GetClassName(hWnd, buffer, 1024);
   return Native.toString(buffer);
 }
Beispiel #12
0
 public String getTitle() {
   char[] buffer = new char[2048];
   User32.INSTANCE.GetWindowText(hWnd, buffer, 1024);
   return Native.toString(buffer);
 }
Beispiel #13
0
 public final void testSHGetSpecialFolderPath() {
   final char[] pszPath = new char[WinDef.MAX_PATH];
   assertTrue(Shell32.INSTANCE.SHGetSpecialFolderPath(null, pszPath, ShlObj.CSIDL_APPDATA, false));
   assertFalse(Native.toString(pszPath).isEmpty());
 }
Beispiel #14
0
 public String GetProcessImageFileNameA(Pointer hProcess) {
   byte[] lpImageFileName = new byte[256];
   psapi.GetProcessImageFileNameA(hProcess, lpImageFileName, 256);
   return Native.toString(lpImageFileName);
 }
Beispiel #15
0
 public String GetModuleFileNameExA(Pointer hProcess, Pointer hModule) {
   byte[] lpImageFileName = new byte[256];
   psapi.GetModuleFileNameExA(hProcess, hModule, lpImageFileName, 256);
   return Native.toString(lpImageFileName);
 }