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//command-generated test.c header file
/* Don't EDIT this file-it are 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 (Copy the c:\jdk\bin below Jni.h and jni_md.h to the folder of the current C program)
________________________________
#include "jni.h"
#include "test.h"
#include <stdio.h>
Jniexport void Jnicall java_test_p (jnienv *env, Jobject obj)
{
printf ("Hello world!\n");
Return
}
Using the DLL generated by code::blocks, use the DLL Export viewer to view its function name as [email protected].
cmd> Java Test throws an error:
JAVA.LANG.UNSATISFIEDLINKERROR:TEST.P () V
___________________________________________
The 1th method to solve:
In the testdll.h file, the "Java_hello_p" function before the "Jnicall" removed, re-compiled DLL can be tested. (Note: #define Jnicall __stdcall), the information found on the Internet is as follows:
HTML Code
The calling convention of the stdcall means that: 1) The parameter is pressed from right to left into the stack, 2) The function itself modifies the stack 3) the letter name automatically adds a leading underscore followed by an @ symbol followed by the dimension of the parameter.
___________________________________________
the 2nd method to solve: @silenceburn (http://topic.csdn.net/u/20110216/15/d85cf526-beb9-4a36-b6c2-7ade7f1eb01d.html)
Just have a codeblocks on hand, test a bit, indeed there will be LZ said the problem,
The dog was searched because Jni wanted the VC-style function declaration, but the codeblocks default gcc compiler generated a gcc-style
The build style needs to be modified with the compile option, and 8L's brother is right.
Modify the location in Codeblocks right-click Properties-build targets-build options-linker setting-other Linker Options, join-WL,--Kill-at Can. I have been tested in my native pass.
___________________________________________
In the test through the Java string to c write DLL, found in C program printed in Chinese will appear garbled situation, after Baidu search to find a solution:
#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
}
Passing strings through Java.
Jniexport void Jnicall JAVA_TEST_ABCD (jnienv *env, Jobject obj, jstring p)
{
const char * str = jstringtowindows (env, p);
printf ("%s\n", str);
/*
If you change the sentence to the next sentence, C will be garbled when it receives the Java string:
Const char* str = (*env)->getstringutfchars (env, p, 0);
printf ("%s", str);
*/
}
Need to use 2 functions to do the encoding format conversion
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 generated DLL in Java JNI call problems encountered