@Override
    public void run() {
      while (loaded) {
        HANDLE[] handles = {nextSkeletonFrame, nextDepthImageFrame, nextInteractionFrame};

        Kernel32.INSTANCE.WaitForMultipleObjects(handles.length, handles, false, Kernel32.INFINITE);

        if (WinBase.WAIT_OBJECT_0 == Kernel32.INSTANCE.WaitForSingleObject(nextSkeletonFrame, 0))
          try {
            processSkeleton();
          } catch (Throwable t) {
            System.out.println("Error in processSkeleton");
          }

        if (WinBase.WAIT_OBJECT_0 == Kernel32.INSTANCE.WaitForSingleObject(nextDepthImageFrame, 0))
          try {
            processDepth();
          } catch (Throwable t) {
            System.out.println("Error in processDepth");
          }

        if (WinBase.WAIT_OBJECT_0 == Kernel32.INSTANCE.WaitForSingleObject(nextInteractionFrame, 0))
          try {
            processInteraction();
          } catch (Throwable t) {
            System.out.println("Error in processInteraction");
          }
      }
    }
Example #2
0
 public GlobalMemory() {
   if (!Kernel32.INSTANCE.GlobalMemoryStatusEx(this._memory)) {
     LOG.error(
         "Failed to Initialize MemoryStatusEx. Error code: {}", Kernel32.INSTANCE.GetLastError());
     this._memory = null;
   }
 }
Example #3
0
    @Override
    public void run() {
      Logger.debug("run()");
      try {
        hHook =
            USER32_INSTANCE.SetWindowsHookEx(
                WinUser.WH_MOUSE_LL,
                WindowsMouseHook.this,
                Kernel32.INSTANCE.GetModuleHandle(null),
                0);
        MSG msg = new MSG();
        while ((USER32_INSTANCE.GetMessage(msg, null, 0, 0)) != 0) {
          // This code is never reached
          USER32_INSTANCE.TranslateMessage(msg);
          USER32_INSTANCE.DispatchMessage(msg);
          if (getHook() == null) {
            break;
          }
        }
      } catch (Exception e) {
        Logger.error("Mouse hook failure", e);
      }

      Logger.debug("mouse hook runnable exits");
    }
  /**
   * 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);
  }
Example #5
0
 public static int getProcessID(Process process) {
   long id = getProcessHandle(process);
   if (id != 0) {
     HANDLE handle = new HANDLE();
     handle.setPointer(Pointer.createConstant(id));
     return Kernel32.INSTANCE.GetProcessId(handle);
   }
   return 0;
 }
Example #6
0
 /** Runs a program and returns the process id. Note: use runCommand instead of this. */
 public static int createProcess(String program, String args, String currentDirectory) {
   STARTUPINFO startupInfo = new STARTUPINFO(); // input
   PROCESS_INFORMATION processInformation = new PROCESS_INFORMATION(); // output
   String cmdline = (args == null || args.length() == 0) ? null : program + " " + args;
   boolean ok =
       Kernel32.INSTANCE.CreateProcess(
           program,
           cmdline,
           null,
           null,
           false,
           new DWORD(0),
           null,
           currentDirectory,
           startupInfo,
           processInformation);
   if (!ok) System.out.println("CreateProcess failed err=" + Kernel32.INSTANCE.GetLastError());
   return processInformation.dwProcessId.intValue();
 }
Example #7
0
  public void run() {
    lib = User32.INSTANCE;
    HMODULE hMod = Kernel32.INSTANCE.GetModuleHandle(null);
    keyboardHook =
        new LowLevelKeyboardProc() {
          public LRESULT callback(int nCode, WPARAM wParam, KBDLLHOOKSTRUCT info) {

            if (nCode >= 0) {
              // To unhook press 'esc' key
              if (info.vkCode == 0x1B) {
                User32.INSTANCE.UnhookWindowsHookEx(hhk);
              }
              switch (info.vkCode) {
                case 0x5B:
                  System.err.println("l win");
                  return new LRESULT(1);
                case 0x5C:
                  System.err.println("r win");
                  return new LRESULT(1);
                case 0xA2:
                  System.err.println("l ctrl");
                  return new LRESULT(1);
                case 0xA3:
                  System.err.println("r ctrl");
                  return new LRESULT(1);
                case 0xA4:
                  System.err.println("l alt");
                  return new LRESULT(1);
                case 0xA5:
                  System.err.println("r alt");
                  return new LRESULT(1);
                default:
                  System.out.println("Key Pressed : " + info.vkCode); // do nothing
              }
            }
            return lib.CallNextHookEx(hhk, nCode, wParam, info.getPointer());
          }
        };
    hhk = lib.SetWindowsHookEx(13, keyboardHook, hMod, 0);
    // This bit never returns from GetMessage
    int result;
    MSG msg = new MSG();

    while ((result = lib.GetMessage(msg, null, 0, 0)) != 0) {
      if (result == -1) {
        break;
      } else {
        lib.TranslateMessage(msg);
        lib.DispatchMessage(msg);
      }
    }

    lib.UnhookWindowsHookEx(hhk);
  }
