Search in sources :

Example 6 with CHAR

use of com.sun.jna.platform.win32.WinDef.CHAR in project jna by java-native-access.

the class PsapiTest method testGetModuleFileNameEx.

@Test
public void testGetModuleFileNameEx() {
    final JFrame w = new JFrame();
    try {
        w.setVisible(true);
        final String searchSubStr = "\\bin\\java";
        final HWND hwnd = new HWND(Native.getComponentPointer(w));
        final IntByReference pid = new IntByReference();
        User32.INSTANCE.GetWindowThreadProcessId(hwnd, pid);
        final HANDLE process = Kernel32.INSTANCE.OpenProcess(0x0400 | 0x0010, false, pid.getValue());
        if (process == null)
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        // check ANSI function
        final byte[] filePathAnsi = new byte[1025];
        int length = Psapi.INSTANCE.GetModuleFileNameExA(process, null, filePathAnsi, filePathAnsi.length - 1);
        if (length == 0)
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        assertTrue("Path didn't contain '" + searchSubStr + "': " + Native.toString(filePathAnsi), Native.toString(filePathAnsi).toLowerCase().contains(searchSubStr));
        // check Unicode function
        final char[] filePathUnicode = new char[1025];
        length = Psapi.INSTANCE.GetModuleFileNameExW(process, null, filePathUnicode, filePathUnicode.length - 1);
        if (length == 0)
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        assertTrue("Path didn't contain '" + searchSubStr + "': " + Native.toString(filePathUnicode), Native.toString(filePathUnicode).toLowerCase().contains(searchSubStr));
        // check default function
        final int memAllocSize = 1025 * Native.WCHAR_SIZE;
        final Memory filePathDefault = new Memory(memAllocSize);
        length = Psapi.INSTANCE.GetModuleFileNameEx(process, null, filePathDefault, (memAllocSize / Native.WCHAR_SIZE) - 1);
        if (length == 0)
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        assertTrue("Path didn't contain '" + searchSubStr + "': " + Native.toString(filePathDefault.getCharArray(0, memAllocSize / Native.WCHAR_SIZE)), Native.toString(filePathDefault.getCharArray(0, memAllocSize / Native.WCHAR_SIZE)).toLowerCase().contains(searchSubStr));
    } finally {
        w.dispose();
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) JFrame(javax.swing.JFrame) Memory(com.sun.jna.Memory) HWND(com.sun.jna.platform.win32.WinDef.HWND) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) Test(org.junit.Test)

Example 7 with CHAR

use of com.sun.jna.platform.win32.WinDef.CHAR in project jna by java-native-access.

the class Excelautomation_KB_219151_Mod method displayQuaterlySales.

private static void displayQuaterlySales(ComIWorksheet sheet) {
    // Determine how many quarters to display data for.
    int iNumQtrs = 4;
    for (; iNumQtrs >= 2; iNumQtrs--) {
        JOptionPane pane = new JOptionPane(String.format("Enter sales data for %d quarter(s)?", iNumQtrs), JOptionPane.QUESTION_MESSAGE);
        pane.setOptionType(JOptionPane.YES_NO_OPTION);
        JDialog dialog = pane.createDialog("Input...");
        dialog.setAlwaysOnTop(true);
        dialog.show();
        if (((Integer) pane.getValue()) == JOptionPane.YES_OPTION) {
            break;
        }
    }
    JOptionPane.showMessageDialog(null, String.format("Displaying data for %d quarter(s).", iNumQtrs));
    // Starting at E1, fill headers for the number of columns selected.
    ComIRange oResizeRange = sheet.getRange("E1", "E1").getResize(VARIANT_MISSING, iNumQtrs);
    oResizeRange.setFormula("=\"Q\" & COLUMN() - 4 & CHAR(10) & \"Sales\"");
    // Change the Orientation and WrapText properties for the headers.
    oResizeRange.setOrientation(38);
    oResizeRange.setWrapText(true);
    // Fill the interior color of the headers.
    oResizeRange.getInterior().setColorIndex(36);
    // Fill the columns with a formula and apply a number format.
    oResizeRange = sheet.getRange("E2", "E6").getResize(VARIANT_MISSING, iNumQtrs);
    oResizeRange.setFormula("=RAND()*100");
    oResizeRange.setNumberFormat("$0.00");
    // Apply borders to the Sales data and headers.
    oResizeRange = sheet.getRange("E1", "E6").getResize(VARIANT_MISSING, iNumQtrs);
    oResizeRange.getBorders().setWeight(XlBorderWeight.xlThin);
    // Add a Totals formula for the sales data and apply a border.
    oResizeRange = sheet.getRange("E8", "E8").getResize(VARIANT_MISSING, iNumQtrs);
    oResizeRange.setFormula("=SUM(E2:E6)");
    Borders oResizeRangeBorders = oResizeRange.getBorders();
    oResizeRangeBorders.setLineStyle(XlLineStyle.xlDouble);
    oResizeRangeBorders.setWeight(XlBorderWeight.xlThick);
    // Add a Chart for the selected data
    oResizeRange = sheet.getRange("E2:E6").getResize(VARIANT_MISSING, iNumQtrs);
    Chart chart = sheet.getParent().getCharts().Add(VARIANT_MISSING, VARIANT_MISSING, VARIANT_MISSING, VARIANT_MISSING);
    // Java note: Assumption is, that VARIANT_MISSING is the correct indicator
    // for missing values, it turns out, NULL is correct in this case...
    chart.ChartWizard(oResizeRange, XlChartType.xl3DColumn, VARIANT_MISSING, XlRowCol.xlColumns, null, null, null, null, null, null, null);
    chart.SeriesCollection(1).setXValues(sheet.getRange("C2", "C6"));
    for (int i = 1; i <= iNumQtrs; i++) {
        chart.SeriesCollection(i).setName("=\"Q" + Integer.toString(i) + "\"");
    }
    chart.Location(XlChartLocation.xlLocationAsObject, sheet.getName());
    // Move the chart so as not to cover your data.
    Shape shape = sheet.getShapes().Item(1);
    shape.setTop(sheet.getRows(10).getTop());
    shape.setLeft(sheet.getColumns(2).getLeft());
}
Also used : ComIRange(com.sun.jna.platform.win32.COM.util.office.excel.ComIRange) Shape(com.sun.jna.platform.win32.COM.util.office.excel.Shape) JOptionPane(javax.swing.JOptionPane) Borders(com.sun.jna.platform.win32.COM.util.office.excel.Borders) JDialog(javax.swing.JDialog) Chart(com.sun.jna.platform.win32.COM.util.office.excel.Chart)

