Search in sources :

Example 56 with PointerByReference

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

the class Kernel32Util method formatMessage.

/**
     * Format a message from the value obtained from
     * {@link Kernel32#GetLastError()} or {@link Native#getLastError()}.
     *
     * @param code The error code
     * @return Formatted message.
     */
public static String formatMessage(int code) {
    PointerByReference buffer = new PointerByReference();
    int nLen = Kernel32.INSTANCE.FormatMessage(WinBase.FORMAT_MESSAGE_ALLOCATE_BUFFER | WinBase.FORMAT_MESSAGE_FROM_SYSTEM | WinBase.FORMAT_MESSAGE_IGNORE_INSERTS, null, code, // TODO: // MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT)
    0, buffer, 0, null);
    if (nLen == 0) {
        throw new LastErrorException(Native.getLastError());
    }
    Pointer ptr = buffer.getValue();
    try {
        String s = ptr.getWideString(0);
        return s.trim();
    } finally {
        freeLocalMemory(ptr);
    }
}
Also used : PointerByReference(com.sun.jna.ptr.PointerByReference) LastErrorException(com.sun.jna.LastErrorException) Pointer(com.sun.jna.Pointer)

Example 57 with PointerByReference

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

the class Netapi32Util method getUsers.

/**
     * Get the names of users on a computer.
     * @param serverName Name of the computer.
     * @return An array of users.
     */
