use of com.sun.jna.platform.win32.WinNT.HANDLEByReference in project jna by java-native-access.
the class MprTest method testWNetEnumConnection.
public void testWNetEnumConnection() throws Exception {
// MSDN recommends this as a reasonable size
int bufferSize = 16 * 1024;
HANDLEByReference lphEnum = new HANDLEByReference();
// Create a local share and connect to it. This ensures the enum will
// find at least one entry.
File fileShareFolder = createTempFolder();
String share = createLocalShare(fileShareFolder);
// Connect to local share
connectToLocalShare(share, null);
try {
// Open an enumeration
assertEquals(WinError.ERROR_SUCCESS, Mpr.INSTANCE.WNetOpenEnum(RESOURCESCOPE.RESOURCE_CONNECTED, RESOURCETYPE.RESOURCETYPE_DISK, RESOURCEUSAGE.RESOURCEUSAGE_ALL, null, lphEnum));
int winError = WinError.ERROR_SUCCESS;
while (true) {
Memory memory = new Memory(bufferSize);
IntByReference lpBufferSize = new IntByReference(bufferSize);
IntByReference lpcCount = new IntByReference(1);
// Get next value
winError = Mpr.INSTANCE.WNetEnumResource(lphEnum.getValue(), lpcCount, memory, lpBufferSize);
// Reached end of enumeration
if (winError == WinError.ERROR_NO_MORE_ITEMS)
break;
// Unlikely, but means our buffer size isn't large enough.
if (winError == WinError.ERROR_MORE_DATA) {
bufferSize = bufferSize * 2;
continue;
}
// If we get here, it means it has to be a success or our
// programming logic was wrong.
assertEquals(winError, WinError.ERROR_SUCCESS);
// Asked for one, should only get one.
assertEquals(1, lpcCount.getValue());
// Create a NETRESOURCE based on the memory
NETRESOURCE resource = new NETRESOURCE(memory);
// Assert things we know for sure.
assertNotNull(resource.lpRemoteName);
}
// Expect ERROR_NO_MORE_ITEMS here.
assertEquals(winError, WinError.ERROR_NO_MORE_ITEMS);
} finally {
// Clean up resources
Mpr.INSTANCE.WNetCloseEnum(lphEnum.getValue());
disconnectFromLocalShare("\\\\" + getLocalComputerName() + "\\" + share);
deleteLocalShare(share);
fileShareFolder.delete();
}
}
use of com.sun.jna.platform.win32.WinNT.HANDLEByReference in project jna by java-native-access.
the class Kernel32Test method testCreatePipe.
public void testCreatePipe() {
HANDLEByReference hReadPipe = new HANDLEByReference();
HANDLEByReference hWritePipe = new HANDLEByReference();
try {
assertTrue(Kernel32.INSTANCE.CreatePipe(hReadPipe, hWritePipe, null, 0));
} finally {
Kernel32Util.closeHandleRefs(hReadPipe, hWritePipe);
}
}
use of com.sun.jna.platform.win32.WinNT.HANDLEByReference in project jna by java-native-access.
the class Advapi32Util method getCurrentUserGroups.
/**
* Return the group memberships of the currently logged on user.
*
* @return An array of groups.
*/
public static Account[] getCurrentUserGroups() {
HANDLEByReference phToken = new HANDLEByReference();
Win32Exception err = null;
try {
// open thread or process token
HANDLE threadHandle = Kernel32.INSTANCE.GetCurrentThread();
if (!Advapi32.INSTANCE.OpenThreadToken(threadHandle, TOKEN_DUPLICATE | TOKEN_QUERY, true, phToken)) {
int rc = Kernel32.INSTANCE.GetLastError();
if (rc != W32Errors.ERROR_NO_TOKEN) {
throw new Win32Exception(rc);
}
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());
} catch (Win32Exception e) {
err = e;
// re-throw in order to invoke finally block
throw err;
} finally {
HANDLE hToken = phToken.getValue();
if (!WinBase.INVALID_HANDLE_VALUE.equals(hToken)) {
try {
Kernel32Util.closeHandle(hToken);
} catch (Win32Exception e) {
if (err == null) {
err = e;
} else {
err.addSuppressed(e);
}
}
}
if (err != null) {
throw err;
}
}
}
use of com.sun.jna.platform.win32.WinNT.HANDLEByReference in project jna by java-native-access.
the class Kernel32Util method getFileType.
/**
* Retrieves the result of GetFileType, provided the file exists.
* @param fileName file name
* @return file type
* @throws FileNotFoundException if file not found
*/
public static int getFileType(String fileName) throws FileNotFoundException {
File f = new File(fileName);
if (!f.exists()) {
throw new FileNotFoundException(fileName);
}
HANDLE hFile = null;
Win32Exception err = null;
try {
hFile = Kernel32.INSTANCE.CreateFile(fileName, WinNT.GENERIC_READ, WinNT.FILE_SHARE_READ, new WinBase.SECURITY_ATTRIBUTES(), WinNT.OPEN_EXISTING, WinNT.FILE_ATTRIBUTE_NORMAL, new HANDLEByReference().getValue());
if (WinBase.INVALID_HANDLE_VALUE.equals(hFile)) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
int type = Kernel32.INSTANCE.GetFileType(hFile);
switch(type) {
case WinNT.FILE_TYPE_UNKNOWN:
int rc = Kernel32.INSTANCE.GetLastError();
switch(rc) {
case WinError.NO_ERROR:
break;
default:
throw new Win32Exception(rc);
}
default:
return type;
}
} catch (Win32Exception e) {
err = e;
// re-throw so finally block executed
throw err;
} finally {
try {
closeHandle(hFile);
} catch (Win32Exception e) {
if (err == null) {
err = e;
} else {
err.addSuppressed(e);
}
}
if (err != null) {
throw err;
}
}
}
use of com.sun.jna.platform.win32.WinNT.HANDLEByReference in project jna by java-native-access.
the class W32Service method addShutdownPrivilegeToProcess.
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());
}
Aggregations