Calling C++ dll from Java

1,358 views
Skip to first unread message

Tamas

unread,
Oct 6, 2017, 3:50:31 PM10/6/17
to Java Native Access

Hi, I'd set up my JNA "environment" and successfully run the printf() C Demo code.


Now, I've created a class in MS Studio and built with the setting can be seen in the picture.




Class looks like


class __declspec(dllexport) CMyClass

{

public:

    void welcome() { ... Hello world ...};

};



And I want to call this welcome method from Java. 


I saw somewhere an example code, but I could find it now. I've found several example for simple C files. But this is different, because I've a .lib too and I don't know whether it has to be used or not.


Thanks for your help!


Tamás

Message has been deleted

L Will Ahonen

unread,
Oct 7, 2017, 2:43:39 AM10/7/17
to Java Native Access
Hi,

You'll save yourself a lot of headaches if you use dumpbin /exports yourdll.dll now, to make sure you're exporting the symbols you expect, without mangling.

Generally, it's Tricky™ to call C++ with JNA. 

A typical pattern is to write all your code in C++, then create a C wrapper api like so:

extern "C" {
int c_declared_function(void* actuallyObject)
{
    cxx_object* o=(cxx_object*)actuallyObject;
    return o->wrappedfunction
}

void* c_declared_factory(parameters)
{
    return new cxx_object(parameters);
}

void c_declared_destructor(void* actuallyObject)
{
    cxx_object* o=(cxx_object*)actuallyObject;
    delete o;
}
}


This lets you have the convenience of writing C++ instead of C, and still avoid the headaches of passing this-pointers etc.

Cheers!
Will

Tamas

unread,
Oct 7, 2017, 4:21:43 AM10/7/17
to jna-...@googlegroups.com
Hi, thanks!

I followed your instructions, but firstly I've decided to try a pure C library.


My hello.c file in MS Studio 2015:

#include <stdio.h>

extern void hello();

void hello()
{
    printf("Hello from C");
}


My Java file in eclipse:

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;

public class PaMMTE {

   public interface CPaMMTE_testDLL extends Library {
   public void welcome();
   }

   public static void main(String[] args) {
   CPaMMTE_testDLL lib = (CPaMMTE_testDLL) Native.loadLibrary("TestCallingFromJava", CPaMMTE_testDLL.class);
   lib.welcome();
       System.out.println("Hello, World");
   }
}


The name of the dll is CPaMMTE_testDLL.

Anyway, I've got the following error message:

Exception in thread "main" java.lang.UnsatisfiedLinkError: %1 is not a valid Win32 application.

at com.sun.jna.Native.open(Native Method)
at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:288)
at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:427)
at com.sun.jna.Library$Handler.<init>(Library.java:179)
at com.sun.jna.Native.loadLibrary(Native.java:569)
at com.sun.jna.Native.loadLibrary(Native.java:544)
at PaMMTE.main(PaMMTE.java:18)



I'm pretty sure that the project configuration is wrong in MS Studio project.
I've tried several different configurations without any good result.
The last one I've tried is:



If I could run this demo, I would summarize and write here my experience if anyone else needs to solve a similar issue.

Thanks for your help!

Mentes a vírusoktól. www.avast.com

--
You received this message because you are subscribed to a topic in the Google Groups "Java Native Access" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jna-users/YTr7IUOIo5I/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jna-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Tamas

unread,
Oct 7, 2017, 11:12:24 PM10/7/17
to Java Native Access
Hi,

I could solve my problem!

The MS Studio 2015 C dll source is:

#include <stdio.h>


__declspec
(dllexport) void hello()

{
    printf
("Hello from C");
}


(Maybe hello -> welcome is also could cause a problem, but I tried several examples and implemented lots of dummy stuff and I think I just copy the wrong sample.)

And the Java is still the same:

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;


public class PaMMTE {


   
public interface CPaMMTE_testDLL extends Library {

   
public void hello();

   
}


   
public static void main(String[] args) {
   
CPaMMTE_testDLL lib = (CPaMMTE_testDLL) Native.loadLibrary("TestCallingFromJava", CPaMMTE_testDLL.class);

    lib
.hello();
       
System.out.println("Hello, World");
   
}
}


And the most important is the MS Studio config:


I don't understand why I shouldn't have had to use extern. Anyway, the next step is to try a class and complex C++ libraries.

Thanks Will for the help, now I'm going to use your code.

Nice day!
Reply all
Reply to author
Forward
0 new messages