如何使用Java中的user32.dll之类的库中的函数? [英] how can i use the functions in library like user32.dll in java?

查看:130
本文介绍了如何使用Java中的user32.dll之类的库中的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从Google处获取它,但没有良好的结果.....

i tried to get it from google but no good results.....

推荐答案

快速搜索后得到了这个Java本机访问 [ ^ ].
看看是否有帮助.
A quick search yielded this Java Native Access[^].
Have a look and see if it helps.


除了André的建议外,您可能还想研究 ^ ].
In addition to André''s suggestion you may like to look into the Java Native Interface[^].


您不能直接从Java程序中使用任意DLL.您可以做的是在C或C ++中编写本机DLL作为项目的一部分,然后可以使用本机DLL(程序的本机部分)中的kernel32.dll和其他dll.您的Java程序还必须至少具有一个在本机DLL内部实现的本机方法.通过本机方法,您可以调用本机DLL,这样就可以轻松访问低级内容.
您必须做什么:
编写您的Java程序并在您的一个类中创建一个本机方法:
NativeTest.java:
You can not use an arbitrary DLL directly from your java program. What you can do is that you write a native DLL as part of your project in C or C++ and then you can use kernel32.dll and other dlls from your native DLL, the native part of your program. Your java program must also has at least one native method that is implemented inside your native DLL. This way via the native method you can call into your native DLL you can easily access low-level stuff.
What you have to do:
Write your java program and create a native method in one of your classes:
NativeTest.java:
package com.xyz;
public class NativeTest {
    public native void doSomeNativeWork();
}



本机方法未在Java中实现.您可以按原样进行编译.编译后,在包含本地方法的类上运行javah程序:



The native method isn''t implemented in java. You can compile this as it is. When its compiled run the javah program on your classes that contain native methods:

javah org.xyz.NativeTest


此命令生成一个C源头文件,其中包含本机方法实现的声明:
com_xyz_NativeTest.h:


This command generates a C source header files that contains the declaration of the native method implementation:
com_xyz_NativeTest.h:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_xyz_NativeTest */

#ifndef _Included_com_xyz_NativeTest
#define _Included_com_xyz_NativeTest
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_xyz_NativeTest
 * Method:    doSomeNativeWork
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_xyz_NativeTest_doSomeNativeWork
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif
</jni.h>


创建您的DLL项目并实现上面的函数声明,并确保它由DLL导出为外部"C".如果您想了解如何使用JNIEnv东西以及如何在C函数中处理Java字符串等,或者您想创建一些Java字符串/对象以将某些结果返回到Java端,请搜索JNI教程.

之后,返回您的Java代码并加载您的本机DLL:
NativeTest.java:


Create your DLL project and implement the above function declaration and make sure that it is exported by your DLL as extern "C". Search for a JNI tutorial if you wan to know how to use the JNIEnv stuff and how to handle java strings and the like in your C function, or if you want to create some java strings/objects to return some results to the java side.

After this go back to your java code and load your native DLL:
NativeTest.java:

package com.xyz;
public class NativeTest {
    public native void doSomeNativeWork();

    static {
        System.loadLibrary("MyNativeDLL.dll");
    }
}


System.loadLibrary()加载您的本机DLL并将其任何导出的函数绑定到Java程序内匹配的本机方法.

从现在开始,您可以从Java调用本机方法,并且可以在本机DLL函数内部使用任何其他DLL及其函数!


The System.loadLibrary() loads your native DLL and binds any of its exported functions to matching native methods inside your java program.

From now you can call the native method from java and inside your native DLL function you can use any other DLLs and their functions too!


这篇关于如何使用Java中的user32.dll之类的库中的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