Package com.sun.jna.examples.win32.WinNT

Examples of com.sun.jna.examples.win32.WinNT.HANDLEByReference


      assertFalse(Advapi32.INSTANCE.CloseServiceHandle(null));
      assertEquals(W32Errors.ERROR_INVALID_HANDLE, Kernel32.INSTANCE.GetLastError());
    }
   
    public void testCreateProcessAsUser() {
      HANDLEByReference hToken = new HANDLEByReference();
      HANDLE processHandle = Kernel32.INSTANCE.GetCurrentProcess();
      assertTrue(Advapi32.INSTANCE.OpenProcessToken(processHandle,
          WinNT.TOKEN_DUPLICATE | WinNT.TOKEN_QUERY, hToken));
     
      assertFalse(Advapi32.INSTANCE.CreateProcessAsUser(hToken.getValue(), null, "InvalidCmdLine.jna",
          null, null, false, 0, null, null, new WinBase.STARTUPINFO(),
          new WinBase.PROCESS_INFORMATION()));
      assertEquals(W32Errors.ERROR_FILE_NOT_FOUND, Kernel32.INSTANCE.GetLastError());
      assertTrue(Kernel32.INSTANCE.CloseHandle(hToken.getValue()));
    }
View Full Code Here


      assertEquals(WinNT.SE_BACKUP_NAME.length(), cchName.getValue());
      assertEquals(WinNT.SE_BACKUP_NAME, Native.toString(lpName));
    }
   
    public void testAdjustTokenPrivileges() {
      HANDLEByReference hToken = new HANDLEByReference();
      assertTrue(Advapi32.INSTANCE.OpenProcessToken(Kernel32.INSTANCE.GetCurrentProcess(),
          WinNT.TOKEN_ADJUST_PRIVILEGES | WinNT.TOKEN_QUERY, hToken));
     
      // Find an already enabled privilege
      TOKEN_PRIVILEGES tp = new TOKEN_PRIVILEGES(1024);
      IntByReference returnLength = new IntByReference();
      assertTrue(Advapi32.INSTANCE.GetTokenInformation(hToken.getValue(),  WinNT.TOKEN_INFORMATION_CLASS.TokenPrivileges,
          tp, tp.size(), returnLength));
      assertTrue(tp.PrivilegeCount.intValue() > 0);
     
      WinNT.LUID luid = null;
      for (int i=0; i<tp.PrivilegeCount.intValue(); i++) {
        if ((tp.Privileges[i].Attributes.intValue() & WinNT.SE_PRIVILEGE_ENABLED) > 0) {
          luid = tp.Privileges[i].Luid;
        }
      }
      assertTrue(luid != null);     
     
      // Re-enable it. That should succeed.
      tp = new WinNT.TOKEN_PRIVILEGES(1);
      tp.Privileges[0] = new WinNT.LUID_AND_ATTRIBUTES(luid, new DWORD(WinNT.SE_PRIVILEGE_ENABLED));
     
      assertTrue(Advapi32.INSTANCE.AdjustTokenPrivileges(hToken.getValue(), false, tp, 0, null, null));
      assertTrue(Kernel32.INSTANCE.CloseHandle(hToken.getValue()));
    }
