use of com.sun.jna.platform.win32.WinDef.CHAR in project jna by java-native-access.
the class WTypesTest method testBSTRBasic.
public void testBSTRBasic() {
String demoString = "inputÖäÜ?!";
// Allocation via system and the "correct" way
BSTR sysAllocated = OleAuto.INSTANCE.SysAllocString(demoString);
// Java based allocation - not suitable if passed via automation
BSTR javaAllocated = new BSTR(demoString);
// Ensure encoding roundtripping works
assertEquals(demoString, sysAllocated.getValue());
assertEquals(demoString, javaAllocated.getValue());
// BSTR is encoded as UTF-16/UCS2, so byte length is 2 * char count
assertEquals(demoString.length(), OleAuto.INSTANCE.SysStringLen(sysAllocated));
assertEquals(demoString.length(), OleAuto.INSTANCE.SysStringLen(javaAllocated));
assertEquals(2 * demoString.length(), OleAuto.INSTANCE.SysStringByteLen(sysAllocated));
assertEquals(2 * demoString.length(), OleAuto.INSTANCE.SysStringByteLen(javaAllocated));
// The BSTR Pointer points 4 bytes into the data itself (beginning of data
// string, the 4 preceding bytes code the string length (in bytes)
assertEquals(2 * demoString.length(), sysAllocated.getPointer().getInt(-4));
assertEquals(2 * demoString.length(), javaAllocated.getPointer().getInt(-4));
OleAuto.INSTANCE.SysFreeString(sysAllocated);
// javaAllocated is allocated via Memory and will be freeed by the
// garbadge collector automaticly
}
use of com.sun.jna.platform.win32.WinDef.CHAR in project processing by processing.
the class WindowsRegistry method getValues.
/**
* Get all values under a key.
*
* @param rootKey root key
* @param key jey name
* @throws java.io.UnsupportedEncodingException on error
* @return TreeMap with name and value pairs
*/
public static TreeMap<String, Object> getValues(REGISTRY_ROOT_KEY rootKey, String key) throws UnsupportedEncodingException {
//Advapi32 advapi32;
//int handle = 0, dwIndex, result = 0;
//char[] lpValueName;
//byte[] lpData;
//IntByReference lpcchValueName, lpType, lpcbData;
//String name;
TreeMap<String, Object> values = new TreeMap<String, Object>(String.CASE_INSENSITIVE_ORDER);
Advapi32 advapi32 = Advapi32.INSTANCE;
HKEY handle = openKey(rootKey, key, WinNT.KEY_READ);
char[] lpValueName = new char[16384];
IntByReference lpcchValueName = new IntByReference(16384);
IntByReference lpType = new IntByReference();
byte[] lpData = new byte[1];
IntByReference lpcbData = new IntByReference();
//if(handle != 0) {
if (handle != null) {
int dwIndex = 0;
int result = 0;
String name;
do {
lpcbData.setValue(0);
result = advapi32.RegEnumValue(handle, dwIndex, lpValueName, lpcchValueName, null, lpType, lpData, lpcbData);
if (result == WinError.ERROR_MORE_DATA) {
lpData = new byte[lpcbData.getValue()];
lpcchValueName = new IntByReference(16384);
result = advapi32.RegEnumValue(handle, dwIndex, lpValueName, lpcchValueName, null, lpType, lpData, lpcbData);
if (result == WinError.ERROR_SUCCESS) {
name = new String(lpValueName, 0, lpcchValueName.getValue());
switch(lpType.getValue()) {
case WinNT.REG_SZ:
values.put(name, convertBufferToString(lpData));
break;
case WinNT.REG_DWORD:
values.put(name, convertBufferToInt(lpData));
break;
default:
break;
}
}
}
dwIndex++;
} while (result == WinError.ERROR_SUCCESS);
advapi32.RegCloseKey(handle);
}
return values;
}
use of com.sun.jna.platform.win32.WinDef.CHAR in project HearthStats.net-Uploader by HearthStats.
the class ProgramHelperWindows method getWindowHandle.
private HWND getWindowHandle() {
// Cache the window handle for five seconds to reduce CPU load and (possibly) minimise memory leaks
long currentTime = System.currentTimeMillis();
if (currentTime < lastWindowsHandleCheck + 5000) {
// It has been less than five seconds since the last check, so use the cached value
return windowHandle;
} else {
debugLog.debug("Updating window handle ({}ms since last update)", currentTime - lastWindowsHandleCheck);
lastWindowsHandleCheck = currentTime;
windowHandle = null;
}
User32.INSTANCE.EnumWindows(new WNDENUMPROC() {
@Override
public boolean callback(HWND hWnd, Pointer arg1) {
int titleLength = User32.INSTANCE.GetWindowTextLength(hWnd) + 1;
char[] title = new char[titleLength];
User32.INSTANCE.GetWindowText(hWnd, title, titleLength);
String wText = Native.toString(title);
if (wText.isEmpty()) {
return true;
}
PointerByReference pointer = new PointerByReference();
User32DLL.GetWindowThreadProcessId(hWnd, pointer);
Pointer process = Kernel32.OpenProcess(Kernel32.PROCESS_QUERY_INFORMATION | Kernel32.PROCESS_VM_READ, false, pointer.getValue());
Psapi.GetModuleBaseNameW(process, null, baseNameBuffer, STRING_BUFFER_LENGTH);
String baseNameString = Native.toString(baseNameBuffer);
// see https://github.com/JeromeDane/HearthStats.net-Uploader/issues/66#issuecomment-33829132
User32.INSTANCE.GetClassName(hWnd, classNameBuffer, STRING_BUFFER_LENGTH);
String classNameString = Native.toString(classNameBuffer);
if (baseNameString.equals(processName) && classNameString.equals("UnityWndClass")) {
windowHandle = hWnd;
if (windowHandleId == null) {
windowHandleId = windowHandle.toString();
if (lastKnownWindowHandleId == null || lastKnownWindowHandleId != windowHandleId) {
// The window handle has changed, so try to find the location the HearthStats executable. This is used to
// find the HS log file. Only compatible with Windows Vista and later, so we skip for Windows XP.
lastKnownWindowHandleId = windowHandleId;
if (Environment.isOsVersionAtLeast(6, 0)) {
debugLog.debug("Windows version is Vista or later so the location of the Hearthstone is being determined from the process");
Kernel32.QueryFullProcessImageNameW(process, 0, processFileNameBuffer, lpdwSize);
String processFileNameString = Native.toString(processFileNameBuffer);
if (processFileNameString != null) {
int lastSlash = processFileNameString.lastIndexOf('\\');
hearthstoneProcessFolder = processFileNameString.substring(0, lastSlash);
}
}
}
_notifyObserversOfChangeTo("Hearthstone window found with process name " + processName);
}
}
return true;
}
}, null);
// notify of window lost
if (windowHandle == null && windowHandleId != null) {
_notifyObserversOfChangeTo("Hearthstone window with process name " + processName + " closed");
windowHandleId = null;
}
return windowHandle;
}
Aggregations