Refine search
public class windows { public static void main(String[] args) { Kernel32 kernel32 = Kernel32.INSTANCE; WinNT.HANDLE snapshot = kernel32.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS, new WinDef.DWORD(0)); Tlhelp32.PROCESSENTRY32.ByReference processEntry = new Tlhelp32.PROCESSENTRY32.ByReference(); try { while (kernel32.Process32Next(snapshot, processEntry)) { HANDLE process = Kernel32.INSTANCE.OpenProcess(0x0400 | 0x0010, false, 0); if (process != null) { int len = psapi.GetModuleFileNameExW(process, null, path, 1024); if (len > 0) System.out.println(new String(path, 0, len)); else System.out.println("GetModuleFileNameW failed"); } else System.out.println("OpenProcess failed"); System.out.println(processEntry.th32ProcessID + "\t" + Native.toString(processEntry.szExeFile) + "\t" + processEntry.cntThreads); } } finally { kernel32.CloseHandle(snapshot); } }
/** * Returns valid drives in the system. * @return * An array of valid drives. */ public static String[] getLogicalDriveStrings() { DWORD dwSize = Kernel32.INSTANCE.GetLogicalDriveStrings(new DWORD(0), null); if (dwSize.intValue() <= 0) { throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); } char buf[] = new char[dwSize.intValue()]; dwSize = Kernel32.INSTANCE.GetLogicalDriveStrings(dwSize, buf); if (dwSize.intValue() <= 0) { throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); } List<String> drives = new ArrayList<String>(); String drive = ""; // the buffer is double-null-terminated for(int i = 0; i < buf.length - 1; i++) { if (buf[i] == 0) { drives.add(drive); drive = ""; } else { drive += buf[i]; } } return drives.toArray(new String[0]); }
WinNT.TOKEN_PRIVILEGES tp = new WinNT.TOKEN_PRIVILEGES(pLuids.length); for (int i = 0; i < pLuids.length; i++) { tp.Privileges[i] = new WinNT.LUID_AND_ATTRIBUTES(pLuids[i], new DWORD(WinNT.SE_PRIVILEGE_ENABLED)); throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); WinNT.TOKEN_PRIVILEGES tp = new WinNT.TOKEN_PRIVILEGES(pLuids.length); for (int i = 0; i < pLuids.length; i++) { tp.Privileges[i] = new WinNT.LUID_AND_ATTRIBUTES(pLuids[i], new DWORD(0)); Kernel32.INSTANCE.CloseHandle(phThreadToken.getValue()); phThreadToken.setValue(null);
/** * Return the path designated for temporary files. * * @return Path. */ public static String getTempPath() { DWORD nBufferLength = new DWORD(WinDef.MAX_PATH); char[] buffer = new char[nBufferLength.intValue()]; if (Kernel32.INSTANCE.GetTempPath(nBufferLength, buffer).intValue() == 0) { throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); } return Native.toString(buffer); }
HANDLE snapshot = Kernel32.INSTANCE.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPMODULE, new DWORD(processID)); if (snapshot == null) { throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); Tlhelp32.MODULEENTRY32W first = new Tlhelp32.MODULEENTRY32W(); if (!Kernel32.INSTANCE.Module32FirstW(snapshot, first)) { throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
/** * Decrypts an encrypted file or directory. * * @param file * The file or directory to decrypt. */ public static void decryptFile(File file) { String lpFileName = file.getAbsolutePath(); if (!Advapi32.INSTANCE.DecryptFile(lpFileName, new DWORD(0))) { throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); } }
try{ int desireAccess = TOKEN_IMPERSONATE | TOKEN_QUERY | TOKEN_DUPLICATE | STANDARD_RIGHTS_READ; HANDLE hProcess = Kernel32.INSTANCE.GetCurrentProcess(); if (!Advapi32.INSTANCE.OpenProcessToken(hProcess, desireAccess, openedAccessToken)) { throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); 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); DWORDByReference rights = new DWORDByReference(new DWORD(permissionToCheck.getCode())); Advapi32.INSTANCE.MapGenericMask(rights, mapping); privileges.PrivilegeCount = new DWORD(0); DWORDByReference privilegeLength = new DWORDByReference(new DWORD(privileges.size()));
/** * Retrieves the names of all sections in an initialization file. * <p> * This operation is atomic; no updates to the initialization file are allowed while this method is executed. * </p> * * @param fileName * The name of the initialization file. If this parameter is {@code NULL}, the function searches the Win.ini file. If this parameter does not * contain a full path to the file, the system searches for the file in the Windows directory. * @return the section names associated with the named file. */ public static final String[] getPrivateProfileSectionNames(final String fileName) { final char buffer[] = new char[65536]; // Maximum INI file size according to MSDN (http://support.microsoft.com/kb/78346) if (Kernel32.INSTANCE.GetPrivateProfileSectionNames(buffer, new DWORD(buffer.length), fileName).intValue() == 0) { throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); } return new String(buffer).split("\0"); }
/** * Retrieves all the keys and values for the specified section of an initialization file. * * <p> * Each string has the following format: {@code key=string}. * </p> * <p> * This operation is atomic; no updates to the specified initialization file are allowed while this method is executed. * </p> * * @param appName * The name of the section in the initialization file. * @param fileName * The name of the initialization file. If this parameter does not contain a full path to the file, the system searches for the file in the * Windows directory. * @return The key name and value pairs associated with the named section. */ public static final String[] getPrivateProfileSection(final String appName, final String fileName) { final char buffer[] = new char[32768]; // Maximum section size according to MSDN (http://msdn.microsoft.com/en-us/library/windows/desktop/ms724348(v=vs.85).aspx) if (Kernel32.INSTANCE.GetPrivateProfileSection(appName, buffer, new DWORD(buffer.length), fileName).intValue() == 0) { throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); } return new String(buffer).split("\0"); }
/** * Convenience method to get the processor information. Takes care of * auto-growing the array. * * @return the array of processor information. */ public static final WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION[] getLogicalProcessorInformation() { int sizePerStruct = new WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION() .size(); WinDef.DWORDByReference bufferSize = new WinDef.DWORDByReference( new WinDef.DWORD(sizePerStruct)); Memory memory; while (true) { memory = new Memory(bufferSize.getValue().intValue()); if (!Kernel32.INSTANCE.GetLogicalProcessorInformation(memory, bufferSize)) { int err = Kernel32.INSTANCE.GetLastError(); if (err != WinError.ERROR_INSUFFICIENT_BUFFER) throw new Win32Exception(err); } else { break; } } WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION firstInformation = new WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION( memory); int returnedStructCount = bufferSize.getValue().intValue() / sizePerStruct; return (WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION[]) firstInformation .toArray(new WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION[returnedStructCount]); }
WinNT.TOKEN_PRIVILEGES tp = new WinNT.TOKEN_PRIVILEGES(pLuids.length); for (int i = 0; i < pLuids.length; i++) { tp.Privileges[i] = new WinNT.LUID_AND_ATTRIBUTES(pLuids[i], new DWORD(0)); Kernel32.INSTANCE.CloseHandle(phThreadToken.getValue()); phThreadToken.setValue(null);
/** * Gets the handle of a process from the process entry. * * @param processEntry The processEntry to use * @return The handle * @throws AutomationException Thrown if the handle cannot be determined */ public static WinNT.HANDLE getHandleFromProcessEntry( final Tlhelp32.PROCESSENTRY32.ByReference processEntry) throws AutomationException { ensureWinApiInstances(); WinNT.HANDLE handle = kernel32.OpenProcess( 0x0400 | /* PROCESS_QUERY_INFORMATION */ 0x0800 | /* PROCESS_SUSPEND_RESUME */ 0x0001 | /* PROCESS_TERMINATE */ 0x00100000 /* SYNCHRONIZE */, false, processEntry.th32ProcessID.intValue()); if (handle == null) { throw new AutomationException("OpenProcess failed"); } return handle; }
private void addShutdownPrivilegeToProcess() { HANDLEByReference hToken = new HANDLEByReference(); LUID luid = new LUID(); Advapi32.INSTANCE.OpenProcessToken(Kernel32.INSTANCE.GetCurrentProcess(), WinNT.TOKEN_ADJUST_PRIVILEGES, hToken); Advapi32.INSTANCE.LookupPrivilegeValue("", WinNT.SE_SHUTDOWN_NAME, luid); TOKEN_PRIVILEGES tp = new TOKEN_PRIVILEGES(1); tp.Privileges[0] = new LUID_AND_ATTRIBUTES(luid, new DWORD(WinNT.SE_PRIVILEGE_ENABLED)); Advapi32.INSTANCE.AdjustTokenPrivileges(hToken.getValue(), false, tp, tp.size(), null, new IntByReference()); }
Kernel32.INSTANCE.GetPrivateProfileString(lpAppName, lpKeyName, lpDefault, buffer, new DWORD(buffer.length), lpFileName); return Native.toString(buffer);
import com.sun.jna.Native; import com.sun.jna.platform.win32.*; import com.sun.jna.win32.W32APIOptions; public class ProcessList { public static void main(String[] args) { WinNT winNT = (WinNT) Native.loadLibrary(WinNT.class, W32APIOptions.UNICODE_OPTIONS); WinNT.HANDLE snapshot = winNT.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS, new WinDef.DWORD(0)); Tlhelp32.PROCESSENTRY32.ByReference processEntry = new Tlhelp32.PROCESSENTRY32.ByReference(); while (winNT.Process32Next(snapshot, processEntry)) { System.out.println(processEntry.th32ProcessID + "\t" + Native.toString(processEntry.szExeFile)); } winNT.CloseHandle(snapshot); } }
@Override public void setAffinity(final long affinity) { final CLibrary lib = CLibrary.INSTANCE; WinDef.DWORD aff = new WinDef.DWORD(affinity); try { lib.SetThreadAffinityMask(lib.GetCurrentThread(), aff); } catch (LastErrorException e) { throw new IllegalStateException("sched_getaffinity((" + Long.SIZE / 8 + ") , &(" + affinity + ") ) errorNo=" + e.getErrorCode(), e); } } }
/** * Sets the value. * * @param value the new value */ public void setValue(DWORD value) { getPointer().setInt(0, value.intValue()); }
/** * @param nbOfPrivileges * Desired size of the Privileges array */ public PRIVILEGE_SET(int nbOfPrivileges) { PrivilegeCount = new DWORD(nbOfPrivileges); if(nbOfPrivileges > 0) { Privileges = new LUID_AND_ATTRIBUTES[nbOfPrivileges]; } }
public void initialize(int afCmd) throws DdemlException { WinDef.DWORDByReference pidInst = new WinDef.DWORDByReference(); Integer result = Ddeml.INSTANCE.DdeInitialize(pidInst, ddeAdapter, afCmd, 0); if(result != Ddeml.DMLERR_NO_ERROR) { throw DdemlException.create(result); } idInst = pidInst.getValue().intValue(); if(ddeAdapter instanceof DdeAdapter) { ddeAdapter.setInstanceIdentifier(idInst); } }