View Full Code Here

   * Return the group memberships of the currently logged on user.
   *
   * @return An array of groups.
   */
  public static Account[] getCurrentUserGroups() {
    HANDLEByReference phToken = new HANDLEByReference();
    try {
      // open thread or process token
      HANDLE threadHandle = Kernel32.INSTANCE.GetCurrentThread();
      if (!Advapi32.INSTANCE.OpenThreadToken(threadHandle,
          TOKEN_DUPLICATE | TOKEN_QUERY, true, phToken)) {
        if (W32Errors.ERROR_NO_TOKEN != Kernel32.INSTANCE
            .GetLastError()) {
          throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
        HANDLE processHandle = Kernel32.INSTANCE.GetCurrentProcess();
        if (!Advapi32.INSTANCE.OpenProcessToken(processHandle,
            TOKEN_DUPLICATE | TOKEN_QUERY, phToken)) {
          throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
      }
      return getTokenGroups(phToken.getValue());
    } finally {
      if (phToken.getValue() != WinBase.INVALID_HANDLE_VALUE) {
        if (!Kernel32.INSTANCE.CloseHandle(phToken.getValue())) {
          throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
      }
    }
  }
View Full Code Here

    System.arraycopy(credentials.szUserName, 0, rasDialParams.szUserName, 0, credentials.szUserName.length);
    System.arraycopy(credentials.szPassword, 0, rasDialParams.szPassword, 0, credentials.szPassword.length);
    System.arraycopy(credentials.szDomain, 0, rasDialParams.szDomain, 0, credentials.szDomain.length);

    // dial
    HANDLEByReference hrasConn = new HANDLEByReference();
    int err = Rasapi32.INSTANCE.RasDial(null, null, rasDialParams, 0, null, hrasConn);
    if (err != WinError.ERROR_SUCCESS) {
      if (hrasConn.getValue() != null) Rasapi32.INSTANCE.RasHangUp(hrasConn.getValue());
      throw new Ras32Exception(err);
    }
    return hrasConn.getValue();
  }
View Full Code Here

    System.arraycopy(credentials.szUserName, 0, rasDialParams.szUserName, 0, credentials.szUserName.length);
    System.arraycopy(credentials.szPassword, 0, rasDialParams.szPassword, 0, credentials.szPassword.length);
    System.arraycopy(credentials.szDomain, 0, rasDialParams.szDomain, 0, credentials.szDomain.length);

    // dial
    HANDLEByReference hrasConn = new HANDLEByReference();
    int err = Rasapi32.INSTANCE.RasDial(null, null, rasDialParams, 2, func2, hrasConn);
    if (err != WinError.ERROR_SUCCESS) {
      if (hrasConn.getValue() != null) Rasapi32.INSTANCE.RasHangUp(hrasConn.getValue());
      throw new Ras32Exception(err);
    }
    return hrasConn.getValue();
  }
View Full Code Here

     */
    public static boolean accessCheck(File file, AccessCheckPermission permissionToCheck) {
        boolean hasAccess = false;
        final Memory securityDescriptorMemoryPointer = getSecurityDescriptorForFile(file.getAbsolutePath().replaceAll("/", "\\"));

        HANDLEByReference openedAccessToken = null;
        final HANDLEByReference duplicatedToken = new HANDLEByReference();
        try{
            openedAccessToken = new HANDLEByReference();

            final int desireAccess = TOKEN_IMPERSONATE | TOKEN_QUERY | TOKEN_DUPLICATE | STANDARD_RIGHTS_READ;
            if(!Advapi32.INSTANCE.OpenProcessToken(Kernel32.INSTANCE.GetCurrentProcess(), desireAccess, openedAccessToken)) {
                throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
            }

            if(!Advapi32.INSTANCE.DuplicateToken(openedAccessToken.getValue(), SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, duplicatedToken)) {
                throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
            }

            final GENERIC_MAPPING mapping = new GENERIC_MAPPING();
            mapping.genericRead = new DWORD(FILE_GENERIC_READ);
            mapping.genericWrite = new DWORD(FILE_GENERIC_WRITE);
            mapping.genericExecute = new DWORD(FILE_GENERIC_EXECUTE);
            mapping.genericAll = new DWORD(FILE_ALL_ACCESS);

            final DWORDByReference rights = new DWORDByReference(new DWORD(permissionToCheck.getCode()));
            Advapi32.INSTANCE.MapGenericMask(rights, mapping);

            final PRIVILEGE_SET privileges = new PRIVILEGE_SET(1);
            privileges.PrivilegeCount = new DWORD(0);
            final DWORDByReference privilegeLength = new DWORDByReference(new DWORD(privileges.size()));

            final DWORDByReference grantedAccess = new DWORDByReference();
            final BOOLByReference result = new BOOLByReference();
            if(!Advapi32.INSTANCE.AccessCheck(securityDescriptorMemoryPointer,
                    duplicatedToken.getValue(),
                    rights.getValue(),
                    mapping,
                    privileges, privilegeLength, grantedAccess, result)) {
                throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
            }

            hasAccess = result.getValue().booleanValue();

        } finally {

            if(openedAccessToken != null && openedAccessToken.getValue() != null) {
                Kernel32.INSTANCE.CloseHandle(openedAccessToken.getValue());
            }

            if(duplicatedToken != null && duplicatedToken.getValue() != null) {
                Kernel32.INSTANCE.CloseHandle(duplicatedToken.getValue());
            }

            if(securityDescriptorMemoryPointer != null) {
                securityDescriptorMemoryPointer.clear();
            }
View Full Code Here

              ptsServerExpiry);
          assertTrue(serverRc == W32Errors.SEC_I_CONTINUE_NEEDED || serverRc == W32Errors.SEC_E_OK);
        }       
      } while(serverRc != W32Errors.SEC_E_OK || clientRc != W32Errors.SEC_E_OK);     
      // query security context token
      HANDLEByReference phContextToken = new HANDLEByReference();
      assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.QuerySecurityContextToken(
          phServerContext, phContextToken));
      // release security context token
      assertTrue(Kernel32.INSTANCE.CloseHandle(phContextToken.getValue()));
      // release server context
      assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.DeleteSecurityContext(
          phServerContext));
      assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.FreeCredentialsHandle(
          phServerCredential));
