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);
}
}
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);
}
}
}
}
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);
}
}
}
}
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());
}
}
}
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);
}
Aggregations