Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. calling a C-dll function from java code

calling a C-dll function from java code

Scheduled Pinned Locked Moved Java
javahelpquestion
5 Posts 3 Posters 20 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Djibril
    wrote on last edited by
    #1

    Hello, I am very new to java. I want to call a C-dll function from my java code and I don't know how. Can you please help? Thanks. Where there is a WISH, there is a WILL.

    M 1 Reply Last reply
    0
    • D Djibril

      Hello, I am very new to java. I want to call a C-dll function from my java code and I don't know how. Can you please help? Thanks. Where there is a WISH, there is a WILL.

      M Offline
      M Offline
      Malcolm McMahon
      wrote on last edited by
      #2

      There are facilities for this kind of thing in the MS version of the java engine. It's documented on the microsoft java site. Basically microsoft have put in some naughty java extensions for this purpose. You do things like /** @dll.import("USER32") */ static native int MessageBox(int hwndOwner, String text, String title, int style); (This must be in a class definition, of course). I rather doubt there are any facilities to do this kind of thing in the Sun java engine. It's too machine dependant.

      M 1 Reply Last reply
      0
      • M Malcolm McMahon

        There are facilities for this kind of thing in the MS version of the java engine. It's documented on the microsoft java site. Basically microsoft have put in some naughty java extensions for this purpose. You do things like /** @dll.import("USER32") */ static native int MessageBox(int hwndOwner, String text, String title, int style); (This must be in a class definition, of course). I rather doubt there are any facilities to do this kind of thing in the Sun java engine. It's too machine dependant.

        M Offline
        M Offline
        markkuk
        wrote on last edited by
        #3

        Malcolm McMahon wrote: I rather doubt there are any facilities to do this kind of thing in the Sun java engine. It's too machine dependant. Yes, there is. It's called Java Native Interface.

        M 1 Reply Last reply
        0
        • M markkuk

          Malcolm McMahon wrote: I rather doubt there are any facilities to do this kind of thing in the Sun java engine. It's too machine dependant. Yes, there is. It's called Java Native Interface.

          M Offline
          M Offline
          Malcolm McMahon
          wrote on last edited by
          #4

          That doesn't look like it would allow you to access an existing DLL though. Only one specially written for this purpose. Of course you could write an interface from there to a standard DLL.

          D 1 Reply Last reply
          0
          • M Malcolm McMahon

            That doesn't look like it would allow you to access an existing DLL though. Only one specially written for this purpose. Of course you could write an interface from there to a standard DLL.

            D Offline
            D Offline
            Djibril
            wrote on last edited by
            #5

            That's right. I found JNI but it sounds like it is very slow. And you can't use existing dll. 1.You write the native methods declaration in a java class 2.Then you compile the java class (javac) 3.Generate a header file from this class using javah -jni ... 4.include this header in you dll project and provide and implementation for the generated header functions. 5.Rebuild your dll From this point, you can use the dll via the java class methods we wrote at step 1. I tried using JNI. It works because I could change the source of the dll and I wanted to use. But it is surprisingly slow. Now I am looking forward to using sockets. Thank you. Where there is a WISH, there is a WILL.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups