Search in sources :

Example 11 with SIZE

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

the class Rasapi32Util method getRasConnection.

/**
	 * Return a RAS connection by name
	 * @param connName the connection name
	 * @return the RAS connection structure
	 * @throws Ras32Exception errors
	 */
public static HANDLE getRasConnection(String connName) throws Ras32Exception {
    // size the array needed
    IntByReference lpcb = new IntByReference(0);
    IntByReference lpcConnections = new IntByReference();
    int err = Rasapi32.INSTANCE.RasEnumConnections(null, lpcb, lpcConnections);
    if (err != WinError.ERROR_SUCCESS && err != WinRas.ERROR_BUFFER_TOO_SMALL)
        throw new Ras32Exception(err);
    if (lpcb.getValue() == 0)
        return null;
    // get the connections
    RASCONN[] connections = new RASCONN[lpcConnections.getValue()];
    for (int i = 0; i < lpcConnections.getValue(); i++) connections[i] = new RASCONN();
    lpcb = new IntByReference(connections[0].dwSize * lpcConnections.getValue());
    err = Rasapi32.INSTANCE.RasEnumConnections(connections, lpcb, lpcConnections);
    if (err != WinError.ERROR_SUCCESS)
        throw new Ras32Exception(err);
    // find connection
    for (int i = 0; i < lpcConnections.getValue(); i++) {
        if (new String(connections[i].szEntryName).equals(connName))
            return connections[i].hrasconn;
    }
    return null;
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) RASCONN(com.sun.jna.platform.win32.WinRas.RASCONN)

Example 12 with SIZE

use of com.sun.jna.platform.win32.WinUser.SIZE in project intellij-community by JetBrains.

the class Restarter method restartOnWindows.

private static void restartOnWindows(String... beforeRestart) throws IOException {
    Kernel32 kernel32 = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);
    Shell32 shell32 = (Shell32) Native.loadLibrary("shell32", Shell32.class);
    int pid = kernel32.GetCurrentProcessId();
    IntByReference argc = new IntByReference();
    Pointer argvPtr = shell32.CommandLineToArgvW(kernel32.GetCommandLineW(), argc);
    String[] argv = getRestartArgv(argvPtr.getWideStringArray(0, argc.getValue()));
    kernel32.LocalFree(argvPtr);
    // See https://blogs.msdn.microsoft.com/oldnewthing/20060515-07/?p=31203
    // argv[0] as the program name is only a convention, i.e. there is no guarantee
    // the name is the full path to the executable.
    //
    // See https://msdn.microsoft.com/en-us/library/windows/desktop/ms683197(v=vs.85).aspx
    // To retrieve the full path to the executable, use "GetModuleFileName(NULL, ...)".
    //
    // Note: We use 32,767 as buffer size to avoid limiting ourselves to MAX_PATH (260).
    char[] buffer = new char[32767];
    if (kernel32.GetModuleFileNameW(null, buffer, new WinDef.DWORD(buffer.length)).intValue() > 0) {
        argv[0] = Native.toString(buffer);
    }
    List<String> args = new ArrayList<>();
    args.add(String.valueOf(pid));
    args.add(String.valueOf(beforeRestart.length));
    Collections.addAll(args, beforeRestart);
    args.add(String.valueOf(argv.length));
    Collections.addAll(args, argv);
    runRestarter(new File(PathManager.getBinPath(), "restarter.exe"), args);
    // Since the process ID is passed through the command line, we want to make sure that we don't exit before the "restarter"
    // process has a chance to open the handle to our process, and that it doesn't wait for the termination of an unrelated
    // process which happened to have the same process ID.
    TimeoutUtil.sleep(500);
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) ArrayList(java.util.ArrayList) Pointer(com.sun.jna.Pointer) WString(com.sun.jna.WString) WinDef(com.sun.jna.platform.win32.WinDef) File(java.io.File)

Example 13 with SIZE

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

the class User32WindowMessagesTest method createWindowAndLoop.

public void createWindowAndLoop(String windowClass) {
    // define new window class
    HMODULE hInst = Kernel32.INSTANCE.GetModuleHandle("");
    WNDCLASSEX wClass = new WNDCLASSEX();
    wClass.hInstance = hInst;
    wClass.lpfnWndProc = new WindowProc() {

        @Override
        public LRESULT callback(HWND hwnd, int uMsg, WPARAM wParam, LPARAM lParam) {
            // log(hwnd + " - received a message : " + uMsg);
            switch(uMsg) {
                case WinUser.WM_CREATE:
                    {
                        log(hwnd + " - onCreate: WM_CREATE");
                        return new LRESULT(0);
                    }
                case WinUser.WM_CLOSE:
                    log(hwnd + " WM_CLOSE");
                    User32.INSTANCE.DestroyWindow(hwnd);
                    return new LRESULT(0);
                case WinUser.WM_DESTROY:
                    {
                        log(hwnd + " - on Destroy.");
                        User32.INSTANCE.PostQuitMessage(0);
                        return new LRESULT(0);
                    }
                case WinUser.WM_USER:
                    {
                        log(hwnd + " - received a WM_USER message with code : '" + wParam + "' and value : '" + lParam + "'");
                        if (wParam.intValue() == MSG_SIMPLE_CODE) {
                            assertEqualsForCallbackExecution(MSG_SIMPLE_VAL, lParam.intValue());
                        }
                        if (wParam.intValue() == MSG_HOOKED_CODE) {
                            assertEqualsForCallbackExecution(MSG_HOOKED_VAL, lParam.intValue());
                        }
                        return new LRESULT(0);
                    }
                case WinUser.WM_COPYDATA:
                    {
                        COPYDATASTRUCT copyDataStruct = new COPYDATASTRUCT(new Pointer(lParam.longValue()));
                        ULONG_PTR uMsg1 = copyDataStruct.dwData;
                        Pointer lParam1 = copyDataStruct.lpData;
                        int wParam1 = copyDataStruct.cbData;
                        log(hwnd + " - received a WM_COPYDATA message with code : '" + uMsg1 + "' of size : '" + wParam1 + "'");
                        switch(uMsg1.intValue()) {
                            case DATA_STRUCT_CODE:
                                {
                                    MsgStruct msg = new MsgStruct(lParam1);
                                    // log(hwnd + " - received structured content : " + msg.toString(true));
                                    log(hwnd + " - message is of type MsgStruct with number = " + msg.number + " and message = '" + msg.message + "'");
                                    assertEqualsForCallbackExecution(MSG_STRUCT_NUMBER, msg.number);
                                    assertEqualsForCallbackExecution(MSG_STRUCT_VAL, msg.message);
                                }
                        }
                        return new LRESULT(0);
                    }
                default:
                    return User32.INSTANCE.DefWindowProc(hwnd, uMsg, wParam, lParam);
            }
        }
    };
    wClass.lpszClassName = windowClass;
    // register window class
    User32.INSTANCE.RegisterClassEx(wClass);
    getLastError();
    // create new window
    HWND hWnd = User32.INSTANCE.CreateWindowEx(User32.WS_EX_TOPMOST, windowClass, "My hidden helper window, used only to catch the windows events", 0, 0, 0, 0, 0, null, null, hInst, null);
    getLastError();
    log("window sucessfully created! window hwnd: " + hWnd.getPointer().toString());
    MSG msg = new MSG();
    while (User32.INSTANCE.GetMessage(msg, hWnd, 0, 0) > 0) {
        User32.INSTANCE.TranslateMessage(msg);
        User32.INSTANCE.DispatchMessage(msg);
    }
    User32.INSTANCE.UnregisterClass(windowClass, hInst);
    User32.INSTANCE.DestroyWindow(hWnd);
    log("program exit!");
}
Also used : MSG(com.sun.jna.platform.win32.WinUser.MSG) HWND(com.sun.jna.platform.win32.WinDef.HWND) Pointer(com.sun.jna.Pointer) HMODULE(com.sun.jna.platform.win32.WinDef.HMODULE) WPARAM(com.sun.jna.platform.win32.WinDef.WPARAM) ULONG_PTR(com.sun.jna.platform.win32.BaseTSD.ULONG_PTR) WindowProc(com.sun.jna.platform.win32.WinUser.WindowProc) LRESULT(com.sun.jna.platform.win32.WinDef.LRESULT) WNDCLASSEX(com.sun.jna.platform.win32.WinUser.WNDCLASSEX) LPARAM(com.sun.jna.platform.win32.WinDef.LPARAM) COPYDATASTRUCT(com.sun.jna.platform.win32.WinUser.COPYDATASTRUCT)

Example 14 with SIZE

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

the class PdhTest method makeCounterPath.

private static String makeCounterPath(Pdh pdh, PDH_COUNTER_PATH_ELEMENTS pathElements) {
    DWORDByReference pcchBufferSize = new DWORDByReference();
    int status = pdh.PdhMakeCounterPath(pathElements, null, pcchBufferSize, 0);
    assertEquals("Unexpected status code: 0x" + Integer.toHexString(status), PdhMsg.PDH_MORE_DATA, status);
    DWORD bufSize = pcchBufferSize.getValue();
    int numChars = bufSize.intValue();
    assertTrue("Bad required buffer size: " + numChars, numChars > 0);
    char[] szFullPathBuffer = new char[numChars + 1];
    pcchBufferSize.setValue(new DWORD(szFullPathBuffer.length));
    assertErrorSuccess("PdhMakeCounterPath", pdh.PdhMakeCounterPath(pathElements, szFullPathBuffer, pcchBufferSize, 0), true);
    return Native.toString(szFullPathBuffer);
}
Also used : DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference) DWORD(com.sun.jna.platform.win32.WinDef.DWORD)

Example 15 with SIZE

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

the class Kernel32Test method testReadFile.

public void testReadFile() throws IOException {
    String expected = "jna - testReadFile";
    File tmp = File.createTempFile("testReadFile", "jna");
    tmp.deleteOnExit();
    FileWriter fw = new FileWriter(tmp);
    try {
        fw.append(expected);
    } finally {
        fw.close();
    }
    HANDLE hFile = Kernel32.INSTANCE.CreateFile(tmp.getAbsolutePath(), WinNT.GENERIC_READ, WinNT.FILE_SHARE_READ, new WinBase.SECURITY_ATTRIBUTES(), WinNT.OPEN_EXISTING, WinNT.FILE_ATTRIBUTE_NORMAL, null);
    assertFalse("Failed to create file handle: " + tmp, WinBase.INVALID_HANDLE_VALUE.equals(hFile));
    try {
        byte[] readBuffer = new byte[expected.length() + Byte.MAX_VALUE];
        IntByReference lpNumberOfBytesRead = new IntByReference(0);
        assertTrue("Failed to read from file", Kernel32.INSTANCE.ReadFile(hFile, readBuffer, readBuffer.length, lpNumberOfBytesRead, null));
        int read = lpNumberOfBytesRead.getValue();
        assertEquals("Mismatched read size", expected.length(), read);
        assertEquals("Mismatched read content", expected, new String(readBuffer, 0, read));
    } finally {
        Kernel32Util.closeHandle(hFile);
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) FileWriter(java.io.FileWriter) File(java.io.File) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

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