first time use JNI success with WinApi .great.
1:copy jni.h to project dir,and rename jni2.h,if not alert error,do not know why.
2:write a java to use jni,
3:use javah create a header file.
4:write implemention of the header,useing MsgBox();
5:comile with user32.lib just append:
C:\JNI>cl -IdC:\j2sdk1.4.1\include -I c:\j2sdk1.4.1\include\win32 -LD HelloWorld
Imp.c -Fehello.dll user32.lib
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.
HelloWorldImp.c
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
/dll
/implib:hello.lib
/out:hello.dll
HelloWorldImp.obj
user32.lib
Creating library hello.lib and object hello.exp
C:\JNI>java HelloWorld
Hello world!
//and also have a Window!!.
all file:
1:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include "jni2.h"
/* Header for class HelloWorld */
#ifndef _Included_HelloWorld
#define _Included_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: HelloWorld
* Method: displayHelloWorld
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
2:
//±¾ÀýÖÐÆðÃûΪHelloWorldImp.c
#include "jni2.h"
#include "HelloWorld.h"
#include <stdio.h>
#include "windows.h"
JNIEXPORT void JNICALL
Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj)
{
printf("Hello world!\n");//Õâ¸öÀý×ÓÖÐÖ»Êä³öÒ»ÐÐHello World!
//CreateWindow("BUTTON","",NULL,NULL,NULL);
MessageBox(NULL,"OK","OK",MB_OK);
return;
}
3:
C:\JNI>cl -IdC:\j2sdk1.4.1\include -I c:\j2sdk1.4.1\include\win32 -LD HelloWorld
Imp.c -Fehello.dll user32.lib
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.
HelloWorldImp.c
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
/dll
/implib:hello.lib
/out:hello.dll
HelloWorldImp.obj
user32.lib
Creating library hello.lib and object hello.exp
C:\JNI>java HelloWorld
Hello world!
C:\JNI>