use of com.sun.jna.platform.win32.Kernel32 in project jna by java-native-access.
the class WevtapiUtil method EvtGetChannelConfigProperty.
/**
* Gets the specified channel configuration property.
*
* @param channelHandle [in] A handle to the channel's configuration properties that
* the {@link Wevtapi#EvtOpenChannelConfig} function returns.
* @param propertyId [in] The identifier of the channel property to retrieve. For a list of property
* identifiers, see the {@link Winevt.EVT_CHANNEL_CONFIG_PROPERTY_ID} enumeration.
* @return EVT_VARIANT(already reading from native memory)
*/
public static EVT_VARIANT EvtGetChannelConfigProperty(EVT_HANDLE channelHandle, int propertyId) {
IntByReference propertyValueBufferUsed = new IntByReference();
boolean result = Wevtapi.INSTANCE.EvtGetChannelConfigProperty(channelHandle, propertyId, 0, 0, null, propertyValueBufferUsed);
int errorCode = Kernel32.INSTANCE.GetLastError();
if ((!result) && errorCode != Kernel32.ERROR_INSUFFICIENT_BUFFER) {
throw new Win32Exception(errorCode);
}
Memory propertyValueBuffer = new Memory(propertyValueBufferUsed.getValue());
result = Wevtapi.INSTANCE.EvtGetChannelConfigProperty(channelHandle, propertyId, 0, (int) propertyValueBuffer.size(), propertyValueBuffer, propertyValueBufferUsed);
if (!result) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
EVT_VARIANT resultEvt = new EVT_VARIANT(propertyValueBuffer);
resultEvt.read();
return resultEvt;
}
use of com.sun.jna.platform.win32.Kernel32 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.platform.win32.Kernel32 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);
}
use of com.sun.jna.platform.win32.Kernel32 in project android by JetBrains.
the class ElevatedCommandLine method executeAsShellCommand.
/**
* On Windows we will execute this command as a shell command.
* This allows us to specify elevated privileges with the "runas" parameter.
*/
private Process executeAsShellCommand() throws IOException {
// First create a wrapper that sets the current work directory, such that batch files
// may call other batch/executable files in the same directory without specifying the
// directory.
// Note: This was needed for the Haxm silent_install.bat.
String exeName = new File(getExePath()).getName();
File wrapper = FileUtil.createTempFile(FileUtil.getNameWithoutExtension(exeName) + "_wrapper", ".bat", true);
String exePath = new File(getExePath()).getParent();
File logFile = FileUtil.createTempFile("haxm_install_log", ".txt");
FileUtil.writeToFile(wrapper, String.format("@echo off\n" + "setlocal enableextensions\n\n" + "cd /d \"%1$s\"\n\n" + "%2$s -log %3$s %%*", exePath, exeName, logFile));
setExePath(wrapper.getPath());
// Setup capturing of stdout and stderr in files.
// ShellExecuteEx does not allow for the capture from code.
final File outFile = FileUtil.createTempFile("haxm_out", ".txt", true);
final File errFile = FileUtil.createTempFile("haxm_err", ".txt", true);
addParameters(">", outFile.getPath(), "2>", errFile.getPath());
final ShellExecuteInfo info = new ShellExecuteInfo(this);
BOOL returnValue = Shell32Ex.INSTANCE.ShellExecuteEx(info);
final int errorCode = returnValue.booleanValue() ? 0 : Kernel32.INSTANCE.GetLastError();
// and wrap stdout and stderr into their respective {@link InputStream}.
return new Process() {
private HANDLE myProcess = info.hProcess;
private IntByReference myExitCode = new IntByReference(errorCode);
@Override
public OutputStream getOutputStream() {
throw new RuntimeException("Unexpected behaviour");
}
@Override
public InputStream getInputStream() {
return toInputStream(outFile);
}
@Override
public InputStream getErrorStream() {
return toInputStream(errFile);
}
@Override
public int waitFor() {
if (myProcess != null) {
Kernel32.INSTANCE.WaitForSingleObject(myProcess, INFINITE);
Kernel32.INSTANCE.GetExitCodeProcess(myProcess, myExitCode);
Kernel32.INSTANCE.CloseHandle(myProcess);
myProcess = null;
}
return myExitCode.getValue();
}
@Override
public int exitValue() {
return waitFor();
}
@Override
public void destroy() {
waitFor();
}
private InputStream toInputStream(@NotNull File file) {
try {
waitFor();
return new FileInputStream(file);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
};
}
use of com.sun.jna.platform.win32.Kernel32 in project jna by java-native-access.
the class User32WindowMessagesTest method createWindowAndLoop.
public void createWindowAndLoop(String windowClass) {
// define new window class
HMODULE hInst = Kernel32.INSTANCE.GetModuleHandle("");
WNDCLASSEX wClass = new WNDCLASSEX();
wClass.hInstance = hInst;
wClass.lpfnWndProc = new WindowProc() {
@Override
public LRESULT callback(HWND hwnd, int uMsg, WPARAM wParam, LPARAM lParam) {
// log(hwnd + " - received a message : " + uMsg);
switch(uMsg) {
case WinUser.WM_CREATE:
{
log(hwnd + " - onCreate: WM_CREATE");
return new LRESULT(0);
}
case WinUser.WM_CLOSE:
log(hwnd + " WM_CLOSE");
User32.INSTANCE.DestroyWindow(hwnd);
return new LRESULT(0);
case WinUser.WM_DESTROY:
{
log(hwnd + " - on Destroy.");
User32.INSTANCE.PostQuitMessage(0);
return new LRESULT(0);
}
case WinUser.WM_USER:
{
log(hwnd + " - received a WM_USER message with code : '" + wParam + "' and value : '" + lParam + "'");
if (wParam.intValue() == MSG_SIMPLE_CODE) {
assertEqualsForCallbackExecution(MSG_SIMPLE_VAL, lParam.intValue());
}
if (wParam.intValue() == MSG_HOOKED_CODE) {
assertEqualsForCallbackExecution(MSG_HOOKED_VAL, lParam.intValue());
}
return new LRESULT(0);
}
case WinUser.WM_COPYDATA:
{
COPYDATASTRUCT copyDataStruct = new COPYDATASTRUCT(new Pointer(lParam.longValue()));
ULONG_PTR uMsg1 = copyDataStruct.dwData;
Pointer lParam1 = copyDataStruct.lpData;
int wParam1 = copyDataStruct.cbData;
log(hwnd + " - received a WM_COPYDATA message with code : '" + uMsg1 + "' of size : '" + wParam1 + "'");
switch(uMsg1.intValue()) {
case DATA_STRUCT_CODE:
{
MsgStruct msg = new MsgStruct(lParam1);
// log(hwnd + " - received structured content : " + msg.toString(true));
log(hwnd + " - message is of type MsgStruct with number = " + msg.number + " and message = '" + msg.message + "'");
assertEqualsForCallbackExecution(MSG_STRUCT_NUMBER, msg.number);
assertEqualsForCallbackExecution(MSG_STRUCT_VAL, msg.message);
}
}
return new LRESULT(0);
}
default:
return User32.INSTANCE.DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}
};
wClass.lpszClassName = windowClass;
// register window class
User32.INSTANCE.RegisterClassEx(wClass);
getLastError();
// create new window
HWND hWnd = User32.INSTANCE.CreateWindowEx(User32.WS_EX_TOPMOST, windowClass, "My hidden helper window, used only to catch the windows events", 0, 0, 0, 0, 0, null, null, hInst, null);
getLastError();
log("window sucessfully created! window hwnd: " + hWnd.getPointer().toString());
MSG msg = new MSG();
while (User32.INSTANCE.GetMessage(msg, hWnd, 0, 0) > 0) {
User32.INSTANCE.TranslateMessage(msg);
User32.INSTANCE.DispatchMessage(msg);
}
User32.INSTANCE.UnregisterClass(windowClass, hInst);
User32.INSTANCE.DestroyWindow(hWnd);
log("program exit!");
}
Aggregations