Re: callback problem

Howard Chivers <[email protected]> Sun, 1 Apr 2007 09:49:28 -0400
Newsgroups gmane.comp.windows.devel.jawin
Message-ID <LISTSERV%[email protected]>
(1) Vitaly,

Thanks for your message. I would prefer to contribute to an open-source
project than buy a commercial alternative; I wish to share freely the
results of my work.

(2) Anyone else

I should have mentioned in my original post - if I run using the jawin
debug dll, then no exception is thrown; however, only 5 callbacks are made,
however many should be available.

regards
Howard



On Sun, 1 Apr 2007 13:53:46 +0200, Vitaly Shelest <[email protected]>
wrote:

>Hi,
>some time ago I also tried to use Native Function call implemented in JAWIN
>but failed. This piece of the project have not been accomplished. They
>suggest to use explicit marshaling that is not clear for Developer.
Function
>calling conventions cdecl, fastcall are not supported (only stdcall).
>Structure marshal is headache (they do not support union, struct. packing,
>etc.). The same problems with call of native functions from virtual tables.
>I also tried to use another projects like xFunctions, JNIWrappers, etc.
>These projects support complex explicit marshaling makes my code huge and
>unclear.
>As a result I started to develop my own API for Native Function Call from
>Java. Having learnt Platform Invoke in .NET I have realized that the same
>can be done in SUN/IBM Java. My API implements in Java .NET Platform Invoke
>paradigm:
>- implicit and explicit marshaling;
>- C Structure marshaling;
>- Java array marshaling;
>- marshal by value and by reference;
>- callback functions;
>- C class functions call from Class Virtual Table, etc.
>
>See links:
>http://www.simtel.net/product.php[id]100916[SiteID]simtel.net
>http://www.programmersheaven.com/download/51865/download.aspx
>
>
>
>Regards,
>Vitaly Shelest,
>
>
>"Howard Chivers" <[email protected]> wrote in message
>news: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------
>>