Search in sources :

Example 6 with User

use of com.sun.jna.platform.win32.Netapi32Util.User in project jna by java-native-access.

the class User32WindowMessagesTest method testWindowMesssages.

/**
	 * Instantiates 2 windows and make them communicate through windows messages, even complex ones throught
	 * WM_COPYDATA.
	 */
@Test
public void testWindowMesssages() {
    // note : check the asserts that are present in the callback implementations here after.
    // Create window 1 named "ping"
    createWindow("ping");
    // let windows create the window before searching its handle.
    sleepCurrThread(4000);
    // Retrieves the created window's handle.
    HWND hwndPing = determineHWNDFromWindowClass("ping");
    assertNotNull(hwndPing);
    HHOOK hook = null;
    try {
        // DEMO 1 : sends a simple message to ping window with code MSG_CODE and value 123456.
        LRESULT result = User32.INSTANCE.SendMessage(hwndPing, WinUser.WM_USER, new WPARAM(MSG_SIMPLE_CODE), new LPARAM(MSG_SIMPLE_VAL));
        log("User Message sent to " + hwndPing + ", result = " + result);
        assertEquals(0, result.doubleValue(), 0);
        // DEMO 2 : send of structured message.
        // copyDataStruct must be held strongly on the java side.
        // cf : https://github.com/java-native-access/jna/pull/774 comments.
        COPYDATASTRUCT copyDataStruct = createStructuredMessage();
        result = User32.INSTANCE.SendMessage(hwndPing, WinUser.WM_COPYDATA, null, /* No current hwnd for this demo */
        new LPARAM(Pointer.nativeValue(copyDataStruct.getPointer())));
        log("COPYDATASTRUCT sent message to " + hwndPing + "(size=" + copyDataStruct.size() + ") code =" + copyDataStruct.dwData);
        assertEquals(0, result.intValue());
        assertEquals(DATA_STRUCT_CODE, copyDataStruct.dwData.doubleValue(), 0);
        // DEMO 3 : hook winproc then send a message to the hooked proc.
        hook = hookwinProc(hwndPing);
        result = User32.INSTANCE.SendMessage(hwndPing, WinUser.WM_USER, new WPARAM(MSG_HOOKED_CODE), new LPARAM(MSG_HOOKED_VAL));
        log("User Message sent to hooked proc " + hwndPing + ", result = " + result);
        assertEquals(0, result.intValue());
        // Waits e few moment before shutdown message.
        sleepCurrThread(3000);
    } finally {
        try {
            // checks that there has been no exception in the created thread for windows messages receival.
            assertNull("Unexpected exception in created Thread : " + exceptionInCreatedThread, exceptionInCreatedThread);
        //assert done in a try block in order not to block the WM_CLOSE message sending.
        } finally {
            User32.INSTANCE.PostMessage(hwndPing, WinUser.WM_CLOSE, null, null);
            log("WM_CLOSE posted to " + hwndPing);
            // Remember to unhook the win proc.
            if (hook != null) {
                User32.INSTANCE.UnhookWindowsHookEx(hook);
            }
            log("Unhook done correctly");
        }
    }
}
Also used : HHOOK(com.sun.jna.platform.win32.WinUser.HHOOK) LRESULT(com.sun.jna.platform.win32.WinDef.LRESULT) HWND(com.sun.jna.platform.win32.WinDef.HWND) LPARAM(com.sun.jna.platform.win32.WinDef.LPARAM) COPYDATASTRUCT(com.sun.jna.platform.win32.WinUser.COPYDATASTRUCT) WPARAM(com.sun.jna.platform.win32.WinDef.WPARAM) Test(org.junit.Test)

Example 7 with User

use of com.sun.jna.platform.win32.Netapi32Util.User in project jna by java-native-access.

the class Netapi32UtilTest method main.

