Search in sources :

Example 6 with SIZE

use of com.sun.jna.platform.win32.WinUser.SIZE in project jna by java-native-access.

the class MprTest method testWNetGetUniversalName.

public void testWNetGetUniversalName() throws Exception {
    // MSDN recommends this as a reasonable size
    int bufferSize = 1024;
    Memory memory = new Memory(bufferSize);
    IntByReference lpBufferSize = new IntByReference(bufferSize);
    File file = null;
    String share = null;
    String driveLetter = new String("x:");
    File fileShareFolder = createTempFolder();
    try {
        // Create a local share and connect to it.
        share = createLocalShare(fileShareFolder);
        // Connect to share using a drive letter.
        connectToLocalShare(share, driveLetter);
        // Create a path on local device redirected to the share.
        String filePath = new String(driveLetter + "\\testfile.txt");
        file = new File(filePath);
        file.createNewFile();
        // Test WNetGetUniversalName using UNIVERSAL_NAME_INFO_LEVEL
        assertEquals(WinError.ERROR_SUCCESS, Mpr.INSTANCE.WNetGetUniversalName(filePath, Winnetwk.UNIVERSAL_NAME_INFO_LEVEL, memory, lpBufferSize));
        UNIVERSAL_NAME_INFO uinfo = new UNIVERSAL_NAME_INFO(memory);
        assertNotNull(uinfo.lpUniversalName);
        // Test WNetGetUniversalName using REMOTE_NAME_INFO_LEVEL
        assertEquals(WinError.ERROR_SUCCESS, Mpr.INSTANCE.WNetGetUniversalName(filePath, Winnetwk.REMOTE_NAME_INFO_LEVEL, memory, lpBufferSize));
        REMOTE_NAME_INFO rinfo = new REMOTE_NAME_INFO(memory);
        assertNotNull(rinfo.lpUniversalName);
        assertNotNull(rinfo.lpConnectionName);
        assertNotNull(rinfo.lpRemainingPath);
    } finally {
        // Clean up resources
        if (file != null)
            file.delete();
        if (share != null) {
            disconnectFromLocalShare(driveLetter);
            deleteLocalShare(share);
            fileShareFolder.delete();
        }
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) Memory(com.sun.jna.Memory) REMOTE_NAME_INFO(com.sun.jna.platform.win32.Winnetwk.REMOTE_NAME_INFO) UNIVERSAL_NAME_INFO(com.sun.jna.platform.win32.Winnetwk.UNIVERSAL_NAME_INFO) File(java.io.File)

Example 7 with SIZE

use of com.sun.jna.platform.win32.WinUser.SIZE in project jna by java-native-access.

the class GDI32Test method testBITMAPINFO.

public void testBITMAPINFO() {
    BITMAPINFO info = new BITMAPINFO();
    assertEquals("Wrong size for BITMAPINFO()", 44, info.size());
    info = new BITMAPINFO(2);
    assertEquals("Wrong size for BITMAPINFO(2)", 48, info.size());
    info = new BITMAPINFO(16);
    assertEquals("Wrong size for BITMAPINFO(16)", 104, info.size());
}
Also used : BITMAPINFO(com.sun.jna.platform.win32.WinGDI.BITMAPINFO)

Example 8 with SIZE

use of com.sun.jna.platform.win32.WinUser.SIZE in project symmetric-ds by JumpMind.

the class WindowsService method install.

@Override
public void install() {
    if (isRunning()) {
        System.out.println("Server must be stopped before installing");
        System.exit(Constants.RC_NO_INSTALL_WHEN_RUNNING);
    }
    Advapi32Ex advapi = Advapi32Ex.INSTANCE;
    SC_HANDLE manager = openServiceManager();
    SC_HANDLE service = advapi.OpenService(manager, config.getName(), Winsvc.SERVICE_ALL_ACCESS);
    try {
        if (service != null) {
            throw new WrapperException(Constants.RC_ALREADY_INSTALLED, 0, "Service " + config.getName() + " is already installed");
        } else {
            System.out.println("Installing " + config.getName() + " ...");
            String dependencies = null;
            if (config.getDependencies() != null && config.getDependencies().size() > 0) {
                StringBuffer sb = new StringBuffer();
                for (String dependency : config.getDependencies()) {
                    sb.append(dependency).append("\0");
                }
                dependencies = sb.append("\0").toString();
            }
            service = advapi.CreateService(manager, config.getName(), config.getDisplayName(), Winsvc.SERVICE_ALL_ACCESS, WinsvcEx.SERVICE_WIN32_OWN_PROCESS, config.isAutoStart() || config.isDelayStart() ? WinsvcEx.SERVICE_AUTO_START : WinsvcEx.SERVICE_DEMAND_START, WinsvcEx.SERVICE_ERROR_NORMAL, commandToString(getWrapperCommand("init")), null, null, dependencies, null, null);
            if (service != null) {
                Advapi32Ex.SERVICE_DESCRIPTION desc = new Advapi32Ex.SERVICE_DESCRIPTION(config.getDescription());
                advapi.ChangeServiceConfig2(service, WinsvcEx.SERVICE_CONFIG_DESCRIPTION, desc);
                if (config.isDelayStart()) {
                    WinsvcEx.SERVICE_DELAYED_AUTO_START_INFO delayedInfo = new WinsvcEx.SERVICE_DELAYED_AUTO_START_INFO(true);
                    advapi.ChangeServiceConfig2(service, WinsvcEx.SERVICE_CONFIG_DELAYED_AUTO_START_INFO, delayedInfo);
                }
            } else {
                throwException("CreateService");
            }
            System.out.println("Done");
        }
    } finally {
        closeServiceHandle(service);
        closeServiceHandle(manager);
    }
}
Also used : WinsvcEx(org.jumpmind.symmetric.wrapper.jna.WinsvcEx) SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE) WString(com.sun.jna.WString) Advapi32Ex(org.jumpmind.symmetric.wrapper.jna.Advapi32Ex)

