This Question is Answered

1 "correct" answer available (4 pts) 2 "helpful" answers available (2 pts)
4 Replies Last post: Aug 1, 2007 8:59 PM by Eugene Toporov  
Bill T   8 posts since
Jul 28, 2007
Currently Being Moderated

Jul 28, 2007 9:33 AM

Simple Stuff Not Working

Ok, I got the sample Buzzer.java to work, so I know my environment is set up right.

 

Now my first real test is to run the callback function EnumWindows and the function GetWindowText for each returned window handle.

 

Here is my code:

 

package test;

 

import com.jniwrapper.Parameter;

import com.jniwrapper.Function;

import com.jniwrapper.Int32;

import java.util.List;

 

public class Test

{

    public static void main(String[] args)

    {

        Parameter retVal = null;

        EnumWindowsProc enumWindowsProc = new EnumWindowsProc();

        Function.call("user32.dll", "EnumWindows", retVal, enumWindowsProc, new Int32(1234));

 

        List list = enumWindowsProc.getHwndList();

 

        for (int i = 0; i < list.size(); i++)

        {

            System.out.println("hwnd = " + list.get(i));

        }

    }

}

 

 

package test;

 

import com.jniwrapper.Callback;

import com.jniwrapper.Pointer;

import com.jniwrapper.UInt32;

import com.jniwrapper.Parameter;

import java.util.List;

import java.util.ArrayList;

 

public class EnumWindowsProc extends Callback

{

    private Pointer.Void _hwnd = new Pointer.Void();

    private UInt32 _lParam = new UInt32();

 

    private List hwndList = new ArrayList();

 

    public EnumWindowsProc()

    {

        init(new Parameter[] {  _hwnd, _lParam }, null);

    }

 

    public List getHwndList()

    {

        return hwndList;

    }

 

    public void callback()

    {

        //System.out.println("window found : " + _hwnd + ", " + _lParam);

        hwndList.add(_hwnd);

        //Parameter retVal = null;

        //Str _lpstr = null;

        //Function.call("user32.dll","GetWindowText",retVal,new Parameter[]{ _hwnd, _lpstr, new Int(100)});

        //System.out.println("retVal = " + retVal);

    }

}

 

 

 

When i run it, I get this error:

 

Exception c0000005, at 00000000

Access violation: attempting to read memory at address 00000000

Native function stack data: 3112720,0,20007,6d0063,31101b0,31101b0,74746553,73676e69

Exception in thread "main" com.jniwrapper.FunctionExecutionException: c0000005

     at com.jniwrapper.Function.invokeCFunc(Native Method)

     at com.jniwrapper.FunctionCall.a(SourceFile:126)

     at com.jniwrapper.FunctionCall.call(SourceFile:34)

     at com.jniwrapper.Function.invoke(SourceFile:164)

     at com.jniwrapper.Function.call(SourceFile:257)

     at com.jniwrapper.Function.call(SourceFile:302)

     at test.Test.main(Test.java:15)

     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

     at java.lang.reflect.Method.invoke(Method.java:597)

     at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)

 

 

Can anybody help me?

 

Regards,

Bill

Serge Piletsky TeamDev Ltd. 670 posts since
Apr 24, 2006
Currently Being Moderated
2. Jul 30, 2007 1:03 PM in response to: Bill T
Re: Simple Stuff Not Working

Hi Bill,

 

That simple example did not work because of several simple mistakes. And here is the updated code which should work correctly. I have added appropriate comments:

 


public class Test {
    public static void main(String[] args) {
        Parameter retVal = new Bool(); // the retVal parameter cannot be 'null'.
        EnumWindowsProc enumWindowsProc = new EnumWindowsProc();
        Function.call("user32.dll", "EnumWindows", retVal, enumWindowsProc, new Int32());
        List list = enumWindowsProc.getHwndList();

        for (int i = 0; i < list.size(); i++) {
            System.out.println("hwnd = " + list.get(i));
        }
    }

    public static class EnumWindowsProc extends Callback {
        private Pointer.Void _hwnd = new Pointer.Void();
        private UInt32 _lParam = new UInt32();
        private Bool _returnValue = new Bool();

        private List hwndList = new ArrayList();

        public EnumWindowsProc() {
            init(new Parameter[]{_hwnd, _lParam}, _returnValue); // this callback should have the return value.
        }

        public List getHwndList() {
            return hwndList;
        }

        public void callback() {
            hwndList.add(new Pointer.Void(_hwnd));
            _returnValue.setValue(true); // should return true to proceed next iteration
        }
    }
}

 

Yes, in our WinPack library there is the Wnd class which provides various useful functionality for working with windows, including such enumeration of windows.

 

-Serge

Eugene Toporov   63 posts since
Apr 24, 2006
Currently Being Moderated
4. Aug 1, 2007 8:59 PM in response to: Bill T
Re: Simple Stuff Not Working

Hi Bill,

 

The exact price for JxCapture is not yet defined but as for all our products, it will be quite reasonable.

 

-Eugene

More Like This

  • Retrieving data ...