|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 |
package
com.jni;public class JNITest { static{ System.loadLibrary("JNI"); } public
native String getCompterName(); public
native void fastWriteFile(String name, byte[] bytes); public
static void main(String[] args) { JNITest test = new
JNITest(); String str = test.getCompterName(); test.fastWriteFile("tst", new
byte[]{}); System.out.println(str); }} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 |
/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class com_jni_JNITest */#ifndef _Included_com_jni_JNITest#define _Included_com_jni_JNITest#ifdef __cplusplusextern
"C" {#endif/* * Class: com_jni_JNITest * Method: getCompterName * Signature: ()Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_com_jni_JNITest_getCompterName (JNIEnv *, jobject);/* * Class: com_jni_JNITest * Method: fastWriteFile * Signature: (Ljava/lang/String;[B)V */JNIEXPORT void
JNICALL Java_com_jni_JNITest_fastWriteFile (JNIEnv *, jobject, jstring, jbyteArray);#ifdef __cplusplus}#endif#endif//------------------------------------- |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 |
/* Replace "dll.h" with the name of your header */#include <stdlib.h> #include <stdio.h> #include "dll.h"#include "windows.h" #include "tlhelp32.h"#include "util.h" /** Class: com_jni_JNITest* Method: getCompterName* Signature: ()Ljava/lang/String;*/JNIEXPORT jstring JNICALL Java_com_jni_JNITest_getCompterName(JNIEnv *env, jobject){char
*res = "test to jstring";PROCESSENTRY32 pe32;//在使用这个结构前,先设置它的大小 pe32.dwSize = sizeof(pe32); //给系统内所有的进程拍个快照 HANDLE
hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); if
(hProcessSnap == INVALID_HANDLE_VALUE) { res = "CreateToolhelp32Snapshot 调用失败.\n"; } //遍历进程快照,轮流显示每个进程的信息 BOOL
bMore = ::Process32First(hProcessSnap,&pe32); while
(bMore) { printf("进程名称:%s ID:%u\n",pe32.szExeFile,pe32.th32ProcessID); bMore = ::Process32Next(hProcessSnap,&pe32); } //不要忘记清除掉snapshot对象 ::CloseHandle(hProcessSnap); jstring result = stoJstring(env,res);return
result;}/** Class: com_jni_JNITest* Method: fastWriteFile* Signature: (Ljava/lang/String;[B)V*/JNIEXPORT void
JNICALL Java_com_jni_JNITest_fastWriteFile(JNIEnv *env, jobject, jstring, jbyteArray){printf("hello jni");} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 |
//-------------------------#include <stdlib.h> #include <stdio.h> #include "dll.h"//char* to jstringjstring stoJstring(JNIEnv* env, const
char* pat){ jclass strClass = env->FindClass("Ljava/lang/String;"); jmethodID ctorID = env->GetMethodID(strClass, "<init>", "([BLjava/lang/String;)V"); jbyteArray bytes = env->NewByteArray(strlen(pat)); env->SetByteArrayRegion(bytes, 0, strlen(pat), (jbyte*)pat); jstring encoding = env->NewStringUTF("utf-8"); return
(jstring)env->NewObject(strClass, ctorID, bytes, encoding);} char* jstringTostring(JNIEnv* env, jstring jstr){ char* rtn = NULL; jclass clsstring = env->FindClass("java/lang/String"); jstring strencode = env->NewStringUTF("utf-8"); jmethodID mid = env->GetMethodID(clsstring, "getBytes", "(Ljava/lang/String;)[B"); jbyteArray barr= (jbyteArray)env->CallObjectMethod(jstr, mid, strencode); jsize alen = env->GetArrayLength(barr); jbyte* ba = env->GetByteArrayElements(barr, JNI_FALSE); if
(alen > 0) { rtn = (char*)malloc(alen + 1); memcpy(rtn, ba, alen); rtn[alen] = 0; } env->ReleaseByteArrayElements(barr, ba, 0); return
rtn;} |
编译请参考:http://www.cnblogs.com/czpblog/p/3760940.html
编译后把dll的目录追加到path
原文:http://www.cnblogs.com/czpblog/p/3761064.html