簡體   English   中英

Java和C:如何將該C源文件編譯為DLL以與Java一起使用?

[英]Java and C: How can I compile this C source file into a DLL for use with Java?

我買了《 Pro Android Games Pro》一書(盡管我不是專業人士,但我正在追趕),並且需要編譯DLL以便在Java項目中使用。 這是代碼:

庫文件

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <jni.h>

#include "include/jni_Natives.h"

#define CB_CLASS "jni/Natives"

/**
 * OnMessage callback
 */
#define CB_CLASS_MSG_CB  "OnMessage"
#define CB_CLASS_MSG_SIG  "(Ljava/lang/String;I)V"

// prototypes

// Lib main Sub
int lib_main(int argc, char **argv) ;

// Used to get the len of a Java Array
const int getArrayLen(JNIEnv * env, jobjectArray jarray);

// printf str messages back to java
void jni_printf(char *format, ...);

// Global env ref (for callbacks)
static JavaVM *g_VM;

// Global Reference to the native Java class jni.Natives.java
static jclass jNativesCls;


/*
 * Class:     jni_Natives
 * Method:    LibMain
 * Signature: ([Ljava/lang/String;)V
 */
JNIEXPORT jint JNICALL Java_jni_Natives_LibMain
  (JNIEnv * env, jclass class, jobjectArray jargv)
{
    // obtain a global ref to the caller jclass
    (*env)->GetJavaVM(env, &g_VM);

    // Extract char ** args from Java array
    jsize clen =  getArrayLen(env, jargv);

    char * args[(int)clen];

    int i;
    jstring jrow;
    for (i = 0; i < clen; i++)
    {
        // Get C string from Java Strin[i]
        jrow = (jstring)(*env)->GetObjectArrayElement(env, jargv, i);
        const char *row  = (*env)->GetStringUTFChars(env, jrow, 0);

        args[i] = malloc( strlen(row) + 1);
        strcpy (args[i], row);

        // print args
        jni_printf("Main argv[%d]=%s", i, args[i]);

        // free java string jrow
        (*env)->ReleaseStringUTFChars(env, jrow, row);
    }

    /*
     * Load the jni.Natives class
     */
    jNativesCls = (*env)->FindClass(env, CB_CLASS);

    if ( jNativesCls == 0 ) {
        jni_printf("Unable to find class: %s", CB_CLASS);
        return -1;
    }

    // Invoke the Lib main sub. This will loop forever
    // Program args come from Java
    lib_main (clen, args);

    return 0;
}


/**
 * Send a string back to Java
 */
jmethodID mSendStr;

static void jni_send_str( const char * text, int level) {
    JNIEnv *env;

    if ( !g_VM) {
        printf("I_JNI-NOVM: %s\n", text);
        return;
    }

    (*g_VM)->AttachCurrentThread (g_VM,  &env, NULL); // (void **)

    // Load jni.Natives if missing
    if ( !jNativesCls ) {
        jNativesCls = (*env)->FindClass(env, CB_CLASS);

        if ( jNativesCls == 0 ) {
                printf("Unable to find class: %s", CB_CLASS);
                return;
        }
    }

    // Call jni.Natives.OnMessage(String, int)
    if (! mSendStr ) {
        // Get  aref to the static method: jni.Natives.OnMessage
        mSendStr = (*env)->GetStaticMethodID(env, jNativesCls
            , CB_CLASS_MSG_CB
            , CB_CLASS_MSG_SIG);
    }
    if (mSendStr) {
        // Call method
        (*env)->CallStaticVoidMethod(env, jNativesCls
                , mSendStr
                , (*env)->NewStringUTF(env, text)
            , (jint) level );
    }
    else {
        printf("Unable to find method: %s, signature: %s\n"
                , CB_CLASS_MSG_CB, CB_CLASS_MSG_SIG );
    }
}

/**
 * Printf into the java layer
 * does a varargs printf into a temp buffer
 * and calls jni_sebd_str
 */
void jni_printf(char *format, ...)
{
    va_list argptr;
    static char string[1024];

    va_start (argptr, format);
    vsprintf (string, format,argptr);
    va_end (argptr);

    jni_send_str (string, 0);
}

/**
 * Get java array length
 */
const int getArrayLen(JNIEnv * env, jobjectArray jarray)
{
    return (*env)->GetArrayLength(env, jarray);
}

/**
 * Library main sub
 */
int lib_main(int argc, char **argv)  
{
    int i;

    jni_printf("Entering LIB MAIN");

    for ( i = 0 ; i < argc ; i++ ) {
        jni_printf("Lib Main argv[%d]=%s", i, argv[i]);
    }

    return 0;
}

當我嘗試使用VS或Dev-CPP進行編譯時, 他們總是抱怨找不到jni.h,即使我將其放在項目的include目錄中也是如此 使用引號不會更改任何內容。 同樣,文件jni.h在系統路徑環境變量中。 還有大約18個其他錯誤,應該不會發生,因為作者能夠將其編譯為適用於Ubuntu的SO。

我寧願不為Linux發行版或雙重引導使用VM的麻煩,但我可能必須這樣做。

非常感謝您,如果有人可以幫助我將其構建為DLL。 :)

關於使用MinGW構建DLL,以下是一些非常簡短的說明。

在C / C ++中編譯DLL,然后從另一個程序調用它

如何使用JNI從Java調用本機(DLL)代碼

本文詳細介紹了使用JNI(Java本機接口)從Java調用本機C / C ++代碼(在DLL中)的過程。

http://support.microsoft.com/kb/222092

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM