callback problem
Howard Chivers <[email protected]> Sun, 1 Apr 2007 04:00:52 -0400
| Newsgroups | gmane.comp.windows.devel.jawin |
|---|---|
| Message-ID | <LISTSERV%[email protected]> |
Hi,
I've been trying to use jawin to wrap some User32 functions - many thanks
for the framework & I'll be happy to donate any stubs that result.
However, I'm having problems with callbacks.
I'd be grateful if anyone could have a quick look at the following; I think
I've read the source correctly & it seems straightforward, but I'd be
grateful for confirmation.
The following example wraps the EnumWindows function. Similar problems
occur with other enum functions. The code works correctly, and calls back
to EnumWindowsProc, passing handles and lparam correctly; after exactly 5
callbacks (and with lots more to enumerate) the system throws a protection
exception - either protected code or access violation. (A more complex
example with enum child has convinced me that this is not a genuine
security problem).
It is possible that I have not correctly worked out how to build the return
from the callback, with the result that the stack is mangled on return from
callback? or is this likley to be a bug?
Thanks
Howard
--------
public void debugEnumWindows() throws COMException, IOException {
User32_a.EnumWindows(new EnumWindowsProc() {
public boolean call(int hWnd,int lParam) {
return true;
}
},0x99);
return;
}
--------------
public static void EnumWindows(EnumWindowsProc ewp,int lParam) throws
IOException, COMException {
FuncPtr fp = new FuncPtr("user32.dll","EnumWindows");
NakedByteStream nbs = new NakedByteStream();
LittleEndianOutputStream leos = new LittleEndianOutputStream
(nbs);
leos.writeInt(0);
leos.writeInt(lParam);
byte[] res = fp.invoke("T2." + EnumWindowsProc.mtoken + "I::",
8, nbs, new Object[] {ewp}, ReturnFlags.CHECK_FALSE);
return;
}
-----------------
public abstract class EnumWindowsProc extends WinFuncPtr {
public static final int mtoken = GenericStub.registerCustomString
("2II:I:");
abstract public boolean call (int hWnd,int lParam) throws IOException;
protected byte[] call(byte[] inargs, Object[] inobjs) throws
IOException {
LittleEndianInputStream leis = new LittleEndianInputStream(new
ByteArrayInputStream(inargs));
int hWnd = leis.readInt();
int lParam = leis.readInt();
boolean ret = call(hWnd,lParam);
NakedByteStream nbs = new NakedByteStream();
LittleEndianOutputStream leos = new LittleEndianOutputStream(nbs);
leos.writeInt(ret ? 1 : 0);
return nbs.getInternalBuffer();
}
}
----end------