Search in sources :

Example 56 with DWORD

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

the class COMTest method testTYPEATTR.

public void testTYPEATTR() {
    int pSize = Native.POINTER_SIZE;
    TYPEATTR typeAttr = new TYPEATTR();
    typeAttr.guid = GUID.fromString("{50A7E9B0-70EF-11D1-B75A-00A0C90564FE}");
    typeAttr.lcid = Kernel32.INSTANCE.GetSystemDefaultLCID();
    typeAttr.dwReserved = new DWORD(1);
    typeAttr.memidConstructor = new MEMBERID(2);
    typeAttr.memidDestructor = new MEMBERID(3);
    typeAttr.lpstrSchema = new LPOLESTR("Hello World");
    typeAttr.cbSizeInstance = new ULONG(4);
    typeAttr.typekind = new TYPEKIND(5);
    typeAttr.cFuncs = new WORD(6);
    typeAttr.cVars = new WORD(7);
    typeAttr.cImplTypes = new WORD(8);
    typeAttr.cbSizeVft = new WORD(9);
    typeAttr.cbAlignment = new WORD(10);
    typeAttr.wMajorVerNum = new WORD(11);
    typeAttr.wMinorVerNum = new WORD(12);
    typeAttr.tdescAlias = new TYPEDESC();
    typeAttr.idldescType = new IDLDESC();
    typeAttr.write();
    typeAttr.read();
//System.out.println(typeAttr.toString());
//System.out.println("TYPEATTR size: " + typeAttr.size());
}
Also used : ULONG(com.sun.jna.platform.win32.WinDef.ULONG) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) WORD(com.sun.jna.platform.win32.WinDef.WORD) MEMBERID(com.sun.jna.platform.win32.OaIdl.MEMBERID) TYPEDESC(com.sun.jna.platform.win32.OaIdl.TYPEDESC) TYPEATTR(com.sun.jna.platform.win32.OaIdl.TYPEATTR) LPOLESTR(com.sun.jna.platform.win32.WTypes.LPOLESTR) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) IDLDESC(com.sun.jna.platform.win32.OaIdl.IDLDESC) TYPEKIND(com.sun.jna.platform.win32.OaIdl.TYPEKIND)

Example 57 with DWORD

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

the class Advapi32Test method testMapGenericExecuteMask.

public void testMapGenericExecuteMask() {
    final GENERIC_MAPPING mapping = new GENERIC_MAPPING();
    mapping.genericRead = new DWORD(FILE_GENERIC_READ);
    mapping.genericWrite = new DWORD(FILE_GENERIC_WRITE);
    mapping.genericExecute = new DWORD(FILE_GENERIC_EXECUTE);
    mapping.genericAll = new DWORD(FILE_ALL_ACCESS);
    final DWORDByReference rights = new DWORDByReference(new DWORD(GENERIC_EXECUTE));
    Advapi32.INSTANCE.MapGenericMask(rights, mapping);
    assertEquals(FILE_GENERIC_EXECUTE, rights.getValue().intValue());
    assertTrue(GENERIC_EXECUTE != (rights.getValue().intValue() & GENERIC_EXECUTE));
}
Also used : GENERIC_MAPPING(com.sun.jna.platform.win32.WinNT.GENERIC_MAPPING) DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference) DWORD(com.sun.jna.platform.win32.WinDef.DWORD)

Example 58 with DWORD

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

the class Advapi32Test method testMapGenericReadMask.

public void testMapGenericReadMask() {
    final GENERIC_MAPPING mapping = new GENERIC_MAPPING();
    mapping.genericRead = new DWORD(FILE_GENERIC_READ);
    mapping.genericWrite = new DWORD(FILE_GENERIC_WRITE);
    mapping.genericExecute = new DWORD(FILE_GENERIC_EXECUTE);
    mapping.genericAll = new DWORD(FILE_ALL_ACCESS);
    final DWORDByReference rights = new DWORDByReference(new DWORD(GENERIC_READ));
    Advapi32.INSTANCE.MapGenericMask(rights, mapping);
    assertEquals(FILE_GENERIC_READ, rights.getValue().intValue());
    assertTrue(GENERIC_READ != (rights.getValue().intValue() & GENERIC_READ));
}
Also used : GENERIC_MAPPING(com.sun.jna.platform.win32.WinNT.GENERIC_MAPPING) DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference) DWORD(com.sun.jna.platform.win32.WinDef.DWORD)

