I am writing a Java applet which loads dll's created in unmanaged C++. I am writing some basic test to get it loading.
Here's the code:
The DLL:
#define DllExport _declspec(dllexport)
DllExport int calc();
DllExport int calc() {
return 1000;
}
The applet:
import java.applet.*;
import java.awt.*;
public class app extends Applet {
int width, height;
private native int calc();
public void init() {
try {
System.loadLibrary("appletdll.dll");
setBackground( Color.black );
}
catch(Exception e) {
setBackground( Color.red );
开发者_如何学Go // for debugging, is there another way to for example print
// exception messages in an applet?
}
//width = getSize().width;
width = calc();
height = getSize().height;
}
public void paint( Graphics g ) {
g.setColor( Color.green );
for ( int i = 0; i < 10; ++i ) {
g.drawLine( width, height, i * width / 10, 0 );
}
}
}
The HTML running the applet:
<html>
<head><title>simple page</title></head>
<body>
<applet width=400 height=400 code="app.class" archive="apptest.jar"> </applet>
</body>
</html>
All I am getting when running in Firefox is a black 400x400 background. When running in applerviewer, I get an UnsatisfiedLinkError on calc().
The .jar is signed using this guide: http://wiki.plexinfo.net/index.php?title=How_to_sign_JAR_files
In the folder I have: app.class, apptest.jar, appletdll.dll, applet.htm,
myKeystoreI am probably doing alot wrong, I am just trying to get some basics working. I've been looking at applet+dll guides but they are mostly outdated. If there is anything that is not clear, just ask.
Thanks
First, the function should be called Java_app_calc(). Second, it should take a single argument of type "JNIEnv *". That should do it.
If you run the program "javah", it can generate your C/C++ header file for you, which will help you get the function names and signatures correct. The argument is the name of a class (it operates on compiled class files:
javah app
精彩评论