Example 9 with SIZE

use of com.sun.jna.platform.win32.WinUser.SIZE in project jna by java-native-access.

the class WininetUtil method getCache.

/**
     * Helper function for traversing wininet's cache and returning all entries.
     * <br>
     * Some entries are cookies, some entries are history items, and some are
     * actual files.<br>
     * 
     * @return A map of cache URL => local file (or URL => empty string for
     *         cookie and history entries)
     */
public static Map<String, String> getCache() {
    List<INTERNET_CACHE_ENTRY_INFO> items = new ArrayList<Wininet.INTERNET_CACHE_ENTRY_INFO>();
    HANDLE cacheHandle = null;
    Win32Exception we = null;
    int lastError = 0;
    // return
    Map<String, String> cacheItems = new LinkedHashMap<String, String>();
    try {
        IntByReference size = new IntByReference();
        // for every entry, we call the API twice:
        // once to get the size into the IntByReference
        // then again to get the actual item
        cacheHandle = Wininet.INSTANCE.FindFirstUrlCacheEntry(null, null, size);
        lastError = Native.getLastError();
        // if there's nothing in the cache, we're done.
        if (lastError == WinError.ERROR_NO_MORE_ITEMS) {
            return cacheItems;
        } else if (lastError != WinError.ERROR_SUCCESS && lastError != WinError.ERROR_INSUFFICIENT_BUFFER) {
            throw new Win32Exception(lastError);
        }
        INTERNET_CACHE_ENTRY_INFO entry = new INTERNET_CACHE_ENTRY_INFO(size.getValue());
        cacheHandle = Wininet.INSTANCE.FindFirstUrlCacheEntry(null, entry, size);
        if (cacheHandle == null) {
            throw new Win32Exception(Native.getLastError());
        }
        items.add(entry);
        while (true) {
            size = new IntByReference();
            // for every entry, we call the API twice:
            // once to get the size into the IntByReference
            // then again to get the actual item
            boolean result = Wininet.INSTANCE.FindNextUrlCacheEntry(cacheHandle, null, size);
            if (!result) {
                lastError = Native.getLastError();
                if (lastError == WinError.ERROR_NO_MORE_ITEMS) {
                    break;
                } else if (lastError != WinError.ERROR_SUCCESS && lastError != WinError.ERROR_INSUFFICIENT_BUFFER) {
                    throw new Win32Exception(lastError);
                }
            }
            entry = new INTERNET_CACHE_ENTRY_INFO(size.getValue());
            result = Wininet.INSTANCE.FindNextUrlCacheEntry(cacheHandle, entry, size);
            if (!result) {
                lastError = Native.getLastError();
                if (lastError == WinError.ERROR_NO_MORE_ITEMS) {
                    break;
                } else if (lastError != WinError.ERROR_SUCCESS && lastError != WinError.ERROR_INSUFFICIENT_BUFFER) {
                    throw new Win32Exception(lastError);
                }
            }
            items.add(entry);
        }
        for (INTERNET_CACHE_ENTRY_INFO item : items) {
            cacheItems.put(item.lpszSourceUrlName.getWideString(0), item.lpszLocalFileName == null ? "" : item.lpszLocalFileName.getWideString(0));
        }
    } catch (Win32Exception e) {
        we = e;
    } finally {
        if (cacheHandle != null) {
            if (!Wininet.INSTANCE.FindCloseUrlCache(cacheHandle)) {
                if (we != null) {
                    Win32Exception e = new Win32Exception(Native.getLastError());
                    e.addSuppressed(we);
                    we = e;
                }
            }
        }
    }
    if (we != null) {
        throw we;
    }
    return cacheItems;
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) ArrayList(java.util.ArrayList) INTERNET_CACHE_ENTRY_INFO(com.sun.jna.platform.win32.Wininet.INTERNET_CACHE_ENTRY_INFO) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) LinkedHashMap(java.util.LinkedHashMap)

