News:

How did you even find this place?

Main Menu

Using DLL's from Java

Started by Joe, March 02, 2006, 04:58:19 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Joe

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.
Quote from: Camel on June 09, 2009, 04:12:23 PMI'd personally do as Joe suggests

Quote from: AntiVirus on October 19, 2010, 02:36:52 PM
You might be right about that, Joe.


MyndFyre

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: Joe on January 23, 2011, 11:47:54 PM
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Quote from: Rule on May 26, 2009, 02:02:12 PMOur species really annoys me.

iago

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. 

Joe

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. =)
Quote from: Camel on June 09, 2009, 04:12:23 PMI'd personally do as Joe suggests

Quote from: AntiVirus on October 19, 2010, 02:36:52 PM
You might be right about that, Joe.