Search in sources :

Example 76 with Pointer

use of com.sun.jna.Pointer in project jna by java-native-access.

the class Kernel32Util method getResourceNames.

/**
     * Gets a list of all resources from the specified executable file
     *
     * @param path
     *            The path to the executable file
     * @return A map of resource type name/ID => resources.<br>
     *         A map key + a single list item + the path to the executable can
     *         be handed off to getResource() to actually get the resource.
     */
public static Map<String, List<String>> getResourceNames(String path) {
    HMODULE target = Kernel32.INSTANCE.LoadLibraryEx(path, null, Kernel32.LOAD_LIBRARY_AS_DATAFILE);
    if (target == null) {
        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
    }
    final List<String> types = new ArrayList<String>();
    final Map<String, List<String>> result = new LinkedHashMap<String, List<String>>();
    WinBase.EnumResTypeProc ertp = new WinBase.EnumResTypeProc() {

        @Override
        public boolean invoke(HMODULE module, Pointer type, Pointer lParam) {
            // otherwise it assumes it's a pointer to a string
            if (Pointer.nativeValue(type) <= 65535) {
                types.add(Pointer.nativeValue(type) + "");
            } else {
                types.add(type.getWideString(0));
            }
            return true;
        }
    };
    WinBase.EnumResNameProc ernp = new WinBase.EnumResNameProc() {

        @Override
        public boolean invoke(HMODULE module, Pointer type, Pointer name, Pointer lParam) {
            String typeName = "";
            if (Pointer.nativeValue(type) <= 65535) {
                typeName = Pointer.nativeValue(type) + "";
            } else {
                typeName = type.getWideString(0);
            }
            if (Pointer.nativeValue(name) < 65535) {
                result.get(typeName).add(Pointer.nativeValue(name) + "");
            } else {
                result.get(typeName).add(name.getWideString(0));
            }
            return true;
        }
    };
    Win32Exception err = null;
    try {
        if (!Kernel32.INSTANCE.EnumResourceTypes(target, ertp, null)) {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
        for (final String typeName : types) {
            result.put(typeName, new ArrayList<String>());
            // simulate MAKEINTRESOURCE macro in WinUser.h
            // basically, if the value passed in can be parsed as a number then convert it into one and run with that.
            // otherwise, assume it's a string and construct a pointer to said string.
            Pointer pointer = null;
            try {
                pointer = new Pointer(Long.parseLong(typeName));
            } catch (NumberFormatException e) {
                pointer = new Memory(Native.WCHAR_SIZE * (typeName.length() + 1));
                pointer.setWideString(0, typeName);
            }
            boolean callResult = Kernel32.INSTANCE.EnumResourceNames(target, pointer, ernp, null);
            if (!callResult) {
                throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
            }
        }
    } catch (Win32Exception e) {
        err = e;
    } finally {
        // on this is the HMODULE from LoadLibrary
        if (target != null) {
            if (!Kernel32.INSTANCE.FreeLibrary(target)) {
                Win32Exception we = new Win32Exception(Kernel32.INSTANCE.GetLastError());
                if (err != null) {
                    we.addSuppressed(err);
                }
                throw we;
            }
        }
    }
    if (err != null) {
        throw err;
    }
    return result;
}
Also used : Memory(com.sun.jna.Memory) ArrayList(java.util.ArrayList) Pointer(com.sun.jna.Pointer) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) List(java.util.List)

Example 77 with Pointer

use of com.sun.jna.Pointer in project jna by java-native-access.

the class Kernel32Util method getResource.

/**
     * Gets the specified resource out of the specified executable file
     *
     * @param path
     *            The path to the executable file
     * @param type
     *            The type of the resource (either a type name or type ID is
     *            allowed)
     * @param name
     *            The name or ID of the resource
     * @return The resource bytes, or null if no such resource exists.
     * @throws IllegalStateException if the call to LockResource fails
     */