View Full Code Here

      assertTrue(Kernel32.INSTANCE.SetHandleInformation(hFile, WinBase.HANDLE_FLAG_PROTECT_FROM_CLOSE, 0));
      assertTrue(Kernel32.INSTANCE.CloseHandle(hFile));
    }

    public void testCreatePipe() {
      HANDLEByReference hReadPipe = new HANDLEByReference();
      HANDLEByReference hWritePipe = new HANDLEByReference();

      assertTrue(Kernel32.INSTANCE.CreatePipe(hReadPipe, hWritePipe, null, 0));
      assertTrue(Kernel32.INSTANCE.CloseHandle(hReadPipe.getValue()));
      assertTrue(Kernel32.INSTANCE.CloseHandle(hWritePipe.getValue()));
    }
View Full Code Here

public class Win32SpoolMonitor {

    public Win32SpoolMonitor() {

        String pPrinterName = "HP Color LaserJet CM4730 MFP PCL 6";
        HANDLEByReference phPrinter = new HANDLEByReference();
        Winspool.INSTANCE.OpenPrinter(pPrinterName, phPrinter, null);

        // Get change notification handle for the printer
        HANDLE chgObject = Winspool.INSTANCE
                .FindFirstPrinterChangeNotification(phPrinter.getValue(),
                        Winspool.PRINTER_CHANGE_JOB, 0, null);

        if (chgObject != null) {
            while (true) {
                // Wait for the change notification
View Full Code Here

      assertTrue(Kernel32.INSTANCE.SetHandleInformation(hFile, WinBase.HANDLE_FLAG_PROTECT_FROM_CLOSE, 0));
      assertTrue(Kernel32.INSTANCE.CloseHandle(hFile));
    }

    public void testCreatePipe() {
      HANDLEByReference hReadPipe = new HANDLEByReference();
      HANDLEByReference hWritePipe = new HANDLEByReference();

      assertTrue(Kernel32.INSTANCE.CreatePipe(hReadPipe, hWritePipe, null, 0));
      assertTrue(Kernel32.INSTANCE.CloseHandle(hReadPipe.getValue()));
      assertTrue(Kernel32.INSTANCE.CloseHandle(hWritePipe.getValue()));
    }
View Full Code Here

TOP

Related Classes of com.sun.jna.examples.win32.WinNT.HANDLEByReference

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.