Search in sources :

Example 1 with LONG

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

the class SAFEARRAYTest method toArrayDirect.

private Object[] toArrayDirect(SAFEARRAY wrap) {
    Pointer dataPointer = wrap.accessData();
    long rowMax = wrap.getUBound(2);
    long columnMax = wrap.getUBound(1);
    VARIANT[] variantData = (VARIANT[]) new VARIANT(dataPointer).toArray((int) ((rowMax + 1) * (columnMax + 1)));
    Object[][] result = new Object[(int) (rowMax + 1)][(int) (columnMax + 1)];
    for (long i = 0; i <= rowMax; i++) {
        long rowOffset = i * columnMax;
        for (long j = 0; j <= columnMax; j++) {
            VARIANT cell = variantData[(int) (rowOffset + j)];
            result[(int) i][(int) j] = cell.getValue();
        }
    }
    wrap.unaccessData();
    return result;
}
Also used : Pointer(com.sun.jna.Pointer) ComObject(com.sun.jna.platform.win32.COM.util.annotation.ComObject) VARIANT(com.sun.jna.platform.win32.Variant.VARIANT)

Example 2 with LONG

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

the class WevtapiTest method testReadEvents.

public void testReadEvents() throws Exception {
    EVT_HANDLE queryHandle = null;
    EVT_HANDLE contextHandle = null;
    File testEvtx = new File(getClass().getResource("/res/WevtapiTest.sample1.evtx").toURI());
    StringBuilder sb = new StringBuilder();
    try {
        // test EvtQuery
        queryHandle = Wevtapi.INSTANCE.EvtQuery(null, testEvtx.getPath(), null, Winevt.EVT_QUERY_FLAGS.EvtQueryFilePath);
        // test EvtCreateRenderContext
        String[] targets = { "Event/System/Provider/@Name", "Event/System/EventRecordID", "Event/System/EventID", "Event/EventData/Data", "Event/System/TimeCreated/@SystemTime" };
        contextHandle = Wevtapi.INSTANCE.EvtCreateRenderContext(targets.length, targets, Winevt.EVT_RENDER_CONTEXT_FLAGS.EvtRenderContextValues);
        // test EvtNext
        int eventArraySize = 10;
        int evtNextTimeout = 1000;
        int arrayIndex = 0;
        EVT_HANDLE[] eventArray = new EVT_HANDLE[eventArraySize];
        IntByReference returned = new IntByReference();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        while (Wevtapi.INSTANCE.EvtNext(queryHandle, eventArraySize, eventArray, evtNextTimeout, 0, returned)) {
            // test EvtRender
            Memory buff;
            IntByReference propertyCount = new IntByReference();
            Winevt.EVT_VARIANT evtVariant = new Winevt.EVT_VARIANT();
            for (int i = 0; i < returned.getValue(); i++) {
                buff = WevtapiUtil.EvtRender(contextHandle, eventArray[i], Winevt.EVT_RENDER_FLAGS.EvtRenderEventValues, propertyCount);
                assertThat("PropertyCount", propertyCount.getValue(), is(5));
                useMemory(evtVariant, buff, 0);
                assertThat("Provider Name", (String) evtVariant.getValue(), is("testSource"));
                sb.append((String) evtVariant.getValue());
                useMemory(evtVariant, buff, 1);
                assertThat("EventRecordID", (Long) evtVariant.getValue(), is((long) arrayIndex * eventArraySize + i + 1));
                useMemory(evtVariant, buff, 2);
                assertThat("EventID", (Short) evtVariant.getValue(), is((short) (5000 + (arrayIndex * eventArraySize + i + 1))));
                useMemory(evtVariant, buff, 3);
                String[] args = (String[]) evtVariant.getValue();
                assertThat("Data#length", args.length, is(1));
                assertThat("Data#value", args[0], is("testMessage" + (arrayIndex * eventArraySize + i + 1)));
                useMemory(evtVariant, buff, 4);
                Date systemtime = ((WinBase.FILETIME) evtVariant.getValue()).toDate();
                assertThat("TimeCreated", dateFormat.format(systemtime), is("2016-08-17"));
            }
            arrayIndex++;
        }
        if (Kernel32.INSTANCE.GetLastError() != WinError.ERROR_SUCCESS && Kernel32.INSTANCE.GetLastError() != WinError.ERROR_NO_MORE_ITEMS) {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
        assertThat(sb.length() > 0, is(true));
    } finally {
        // test EvtClose
        if (queryHandle != null) {
            Wevtapi.INSTANCE.EvtClose(queryHandle);
        }
        if (contextHandle != null) {
            Wevtapi.INSTANCE.EvtClose(contextHandle);
        }
    }
    // =========== Test accessing binary data and empty value ================
    queryHandle = null;
    contextHandle = null;
    testEvtx = new File(getClass().getResource("/res/WevtapiTest.sample2.evtx").toURI());
    try {
        queryHandle = Wevtapi.INSTANCE.EvtQuery(null, testEvtx.getPath(), null, Winevt.EVT_QUERY_FLAGS.EvtQueryFilePath);
        String[] targets = { "Event/EventData/Binary", "Event/System/Correlation" };
        contextHandle = Wevtapi.INSTANCE.EvtCreateRenderContext(targets.length, targets, Winevt.EVT_RENDER_CONTEXT_FLAGS.EvtRenderContextValues);
        int read = 0;
        int eventArraySize = 1;
        int evtNextTimeout = 1000;
        EVT_HANDLE[] eventArray = new EVT_HANDLE[eventArraySize];
        IntByReference returned = new IntByReference();
        while (Wevtapi.INSTANCE.EvtNext(queryHandle, eventArraySize, eventArray, evtNextTimeout, 0, returned)) {
            Memory buff;
            IntByReference propertyCount = new IntByReference();
            Winevt.EVT_VARIANT evtVariant = new Winevt.EVT_VARIANT();
            for (int i = 0; i < returned.getValue(); i++) {
                read++;
                buff = WevtapiUtil.EvtRender(contextHandle, eventArray[i], Winevt.EVT_RENDER_FLAGS.EvtRenderEventValues, propertyCount);
                assertThat("PropertyCount", propertyCount.getValue(), is(2));
                useMemory(evtVariant, buff, 0);
                assertThat("Binary", (byte[]) evtVariant.getValue(), is(new byte[] { (byte) 0xD9, (byte) 0x06, 0, 0 }));
                useMemory(evtVariant, buff, 1);
                assertThat("Correlation", evtVariant.getValue(), nullValue());
            }
        }
        assertThat(read, is(1));
    } finally {
        // test EvtClose
        if (queryHandle != null) {
            Wevtapi.INSTANCE.EvtClose(queryHandle);
        }
        if (contextHandle != null) {
            Wevtapi.INSTANCE.EvtClose(contextHandle);
        }
    }
    // =========== Test accessing GUID + SID data ================
    queryHandle = null;
    contextHandle = null;
    testEvtx = new File(getClass().getResource("/res/WevtapiTest.sample3.evtx").toURI());
    try {
        queryHandle = Wevtapi.INSTANCE.EvtQuery(null, testEvtx.getPath(), null, Winevt.EVT_QUERY_FLAGS.EvtQueryFilePath);
        String[] targets = { "Event/System/Security/@UserID", "Event/System/Provider/@Guid" };
        contextHandle = Wevtapi.INSTANCE.EvtCreateRenderContext(targets.length, targets, Winevt.EVT_RENDER_CONTEXT_FLAGS.EvtRenderContextValues);
        int read = 0;
        int eventArraySize = 1;
        int evtNextTimeout = 1000;
        EVT_HANDLE[] eventArray = new EVT_HANDLE[eventArraySize];
        IntByReference returned = new IntByReference();
        while (Wevtapi.INSTANCE.EvtNext(queryHandle, eventArraySize, eventArray, evtNextTimeout, 0, returned)) {
            Memory buff;
            IntByReference propertyCount = new IntByReference();
            Winevt.EVT_VARIANT evtVariant = new Winevt.EVT_VARIANT();
            for (int i = 0; i < returned.getValue(); i++) {
                read++;
                buff = WevtapiUtil.EvtRender(contextHandle, eventArray[i], Winevt.EVT_RENDER_FLAGS.EvtRenderEventValues, propertyCount);
                assertThat("PropertyCount", propertyCount.getValue(), is(2));
                useMemory(evtVariant, buff, 0);
                assertThat("Security#UserID", ((WinNT.PSID) evtVariant.getValue()).getSidString(), is("S-1-5-21-3178902164-3053647283-518304804-1001"));
                useMemory(evtVariant, buff, 1);
                assertThat("Provider#GUID", ((Guid.GUID) evtVariant.getValue()).toGuidString(), is("{B0AA8734-56F7-41CC-B2F4-DE228E98B946}"));
            }
        }
        assertThat(read, is(1));
    } finally {
        // test EvtClose
        if (queryHandle != null) {
            Wevtapi.INSTANCE.EvtClose(queryHandle);
        }
        if (contextHandle != null) {
            Wevtapi.INSTANCE.EvtClose(contextHandle);
        }
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) Memory(com.sun.jna.Memory) Date(java.util.Date) EVT_HANDLE(com.sun.jna.platform.win32.Winevt.EVT_HANDLE) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat)

Example 3 with LONG

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

the class WinBaseTest method testFiletime.

public void testFiletime() {
    // subtract to convert ms after 1/1/1970 to ms after 1/1/1601
    long epochDiff = 11644473600000L;
    // Construct filetimes for ms after 1/1/1601, check for 100-ns after
    assertEquals("Mismatched filetime for 2ms", (new FILETIME(new Date(2L - epochDiff))).toDWordLong().longValue(), 2L * 10000);
    assertEquals("Mismatched filetime for 2^16ms", (new FILETIME(new Date((1L << 16) - epochDiff))).toDWordLong().longValue(), (1L << 16) * 10000);
    assertEquals("Mismatched filetime for 2^32ms", (new FILETIME(new Date((1L << 32) - epochDiff))).toDWordLong().longValue(), (1L << 32) * 10000);
    assertEquals("Mismatched filetime for 2^49ms", (new FILETIME(new Date((1L << 49) - epochDiff))).toDWordLong().longValue(), (1L << 49) * 10000);
}
Also used : FILETIME(com.sun.jna.platform.win32.WinBase.FILETIME) Date(java.util.Date)

Example 4 with LONG

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

the class MSOfficeDemo method testMSWord.

public void testMSWord() throws IOException {
    File demoDocument = null;
    MSWord msWord = null;
    // http://msdn.microsoft.com/en-us/library/office/ff839952(v=office.15).aspx
    // PDF format.
    LONG wdFormatPDF = new LONG(17);
    // Rich text format (RTF).
    LONG wdFormatRTF = new LONG(6);
    // Standard HTML format.
    LONG wdFormatHTML = new LONG(8);
    // Microsoft Office Word 97 - 2003 binary file format.
    LONG wdFormatDocument = new LONG(0);
    // Word default document file format. For Word 2010, this is the DOCX format.
    LONG wdFormatDocumentDefault = new LONG(16);
    // http://msdn.microsoft.com/en-us/library/office/ff838709(v=office.15).aspx
    // Original document format.
    LONG wdOriginalDocumentFormat = new LONG(1);
    // Prompt user to select a document format.
    LONG wdPromptUser = new LONG(2);
    // Microsoft Word document format.        
    LONG wdWordDocument = new LONG(0);
    try {
        msWord = new MSWord();
        System.out.println("MSWord version: " + msWord.getVersion());
        msWord.setVisible(true);
        Helper.sleep(5);
        demoDocument = Helper.createNotExistingFile("jnatest", ".doc");
        Helper.extractClasspathFileToReal("/com/sun/jna/platform/win32/COM/util/office/resources/jnatest.doc", demoDocument);
        msWord.openDocument(demoDocument.getAbsolutePath());
        msWord.insertText("Hello from JNA! \n\n");
        // wait 10sec. before closing
        Helper.sleep(10);
        // save in different formats
        // pdf format is only supported in MSWord 2007 and above
        System.out.println("Wrinting files to: " + Helper.tempDir);
        msWord.SaveAs(new File(Helper.tempDir, "jnatestSaveAs.doc").getAbsolutePath(), wdFormatDocument);
        msWord.SaveAs(new File(Helper.tempDir, "jnatestSaveAs.pdf").getAbsolutePath(), wdFormatPDF);
        msWord.SaveAs(new File(Helper.tempDir, "jnatestSaveAs.rtf").getAbsolutePath(), wdFormatRTF);
        msWord.SaveAs(new File(Helper.tempDir, "jnatestSaveAs.html").getAbsolutePath(), wdFormatHTML);
        // close and save the document
        msWord.closeActiveDocument(false);
        msWord.newDocument();
        // msWord.openDocument(currentWorkingDir + "jnatest.doc", true);
        msWord.insertText("Hello from JNA! \n Please notice that JNA can control MS Word via the new COM interface! \nHere we are creating a new word document and we save it to the 'TEMP' directory!");
        // save with no user prompt
        msWord.SaveAs(new File(Helper.tempDir, "jnatestNewDoc1.docx").getAbsolutePath(), wdFormatDocumentDefault);
        msWord.SaveAs(new File(Helper.tempDir, "jnatestNewDoc2.docx").getAbsolutePath(), wdFormatDocumentDefault);
        msWord.SaveAs(new File(Helper.tempDir, "jnatestNewDoc3.docx").getAbsolutePath(), wdFormatDocumentDefault);
        // close and save the document
        msWord.closeActiveDocument(false);
        // open 3 documents
        msWord.openDocument(new File(Helper.tempDir, "jnatestNewDoc1.docx").getAbsolutePath());
        msWord.insertText("Hello some changes from JNA!\n");
        msWord.openDocument(new File(Helper.tempDir, "jnatestNewDoc2.docx").getAbsolutePath());
        msWord.insertText("Hello some changes from JNA!\n");
        msWord.openDocument(new File(Helper.tempDir, "jnatestNewDoc3.docx").getAbsolutePath());
        msWord.insertText("Hello some changes from JNA!\n");
        // save the document and prompt the user
        msWord.Save(false, wdPromptUser);
    } catch (COMException e) {
        if (e.getExcepInfo() != null) {
            System.out.println("bstrSource: " + e.getExcepInfo().bstrSource);
            System.out.println("bstrDescription: " + e.getExcepInfo().bstrDescription);
        }
    } finally {
        if (msWord != null) {
            msWord.quit();
        }
        if (demoDocument != null && demoDocument.exists()) {
            demoDocument.delete();
        }
    }
}
Also used : COMException(com.sun.jna.platform.win32.COM.COMException) File(java.io.File) LONG(com.sun.jna.platform.win32.WinDef.LONG)

Example 5 with LONG

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

the class MSWord method Save.

public void Save(boolean bNoPrompt, LONG originalFormat) throws COMException {
    VARIANT vtNoPrompt = new VARIANT(bNoPrompt);
    VARIANT vtOriginalFormat = new VARIANT(originalFormat);
    this.invokeNoReply("Save", this.getDocuments(), vtNoPrompt, vtOriginalFormat);
}
Also used : VARIANT(com.sun.jna.platform.win32.Variant.VARIANT)

Aggregations

VARIANT (com.sun.jna.platform.win32.Variant.VARIANT)6 Pointer (com.sun.jna.Pointer)5 Date (java.util.Date)5 LONG (com.sun.jna.platform.win32.WinDef.LONG)4 Test (org.junit.Test)4 ComObject (com.sun.jna.platform.win32.COM.util.annotation.ComObject)3 DATE (com.sun.jna.platform.win32.OaIdl.DATE)3 SAFEARRAY (com.sun.jna.platform.win32.OaIdl.SAFEARRAY)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 SHORT (com.sun.jna.platform.win32.WinDef.SHORT)3 IntByReference (com.sun.jna.ptr.IntByReference)3 Memory (com.sun.jna.Memory)2 COMException (com.sun.jna.platform.win32.COM.COMException)2 IConnectionPoint (com.sun.jna.platform.win32.COM.util.IConnectionPoint)2 VARIANT_BOOL (com.sun.jna.platform.win32.OaIdl.VARIANT_BOOL)2 VT_BSTR (com.sun.jna.platform.win32.Variant.VT_BSTR)2 VT_DATE (com.sun.jna.platform.win32.Variant.VT_DATE)2 FILETIME (com.sun.jna.platform.win32.WinBase.FILETIME)2