Example #8
0
  public WindowsOSSystemInfo() {

    SYSTEM_INFO si = new SYSTEM_INFO();
    Kernel32.INSTANCE.GetSystemInfo(si);

    try {
      IntByReference isWow64 = new IntByReference();
      HANDLE hProcess = Kernel32.INSTANCE.GetCurrentProcess();
      if (Kernel32.INSTANCE.IsWow64Process(hProcess, isWow64)) {
        if (isWow64.getValue() > 0) {
          Kernel32.INSTANCE.GetNativeSystemInfo(si);
        }
      }
    } catch (UnsatisfiedLinkError e) {
      // no WOW64 support
      LOG.trace("", e);
    }

    this._si = si;
    LOG.debug("Initialized OSNativeSystemInfo");
  }
Example #9
0
  @Override
  public void execute() {

    // Call the real windows API
    HANDLE handle = Kernel32.INSTANCE.GetCurrentProcess();
    long value = Pointer.nativeValue(handle.getPointer());

    System.out.println("Return Value: " + value);

    // Store
    register.mov("eax", new LongValue(value));
  }
  @Override
  public void destroy() {
    if (loaded) {
      loaded = false;

      if (interactionStream != null) {
        interactionStream.Disable();
        interactionStream.Release();
        interactionStream = null;
      }

      if (interactionClient != null) {
        interactionClient.Release();
        interactionClient = null;
      }

      if (colorStream != null) {
        Kernel32.INSTANCE.CloseHandle(colorStream);
        colorStream = null;
      }

      if (depthStream != null) {
        Kernel32.INSTANCE.CloseHandle(depthStream);
        depthStream = null;
      }

      if (nextColorImageFrame != null) {
        Kernel32.INSTANCE.CloseHandle(nextColorImageFrame);
        nextColorImageFrame = null;
      }

      if (nextDepthImageFrame != null) {
        Kernel32.INSTANCE.CloseHandle(nextDepthImageFrame);
        nextDepthImageFrame = null;
      }

      if (nextSkeletonFrame != null) {
        Kernel32.INSTANCE.CloseHandle(nextSkeletonFrame);
        nextSkeletonFrame = null;
      }

      if (nextInteractionFrame != null) {
        Kernel32.INSTANCE.CloseHandle(nextInteractionFrame);
        nextInteractionFrame = null;
      }

      if (device != null) {
        checkRC(device.NuiSkeletonTrackingDisable());
        device.NuiShutdown();
        device.Release();
        device = null;
      }

      // Remove all receivers connected to this driver
      EventManager.getInstance().removeReceivers(EventType.HAND_CREATED);
      EventManager.getInstance().removeReceivers(EventType.HAND_UPDATED);
      EventManager.getInstance().removeReceivers(EventType.HAND_DESTROYED);
    }
  }
  @After
  public void after() {
    // Close Word
    Dispatch d = new Dispatch(this.ppWordApp.getValue());
    DISPID dispIdMember = new DISPID(1105); // Quit
    REFIID.ByValue riid = new REFIID.ByValue(Guid.IID_NULL);
    LCID lcid = Kernel32.INSTANCE.GetSystemDefaultLCID();
    WinDef.WORD wFlags = new WinDef.WORD(1);
    DISPPARAMS.ByReference pDispParams = new DISPPARAMS.ByReference();
    VARIANT.ByReference pVarResult = new VARIANT.ByReference();
    IntByReference puArgErr = new IntByReference();
    EXCEPINFO.ByReference pExcepInfo = new EXCEPINFO.ByReference();
    d.Invoke(dispIdMember, riid, lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);

    Ole32.INSTANCE.CoUninitialize();
  }
  @Override
  public ByteBuffer getVisualData() {
    Kernel32.INSTANCE.WaitForSingleObject(nextColorImageFrame, Kernel32.INFINITE);
    NUI_IMAGE_FRAME imageFrame = new NUI_IMAGE_FRAME();
    checkRC(device.NuiImageStreamGetNextFrame(colorStream, new DWORD(0), imageFrame));
    NUI_LOCKED_RECT lockedRect = new NUI_LOCKED_RECT();
    checkRC(imageFrame.pFrameTexture.LockRect(new UINT(0), lockedRect, null, new DWORD(0)));

    if (lockedRect.Pitch == 0) throw new RuntimeException("Kinect didn't give us data");

    ByteBuffer buf = ByteBuffer.wrap(lockedRect.getBytes());
    checkRC(imageFrame.pFrameTexture.UnlockRect(0));

    // Release the frame
    checkRC(device.NuiImageStreamReleaseFrame(colorStream, imageFrame));

    return buf;
  }
