time.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.Dll; import stec.jenie.Function; import stec.jenie.NativeParameter; import stec.jenie.Structure; import stec.jenie.USHORT; import stec.jenie.NativeObject; import stec.jenie.Pointer; import stec.jenie.NativeException; /** * This example uses the WIN32 API GetLocalTime function in Kernel32.dll to retrieve the local time that will be displayed on the console. */ public class time { public static void main(String[] args) throws Exception { System.out.println(getLocalTime()); } 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()) } ); } } }