Author Topic: Using DLL's from Java  (Read 2611 times)

0 Members and 1 Guest are viewing this topic.

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Using DLL's from Java
« on: March 02, 2006, 04:58:19 pm »
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'd personally do as Joe suggests

You might be right about that, Joe.


Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: Using DLL's from Java
« Reply #1 on: March 02, 2006, 07:11:14 pm »
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?
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: Using DLL's from Java
« Reply #2 on: March 02, 2006, 08:51:55 pm »
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. 

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: Using DLL's from Java
« Reply #3 on: March 03, 2006, 07:47:54 am »
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. =)
I'd personally do as Joe suggests

You might be right about that, Joe.