Example 8 with CHAR

use of com.sun.jna.platform.win32.WinDef.CHAR in project jna by java-native-access.

the class Kernel32Test method testGetPrivateProfileSection.

public final void testGetPrivateProfileSection() throws IOException {
    final File tmp = File.createTempFile("testGetPrivateProfileSection", ".ini");
    tmp.deleteOnExit();
    final PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(tmp)));
    try {
        writer.println("[X]");
        writer.println("A=1");
        writer.println("B=X");
    } finally {
        writer.close();
    }
    final char[] buffer = new char[9];
    final DWORD len = Kernel32.INSTANCE.GetPrivateProfileSection("X", buffer, new DWORD(buffer.length), tmp.getCanonicalPath());
    assertEquals(len.intValue(), 7);
    assertEquals(new String(buffer), "A=1\0B=X\0\0");
}
Also used : FileWriter(java.io.FileWriter) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) File(java.io.File) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Example 9 with CHAR

use of com.sun.jna.platform.win32.WinDef.CHAR in project jna by java-native-access.

the class Kernel32VolumeManagementFunctionsTest method testEnumVolumes.

@Test
public void testEnumVolumes() {
    char[] lpszVolumeName = new char[WinDef.MAX_PATH + 1];
    HANDLE hFindVolume = assertValidHandle("FindFirstVolume", Kernel32.INSTANCE.FindFirstVolume(lpszVolumeName, lpszVolumeName.length));
    try {
        int foundPaths = 0;
        do {
            String volumeGUID = Native.toString(lpszVolumeName);
            testEnumVolumeMountMoints(volumeGUID);
            foundPaths += testGetVolumePathNamesForVolumeName(volumeGUID);
        } while (Kernel32.INSTANCE.FindNextVolume(hFindVolume, lpszVolumeName, lpszVolumeName.length));
        assertTrue("No paths were found", foundPaths > 0);
        int hr = Kernel32.INSTANCE.GetLastError();
        assertEquals("Bad volumes enum termination reason", WinError.ERROR_NO_MORE_FILES, hr);
    } finally {
        assertCallSucceeded("FindVolumeClose", Kernel32.INSTANCE.FindVolumeClose(hFindVolume));
    }
}
Also used : HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) Test(org.junit.Test)

Example 10 with CHAR

use of com.sun.jna.platform.win32.WinDef.CHAR in project jna by java-native-access.

the class Kernel32Test method testGetPrivateProfileSectionNames.

public final void testGetPrivateProfileSectionNames() throws IOException {
    final File tmp = File.createTempFile("testGetPrivateProfileSectionNames", ".ini");
    tmp.deleteOnExit();
    final PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(tmp)));
    try {
        writer.println("[S1]");
        writer.println("[S2]");
    } finally {
        writer.close();
    }
    final char[] buffer = new char[7];
    final DWORD len = Kernel32.INSTANCE.GetPrivateProfileSectionNames(buffer, new DWORD(buffer.length), tmp.getCanonicalPath());
    assertEquals(len.intValue(), 5);
    assertEquals(new String(buffer), "S1\0S2\0\0");
}
Also used : FileWriter(java.io.FileWriter) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) File(java.io.File) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Aggregations

IntByReference (com.sun.jna.ptr.IntByReference)15 HKEYByReference (com.sun.jna.platform.win32.WinReg.HKEYByReference)7 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)5 VARIANT (com.sun.jna.platform.win32.Variant.VARIANT)4 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)4 File (java.io.File)4 Test (org.junit.Test)4 Memory (com.sun.jna.Memory)3 BSTR (com.sun.jna.platform.win32.WTypes.BSTR)3 BYTE (com.sun.jna.platform.win32.WinDef.BYTE)3 CHAR (com.sun.jna.platform.win32.WinDef.CHAR)3 LONG (com.sun.jna.platform.win32.WinDef.LONG)3 SHORT (com.sun.jna.platform.win32.WinDef.SHORT)3 PSID (com.sun.jna.platform.win32.WinNT.PSID)3 PointerByReference (com.sun.jna.ptr.PointerByReference)3 BufferedWriter (java.io.BufferedWriter)3 FileWriter (java.io.FileWriter)3 PrintWriter (java.io.PrintWriter)3 Pointer (com.sun.jna.Pointer)2 Advapi32 (com.sun.jna.platform.win32.Advapi32)2