【发布时间】:2020-08-05 21:27:25
【问题描述】:
我有一个本地方法可以显示给定路径的文件所有者。
public class ACL{
static {
System.loadLibrary("owner"); // hello.dll (Windows) or libhello.so (Unixes)
}
// Declare native method
private native String displayFileOwner(String path);
public static void main(String[] args) {
String str = "C:/Users/pradeep-pt3689/eclipse-workspace/Permission/jni/owner.c";
System.out.println(":"+new ACL().displayFileOwner(str));
}
}
运行时显示此错误的代码
线程“main”中的异常 java.lang.UnsatisfiedLinkError: ACL.displayFileOwner(Ljava/lang/String;)Ljava/lang/String; 在 ACL.displayFileOwner(本机方法) 在 ACL.main(ACL.java:10)
本机代码为:
#include <jni.h>
#include <iostream>
#include <windows.h>
#include <tchar.h>
#include "ACL.h"
#include "accctrl.h"
#include "aclapi.h"
using namespace std;
JNIEXPORT jstring JNICALL Java_ACL_displayFileOwner(JNIEnv *env, jobject obj, jstring path)
{
jstring jstr=NULL;
const char *str=env->GetStringUTFChars(path, NULL);
DWORD dwRtnCode = 0;
PSID pSidOwner = NULL;
BOOL bRtnBool = TRUE;
LPTSTR AcctName = NULL;
LPTSTR DomainName = NULL;
DWORD dwAcctName = 1, dwDomainName = 1;
SID_NAME_USE eUse = SidTypeUnknown;
HANDLE hFile;
PSECURITY_DESCRIPTOR pSD = NULL;
// Get the handle of the file object.
hFile = CreateFile(
str,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
// Check GetLastError for CreateFile error code.
if (hFile == INVALID_HANDLE_VALUE) {
DWORD dwErrorCode = 0;
dwErrorCode = GetLastError();
jstr = env->NewStringUTF("CreateFile error.\n");
return jstr;
}
// Get the owner SID of the file.
dwRtnCode = GetSecurityInfo(
hFile,
SE_FILE_OBJECT,
OWNER_SECURITY_INFORMATION,
&pSidOwner,
NULL,
NULL,
NULL,
&pSD);
// Check GetLastError for GetSecurityInfo error condition.
if (dwRtnCode != ERROR_SUCCESS) {
DWORD dwErrorCode = 0;
dwErrorCode = GetLastError();
jstr = env->NewStringUTF("GetSecurityInfo error.\n");
return jstr;
}
// First call to LookupAccountSid to get the buffer sizes.
bRtnBool = LookupAccountSid(
NULL, // local computer
pSidOwner,
AcctName,
(LPDWORD)&dwAcctName,
DomainName,
(LPDWORD)&dwDomainName,
&eUse);
// Reallocate memory for the buffers.
AcctName = (LPTSTR)GlobalAlloc(
GMEM_FIXED,
dwAcctName);
// Check GetLastError for GlobalAlloc error condition.
if (AcctName == NULL) {
DWORD dwErrorCode = 0;
dwErrorCode = GetLastError();
jstr = env->NewStringUTF("GlobalAlloc error.\n");
return jstr;
}
DomainName = (LPTSTR)GlobalAlloc(
GMEM_FIXED,
dwDomainName);
// Check GetLastError for GlobalAlloc error condition.
if (DomainName == NULL) {
DWORD dwErrorCode = 0;
dwErrorCode = GetLastError();
jstr = env->NewStringUTF("GlobalAlloc error.\n");
return jstr;
}
// Second call to LookupAccountSid to get the account name.
bRtnBool = LookupAccountSid(
NULL, // name of local or remote computer
pSidOwner, // security identifier
AcctName, // account name buffer
(LPDWORD)&dwAcctName, // size of account name buffer
DomainName, // domain name
(LPDWORD)&dwDomainName, // size of domain name buffer
&eUse); // SID type
// Check GetLastError for LookupAccountSid error condition.
if (bRtnBool == FALSE) {
DWORD dwErrorCode = 0;
dwErrorCode = GetLastError();
if (dwErrorCode == ERROR_NONE_MAPPED)
jstr = env->NewStringUTF("Account owner not found for specified SID.\n");
else
jstr = env->NewStringUTF("Error in LookupAccountSid.\n");
return jstr;
} else if (bRtnBool == TRUE)
{// Print the account name.
jstr = env->NewStringUTF(AcctName);
env->ReleaseStringUTFChars(path, str);
}
return jstr;
}
我在 web 项目中实际使用的相同 jni 方法来获取文本字段中的文件路径和打印所有者,它工作得非常好。 enter image description here
【问题讨论】:
-
用dumpbin检查你的dll导出的符号。编辑您的问题,向我们展示所有包含 displayFileProperty 的符号
-
好的,这听起来很奇怪,但我尝试使用字符串返回类型编译相同类型的方法,它在不同的项目名称下工作得非常好。我什至尝试将仅简单的代码 sn-p 复制到上面的 C 源文件中,它仍然会引发相同的错误。我还是不明白是什么问题。
-
Java 抱怨缺少符号,所以用 dumpbin 检查符号。这会告诉你发生了什么。
-
你把你的班级放在不同的包里了吗?将相应的 C 代码添加到您的问题和 Botje 所说的 dumpbin/nm 输出中,并在您的 Java 代码中包含包名称。
-
我尝试在另一台机器上运行相同的代码,它可以工作,但这里显示无法加载 dll 文件。如何检查垃圾箱?我没用过也没听过。 @Botje
标签: java java-native-interface