public static User[] getUsers(String serverName) {
    PointerByReference bufptr = new PointerByReference();
    IntByReference entriesRead = new IntByReference();
    IntByReference totalEntries = new IntByReference();
    try {
        int rc = Netapi32.INSTANCE.NetUserEnum(serverName, 1, 0, bufptr, LMCons.MAX_PREFERRED_LENGTH, entriesRead, totalEntries, null);
        if (LMErr.NERR_Success != rc || bufptr.getValue() == Pointer.NULL) {
            throw new Win32Exception(rc);
        }
        LMAccess.USER_INFO_1 user = new LMAccess.USER_INFO_1(bufptr.getValue());
        LMAccess.USER_INFO_1[] users = (LMAccess.USER_INFO_1[]) user.toArray(entriesRead.getValue());
        ArrayList<User> result = new ArrayList<User>();
        for (LMAccess.USER_INFO_1 lu : users) {
            User auser = new User();
            if (lu.usri1_name != null) {
                auser.name = lu.usri1_name.toString();
            }
            result.add(auser);
        }
        return result.toArray(new User[0]);
    } finally {
        if (bufptr.getValue() != Pointer.NULL) {
            int rc = Netapi32.INSTANCE.NetApiBufferFree(bufptr.getValue());
            if (LMErr.NERR_Success != rc) {
                throw new Win32Exception(rc);
            }
        }
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) PointerByReference(com.sun.jna.ptr.PointerByReference) ArrayList(java.util.ArrayList)

Example 58 with PointerByReference

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

the class Netapi32Util method getUserGroups.

/**
     * Get groups of a given user on a given system.
     * @param userName User name.
     * @param serverName Server name.
     * @return Groups.
     */
public static Group[] getUserGroups(String userName, String serverName) {
    PointerByReference bufptr = new PointerByReference();
    IntByReference entriesread = new IntByReference();
    IntByReference totalentries = new IntByReference();
    try {
        int rc = Netapi32.INSTANCE.NetUserGetGroups(serverName, userName, 0, bufptr, LMCons.MAX_PREFERRED_LENGTH, entriesread, totalentries);
        if (rc != LMErr.NERR_Success) {
            throw new Win32Exception(rc);
        }
        GROUP_USERS_INFO_0 lgroup = new GROUP_USERS_INFO_0(bufptr.getValue());
        GROUP_USERS_INFO_0[] lgroups = (GROUP_USERS_INFO_0[]) lgroup.toArray(entriesread.getValue());
        ArrayList<Group> result = new ArrayList<Group>();
        for (GROUP_USERS_INFO_0 lgpi : lgroups) {
            Group lgp = new Group();
            if (lgpi.grui0_name != null) {
                lgp.name = lgpi.grui0_name.toString();
            }
            result.add(lgp);
        }
        return result.toArray(new Group[0]);
    } finally {
        if (bufptr.getValue() != Pointer.NULL) {
            int rc = Netapi32.INSTANCE.NetApiBufferFree(bufptr.getValue());
            if (LMErr.NERR_Success != rc) {
                throw new Win32Exception(rc);
            }
        }
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) GROUP_USERS_INFO_0(com.sun.jna.platform.win32.LMAccess.GROUP_USERS_INFO_0) LOCALGROUP_USERS_INFO_0(com.sun.jna.platform.win32.LMAccess.LOCALGROUP_USERS_INFO_0) PointerByReference(com.sun.jna.ptr.PointerByReference) ArrayList(java.util.ArrayList)

Example 59 with PointerByReference

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

the class Netapi32Util method getUserInfo.

public static UserInfo getUserInfo(String accountName, String domainName) {
    PointerByReference bufptr = new PointerByReference();
    int rc = -1;
    try {
        rc = Netapi32.INSTANCE.NetUserGetInfo(domainName, accountName, (short) 23, bufptr);
        if (rc == LMErr.NERR_Success) {
            USER_INFO_23 info_23 = new USER_INFO_23(bufptr.getValue());
            UserInfo userInfo = new UserInfo();
            if (info_23.usri23_comment != null) {
                userInfo.comment = info_23.usri23_comment.toString();
            }
            userInfo.flags = info_23.usri23_flags;
            if (info_23.usri23_full_name != null) {
                userInfo.fullName = info_23.usri23_full_name.toString();
            }
            if (info_23.usri23_name != null) {
                userInfo.name = info_23.usri23_name.toString();
            }
            if (info_23.usri23_user_sid != null) {
                userInfo.sidString = Advapi32Util.convertSidToStringSid(info_23.usri23_user_sid);
            }
            userInfo.sid = info_23.usri23_user_sid;
            return userInfo;
        } else {
            throw new Win32Exception(rc);
        }
    } finally {
        if (bufptr.getValue() != Pointer.NULL) {
            Netapi32.INSTANCE.NetApiBufferFree(bufptr.getValue());
        }
    }
}
Also used : PointerByReference(com.sun.jna.ptr.PointerByReference) USER_INFO_23(com.sun.jna.platform.win32.LMAccess.USER_INFO_23)

Example 60 with PointerByReference

use of com.sun.jna.ptr.PointerByReference in project dukescript-presenters by dukescript.

the class WebKitPresenter method defineFn.

@Override
public Fn defineFn(String code, String[] names, boolean[] keepAlive) {
    JSC jsc = shell.jsc();
    Pointer[] jsNames = new Pointer[names.length];
    for (int i = 0; i < jsNames.length; i++) {
        jsNames[i] = jsc.JSStringCreateWithUTF8CString(names[i]);
    }
    Pointer jsCode = jsc.JSStringCreateWithUTF8CString(code);
    PointerByReference exc = new PointerByReference();
    Pointer fn = jsc.JSObjectMakeFunction(ctx, null, names.length, jsNames, jsCode, null, 1, exc);
    if (fn == null) {
        throw new IllegalStateException("Cannot initialize function: " + exc.getValue());
    }
    jsc.JSStringRelease(jsCode);
    for (Pointer jsName : jsNames) {
        jsc.JSStringRelease(jsName);
    }
    return new JSCFn(fn, keepAlive);
}
Also used : PointerByReference(com.sun.jna.ptr.PointerByReference) Pointer(com.sun.jna.Pointer) JSC(com.dukescript.presenters.renderer.JSC)

Aggregations

PointerByReference (com.sun.jna.ptr.PointerByReference)128 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