clock.java /* * Copyright (c) 2004-2005 Servertec. All Rights Reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * THIS NOTICE MUST NOT BE ALTERED NOR REMOVED. * * CopyrightVersion 1.0 */ import java.text.MessageFormat; import stec.jenie.Structure; import stec.jenie.UINT; import stec.jenie.HANDLE; import stec.jenie.Dll; import stec.jenie.Function; import stec.jenie.LONG; import stec.jenie.BOOL; import stec.jenie.Pointer; import stec.jenie.NativeParameter; import stec.jenie.NativeObject; import stec.jenie.NativeException; import stec.jenie.Callback; import stec.jenie.UINT32; import stec.jenie.INT32; import stec.jenie.USHORT; /** * This example uses the WIN32 API SetTimer function in User32.dll to start a 1 second timer. SetTimer calls the clock's callback method each time the timer expires. */ public class clock extends Callback { private Pointer hwnd = new Pointer(); private UINT timer_msg = new UINT(); private Pointer timer_id = new Pointer(); private UINT32 elapsed_time = new UINT32(); int count = 0; public clock() { super(); setParameters(new NativeParameter[] {hwnd, timer_msg, timer_id, elapsed_time}); setReturnValue(null); // required to run under Java 1.1.x setCallbackHandling(USING_ATTACHED_THREAD); } public static void main(String[] args) throws Exception { new clock().run(); } public void callback() { try { System.out.println(getLocalTime()); } catch(Exception exception) {} if(count > 10) { try { Dll dll = new Dll("User32"); try { dll.getFunction("PostQuitMessage").call(new UINT32(0), null); } finally { dll.release(); } } catch(Exception ex) { ex.printStackTrace(); } } count++; } private void run() throws NativeException { Dll dll = new Dll("User32"); try { Function getMessage = dll.getFunction("GetMessageA"); Function translateMessage = dll.getFunction("TranslateMessage"); Function dispatchMessage = dll.getFunction("DispatchMessageA"); MSG msg = new MSG(); Pointer pmsg = new Pointer(msg); INT32 return_code = new INT32(); NativeParameter[] getMessageParameters = {pmsg, new Pointer(), new UINT(), new UINT()}; dll.getFunction("SetTimer").call(new NativeParameter[] {new Pointer(), new UINT(0), new UINT(1000), new Pointer(this)}, timer_id); if (timer_id.getValue() == 0) { throw new NativeException("Clock Timer could not be started: " + getLastError()); } while(true) { getMessage.call(getMessageParameters, return_code); if(return_code.getValue() == 0) { break; } translateMessage.call(pmsg, null); dispatchMessage.call(pmsg, null); } } finally { dll.release(); } } private static class POINT extends Structure { public LONG x = new LONG(); public LONG y = new LONG(); public POINT() { setMembers(new NativeObject[] {x, y}); } } private static class MSG extends Structure { Pointer hwnd = new Pointer(); UINT message = new UINT(); UINT wParam = new UINT(); UINT lParam = new UINT(); UINT time = new UINT(); POINT pt = new POINT(); public MSG() { setMembers(new NativeObject[] {hwnd, message, wParam, lParam, time, pt}); } } public static String getLocalTime() throws NativeException { SystemTimeStructure system_time = new SystemTimeStructure(); Dll dll = new Dll("Kernel32"); try { dll.getFunction("GetLocalTime").call(new Pointer(system_time), null); return system_time.toString(); } finally { dll.release(); } } public static class SystemTimeStructure extends Structure { USHORT year = new USHORT(); USHORT month = new USHORT(); USHORT dayOfWeek = new USHORT(); USHORT day = new USHORT(); USHORT hour = new USHORT(); USHORT minutes = new USHORT(); USHORT seconds = new USHORT(); USHORT milliseconds = new USHORT(); SystemTimeStructure() { setMembers(new NativeObject[] {year, month, dayOfWeek, day, hour, minutes, seconds, milliseconds}); } public String toString() { return new MessageFormat ( "{0,number,00}:{1,number,00}:{2,number,00}.{3,number,000}" ).format ( new Object[] { new Integer(hour.getValue()), new Integer(minutes.getValue()), new Integer(seconds.getValue()), new Integer(milliseconds.getValue()) } ); } } private static long getLastError() throws NativeException { Dll dll = new Dll("Kernel32"); try { UINT32 return_value = new UINT32(); dll.getFunction("GetLastError").call((NativeParameter[])null, return_value); return return_value.getValue(); } finally { dll.release(); } } }