zoukankan      html  css  js  c++  java
  • JNIdemo

    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);
    	}
    }
    

      

    /* 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 __cplusplus
    extern "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
    
    
    //-------------------------------------
    /* 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 调用失败.
    "; 
    } 
    //遍历进程快照,轮流显示每个进程的信息 
    BOOL bMore = ::Process32First(hProcessSnap,&pe32); 
    while (bMore) 
    { 
    printf("进程名称:%s ID:%u
    ",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");
    }
    

      

      //-------------------------
    #include <stdlib.h>  
    #include <stdio.h> 
    #include "dll.h"
    
    //char* to jstring
    jstring 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

  • 相关阅读:
    bzoj 3572 [Hnoi2014]世界树 (虚树+树形dp)
    2018 计算之道初赛第二场 阿里巴巴的手机代理商(困难)(反向可持久化Trie)
    hdu 3089 (快速约瑟夫环)
    Codeforces Round #479 (Div. 3)
    牛客练习赛17
    BNU校赛总决赛J 小白兔小灰兔 相交计算几何模板
    2018BNU校赛总决赛
    Educational Codeforces Round 43 (Rated for Div. 2) ABCDE
    Codeforces Round #478 (Div. 2) ABCDE
    牛客练习赛 16
  • 原文地址:https://www.cnblogs.com/czpblog/p/3761064.html
Copyright © 2011-2022 走看看