Search in sources :

Example 16 with PointerByReference

use of com.sun.jna.ptr.PointerByReference in project jna by java-native-access.

the class Advapi32Test method testGetNamedSecurityInfoForFileWithSACL.

public void testGetNamedSecurityInfoForFileWithSACL() throws Exception {
    boolean impersontating = false;
    WinNT.LUID pLuid = new WinNT.LUID();
    assertTrue(Advapi32.INSTANCE.LookupPrivilegeValue(null, SE_SECURITY_NAME, pLuid));
    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);
        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));
        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();
        File file = createTempFile();
        String filePath = file.getAbsolutePath();
        try {
            try {
                assertEquals("GetNamedSecurityInfo(" + filePath + ")", 0, Advapi32.INSTANCE.GetNamedSecurityInfo(filePath, AccCtrl.SE_OBJECT_TYPE.SE_FILE_OBJECT, infoType, ppsidOwner, ppsidGroup, ppDacl, ppSacl, ppSecurityDescriptor));
            } finally {
                file.delete();
            }
        } finally {
            Kernel32Util.freeLocalMemory(ppSecurityDescriptor.getValue());
        }
        if (impersontating) {
            Advapi32.INSTANCE.SetThreadToken(null, null);
        } else {
            tp.Privileges[0] = new WinNT.LUID_AND_ATTRIBUTES(pLuid, new DWORD(0));
            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)

Example 17 with PointerByReference

use of com.sun.jna.ptr.PointerByReference in project jna by java-native-access.

the class Advapi32Test method testGetNamedSecurityInfoForFileNoSACL.

public void testGetNamedSecurityInfoForFileNoSACL() throws Exception {
    int infoType = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION;
    PointerByReference ppsidOwner = new PointerByReference();
    PointerByReference ppsidGroup = new PointerByReference();
    PointerByReference ppDacl = new PointerByReference();
    PointerByReference ppSecurityDescriptor = new PointerByReference();
    File file = createTempFile();
    try {
        try {
            assertEquals("GetNamedSecurityInfo(" + file + ")", 0, Advapi32.INSTANCE.GetNamedSecurityInfo(file.getAbsolutePath(), AccCtrl.SE_OBJECT_TYPE.SE_FILE_OBJECT, infoType, ppsidOwner, ppsidGroup, ppDacl, null, ppSecurityDescriptor));
        } finally {
            file.delete();
        }
    } finally {
        Kernel32Util.freeLocalMemory(ppSecurityDescriptor.getValue());
    }
}
Also used : PointerByReference(com.sun.jna.ptr.PointerByReference) File(java.io.File)

Example 18 with PointerByReference

use of com.sun.jna.ptr.PointerByReference in project jna by java-native-access.

the class RunningObjectTable_Test method IsRunning.

@Test
public void IsRunning() {
    PointerByReference pprot = new PointerByReference();
    HRESULT hr = Ole32.INSTANCE.GetRunningObjectTable(new DWORD(0), pprot);
    COMUtils.checkRC(hr);
    IRunningObjectTable rot = new RunningObjectTable(pprot.getValue());
//Can't yet be tested as IMoniker is not fully implemented,
//rot.IsRunning(pmkObjectName);
}
Also used : HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) PointerByReference(com.sun.jna.ptr.PointerByReference) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) Test(org.junit.Test)

Example 19 with PointerByReference

use of com.sun.jna.ptr.PointerByReference in project jna by java-native-access.

the class RunningObjectTable_Test method EnumRunning.

@Test
public void EnumRunning() {
    PointerByReference pprot = new PointerByReference();
    HRESULT hr = Ole32.INSTANCE.GetRunningObjectTable(new DWORD(0), pprot);
    COMUtils.checkRC(hr);
    IRunningObjectTable rot = new RunningObjectTable(pprot.getValue());
    PointerByReference ppenumMoniker = new PointerByReference();
    hr = rot.EnumRunning(ppenumMoniker);
    COMUtils.checkRC(hr);
    assertNotNull(ppenumMoniker.getValue());
}
Also used : HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) PointerByReference(com.sun.jna.ptr.PointerByReference) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) Test(org.junit.Test)

Example 20 with PointerByReference

use of com.sun.jna.ptr.PointerByReference in project jna by java-native-access.

the class TypeLibUtilTest method testFindName.

public void testFindName() {
    // Test is modelled after ITypeLibTest#testFindName
    TypeLibUtil shellTypeLib = loadShellTypeLib();
    String memberValue = "count";
    String memberValueOk = "Count";
    FindName result = shellTypeLib.FindName(memberValue, 0, (short) 100);
    // 2 matches come from manual tests
    assertEquals(2, result.getFound());
    // Check that function return corrected member name (Count) - see uppercase C
    assertEquals(memberValueOk, result.getNameBuf());
    // There have to be as many pointers as reported by pcFound
    ITypeInfo[] typelib = result.getTInfo();
    assertEquals(2, typelib.length);
    assertNotNull(typelib[0]);
    assertNotNull(typelib[1]);
    PointerByReference pbr = new PointerByReference();
    HRESULT hr = typelib[1].GetTypeAttr(pbr);
    assertTrue(COMUtils.SUCCEEDED(hr));
    OaIdl.TYPEATTR pTypeAttr = new OaIdl.TYPEATTR(pbr.getValue());
    // Either interface FolderItemVerbs ({1F8352C0-50B0-11CF-960C-0080C7F4EE85})
    // or FolderItems ({744129E0-CBE5-11CE-8350-444553540000})
    String typeGUID = pTypeAttr.guid.toGuidString();
    assertTrue(typeGUID.equals("{1F8352C0-50B0-11CF-960C-0080C7F4EE85}") || typeGUID.equals("{744129E0-CBE5-11CE-8350-444553540000}"));
    typelib[1].ReleaseTypeAttr(pTypeAttr);
}
Also used : TYPEATTR(com.sun.jna.platform.win32.OaIdl.TYPEATTR) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) PointerByReference(com.sun.jna.ptr.PointerByReference) TYPEATTR(com.sun.jna.platform.win32.OaIdl.TYPEATTR) OaIdl(com.sun.jna.platform.win32.OaIdl) FindName(com.sun.jna.platform.win32.COM.TypeLibUtil.FindName)

Aggregations

PointerByReference (com.sun.jna.ptr.PointerByReference)127 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)54 IntByReference (com.sun.jna.ptr.IntByReference)35 Pointer (com.sun.jna.Pointer)20 Test (org.junit.Test)19 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)18 REFIID (com.sun.jna.platform.win32.Guid.REFIID)13 File (java.io.File)13 ULONG (com.sun.jna.platform.win32.WinDef.ULONG)10 PSID (com.sun.jna.platform.win32.WinNT.PSID)10 Memory (com.sun.jna.Memory)8 WString (com.sun.jna.WString)8 Dispatch (com.sun.jna.platform.win32.COM.Dispatch)7 ULONGByReference (com.sun.jna.platform.win32.WinDef.ULONGByReference)7 WinNT (com.sun.jna.platform.win32.WinNT)7 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)7 IID (com.sun.jna.platform.win32.Guid.IID)6 ArrayList (java.util.ArrayList)6 IDispatch (com.sun.jna.platform.win32.COM.IDispatch)5 UINT (com.sun.jna.platform.win32.WinDef.UINT)5