Example 10 with SIZE

use of com.sun.jna.platform.win32.WinUser.SIZE in project jna by java-native-access.

the class W32Service method queryStatus.

/**
	 * Retrieves the current status of the specified service based on the specified information level.
	 * @return 
	 *  Service status information
	 */
public SERVICE_STATUS_PROCESS queryStatus() {
    IntByReference size = new IntByReference();
    Advapi32.INSTANCE.QueryServiceStatusEx(_handle, SC_STATUS_TYPE.SC_STATUS_PROCESS_INFO, null, 0, size);
    SERVICE_STATUS_PROCESS status = new SERVICE_STATUS_PROCESS(size.getValue());
    if (!Advapi32.INSTANCE.QueryServiceStatusEx(_handle, SC_STATUS_TYPE.SC_STATUS_PROCESS_INFO, status, status.size(), size)) {
        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
    }
    return status;
}
Also used : SERVICE_STATUS_PROCESS(com.sun.jna.platform.win32.Winsvc.SERVICE_STATUS_PROCESS) IntByReference(com.sun.jna.ptr.IntByReference)

Aggregations

IntByReference (com.sun.jna.ptr.IntByReference)12 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)6 Pointer (com.sun.jna.Pointer)5 Test (org.junit.Test)5 Memory (com.sun.jna.Memory)3 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)3 HWND (com.sun.jna.platform.win32.WinDef.HWND)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 WString (com.sun.jna.WString)2 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)2 LPARAM (com.sun.jna.platform.win32.WinDef.LPARAM)2 LRESULT (com.sun.jna.platform.win32.WinDef.LRESULT)2 WPARAM (com.sun.jna.platform.win32.WinDef.WPARAM)2 BITMAPINFO (com.sun.jna.platform.win32.WinGDI.BITMAPINFO)2 COPYDATASTRUCT (com.sun.jna.platform.win32.WinUser.COPYDATASTRUCT)2 ULONG_PTR (com.sun.jna.platform.win32.BaseTSD.ULONG_PTR)1 COMException (com.sun.jna.platform.win32.COM.COMException)1 IConnectionPoint (com.sun.jna.platform.win32.COM.util.IConnectionPoint)1 ObjectFactory (com.sun.jna.platform.win32.COM.util.ObjectFactory)1