gpt4 book ai didi

java - 加载 JNI Dll

转载 作者:行者123 更新时间:2023-11-30 04:23:14 25 4
gpt4 key购买 nike

我有一个写成如下的文件:

package JNI;

public class Natives {

public static final int PAGE_READONLY = 0x02;
public static final int PAGE_READWRITE = 0x04;
public static final int PAGE_WRITECOPY = 0x08;
public static final int FILE_MAP_COPY = 0x0001;
public static final int FILE_MAP_WRITE = 0x0002;
public static final int FILE_MAP_READ = 0x0004;

public static native long createFileMapping(long hFile, int lpAttributes, int dwMaximumSizeHigh, int dwMaximumSizeLow, String lpName);

public static native long openFileMapping(int dwDesiredAccess, boolean bInheritHandle, String lpName);

public static native long mapViewOfFile(long hFileMap, int dwDesiredAccess, int dwFileOffsetHigh, int dwFileOffsetLow, int dwNumberOfBytesToMap);

public static native long unmapViewOfFile(long hFileMap);

public static native long closeHandle(long hFile);
}

然后我使用 javah 生成 jni 文件:

extern "C" JNIEXPORT jlong JNICALL Java_JNI_Natives_createFileMapping(JNIEnv *env, jclass, jlong hFile, jint lpProtect, jint dwMaximumSizeHigh, jint dwMaximumSizeLow, jstring lpName)
{
HANDLE hFileMap = nullptr;
LPCSTR Name = env->GetStringUTFChars(lpName, nullptr);

if ((hFileMap = CreateFileMapping(hFile == -1 ? INVALID_HANDLE_VALUE : reinterpret_cast<HANDLE>(hFile), nullptr, lpProtect, dwMaximumSizeHigh, dwMaximumSizeLow, Name)) != nullptr)
{
if (errno == ERROR_ALREADY_EXISTS)
{
CloseHandle(hFileMap);
}
}

env->ReleaseStringUTFChars(lpName, Name);
return reinterpret_cast<jlong>(hFileMap);
}

extern "C" JNIEXPORT jlong JNICALL Java_JNI_Natives_openFileMapping(JNIEnv *env, jclass, jint dwDesiredAccess, jboolean bInheritHandle, jstring lpName)
{
LPCSTR Name = env->GetStringUTFChars(lpName, nullptr);
HANDLE hFileMap = OpenFileMapping(dwDesiredAccess, static_cast<bool>(bInheritHandle), Name);
env->ReleaseStringUTFChars(lpName, Name);
return reinterpret_cast<jlong>(hFileMap);
}

extern "C" JNIEXPORT jlong JNICALL Java_JNI_Natives_mapViewOfFile(JNIEnv *, jclass, jlong hFileMap, jint dwDesiredAccess, jint dwFileOffsetHigh, jint dwFileOffsetLow, jint dwNumberOfBytesToMap)
{
void* pData = MapViewOfFile(reinterpret_cast<HANDLE>(hFileMap), dwDesiredAccess, dwFileOffsetHigh, dwFileOffsetLow, dwNumberOfBytesToMap);
return reinterpret_cast<jlong>(pData);
}

extern "C" JNIEXPORT jlong JNICALL Java_JNI_Natives_unmapViewOfFile(JNIEnv *, jclass, jlong hFileMap)
{
return reinterpret_cast<jboolean>(static_cast<uint8_t>(UnmapViewOfFile(reinterpret_cast<void*>(hFileMap))));
}

extern "C" JNIEXPORT jlong JNICALL Java_JNI_Natives_closeHandle(JNIEnv *, jclass, jlong hFile)
{
return reinterpret_cast<jboolean>(static_cast<uint8_t>(CloseHandle(reinterpret_cast<HANDLE>(hFile))));
}

但是一旦 DLL 加载,我就调用如下函数:

long Handle = Natives.createFileMapping(-1, Natives.FILE_MAP_READ | Natives.FILE_MAP_WRITE, 500, 0, "NativeDLL"); 调试(句柄);

它打印:

运行:

#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000000061288f69, pid=4692, tid=844
#
# JRE version: 7.0_21-b11
# Java VM: Java HotSpot(TM) 64-Bit Server VM (23.21-b01 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# V [jvm.dll+0x38f69]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# C:\Users\Brandon\Documents\NetBeansProjects\Smart\hs_err_pid4692.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.sun.com/bugreport/crash.jsp
#
Java Result: 1

知道我做错了什么吗?

最佳答案

我建议您编码并构建支持 Unicode 的 JNI DLL,并使用 GetStringChars 创建 UTF-16 字符串以传递给 CreateFileMappingCreateFileMapping 没有任何版本采用 GetStringUTFChars 返回的 UTF-8 字符串。如果必须使用 ANSI 版本,则在调用 CreateFileMapping 之前执行从 Unicode 到 ANSI 字符串的正确转换。

IMO,GetStringUTFChars 应命名为 GetStringUTF8Chars

不要忘记将 GetStringCharsReleaseStringChars 配对。

关于java - 加载 JNI Dll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16484770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com