这是 Native.cpp :
// Native.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#define ALLEGRO_NO_MAGIC_MAIN
#include <stdio.h>
#include <string>
#include <windows.h>
#include "generic_interface.h"
#include "NativeC.h"
using namespace std;
// Some useful defines I liked from Sun's stuff
#define JNIEXPORT __declspec(dllexport)
#define JNICALL __cdecl
#define jint long
typedef ExportedClass* (__cdecl *exported_class)();
HINSTANCE temptDLL;
ExportedClass** importedClasses;
char** classNamePerIndex;
int libraryLength = 0;
JNIEXPORT void JNICALL _JAVA_initiate(HNative *self, jint libraryLength) {
importedClasses = new ExportedClass*[libraryLength];
classNamePerIndex = new char*[libraryLength];
}
实现并加载从上述 Native.cpp 文件生成的本机 dll 的 Java 类如下:
public class Native {
// guess?
native public void initiate(int libraryLength);
// Loads the file Native.DLL at run-time
static {
System.loadLibrary("Native");
}
// Constructor
public Native()
{
}
}
但是打电话的时候
(new Native()).initiate(1);
我收到此运行时错误:
线程“main”中的异常 java.lang.UnsatisfiedLinkError: Native.initiate(I)V
我试图将 _JAVA_initiate 重命名为 JAVA_initiate 和 NATIVE_initiate 和 _JAVA_NATIVE_inititate 甚至 JAVA_NATIVE_inititate,但它仍然无法正常工作
该库加载得很好,只是在调用本机方法时,它给出了链接错误。
编辑:下面列出的是已经包含在 Native.cpp 中的 NativeC.h
/* DO NOT EDIT - automatically generated by javah */
#include "Native.h"
/* Header for class Native */
#ifndef _Included_Native
#define _Included_Native
typedef struct ClassNative {
#pragma pack(push,4)
int32_t MSReserved;
struct Hjava_lang_String * string_;
/*boolean*/ long boolean_;
/*byte*/ long byte_;
/*char*/ long char_;
double double_;
float float_;
long int_;
int64_t long_;
/*short*/ long short_;
struct Hjava_lang_String * w;
long x;
long y;
#pragma pack(pop)
} ClassNative;
#define HNative ClassNative
#ifdef __cplusplus
extern "C" {
#endif
__declspec(dllexport) void __cdecl _JAVA_initiate (struct HNative *, long);
__declspec(dllexport) void __cdecl _JAVA_loadLibraryAndInitiate (struct HNative *, struct Hjava_lang_String *);
__declspec(dllexport) long __cdecl _JAVA_evaluateLibrary (struct HNative *, struct Hjava_lang_String *, struct Hjava_lang_String *);
#ifdef __cplusplus
}
#endif
#endif