標籤:
Test.java
__________________________________
public class Test
{
public static native void p();
public static void main(String[] args)
{
System.loadLibrary("TestDll");
p();
}
}
javac Test.java
javah -jni Test //命令產生的test.c標頭檔
/* DO NOT EDIT THIS FILE - it is machine generated */
#include "jni.h"
/* Header for class Test */
#ifndef _Included_Test
#define _Included_Test
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: Test
* Method: p
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_Test_p
(JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
testdll.c (將 c:\jdk\bin下面的 jni.h 和 jni_md.h 複製到當前c程式的檔案夾裡)
________________________________
#include "jni.h"
#include "test.h"
#include <stdio.h>
JNIEXPORT void JNICALL Java_Test_p(JNIEnv *env, jobject obj)
{
printf("Hello world!\n");
return;
}
用code::Blocks產生的dll,使用DLL Export Viewer查看其函數名是[email protected]。
cmd> java Test 拋出錯誤:
java.lang.UnsatisfiedLinkError: Test.p()V
___________________________________________
解決的第1種方法:
在testdll.h檔案中把"Java_Hello_p"函數前的"JNICALL"去掉,重編dll就能測試通過。(注: #define JNICALL __stdcall),網上的查到的資料如下:
HTML code
stdcall的呼叫慣例意味著:1)參數從右向左壓入堆棧,2)函數自身修改堆棧 3)函數名自動加前置的底線,後面緊跟一個@符號,其後緊跟著參數的尺寸。
___________________________________________
解決的第2種方法:@silenceburn (http://topic.csdn.net/u/20110216/15/d85cf526-beb9-4a36-b6c2-7ade7f1eb01d.html)
正好手邊有codeBlocks,就測試了一下,的確會有LZ說的問題,
放狗搜了搜,是因為JNI想要VC風格的函式宣告,但是CodeBlocks預設的GCC編譯器產生的是GCC風格的
需要用編譯選項修改產生風格,8L的兄弟說的是對的。
修改的位置在CodeBlocks的工程右鍵properties - Build Targets - Build Options - Linker Setting - Other Linker Options ,加入 -Wl,--kill-at 即可。我在我本機已經測試通過。
___________________________________________
在測試通過java傳字串到c寫的dll時,發現在c程式中列印出來的中文會出現亂碼情況,在百度搜尋後找到一個解決方案:
#include "jni.h"
#include "test.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
char* jstringToWindows( JNIEnv *env, jstring jstr );
jstring WindowsTojstring( JNIEnv* env, char* str );
JNIEXPORT void JNICALL Java_Test_p(JNIEnv *env, jobject obj)
{
printf("Hello world!\n");
return;
}
//通過java傳字串過來
JNIEXPORT void JNICALL Java_Test_abcd(JNIEnv *env, jobject obj, jstring p)
{
const char * str = jstringToWindows( env, p );
printf( "%s\n" , str);
/*
如果把上句改為下句,C接收Java字串時將出現亂碼:
//const char* str = (*env)->GetStringUTFChars(env, p, 0);
//printf("%s",str);
*/
}
//需要用一下2個函數做編碼格式轉換
char* jstringToWindows( JNIEnv *env, jstring jstr )
{
int length = (*env)->GetStringLength(env,jstr );
const jchar* jcstr = (*env)->GetStringChars(env,jstr, 0 );
char* rtn = (char*)malloc( length*2+1 );
int size = 0;
size = WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)jcstr, length, rtn,(length*2+1), NULL, NULL );
if( size <= 0 )
return NULL;
(*env)->ReleaseStringChars(env,jstr, jcstr );
rtn[size] = 0;
return rtn;
}
jstring WindowsTojstring( JNIEnv* env, char* str )
{
jstring rtn = 0;
int slen = strlen(str);
unsigned short* buffer = 0;
if( slen == 0 )
rtn = (*env)->NewStringUTF(env,str );
else
{
int length = MultiByteToWideChar( CP_ACP, 0, (LPCSTR)str, slen, NULL, 0 );
buffer = malloc( length*2 + 1 );
if( MultiByteToWideChar( CP_ACP, 0, (LPCSTR)str, slen, (LPWSTR)buffer, length ) >0 )
rtn = (*env)->NewString( env, (jchar*)buffer, length );
}
if( buffer )
free( buffer );
return rtn;
}
code::Blocks產生的dll 在 java jni 調用遇到的問題