Example #13
0
 /*
  * (non-Javadoc)
  * @see waffle.windows.auth.IWindowsIdentity#dispose()
  */
 @Override
 public void dispose() {
     if (this.windowsIdentity != null) {
         Kernel32.INSTANCE.CloseHandle(this.windowsIdentity);
     }
 }
 /**
  * Impersonate a logged on user.
  *
  * @param windowsIdentity Windows identity obtained via LogonUser.
  */
 public WindowsIdentityImpersonationContextImpl(HANDLE windowsIdentity) {
   if (!Advapi32.INSTANCE.ImpersonateLoggedOnUser(windowsIdentity)) {
     throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
   }
 }
  @Override
  public void load() throws InvalidConfigurationFileException, DriverException {
    if (loaded) destroy(); // reset driver

    loaded = true;

    IntByReference pcount = new IntByReference(0);
    checkRC(KinectLibrary.INSTANCE.NuiGetSensorCount(pcount));

    if (pcount.getValue() == 0) {
      throw new DriverException("No connected devices");
    }

    KinectDevice.ByReference newDevice = new KinectDevice.ByReference();

    checkRC(KinectLibrary.INSTANCE.NuiCreateSensorByIndex(0, newDevice));
    device = newDevice.getDevice();
    checkRC(
        device.NuiInitialize(
            new DWORD(
                KinectLibrary.NUI_INITIALIZE_FLAG_USES_COLOR
                    | KinectLibrary.NUI_INITIALIZE_FLAG_USES_SKELETON
                    | KinectLibrary.NUI_INITIALIZE_FLAG_USES_DEPTH)));

    nextColorImageFrame = Kernel32.INSTANCE.CreateEvent(null, true, false, null);
    nextDepthImageFrame = Kernel32.INSTANCE.CreateEvent(null, true, false, null);
    nextSkeletonFrame = Kernel32.INSTANCE.CreateEvent(null, true, false, null);
    nextInteractionFrame = Kernel32.INSTANCE.CreateEvent(null, true, false, null);

    HANDLEByReference handle = new HANDLEByReference();
    checkRC(
        device.NuiImageStreamOpen(
            new JnaEnumWrapper<>(NUI_IMAGE_TYPE.NUI_IMAGE_TYPE_COLOR),
            new JnaEnumWrapper<>(NUI_IMAGE_RESOLUTION.NUI_IMAGE_RESOLUTION_640x480),
            new DWORD(0),
            new DWORD(2),
            nextColorImageFrame,
            handle));

    colorStream = handle.getValue();

    checkRC(
        device.NuiImageStreamOpen(
            new JnaEnumWrapper<>(NUI_IMAGE_TYPE.NUI_IMAGE_TYPE_DEPTH),
            new JnaEnumWrapper<>(NUI_IMAGE_RESOLUTION.NUI_IMAGE_RESOLUTION_640x480),
            new DWORD(0),
            new DWORD(2),
            nextDepthImageFrame,
            handle));

    depthStream = handle.getValue();

    checkRC(device.NuiSkeletonTrackingEnable(nextSkeletonFrame, new DWORD(0)));

    interactionClient = new INuiInteractionClient();

    INuiInteractionStream.ByReference newStream = new INuiInteractionStream.ByReference();
    checkRC(
        KinectToolkitLibrary.INSTANCE.NuiCreateInteractionStream(
            device, interactionClient, newStream));
    interactionStream = newStream.getStream();

    checkRC(interactionStream.Enable(nextInteractionFrame));
    new Thread(new BackgroundThread()).start();
  }