Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

JNI & windows message queue problem

sampoysampoy Member Posts: 1
Hi,

i'm facing a problem with JNI & windows message queue. The C-code should create a thread and do its things until windows WM_QUIT message arrives. I load the DLL, call native methods loadDll and DestroyMP() from java side. But in C-code in the loop where i do GetMessage , it never get the WM_QUIT. Seems that the Java VM cannot stop the process, since C-created thread is still executing. Can someone help me, I'm quite new to JNI & WIN messaqe queues.

C-code:

BOOL APIENTRY DllMain( HINSTANCE hinstDll,
DWORD ul_reason_for_call,
LPVOID lpReserved )
{
if( ul_reason_for_call == DLL_PROCESS_ATTACH )
{
close=FALSE;
hInstance = hinstDll;
RegisterWindowClass();
}
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) {
printf("here
");
if (Msg == WM_QUIT) {
printf("quit
");
close=TRUE;
}
switch (LOWORD(wParam))
{
case TASK_RECEIVEFILE:
if (lParam)
FileReceiveSuccess();
else
printf( "File Receive Fail!" );
return 0;
;
}
return DefWindowProc(hWnd, Msg, wParam, lParam);
}

unsigned WINAPI CreateWndThread(LPVOID pThreadParam) {
hWnd = CreateWindow(_T("NotShown"), NULL, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
if(hWnd == NULL) {
printf("Error creating window
");
return 0;
}
jint nSize = 1;
jint nVms;
jint nStatus = JNI_GetCreatedJavaVMs(&g_pJvm, nSize, &nVms);
if(nStatus == 0) {
nStatus = (*g_pJvm)->AttachCurrentThread(g_pJvm,(void**)&g_pEnv, NULL);
if(nStatus != 0) printf("Can not attach thread");
}
else {
printf("Can not get the jvm");
}

MSG Msg;

while(GetMessage(&Msg,hWnd,0,0)!=0) {
printf("message arrived:%d",Msg);
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}

(*g_pJvm)->DetachCurrentThread(g_pJvm);
printf("FINISHED");
return Msg.wParam;
}

void RegisterWindowClass() {
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = 0;
wcex.hCursor = 0;
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = 0;
wcex.lpszClassName = _T("NotShown");
wcex.hIconSm = 0;
RegisterClassEx(&wcex);
}

JNIEXPORT void JNICALL Java_JniTest_DestroyMP
(JNIEnv *env, jclass cls)
{
if(g_jobj) (*env)->DeleteGlobalRef(env,g_jobj);
int temp = PostThreadMessage(uThreadId, WM_QUIT, 0, 0);
printf("At DestroyMP, %d
",temp);

//SendMessage(hWnd,WM_QUIT,0,0); // if this is added, WndProc gets WM_QUIT, but GetMessage loop does not.
}

JNIEXPORT jboolean JNICALL
Java_JniTest_loadDLL(JNIEnv *env, jobject obj)
{

g_jobj = (*env)->NewGlobalRef(env,obj);

HANDLE hThread;
hThread = (HANDLE)_beginthreadex(NULL, 0,
&CreateWndThread, NULL, 0, &uThreadId);
if(!hThread) {
printf("Failed to create thread");
return FALSE;
}
return TRUE;
}
Sign In or Register to comment.