http://javajeff.mb.ca/aa/9/9.html
Nice tutorial. I followed most of it, and ended up with this:
public class Uptime
{
// Import kernel32.dll
static
{
try
{
System.loadLibrary("kernel32");
}
catch (UnsatisfiedLinkError e)
{
System.err.println("Unable to load kernel32.dll.");
System.err.println("Are you sure you're using Windows?");
}
}
// Win32.dll imports
public static native int GetTickCount();
// ----
public static void main(String args[])
{
int miliseconds = GetTickCount();
int days, hours, minutes, seconds;
// Copy/pasted from C++ uptime project
seconds = miliseconds / (1000); seconds = seconds % 60;
minutes = miliseconds / (1000 * 60); minutes = minutes % 60;
hours = miliseconds / (1000 * 60 * 60); hours = hours % 24;
days = miliseconds / (1000 * 60 * 60 * 24);
miliseconds = miliseconds % 100;
System.out.println("System uptime: " + days + " days, " + hours + " hours, " + minutes + " minutes, " + seconds + "." + miliseconds + " seconds.");
}
}
But then I realized that kernel32's GetTickCount is an INT, not a JINT, and needs to be wrapped (hence Win32.dll instead of Kernel32.dll). Not having a C compiler, nor any C knowledge, I had a hard time wrapping it (in fact, I didn't do it). Might be worth checking out.
I'm pretty sure Java's int and C's (in terms of Windows) int are the same 32-bit signed integer. Why do you need to wrap?
Quote from: Faxx86] link=topic=5051.msg58164#msg58164 date=1141344674]
I'm pretty sure Java's int and C's (in terms of Windows) int are the same 32-bit signed integer. Why do you need to wrap?
It might be trying to make it portable. Of course, when you're using a .dll, that doesn't make sense. I guess potentially portable across Windows versions? *shrug*
Anyway, I'm against doing this except for very specialized projects. The main advantage to Java is crossplatformness, which this loses.
I was doing this just for the enjoyment of doing it, and for some extra credit. *nix already has uptime, and I've written my own C++ uptime EXE.
JINTs are the same as INTs, then? I figured they'd need to be different, but if they don't, then I'll work on this some more when I get home tomorrow. =)