用jni调用本机代码时崩溃jvm?

时间:2018-08-29 08:47:51

标签: java c jvm java-native-interface

当我尝试调用本机代码jvm崩溃时,我用java编写了一个使用jni调用本机代码的程序? EXCEPTION_ACCESS_VIOLATION(0xc0000005)at pc = 0x0000000076f39958,pid = 4376,tid = 4388 siginfo:EXCEPTION_ACCESS_VIOLATION(0xc0000005),读取地址0x0000000012004926

bwm_shutdownEvent.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class bwm_shutdownEvent */

#ifndef _Included_bwm_shutdownEvent
#define _Included_bwm_shutdownEvent
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class:     bwm_shutdownEvent
* Method:    registerEvent
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_bwm_shutdownEvent_registerEvent
(JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

shutdownEvent.cpp

#include <jni.h>
#include "bwm_shutdownEvent.h"
#include <windows.h>
#include <signal.h>
JavaVM *jvm=NULL;
const char g_szClassName[] = "bwm";
void callCallBack() {
JNIEnv *env;
jvm->AttachCurrentThread((void **)&env, NULL);
jclass cls=env->FindClass("bwm/shutdownEvent");
jmethodID mid=env->GetStaticMethodID(cls,"callback","()V");      
env->CallStaticVoidMethod(cls, mid);
jvm->DetachCurrentThread();
}
void callReload() {
JNIEnv *env;
jvm->AttachCurrentThread((void **)&env, NULL);
jclass cls=env->FindClass("bwm/shutdownEvent");
jmethodID mid=env->GetStaticMethodID(cls,"reload","()V");      
env->CallStaticVoidMethod(cls, mid);
jvm->DetachCurrentThread();
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam,LPARAM lParam)
{

switch(msg) {
    case WM_CREATE:
        break;
    case WM_POWERBROADCAST:
        if (wParam==PBT_APMSUSPEND){/*go to sleep and hibernate*/
          callCallBack(); 
        }
        if(wParam==PBT_APMRESUMESUSPEND){/*wake up*/ 
           // callReload();
        }
        break;
    case WM_ENDSESSION:/*shut down and log off*/
        callCallBack();
         PostQuitMessage(0);
        break; 
    case WM_CLOSE:
        PostQuitMessage(0);
        break;    
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
    break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
void registerw() {
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
//Step 1: Registering the Window Class
wc.cbSize        = sizeof(WNDCLASSEX);
wc.style         = 0;
wc.lpfnWndProc   = WndProc;
wc.cbClsExtra    = 0;
wc.cbWndExtra    = 0;
wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName  = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

if(!RegisterClassEx(&wc))
{
    MessageBox(NULL, "Window Registration Failed!", "Error!",
        MB_ICONEXCLAMATION | MB_OK);
    return;
}

// Step 2: Creating the Window
hwnd = CreateWindowEx(
    WS_EX_CLIENTEDGE,
    g_szClassName,
    "The title of my window",
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, CW_USEDEFAULT, 24, 12,
    NULL, NULL, NULL, NULL);

if(hwnd == NULL)
{
    MessageBox(NULL, "Window Creation Failed!", "Error!",
        MB_ICONEXCLAMATION | MB_OK);
    return;
}

ShowWindow(hwnd, 1);
UpdateWindow(hwnd);
// Step 3: The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
    TranslateMessage(&Msg);
    DispatchMessage(&Msg);
}
}
JNIEXPORT void JNICALL Java_bwm_shutdownEvent_registerEvent
(JNIEnv *env, jobject){ 
jint rs = env->GetJavaVM(&jvm);
registerw();/* Creates window & enters message loop */
} 

shutdownEvent.java

 public class shutdownEvent {
  /**
 * @param args the command line arguments
 */
static{
    try{



  System.loadLibrary("shutdownEvent.dll");
    }
catch(Exception e){
     System.out.println("can not find dll");  
    }
}
public native void registerEvent();
private static void callback() throws IOException{
  System.out.println("ok that is ok"); 
  Alert alert=new Alert(Alert.AlertType.ERROR);
  alert.setContentText("hi");
 }
 private static void reload(){

 }
}

0 个答案:

没有答案