Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Calling java method from outside of JNIExport Function

843829Aug 22 2001 — edited Aug 24 2001
Is it possible? From pure interest I created a dll which creates a c++ win32 API window. However i've ran into a problem, some code:

C implementation.
#include <jni.h>
#include "HelloWorld.h"
#include <stdio.h>
#include <windows.h>

#define APPNAME "test"
#define VERSION "1"
#define AUTHOR  "tester"

HWND hWindow;	// main window
HINSTANCE hInstance;

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message) 
	{
		case WM_CREATE:
			break;
			
		case WM_DESTROY:
			printf("Destroying Window...");
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

JNIEXPORT jstring JNICALL Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj) 
{   	
    	MSG msg;
	//HWND isRunning;
	WNDCLASSEX wcex;
	char class_name[20];
	char window_title[50];

	sprintf(class_name,"%s_win",APPNAME);
	sprintf(window_title,"%s %s by %s",APPNAME,VERSION,AUTHOR);

	//isRunning = FindWindow(class_name,window_title);

	wcex.cbSize = sizeof(WNDCLASSEX); 

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= NULL;
	wcex.hCursor		= LoadCursor(NULL,IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)COLOR_APPWORKSPACE;
	wcex.lpszMenuName	= NULL;
	wcex.lpszClassName	= class_name;
	wcex.hIconSm		= NULL;

	RegisterClassEx(&wcex);

	hWindow = CreateWindowEx(0,class_name, window_title, 
				WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN|WS_CLIPSIBLINGS,
				CW_USEDEFAULT, CW_USEDEFAULT, 
				CW_USEDEFAULT, CW_USEDEFAULT, 
				NULL, NULL, hInstance, NULL);

	ShowWindow(hWindow,SW_SHOWNORMAL);
	UpdateWindow(hWindow);
	
	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return msg.wParam;
}
Java Implementation:
import java.io.*;

class HelloWorld {
    public native String displayHelloWorld();
    public String destroyWindow()
    {
    	return "Destroying Window...";
    }

    static {
        System.loadLibrary("HelloWorldImp");
    }
    
    public static void main(String[] args) {
        System.out.println("Creating Window...");
        new HelloWorld().displayHelloWorld();
    }
}
Basically I want to call the java function destroyWindow() at case WM_DESTROY: in the LRESULT CALLBACK. I created a global reference and tried calling the function but received a javavm error I believe. Need a little help :)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 21 2001
Added on Aug 22 2001
2 comments
257 views