public static void main(String[] args) {
    junit.textui.TestRunner.run(Netapi32UtilTest.class);
    System.out.println("Domain: " + Netapi32Util.getDomainName("localhost"));
    // server local groups
    Netapi32Util.LocalGroup[] localGroups = Netapi32Util.getLocalGroups();
    System.out.println("Local groups: " + localGroups.length);
    for (Netapi32Util.LocalGroup localGroup : localGroups) {
        System.out.println(" " + localGroup.name + " (" + localGroup.comment + ")");
    }
    // global groups
    Netapi32Util.Group[] groups = Netapi32Util.getGlobalGroups();
    System.out.println("Global groups: " + groups.length);
    for (Netapi32Util.Group group : groups) {
        System.out.println(" " + group.name);
    }
    // server users
    Netapi32Util.User[] users = Netapi32Util.getUsers();
    System.out.println("Users: " + users.length);
    for (Netapi32Util.User user : users) {
        System.out.println(" " + user.name);
    }
    // user local groups
    Netapi32Util.Group[] userLocalGroups = Netapi32Util.getCurrentUserLocalGroups();
    System.out.println("Local user groups: " + userLocalGroups.length);
    for (Netapi32Util.Group localGroup : userLocalGroups) {
        System.out.println(" " + localGroup.name);
    }
    // domain controller
    if (Netapi32Util.getJoinStatus() == LMJoin.NETSETUP_JOIN_STATUS.NetSetupDomainName) {
        System.out.println("Pdc: " + Netapi32Util.getDCName());
        DomainController dc = Netapi32Util.getDC();
        System.out.println("Domain controller:");
        System.out.println("    name: " + dc.name);
        System.out.println(" address: " + dc.address);
        System.out.println("  domain: " + dc.domainName);
        System.out.println("    site: " + dc.clientSiteName);
        System.out.println("  forest: " + dc.dnsForestName);
        System.out.println("    guid: " + Ole32Util.getStringFromGUID(dc.domainGuid));
    }
    // domain trusts
    if (Netapi32Util.getJoinStatus() == LMJoin.NETSETUP_JOIN_STATUS.NetSetupDomainName) {
        DomainTrust[] trusts = Netapi32Util.getDomainTrusts();
        System.out.println("Domain trusts: (" + trusts.length + ")");
        for (DomainTrust trust : trusts) {
            System.out.println(" " + trust.NetbiosDomainName + ": " + trust.DnsDomainName + " (" + trust.DomainSidString + ")");
        }
    }
}
Also used : DomainController(com.sun.jna.platform.win32.Netapi32Util.DomainController) DomainTrust(com.sun.jna.platform.win32.Netapi32Util.DomainTrust)

Example 8 with User

use of com.sun.jna.platform.win32.Netapi32Util.User in project jna by java-native-access.

the class Netapi32UtilTest method testGetUserInfoWithDomainSpecified.

public void testGetUserInfoWithDomainSpecified() {
    UserInfo userInfo = Netapi32Util.getUserInfo(Advapi32Util.getUserName(), System.getenv("USERDOMAIN"));
    assertNotNull("No user info retrieved", userInfo);
    assertTrue("Invalid user SID", Advapi32.INSTANCE.IsValidSid(userInfo.sid));
}
Also used : UserInfo(com.sun.jna.platform.win32.Netapi32Util.UserInfo)

Example 9 with User

use of com.sun.jna.platform.win32.Netapi32Util.User in project jna by java-native-access.

the class OleAutoTest method testLoadRegTypeLib.

public void testLoadRegTypeLib() {
    CLSID.ByReference clsid = new CLSID.ByReference();
    // get CLSID from string, Microsoft Scripting Engine
    HRESULT hr = Ole32.INSTANCE.CLSIDFromString(new WString("{420B2830-E718-11CF-893D-00A0C9054228}"), clsid);
    COMUtils.checkRC(hr);
    assertEquals(0, hr.intValue());
    // get user default lcid
    LCID lcid = Kernel32.INSTANCE.GetUserDefaultLCID();
    PointerByReference pWordTypeLib = new PointerByReference();
    // get typelib version 1.0
    hr = OleAuto.INSTANCE.LoadRegTypeLib(clsid, 1, 0, lcid, pWordTypeLib);
    COMUtils.checkRC(hr);
    assertEquals(0, hr.intValue());
}
Also used : WString(com.sun.jna.WString) LCID(com.sun.jna.platform.win32.WinDef.LCID) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) CLSID(com.sun.jna.platform.win32.Guid.CLSID) PointerByReference(com.sun.jna.ptr.PointerByReference) PointerByReference(com.sun.jna.ptr.PointerByReference)

Example 10 with User

use of com.sun.jna.platform.win32.Netapi32Util.User 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)

Aggregations

HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)7 HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)6 IntByReference (com.sun.jna.ptr.IntByReference)6 PointerByReference (com.sun.jna.ptr.PointerByReference)6 USER_INFO_1 (com.sun.jna.platform.win32.LMAccess.USER_INFO_1)5 LOCALGROUP_USERS_INFO_0 (com.sun.jna.platform.win32.LMAccess.LOCALGROUP_USERS_INFO_0)3 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 WString (com.sun.jna.WString)2 Account (com.sun.jna.platform.win32.Advapi32Util.Account)2 CLSID (com.sun.jna.platform.win32.Guid.CLSID)2 GROUP_USERS_INFO_0 (com.sun.jna.platform.win32.LMAccess.GROUP_USERS_INFO_0)2 HWND (com.sun.jna.platform.win32.WinDef.HWND)2 LCID (com.sun.jna.platform.win32.WinDef.LCID)2 SC_HANDLE (com.sun.jna.platform.win32.Winsvc.SC_HANDLE)2 UserModel (com.gitblit.models.UserModel)1 Pointer (com.sun.jna.Pointer)1 COMException (com.sun.jna.platform.win32.COM.COMException)1 Factory (com.sun.jna.platform.win32.COM.util.Factory)1