Search in sources :

Example 11 with HANDLEByReference

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();
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) Memory(com.sun.jna.Memory) HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference) NETRESOURCE(com.sun.jna.platform.win32.Winnetwk.NETRESOURCE) File(java.io.File)

Example 12 with HANDLEByReference

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);
    }
}
Also used : HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference)

Example 13 with HANDLEByReference

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;
        }
    }
}
Also used : HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 14 with HANDLEByReference

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;
        }
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference) File(java.io.File) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 15 with HANDLEByReference

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());
}
Also used : TOKEN_PRIVILEGES(com.sun.jna.platform.win32.WinNT.TOKEN_PRIVILEGES) IntByReference(com.sun.jna.ptr.IntByReference) LUID(com.sun.jna.platform.win32.WinNT.LUID) LUID_AND_ATTRIBUTES(com.sun.jna.platform.win32.WinNT.LUID_AND_ATTRIBUTES) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference)

Aggregations

HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)33 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)22 SC_HANDLE (com.sun.jna.platform.win32.Winsvc.SC_HANDLE)15 IntByReference (com.sun.jna.ptr.IntByReference)11 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)7 TOKEN_PRIVILEGES (com.sun.jna.platform.win32.WinNT.TOKEN_PRIVILEGES)5 File (java.io.File)5 USER_INFO_1 (com.sun.jna.platform.win32.LMAccess.USER_INFO_1)3 BOOLByReference (com.sun.jna.platform.win32.WinDef.BOOLByReference)3 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)3 PointerByReference (com.sun.jna.ptr.PointerByReference)3 Memory (com.sun.jna.Memory)2 Account (com.sun.jna.platform.win32.Advapi32Util.Account)2 PDH_COUNTER_PATH_ELEMENTS (com.sun.jna.platform.win32.Pdh.PDH_COUNTER_PATH_ELEMENTS)2 PDH_RAW_COUNTER (com.sun.jna.platform.win32.Pdh.PDH_RAW_COUNTER)2 RASCREDENTIALS (com.sun.jna.platform.win32.WinRas.RASCREDENTIALS)2 RASDIALPARAMS (com.sun.jna.platform.win32.WinRas.RASDIALPARAMS)2 Test (org.junit.Test)2 CredHandle (com.sun.jna.platform.win32.Sspi.CredHandle)1 CtxtHandle (com.sun.jna.platform.win32.Sspi.CtxtHandle)1