Java 小程序,加载本机 dll 时出现问题
我正在编写一个 Java 小程序,它加载在非托管 C++ 中创建的 dll。我正在编写一些基本测试来加载它。
代码如下:
DLL:
#define DllExport _declspec(dllexport)
DllExport int calc();
DllExport int calc() {
return 1000;
}
小程序:
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 );
// 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 );
}
}
}
运行小程序的 HTML:
<html>
<head><title>simple page</title></head>
<body>
<applet width=400 height=400 code="app.class" archive="apptest.jar"> </applet>
</body>
</html>
在 Firefox 中运行时,我得到的只是黑色的 400x400 背景。 在 applerviewer 中运行时,我在 calc() 上收到 UnsatisfiedLinkError。
使用本指南对 .jar 进行签名: http://wiki.plexinfo.net/index.php?title=How_to_sign_JAR_files
在我的文件夹中有: 应用程序类, 应用测试.jar, 小程序dll.dll, 小程序.htm,
myKeystore
我可能做错了很多,我只是想让一些基础知识发挥作用。我一直在查看 applet+dll 指南,但它们大多已经过时了。 如果有什么不清楚的地方,尽管问。
谢谢
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 );
// 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,
myKeystore
I 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,该函数应调用 Java_app_calc()。其次,它应该采用“JNIEnv *”类型的单个参数。应该可以做到这一点。
如果您运行程序“javah”,它可以为您生成C/C++头文件,这将帮助您获得正确的函数名称和签名。参数是类的名称(它在编译的类文件上运行:
javah app
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