Search in sources :

Example 1 with NETRESOURCE

use of com.sun.jna.platform.win32.Winnetwk.NETRESOURCE 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 2 with NETRESOURCE

use of com.sun.jna.platform.win32.Winnetwk.NETRESOURCE in project jna by java-native-access.

the class MprTest method connectToLocalShare.

/**
     * Connect to a local share on the local machine. Assumes the share is
     * already present
     *
     * @param share
     *            name of share on local computer.
     * @param lpLocalName
     *            name of local device to redirect to. E.g. F:. If null, makes a
     *            connection without redirecting.
     * @throws Exception
     *             the exception
     */
private void connectToLocalShare(String share, String lpLocalName) throws Exception {
    NETRESOURCE resource = new NETRESOURCE();
    resource.dwDisplayType = 0;
    resource.dwScope = 0;
    resource.dwType = RESOURCETYPE.RESOURCETYPE_DISK;
    resource.lpLocalName = lpLocalName;
    resource.lpRemoteName = "\\\\" + getLocalComputerName() + "\\" + share;
    // Establish connection
    assertEquals(WinError.ERROR_SUCCESS, Mpr.INSTANCE.WNetAddConnection3(null, resource, null, null, 0));
}
Also used : NETRESOURCE(com.sun.jna.platform.win32.Winnetwk.NETRESOURCE)

Example 3 with NETRESOURCE

use of com.sun.jna.platform.win32.Winnetwk.NETRESOURCE in project jna by java-native-access.

the class MprTest method testWNetAddConnection3.

public void testWNetAddConnection3() throws Exception {
    // First create a share on the local machine
    File fileShareFolder = createTempFolder();
    String share = createLocalShare(fileShareFolder);
    NETRESOURCE resource = new NETRESOURCE();
    resource.dwDisplayType = 0;
    resource.dwScope = 0;
    resource.dwType = RESOURCETYPE.RESOURCETYPE_DISK;
    resource.lpRemoteName = "\\\\" + getLocalComputerName() + "\\" + share;
    try {
        // Cancel any existing connections of the same name
        Mpr.INSTANCE.WNetCancelConnection2(resource.lpRemoteName, 0, true);
        // Establish a new one
        assertEquals(WinError.ERROR_SUCCESS, Mpr.INSTANCE.WNetAddConnection3(null, resource, null, null, 0));
    } finally {
        // Clean up resources
        Mpr.INSTANCE.WNetCancelConnection2(resource.lpRemoteName, 0, true);
        Netapi32.INSTANCE.NetShareDel(null, share, 0);
        fileShareFolder.delete();
    }
}
Also used : NETRESOURCE(com.sun.jna.platform.win32.Winnetwk.NETRESOURCE) File(java.io.File)

Example 4 with NETRESOURCE

use of com.sun.jna.platform.win32.Winnetwk.NETRESOURCE in project jna by java-native-access.

the class MprTest method testWNetUseConnection.

public void testWNetUseConnection() throws Exception {
    // First create a share on the local machine
    File fileShareFolder = createTempFolder();
    String share = createLocalShare(fileShareFolder);
    NETRESOURCE resource = new NETRESOURCE();
    resource.dwDisplayType = 0;
    resource.dwScope = 0;
    resource.dwType = RESOURCETYPE.RESOURCETYPE_DISK;
    resource.lpRemoteName = "\\\\" + getLocalComputerName() + "\\" + share;
    try {
        // Cancel any existing connections of the same name
        Mpr.INSTANCE.WNetCancelConnection2(resource.lpRemoteName, 0, true);
        // Establish a new one
        assertEquals(WinError.ERROR_SUCCESS, Mpr.INSTANCE.WNetUseConnection(null, resource, null, null, 0, null, null, null));
    } finally {
        // Clean up resources
        Mpr.INSTANCE.WNetCancelConnection2(resource.lpRemoteName, 0, true);
        Netapi32.INSTANCE.NetShareDel(null, share, 0);
        fileShareFolder.delete();
    }
}
Also used : NETRESOURCE(com.sun.jna.platform.win32.Winnetwk.NETRESOURCE) File(java.io.File)

Aggregations

NETRESOURCE (com.sun.jna.platform.win32.Winnetwk.NETRESOURCE)4 File (java.io.File)3 Memory (com.sun.jna.Memory)1 HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)1 IntByReference (com.sun.jna.ptr.IntByReference)1