public static byte[] getResource(String path, String type, String name) {
    HMODULE target = Kernel32.INSTANCE.LoadLibraryEx(path, null, Kernel32.LOAD_LIBRARY_AS_DATAFILE);
    if (target == null) {
        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
    }
    Win32Exception err = null;
    Pointer start = null;
    int length = 0;
    byte[] results = null;
    try {
        Pointer t = null;
        try {
            t = new Pointer(Long.parseLong(type));
        } catch (NumberFormatException e) {
            t = new Memory(Native.WCHAR_SIZE * (type.length() + 1));
            t.setWideString(0, type);
        }
        Pointer n = null;
        try {
            n = new Pointer(Long.parseLong(name));
        } catch (NumberFormatException e) {
            n = new Memory(Native.WCHAR_SIZE * (name.length() + 1));
            n.setWideString(0, name);
        }
        HRSRC hrsrc = Kernel32.INSTANCE.FindResource(target, n, t);
        if (hrsrc == null) {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
        // according to MSDN, on 32 bit Windows or newer, calling FreeResource() is not necessary - and in fact does nothing but return false.
        HANDLE loaded = Kernel32.INSTANCE.LoadResource(target, hrsrc);
        if (loaded == null) {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
        length = Kernel32.INSTANCE.SizeofResource(target, hrsrc);
        if (length == 0) {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
        // MSDN: It is not necessary to unlock resources because the system automatically deletes them when the process that created them terminates.
        // MSDN does not say that LockResource sets GetLastError
        start = Kernel32.INSTANCE.LockResource(loaded);
        if (start == null) {
            throw new IllegalStateException("LockResource returned null.");
        }
        // have to capture it into a byte array before you free the library, otherwise bad things happen.
        results = start.getByteArray(0, length);
    } catch (Win32Exception we) {
        err = we;
    } finally {
        // from what I can tell on MSDN, the only thing that needs cleanup on this is the HMODULE from LoadLibrary
        if (target != null) {
            if (!Kernel32.INSTANCE.FreeLibrary(target)) {
                Win32Exception we = new Win32Exception(Kernel32.INSTANCE.GetLastError());
                if (err != null) {
                    we.addSuppressed(err);
                }
                throw we;
            }
        }
    }
    if (err != null) {
        throw err;
    }
    return results;
}
Also used : Memory(com.sun.jna.Memory) Pointer(com.sun.jna.Pointer) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 78 with Pointer

use of com.sun.jna.Pointer in project jna by java-native-access.

the class TypeLibUtil method FindName.

/**
     * Find name.
     * 
     * @param name
     *            the name
     * @param hashVal
     *            the hash val or 0 if unknown
     * @param maxResult
     *            maximum number of items to search
     * @return the find name
     */
public FindName FindName(String name, int hashVal, short maxResult) {
    Pointer p = Ole32.INSTANCE.CoTaskMemAlloc((name.length() + 1L) * Native.WCHAR_SIZE);
    WTypes.LPOLESTR olestr = new WTypes.LPOLESTR(p);
    olestr.setValue(name);
    ULONG lHashVal = new ULONG(hashVal);
    USHORTByReference pcFound = new USHORTByReference(maxResult);
    Pointer[] ppTInfo = new Pointer[maxResult];
    MEMBERID[] rgMemId = new MEMBERID[maxResult];
    HRESULT hr = this.typelib.FindName(olestr, lHashVal, ppTInfo, rgMemId, pcFound);
    COMUtils.checkRC(hr);
    FindName findName = new FindName(olestr.getValue(), ppTInfo, rgMemId, pcFound.getValue().shortValue());
    Ole32.INSTANCE.CoTaskMemFree(p);
    return findName;
}
Also used : ULONG(com.sun.jna.platform.win32.WinDef.ULONG) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) LPOLESTR(com.sun.jna.platform.win32.WTypes.LPOLESTR) MEMBERID(com.sun.jna.platform.win32.OaIdl.MEMBERID) LPOLESTR(com.sun.jna.platform.win32.WTypes.LPOLESTR) USHORTByReference(com.sun.jna.platform.win32.WinDef.USHORTByReference) Pointer(com.sun.jna.Pointer) WTypes(com.sun.jna.platform.win32.WTypes)

Example 79 with Pointer

use of com.sun.jna.Pointer in project jna by java-native-access.

the class W32Service method queryServiceConfig2.

private Pointer queryServiceConfig2(int type) {
    IntByReference bufferSize = new IntByReference();
    Advapi32.INSTANCE.QueryServiceConfig2(_handle, type, Pointer.NULL, 0, bufferSize);
    Pointer buffer = new Memory(bufferSize.getValue());
    if (!Advapi32.INSTANCE.QueryServiceConfig2(_handle, type, buffer, bufferSize.getValue(), new IntByReference())) {
        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
    }
    return buffer;
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) Memory(com.sun.jna.Memory) Pointer(com.sun.jna.Pointer)

Example 80 with Pointer

use of com.sun.jna.Pointer in project jna by java-native-access.

the class Advapi32Test method testConvertSid.

public void testConvertSid() {
    String sidString = EVERYONE;
    PSIDByReference sid = new PSIDByReference();
    assertTrue("Failed to convert SID string", Advapi32.INSTANCE.ConvertStringSidToSid(sidString, sid));
    PSID value = sid.getValue();
    try {
        PointerByReference convertedSidStringPtr = new PointerByReference();
        assertTrue("Failed to convert SID string", Advapi32.INSTANCE.ConvertSidToStringSid(value, convertedSidStringPtr));
        Pointer conv = convertedSidStringPtr.getValue();
        try {
            String convertedSidString = conv.getWideString(0);
            assertEquals("Mismatched SID string", convertedSidString, sidString);
        } finally {
            Kernel32Util.freeLocalMemory(conv);
        }
    } finally {
        Kernel32Util.freeLocalMemory(value.getPointer());
    }
}
Also used : PSIDByReference(com.sun.jna.platform.win32.WinNT.PSIDByReference) PointerByReference(com.sun.jna.ptr.PointerByReference) Pointer(com.sun.jna.Pointer) PSID(com.sun.jna.platform.win32.WinNT.PSID)

Aggregations

Pointer (com.sun.jna.Pointer)93 PointerByReference (com.sun.jna.ptr.PointerByReference)20 Memory (com.sun.jna.Memory)15 IntByReference (com.sun.jna.ptr.IntByReference)15 Test (org.junit.Test)12 HDDEDATA (com.sun.jna.platform.win32.Ddeml.HDDEDATA)8 HSZ (com.sun.jna.platform.win32.Ddeml.HSZ)8 HCONV (com.sun.jna.platform.win32.Ddeml.HCONV)7 ConnectHandler (com.sun.jna.platform.win32.DdemlUtil.ConnectHandler)7 IDdeConnection (com.sun.jna.platform.win32.DdemlUtil.IDdeConnection)7 StandaloneDdeClient (com.sun.jna.platform.win32.DdemlUtil.StandaloneDdeClient)7 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)7 File (java.io.File)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 HWND (com.sun.jna.platform.win32.WinDef.HWND)6 ULONG (com.sun.jna.platform.win32.WinDef.ULONG)6 IOException (java.io.IOException)6 BufferedImage (java.awt.image.BufferedImage)5 Function (com.sun.jna.Function)4 ULONG_PTR (com.sun.jna.platform.win32.BaseTSD.ULONG_PTR)4