Example 59 with DWORD

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

the class Advapi32Test method testMapGenericWriteMask.

public void testMapGenericWriteMask() {
    final GENERIC_MAPPING mapping = new GENERIC_MAPPING();
    mapping.genericRead = new DWORD(FILE_GENERIC_READ);
    mapping.genericWrite = new DWORD(FILE_GENERIC_WRITE);
    mapping.genericExecute = new DWORD(FILE_GENERIC_EXECUTE);
    mapping.genericAll = new DWORD(FILE_ALL_ACCESS);
    final DWORDByReference rights = new DWORDByReference(new DWORD(GENERIC_WRITE));
    Advapi32.INSTANCE.MapGenericMask(rights, mapping);
    assertEquals(FILE_GENERIC_WRITE, rights.getValue().intValue());
    assertTrue(GENERIC_WRITE != (rights.getValue().intValue() & GENERIC_WRITE));
}
Also used : GENERIC_MAPPING(com.sun.jna.platform.win32.WinNT.GENERIC_MAPPING) DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference) DWORD(com.sun.jna.platform.win32.WinDef.DWORD)

Example 60 with DWORD

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

the class Advapi32Test method testGetSetSecurityInfoForFileWithSACL.

public void testGetSetSecurityInfoForFileWithSACL() throws Exception {
    boolean impersontating = false;
    HANDLEByReference phToken = new HANDLEByReference();
    HANDLEByReference phTokenDuplicate = new HANDLEByReference();
    try {
        // open thread or process token, elevate
        if (!Advapi32.INSTANCE.OpenThreadToken(Kernel32.INSTANCE.GetCurrentThread(), TOKEN_ADJUST_PRIVILEGES, false, phToken)) {
            assertEquals(W32Errors.ERROR_NO_TOKEN, Kernel32.INSTANCE.GetLastError());
            // OpenThreadToken may fail with W32Errors.ERROR_NO_TOKEN if current thread is anonymous.  When this happens,
            // we need to open the process token to duplicate it, then set our thread token.
            assertTrue(Advapi32.INSTANCE.OpenProcessToken(Kernel32.INSTANCE.GetCurrentProcess(), TOKEN_DUPLICATE, phToken));
            // Process token opened, now duplicate
            assertTrue(Advapi32.INSTANCE.DuplicateTokenEx(phToken.getValue(), TOKEN_ADJUST_PRIVILEGES | TOKEN_IMPERSONATE, null, SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, TOKEN_TYPE.TokenImpersonation, phTokenDuplicate));
            // And set thread token.
            assertTrue(Advapi32.INSTANCE.SetThreadToken(null, phTokenDuplicate.getValue()));
            impersontating = true;
        }
        // Which token to adjust depends on whether we had to impersonate or not.
        HANDLE tokenAdjust = impersontating ? phTokenDuplicate.getValue() : phToken.getValue();
        WinNT.TOKEN_PRIVILEGES tp = new WinNT.TOKEN_PRIVILEGES(1);
        WinNT.LUID pLuid = new WinNT.LUID();
        assertTrue(Advapi32.INSTANCE.LookupPrivilegeValue(null, SE_SECURITY_NAME, pLuid));
        tp.Privileges[0] = new WinNT.LUID_AND_ATTRIBUTES(pLuid, new DWORD(WinNT.SE_PRIVILEGE_ENABLED));
        assertTrue(Advapi32.INSTANCE.AdjustTokenPrivileges(tokenAdjust, false, tp, 0, null, null));
        assertTrue(Advapi32.INSTANCE.LookupPrivilegeValue(null, SE_RESTORE_NAME, pLuid));
        tp.Privileges[0] = new WinNT.LUID_AND_ATTRIBUTES(pLuid, new DWORD(WinNT.SE_PRIVILEGE_ENABLED));
        assertTrue(Advapi32.INSTANCE.AdjustTokenPrivileges(tokenAdjust, false, tp, 0, null, null));
        // create a temp file
        File file = createTempFile();
        int infoType = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION;
        PointerByReference ppsidOwner = new PointerByReference();
        PointerByReference ppsidGroup = new PointerByReference();
        PointerByReference ppDacl = new PointerByReference();
        PointerByReference ppSacl = new PointerByReference();
        PointerByReference ppSecurityDescriptor = new PointerByReference();
        String filePath = file.getAbsolutePath();
        HANDLE hFile = WinBase.INVALID_HANDLE_VALUE;
        try {
            try {
                hFile = Kernel32.INSTANCE.CreateFile(filePath, WinNT.ACCESS_SYSTEM_SECURITY | WinNT.GENERIC_WRITE | WinNT.WRITE_OWNER | WinNT.WRITE_DAC, WinNT.FILE_SHARE_READ, new WinBase.SECURITY_ATTRIBUTES(), WinNT.OPEN_EXISTING, WinNT.FILE_ATTRIBUTE_NORMAL, null);
                assertEquals("GetSecurityInfo(" + filePath + ")", 0, Advapi32.INSTANCE.GetSecurityInfo(hFile, AccCtrl.SE_OBJECT_TYPE.SE_FILE_OBJECT, infoType, ppsidOwner, ppsidGroup, ppDacl, ppSacl, ppSecurityDescriptor));
                assertEquals("SetSecurityInfo(" + filePath + ")", 0, Advapi32.INSTANCE.SetSecurityInfo(hFile, AccCtrl.SE_OBJECT_TYPE.SE_FILE_OBJECT, infoType, ppsidOwner.getValue(), ppsidGroup.getValue(), ppDacl.getValue(), ppSacl.getValue()));
            } finally {
                if (hFile != WinBase.INVALID_HANDLE_VALUE)
                    Kernel32.INSTANCE.CloseHandle(hFile);
                file.delete();
            }
        } finally {
            Kernel32Util.freeLocalMemory(ppSecurityDescriptor.getValue());
        }
        if (impersontating) {
            assertTrue("SetThreadToken", Advapi32.INSTANCE.SetThreadToken(null, null));
        } else {
            tp.Privileges[0] = new WinNT.LUID_AND_ATTRIBUTES(pLuid, new DWORD(0));
            assertTrue("AdjustTokenPrivileges", Advapi32.INSTANCE.AdjustTokenPrivileges(tokenAdjust, false, tp, 0, null, null));
        }
    } finally {
        Kernel32Util.closeHandleRefs(phToken, phTokenDuplicate);
    }
}
Also used : TOKEN_PRIVILEGES(com.sun.jna.platform.win32.WinNT.TOKEN_PRIVILEGES) TOKEN_PRIVILEGES(com.sun.jna.platform.win32.WinNT.TOKEN_PRIVILEGES) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) PointerByReference(com.sun.jna.ptr.PointerByReference) HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference) SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) File(java.io.File)

Aggregations

DWORD (com.sun.jna.platform.win32.WinDef.DWORD)56 PointerByReference (com.sun.jna.ptr.PointerByReference)23 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)17 File (java.io.File)17 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)16 Test (org.junit.Test)15 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)11 Memory (com.sun.jna.Memory)9 ULONG (com.sun.jna.platform.win32.WinDef.ULONG)9 ULONGByReference (com.sun.jna.platform.win32.WinDef.ULONGByReference)7 HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)7 GENERIC_MAPPING (com.sun.jna.platform.win32.WinNT.GENERIC_MAPPING)6 UINT_PTR (com.sun.jna.platform.win32.WinDef.UINT_PTR)5 TOKEN_PRIVILEGES (com.sun.jna.platform.win32.WinNT.TOKEN_PRIVILEGES)5 IntByReference (com.sun.jna.ptr.IntByReference)5 APPBARDATA (com.sun.jna.platform.win32.ShellAPI.APPBARDATA)4 WinNT (com.sun.jna.platform.win32.WinNT)4 Pointer (com.sun.jna.Pointer)3 SC_HANDLE (com.sun.jna.platform.win32.Winsvc.SC_HANDLE)3 BufferedWriter (java.io.BufferedWriter)3