use of com.sun.jna.Pointer in project dukescript-presenters by dukescript.
the class WebKitPresenter method convertFromJava.
Pointer[] convertFromJava(Object[] args, boolean[] keepAlive) throws Exception {
JSC jsc = shell.jsc();
Pointer[] arr = new Pointer[args.length];
for (int i = 0; i < arr.length; i++) {
Object v = args[i];
if (v == null) {
v = jsc.JSValueMakeNull(ctx);
} else if (v instanceof Number) {
v = jsc.JSValueMakeNumber(ctx, ((Number) v).doubleValue());
} else if (v instanceof Boolean) {
v = ((Boolean) v) ? valueTrue : valueFalse;
} else if (v instanceof String) {
Pointer str = jsc.JSStringCreateWithUTF8CString((String) v);
v = jsc.JSValueMakeString(ctx, str);
jsc.JSStringRelease(str);
} else if (v instanceof Enum) {
Pointer str = jsc.JSStringCreateWithUTF8CString(((Enum) v).name());
v = jsc.JSValueMakeString(ctx, str);
jsc.JSStringRelease(str);
} else if (v instanceof Character) {
v = jsc.JSValueMakeNumber(ctx, (Character) v);
} else if (v instanceof JSObject) {
v = ((JSObject) v).value;
} else if (v instanceof int[]) {
int[] numbers = (int[]) v;
Pointer[] content = new Pointer[numbers.length];
for (int j = 0; j < content.length; j++) {
content[j] = jsc.JSValueMakeNumber(ctx, numbers[j]);
}
v = jsc.JSObjectMakeArray(ctx, content.length, content, null);
} else if (v instanceof double[]) {
double[] numbers = (double[]) v;
Pointer[] content = new Pointer[numbers.length];
for (int j = 0; j < content.length; j++) {
content[j] = jsc.JSValueMakeNumber(ctx, numbers[j]);
}
v = jsc.JSObjectMakeArray(ctx, content.length, content, null);
} else if (v instanceof Object[]) {
Pointer[] content = convertFromJava((Object[]) v);
v = jsc.JSObjectMakeArray(ctx, content.length, content, null);
} else if (v.getClass().isArray()) {
int len = Array.getLength(v);
Object[] boxed = new Object[len];
for (int j = 0; j < len; j++) {
boxed[j] = Array.get(v, j);
}
Pointer[] content = convertFromJava(boxed);
v = jsc.JSObjectMakeArray(ctx, content.length, content, null);
} else if (v.getClass().getSimpleName().equals("$JsCallbacks$")) {
Pointer vm = jsc.JSObjectMake(ctx, null, null);
for (Method method : v.getClass().getMethods()) {
if (method.getDeclaringClass() != v.getClass()) {
continue;
}
Pointer name = jsc.JSStringCreateWithUTF8CString(method.getName());
FnCallback fnC = new FnCallback(v, method);
toJava.put(fnC, fnC);
Pointer fn = jsc.JSObjectMakeFunctionWithCallback(ctx, null, fnC);
jsc.JSObjectSetProperty(ctx, vm, name, fn, 0, null);
jsc.JSStringRelease(name);
}
v = vm;
} else {
Pointer p = jsc.JSObjectMake(ctx, javaClazz, v);
if (keepAlive == null || keepAlive[i]) {
toJava.put(p, v);
} else {
toJava.put(p, new WeakVal(v));
}
protect(v, p);
v = p;
}
arr[i] = (Pointer) v;
}
return arr;
}
use of com.sun.jna.Pointer in project dukescript-presenters by dukescript.
the class WebKitPresenter method jsContext.
final void jsContext(Pointer ctx) {
this.ctx = ctx;
JSC jsc = shell.jsc();
onFinalize = new WebKitPresenter.OnFinalize();
javaClazz = jsc.JSClassCreate(new JSC.JSClassDefinition(onFinalize));
boolean testInstance = false;
assert testInstance = true;
if (testInstance) {
Pointer testObj = jsc.JSObjectMake(ctx, javaClazz, null);
assert isJavaClazz(testObj) : "Own classes has to be recognized";
}
{
Pointer jsGlobal = ctx;
Pointer arrArg = jsc.JSStringCreateWithUTF8CString("x");
Pointer arrT = jsc.JSStringCreateWithUTF8CString("var res = x.constructor === Array ? x.length : -1; return res;");
Pointer arrFn = jsc.JSObjectMakeFunction(jsGlobal, null, 1, new Pointer[] { arrArg }, arrT, null, 0, null);
arrayLength = arrFn;
jsc.JSValueProtect(ctx, arrFn);
assert !isJavaClazz(arrayLength) : "functions aren't Java classes";
}
{
Pointer trueScr = jsc.JSStringCreateWithUTF8CString("true");
valueTrue = jsc.JSEvaluateScript(ctx, trueScr, null, null, 1, null);
jsc.JSStringRelease(trueScr);
jsc.JSValueProtect(ctx, valueTrue);
int vT = jsc.JSValueGetType(ctx, valueTrue);
assert vT == 2;
assert !isJavaClazz(valueTrue) : "true isn't Java class";
}
{
Pointer falseScr = jsc.JSStringCreateWithUTF8CString("false");
valueFalse = jsc.JSEvaluateScript(ctx, falseScr, null, null, 1, null);
jsc.JSValueProtect(ctx, valueFalse);
jsc.JSStringRelease(falseScr);
int vF = jsc.JSValueGetType(ctx, valueFalse);
assert vF == 2;
assert !isJavaClazz(valueFalse) : "false isn't Java class";
}
}
use of com.sun.jna.Pointer 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!");
}
use of com.sun.jna.Pointer in project jna by java-native-access.
the class User32WindowMessagesTest method hookwinProc.
private HHOOK hookwinProc(HWND hwndToHook) {
HOOKPROC hookProc = new HOOKPROC() {
/**
* Callback method. cf : https://msdn.microsoft.com/en-us/library/windows/desktop/ms644975(v=vs.85).aspx
*
* nCode [in] : Specifies whether the hook procedure must process the message. If nCode is HC_ACTION, the
* hook procedure must process the message. If nCode is less than zero, the hook procedure must pass the
* message to the CallNextHookEx function without further processing and must return the value returned by
* CallNextHookEx. wParam [in] : Specifies whether the message was sent by the current thread. If the
* message was sent by the current thread, it is nonzero; otherwise, it is zero. lParam [in] : A pointer to
* a CWPSTRUCT structure that contains details about the message.
*
*/
// used by introspection from jna.
@SuppressWarnings("unused")
public LRESULT callback(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode < 0) {
return User32.INSTANCE.CallNextHookEx(null, nCode, wParam, lParam);
}
try {
WinUser.CWPSTRUCT cwp = new WinUser.CWPSTRUCT(new Pointer(lParam.longValue()));
switch(cwp.message) {
case WinUser.WM_USER:
{
HWND hWndSource = new HWND(new Pointer(cwp.wParam.longValue()));
log(cwp.hwnd + " - Received a message from " + hWndSource + " hooked proc : code= " + cwp.wParam + ", value = " + cwp.lParam);
assertEqualsForCallbackExecution(MSG_HOOKED_CODE, cwp.wParam.intValue());
assertEqualsForCallbackExecution(MSG_HOOKED_VAL, cwp.lParam.intValue());
return new LRESULT(0);
}
}
// Send message to next hook.
return User32.INSTANCE.CallNextHookEx(null, nCode, wParam, lParam);
} catch (Throwable t) {
t.printStackTrace();
return new LRESULT(0);
}
}
};
HINSTANCE hInst = Kernel32.INSTANCE.GetModuleHandle(null);
int threadtoHook = User32.INSTANCE.GetWindowThreadProcessId(hwndToHook, null);
// Hook of the wndProc
return User32.INSTANCE.SetWindowsHookEx(WinUser.WH_CALLWNDPROC, hookProc, hInst, threadtoHook);
}
use of com.sun.jna.Pointer in project jna by java-native-access.
the class SAFEARRAYTest method testDataTypes.
@Test
public void testDataTypes() {
int idx = 1;
Pointer dataPointer;
SAFEARRAY sa;
long elementSize;
Object[] objectResult;
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_BOOL), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(2L));
dataPointer = sa.accessData();
sa.putElement(true, idx);
short[] shortResult = dataPointer.getShortArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((Boolean) sa.getElement(idx), equalTo(true));
assertThat(shortResult[idx], equalTo((short) 0xFFFF));
assertThat((Short) dataPointer.getShort(idx * elementSize), equalTo((short) 0xFFFF));
assertThat((Boolean) objectResult[idx], equalTo(true));
sa.unaccessData();
sa.destroy();
byte testByte = 67;
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_UI1), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(1L));
dataPointer = sa.accessData();
sa.putElement(testByte, idx);
byte[] byteResult = dataPointer.getByteArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((Byte) sa.getElement(idx), equalTo(testByte));
assertThat(dataPointer.getByte(idx * elementSize), equalTo(testByte));
assertThat(byteResult[idx], equalTo(testByte));
assertThat((Byte) objectResult[idx], equalTo(testByte));
sa.unaccessData();
sa.destroy();
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_I1), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(1L));
dataPointer = sa.accessData();
sa.putElement(testByte, idx);
byteResult = dataPointer.getByteArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((Byte) sa.getElement(idx), equalTo(testByte));
assertThat(dataPointer.getByte(idx * elementSize), equalTo(testByte));
assertThat(byteResult[idx], equalTo(testByte));
assertThat((Byte) objectResult[idx], equalTo(testByte));
sa.unaccessData();
sa.destroy();
short testShort = Short.MAX_VALUE - 1;
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_UI2), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(2L));
dataPointer = sa.accessData();
sa.putElement(testShort, idx);
shortResult = dataPointer.getShortArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((Short) sa.getElement(idx), equalTo(testShort));
assertThat(dataPointer.getShort(idx * elementSize), equalTo(testShort));
assertThat(shortResult[idx], equalTo(testShort));
assertThat((Short) objectResult[idx], equalTo(testShort));
sa.unaccessData();
sa.destroy();
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_I2), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(2L));
dataPointer = sa.accessData();
sa.putElement(testShort, idx);
shortResult = dataPointer.getShortArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((Short) sa.getElement(idx), equalTo(testShort));
assertThat(dataPointer.getShort(idx * elementSize), equalTo(testShort));
assertThat(shortResult[idx], equalTo(testShort));
assertThat((Short) objectResult[idx], equalTo(testShort));
sa.unaccessData();
sa.destroy();
int testInt = Integer.MAX_VALUE - 1;
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_UI4), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(4L));
dataPointer = sa.accessData();
sa.putElement(testInt, idx);
int[] intResult = dataPointer.getIntArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((Integer) sa.getElement(idx), equalTo(testInt));
assertThat(dataPointer.getInt(idx * elementSize), equalTo(testInt));
assertThat(intResult[idx], equalTo(testInt));
assertThat((Integer) objectResult[idx], equalTo(testInt));
sa.unaccessData();
sa.destroy();
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_I4), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(4L));
dataPointer = sa.accessData();
sa.putElement(testInt, idx);
intResult = dataPointer.getIntArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((Integer) sa.getElement(idx), equalTo(testInt));
assertThat(dataPointer.getInt(idx * elementSize), equalTo(testInt));
assertThat(intResult[idx], equalTo(testInt));
assertThat((Integer) objectResult[idx], equalTo(testInt));
sa.unaccessData();
sa.destroy();
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_UINT), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(4L));
dataPointer = sa.accessData();
sa.putElement(testInt, idx);
intResult = dataPointer.getIntArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((Integer) sa.getElement(idx), equalTo(testInt));
assertThat(dataPointer.getInt(idx * elementSize), equalTo(testInt));
assertThat(intResult[idx], equalTo(testInt));
assertThat((Integer) objectResult[idx], equalTo(testInt));
sa.unaccessData();
sa.destroy();
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_INT), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(4L));
dataPointer = sa.accessData();
sa.putElement(testInt, idx);
intResult = dataPointer.getIntArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((Integer) sa.getElement(idx), equalTo(testInt));
assertThat(dataPointer.getInt(idx * elementSize), equalTo(testInt));
assertThat(intResult[idx], equalTo(testInt));
assertThat((Integer) objectResult[idx], equalTo(testInt));
sa.unaccessData();
sa.destroy();
SCODE testSCODE = new SCODE(47);
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_ERROR), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(4L));
dataPointer = sa.accessData();
sa.putElement(testSCODE, idx);
intResult = dataPointer.getIntArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((SCODE) sa.getElement(idx), equalTo(testSCODE));
assertThat(dataPointer.getInt(idx * elementSize), equalTo(47));
assertThat(intResult[idx], equalTo(47));
assertThat((SCODE) objectResult[idx], equalTo(testSCODE));
sa.unaccessData();
sa.destroy();
float testFloat = 42.23f;
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_R4), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(4L));
dataPointer = sa.accessData();
sa.putElement(testFloat, idx);
float[] floatResult = dataPointer.getFloatArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((Float) sa.getElement(idx), equalTo(testFloat));
assertThat((Float) dataPointer.getFloat(idx * elementSize), equalTo(testFloat));
assertThat(floatResult[idx], equalTo(testFloat));
assertThat((Float) objectResult[idx], equalTo(testFloat));
sa.unaccessData();
sa.destroy();
double testDouble = 42.23d;
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_R8), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(8L));
dataPointer = sa.accessData();
sa.putElement(testDouble, idx);
double[] doubleResult = dataPointer.getDoubleArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((Double) sa.getElement(idx), equalTo(testDouble));
assertThat((Double) dataPointer.getDouble(idx * elementSize), equalTo(testDouble));
assertThat(doubleResult[idx], equalTo(testDouble));
assertThat((Double) objectResult[idx], equalTo(testDouble));
sa.unaccessData();
sa.destroy();
Date testDate = new Date(1923, 1, 1, 5, 0, 0);
DATE testDATE = new DATE(testDate);
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_DATE), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(8L));
dataPointer = sa.accessData();
sa.putElement(testDATE, idx);
doubleResult = dataPointer.getDoubleArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat(((DATE) sa.getElement(idx)).date, equalTo(testDATE.date));
assertThat((Double) dataPointer.getDouble(idx * elementSize), equalTo(testDATE.date));
assertThat(((DATE) sa.getElement(idx)).getAsJavaDate(), equalTo(testDate));
assertThat(doubleResult[idx], equalTo(testDATE.date));
assertThat((Date) objectResult[idx], equalTo(testDate));
sa.unaccessData();
sa.destroy();
String testString = "äöüßAE!";
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_BSTR), 2);
elementSize = sa.getElemsize();
dataPointer = sa.accessData();
sa.putElement(testString, idx);
Pointer[] pointerResult = dataPointer.getPointerArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat(((String) sa.getElement(idx)), equalTo(testString));
assertThat(new BSTR(dataPointer.getPointer(idx * elementSize)).getValue(), equalTo(testString));
assertThat(new BSTR(pointerResult[idx]).getValue(), equalTo(testString));
assertThat((String) objectResult[idx], equalTo(testString));
sa.unaccessData();
sa.destroy();
// VT_VARIANT is tested in testADODB
// untested: VT_UNKNOWN
// untested: VT_DISPATCH
// untested: VT_CY
// untested: VT_DECIMAL
// unsupported: VT_RECORD
}
Aggregations