use of com.sun.jna.ptr.IntByReference in project jna by java-native-access.
the class Rasapi32Util method getIPProjection.
/**
* Get the connection's IP projection
* @param hrasConn the RAS connection handle
* @return the IP projection
* @throws Ras32Exception errors
*/
public static RASPPPIP getIPProjection(HANDLE hrasConn) throws Ras32Exception {
RASPPPIP pppIpProjection = new RASPPPIP();
IntByReference lpcb = new IntByReference(pppIpProjection.size());
pppIpProjection.write();
int err = Rasapi32.INSTANCE.RasGetProjectionInfo(hrasConn, RASP_PppIp, pppIpProjection.getPointer(), lpcb);
if (err != WinError.ERROR_SUCCESS)
throw new Ras32Exception(err);
pppIpProjection.read();
return pppIpProjection;
}
use of com.sun.jna.ptr.IntByReference in project jna by java-native-access.
the class Secur32Util method getUserNameEx.
/**
* Retrieves the name of the user or other security principal associated
* with the calling thread.
*
* @param format User name format.
* @return User name in a given format.
*/
public static String getUserNameEx(int format) {
char[] buffer = new char[128];
IntByReference len = new IntByReference(buffer.length);
boolean result = Secur32.INSTANCE.GetUserNameEx(format, buffer, len);
if (!result) {
int rc = Kernel32.INSTANCE.GetLastError();
switch(rc) {
case W32Errors.ERROR_MORE_DATA:
buffer = new char[len.getValue() + 1];
break;
default:
throw new Win32Exception(Native.getLastError());
}
result = Secur32.INSTANCE.GetUserNameEx(format, buffer, len);
}
if (!result) {
throw new Win32Exception(Native.getLastError());
}
return Native.toString(buffer);
}
use of com.sun.jna.ptr.IntByReference in project jna by java-native-access.
the class User32Util method GetRawInputDeviceList.
public static final List<RAWINPUTDEVICELIST> GetRawInputDeviceList() {
IntByReference puiNumDevices = new IntByReference(0);
RAWINPUTDEVICELIST placeholder = new RAWINPUTDEVICELIST();
int cbSize = placeholder.sizeof();
// first call is with NULL so we query the expected number of devices
int returnValue = User32.INSTANCE.GetRawInputDeviceList(null, puiNumDevices, cbSize);
if (returnValue != 0) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
int deviceCount = puiNumDevices.getValue();
RAWINPUTDEVICELIST[] records = (RAWINPUTDEVICELIST[]) placeholder.toArray(deviceCount);
returnValue = User32.INSTANCE.GetRawInputDeviceList(records, puiNumDevices, cbSize);
if (returnValue == (-1)) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
if (returnValue != records.length) {
throw new IllegalStateException("Mismatched allocated (" + records.length + ") vs. received devices count (" + returnValue + ")");
}
return Arrays.asList(records);
}
use of com.sun.jna.ptr.IntByReference in project jna by java-native-access.
the class Rasapi32Util method getRasConnection.
/**
* Return a RAS connection by name
* @param connName the connection name
* @return the RAS connection structure
* @throws Ras32Exception errors
*/
public static HANDLE getRasConnection(String connName) throws Ras32Exception {
// size the array needed
IntByReference lpcb = new IntByReference(0);
IntByReference lpcConnections = new IntByReference();
int err = Rasapi32.INSTANCE.RasEnumConnections(null, lpcb, lpcConnections);
if (err != WinError.ERROR_SUCCESS && err != WinRas.ERROR_BUFFER_TOO_SMALL)
throw new Ras32Exception(err);
if (lpcb.getValue() == 0)
return null;
// get the connections
RASCONN[] connections = new RASCONN[lpcConnections.getValue()];
for (int i = 0; i < lpcConnections.getValue(); i++) connections[i] = new RASCONN();
lpcb = new IntByReference(connections[0].dwSize * lpcConnections.getValue());
err = Rasapi32.INSTANCE.RasEnumConnections(connections, lpcb, lpcConnections);
if (err != WinError.ERROR_SUCCESS)
throw new Ras32Exception(err);
// find connection
for (int i = 0; i < lpcConnections.getValue(); i++) {
if (new String(connections[i].szEntryName).equals(connName))
return connections[i].hrasconn;
}
return null;
}
use of com.sun.jna.ptr.IntByReference in project intellij-community by JetBrains.
the class Restarter method restartOnWindows.
private static void restartOnWindows(String... beforeRestart) throws IOException {
Kernel32 kernel32 = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);
Shell32 shell32 = (Shell32) Native.loadLibrary("shell32", Shell32.class);
int pid = kernel32.GetCurrentProcessId();
IntByReference argc = new IntByReference();
Pointer argvPtr = shell32.CommandLineToArgvW(kernel32.GetCommandLineW(), argc);
String[] argv = getRestartArgv(argvPtr.getWideStringArray(0, argc.getValue()));
kernel32.LocalFree(argvPtr);
// See https://blogs.msdn.microsoft.com/oldnewthing/20060515-07/?p=31203
// argv[0] as the program name is only a convention, i.e. there is no guarantee
// the name is the full path to the executable.
//
// See https://msdn.microsoft.com/en-us/library/windows/desktop/ms683197(v=vs.85).aspx
// To retrieve the full path to the executable, use "GetModuleFileName(NULL, ...)".
//
// Note: We use 32,767 as buffer size to avoid limiting ourselves to MAX_PATH (260).
char[] buffer = new char[32767];
if (kernel32.GetModuleFileNameW(null, buffer, new WinDef.DWORD(buffer.length)).intValue() > 0) {
argv[0] = Native.toString(buffer);
}
List<String> args = new ArrayList<>();
args.add(String.valueOf(pid));
args.add(String.valueOf(beforeRestart.length));
Collections.addAll(args, beforeRestart);
args.add(String.valueOf(argv.length));
Collections.addAll(args, argv);
runRestarter(new File(PathManager.getBinPath(), "restarter.exe"), args);
// Since the process ID is passed through the command line, we want to make sure that we don't exit before the "restarter"
// process has a chance to open the handle to our process, and that it doesn't wait for the termination of an unrelated
// process which happened to have the same process ID.
TimeoutUtil.sleep(500);
}
Aggregations