/////////////////////////////////////////////////////////////////
//
// MessageBoxTest.java
//
/**
* Demonstrate JDirect, JNI and RNI calls.
* @author Jonathan Locke
*/
public class MessageBoxTest
{
static boolean isJNISupported;
static boolean isRNISupported;
static boolean isJDirectSupported;
/** @dll.import("USER32") */
static native void MessageBox(int hwndOwner, String text,
String title, int fuStyle);
/**
* Package parameters and call the Win32 message box directly through
* J/Direct
* @param title Title of message box
* @param message The message to show
*/
static void messageBox(String title, String message)
{
MessageBox(0, message, title, 0);
}
/**
* Main application entrypoint.
* @param arg Command line arguments
*/
static public void main(String[] arg)
{
// JNI is not currently supported in the Microsoft VM. Conversely,
// RNI, COM and J/Direct are not supported in the JDK's VM.
String vendor = System.getProperty("java.vendor").toLowerCase();
isRNISupported = vendor.indexOf("microsoft") != -1;
isJDirectSupported = isRNISupported;
isJNISupported = !isRNISupported;
String message = "Brought to you by the letter 'J'.";
if (isJDirectSupported)
{
messageBox("JDirectMessageBox", message);
}
if (isJNISupported)
{
JNIMessageBox.messageBox("JNIMessageBox", message);
}
if (isRNISupported)
{
RNIMessageBox.messageBox("RNIMessageBox", message);
}
}
}
Figure 2 JNIMessageBox
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class JNIMessageBox */
#ifndef _Included_JNIMessageBox
#define _Included_JNIMessageBox
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: JNIMessageBox
* Method: messageBox
* Signature: (Ljava/lang/String;Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_JNIMessageBox_messageBox
(JNIEnv *, jclass, jstring, jstring);
#ifdef __cplusplus
}
#endif
#endif
Figure 3 JNIMessageBox.cpp
/////////////////////////////////////////////////////////////////
//
// JNIMessageBox.cpp
//
#define STRICT
#pragma warning(disable: 4201) // Nameless struct/union
#pragma warning(disable: 4514) // Unreferenced inline function removed
#include <windows.h>
#include "JNIMessageBox.h"
extern "C"
{
///////////////////////////////////////////////////////////////////////////////
//
// @func JNI call to put up a message box
//
JNIEXPORT void JNICALL Java_JNIMessageBox_messageBox
(
JNIEnv* env, // @parm JNI environment pointer
jclass, // @parm Unreferenced
jstring title, // @parm Title of message box
jstring message // @parm Message to display
)
{
jboolean isCopy;
// Use JNI environment pointer to call the appropriate JNI method
// to get the characters for each string passed in
const jchar* pwszTitle = env->GetStringChars(title, &isCopy);
const jchar* pwszMessage = env->GetStringChars(message, &isCopy);
// Put up a MessageBox
::MessageBoxW(NULL, pwszMessage, pwszTitle, MB_OK);
// Release the character strings
env->ReleaseStringChars(title, pwszTitle);
env->ReleaseStringChars(message, pwszMessage);
}
///////////////////////////////////////////////////////////////////////////////
//
// @mfunc entrypoint to dynamic link library
// @rdesc non-zero on success, zero on failure
//
__declspec(dllexport) BOOL WINAPI DllMain
(
HINSTANCE, // @Parm Instance handle of dll
DWORD, // @Parm Reason for call to DllMain
LPVOID // @Parm reserved
)
{
return TRUE;
}
} // extern "C"
Figure 4 RNIMessageBox.java
/////////////////////////////////////////////////////////////////
//
// RNIMessageBox.java
//
public class RNIMessageBox
{
static native void messageBox(String title, String message);
static
{
System.loadLibrary("RNIMessageBox");
}
}
///////////////////////////////// End of File /////////////////////////////////
/* DO NOT EDIT - automatically generated by msjavah */
#include <native.h>
#pragma warning(disable:4510)
#pragma warning(disable:4512)
#pragma warning(disable:4610)
struct Classjava_lang_String;
#define Hjava_lang_String Classjava_lang_String
/* Header for class RNIMessageBox */
#ifndef _Included_RNIMessageBox
#define _Included_RNIMessageBox
#define HRNIMessageBox ClassRNIMessageBox
typedef struct ClassRNIMessageBox {
#include <pshpack4.h>
long MSReserved;
#include <poppack.h>
} ClassRNIMessageBox;
typedef struct ClassArrayOfRNIMessageBox {
int32_t MSReserved;
const unsigned long length;
HRNIMessageBox * const body[1];
} ClassArrayOfRNIMessageBox;
#define HArrayOfRNIMessageBox ClassArrayOfRNIMessageBox
#define ArrayOfRNIMessageBox ClassArrayOfRNIMessageBox
#ifdef __cplusplus
extern "C" {
#endif
__declspec(dllexport) void __cdecl RNIMessageBox_messageBox (
struct HRNIMessageBox *, struct Hjava_lang_String *,
struct Hjava_lang_String *);
#ifdef __cplusplus
}
#endif
#endif /* _Included_RNIMessageBox */
#pragma warning(default:4510)
#pragma warning(default:4512)
#pragma warning(default:4610)
/////////////////////////////////////////////////////////////////
//
// RNIMessageBox.cpp
//
#define STRICT
#pragma warning(disable: 4201) // Nameless struct/union
#pragma warning(disable: 4514) // Unreferenced inline function removed
#include <windows.h>
#include <native.h>
#include "RNIMessageBox.h"
extern "C"
{
///////////////////////////////////////////////////////////////////////////////
//
// @func Gets the version of RNI that this library was built against
// @rdesc RNIVER value when the library was statically linked together
//
__declspec(dllexport) DWORD __cdecl RNIGetCompatibleVersion()
{
return RNIVER;
}
///////////////////////////////////////////////////////////////////////////////
//
// @func Static method that puts up a message box
// @rdesc
//
__declspec(dllexport) void __cdecl RNIMessageBox_messageBox
(
struct HRNIMessageBox*, // @Parm NULL instance variable
struct Hjava_lang_String* title, // @Parm Title of message box
struct Hjava_lang_String* message // @Parm Message to show
)
{
MessageBoxW(0, javaStringStart(message), javaStringStart(title), MB_OK);
}
} // extern "C"
///////////////////////////////// End of File /////////////////////////////////