Hello, Win32 API(CNI) World!

Posted on 3月 26th, 2013 by cx20

Win32 API(CNI)

Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
以下は、GCJ にて CNI を利用した Win32 API の呼出し例となっている。

ソースコード(Java)

public class Hello {
    public static native int messageBox( int hwnd, String text, String caption, int type );
 
    public static void main( String[] args ) {
        messageBox( 0, "Hello, Win32 API World!

クラスファイル作成

C:¥> gcj -C Hello.java

C++ ヘッダファイル作成

C:¥> gcjh Hello.class

ソースコード(C++ ヘッダファイル)

// DO NOT EDIT THIS FILE - it is machine generated -*- c++ -*-
 
#ifndef __Hello__
#define __Hello__
 
#pragma interface
 
#include <java/lang/Object.h>
#include <gcj/array.h>
 
extern "Java"
{
    class Hello;
}
 
class Hello : public ::java::lang::Object
{
 
public:
  Hello();
  static jint messageBox(jint, ::java::lang::String *, ::java::lang::String *, jint);
  static void main(JArray< ::java::lang::String * > *);
  static ::java::lang::Class class$;
};
 
#endif // __Hello__

ソースコード(C++ 実装ファイル)

#include <gcj/cni.h>
#include <windows.h>
#include "Hello.h"
 
jint Hello::messageBox(jint hwnd, ::java::lang::String* text, ::java::lang::String* caption, jint type)
{
    jchar* _text = JvGetStringChars( text );
    jchar* _caption = JvGetStringChars( caption );
    int result = MessageBoxW( (HWND)hwnd, (LPCWSTR)_text, (LPCWSTR)_caption, (UINT)type );
    return result;
}

コンパイル方法

C:¥> gcj -c Hello.cpp
C:¥> gcj --main=Hello -o Hello Hello.o Hello.class

実行結果

---------------------------
Hello, World!
---------------------------
Hello, Win32 API World!
---------------------------
OK   
---------------------------

Tags:

Categories: CNI, GCJ, Win32 API

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

WP-SpamFree by Pole Position Marketing