C++;代码工作正常,但不';从Java(JNI)调用时无法工作
我以前在调用C++;代码工作正常,但不';从Java(JNI)调用时无法工作,java,c++,java-native-interface,Java,C++,Java Native Interface,我以前在调用ExitWindowsEXwindowsapi函数时遇到一些特权问题 因此,我编写了以下代码以获得特权: 这在C++中运行良好 #include <cstdlib> #include <windows.h> #include <iostream> using namespace std; /* * */ int MyExitWindows(int flag, int reason); int main(int argc, char*
ExitWindowsEX
windowsapi函数时遇到一些特权问题
因此,我编写了以下代码以获得特权:
这在C++中运行良好
#include <cstdlib>
#include <windows.h>
#include <iostream>
using namespace std;
/*
*
*/
int MyExitWindows(int flag, int reason);
int main(int argc, char** argv) {
MyExitWindows(EWX_SHUTDOWN, 0);
}
int MyExitWindows(int flag, int reason) {
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
// Get a token for this process.
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return GetLastError();
// Get the LUID for the shutdown privilege.
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
// Get the shutdown privilege for this process.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES) NULL, 0);
// Cannot test the return value of AdjustTokenPrivileges.
ExitWindowsEx(flag, reason);
if (GetLastError() != ERROR_SUCCESS) {
return GetLastError();
}
return 0;
}
#包括
#包括
#包括
使用名称空间std;
/*
*
*/
int myexit窗口(int标志,int原因);
int main(int argc,字符**argv){
myexit窗口(EWX_关闭,0);
}
int MyExitWindows(int标志,int原因){
拉赫托肯;
令牌特权tkp;
//获取此进程的令牌。
if(!OpenProcessToken(GetCurrentProcess()、标记调整特权、标记查询和hToken))
返回GetLastError();
//获取关闭权限的LUID。
LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid);
tkp.PrivilegeCount=1;//一个要设置的权限
tkp.Privileges[0]。属性=SE_PRIVILEGE_ENABLED;
//获取此进程的关闭权限。
AdjustTokenPrivileges(hToken、FALSE和tkp、0、,
(PTOKEN_特权)NULL,0);
//无法测试AdjustTokenPrivileges的返回值。
性别(标志、原因);
如果(GetLastError()!=错误\u成功){
返回GetLastError();
}
返回0;
}
但是当我从Java调用它时,它不起作用
#include <jni.h>
#include <cstdlib>
#include <windows.h>
#include "com_ehsunbehravesh_jshutdown_system_Shutdowner.h"
using namespace std;
int MyExitWindows(int flag, int reason);
JNIEXPORT jint JNICALL Java_com_ehsunbehravesh_jshutdown_system_Shutdowner_exitWindowsEx
(JNIEnv *env, jobject obj, jlong flag, jlong reason) {
return MyExitWindows(flag, reason);
}
int MyExitWindows(int flag, int reason) {
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
// Get a token for this process.
int cpid = GetCurrentProcessId();
printf("%d", cpid);
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return GetLastError();
// Get the LUID for the shutdown privilege.
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
// Get the shutdown privilege for this process.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES) NULL, 0);
// Cannot test the return value of AdjustTokenPrivileges.
ExitWindowsEx(flag, reason);
if (GetLastError() != ERROR_SUCCESS) {
return GetLastError();
}
return 0;
}
#包括
#包括
#包括
#包括“com_ehsunbehravesh_jshutdown_system_Shutdowner.h”
使用名称空间std;
int myexit窗口(int标志,int原因);
JNIEXPORT jint JNICALL Java\u com\u ehsunbehravesh\u JSHUTTOWN\u system\u Shutdowner\u exitWindowsEx
(JNIEnv*env、jobject对象、jlong标志、jlong原因){
返回窗口(标志、原因);
}
int MyExitWindows(int标志,int原因){
拉赫托肯;
令牌特权tkp;
//获取此进程的令牌。
int cpid=GetCurrentProcessId();
printf(“%d”,cpid);
if(!OpenProcessToken(GetCurrentProcess()、标记调整特权、标记查询和hToken))
返回GetLastError();
//获取关闭权限的LUID。
LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid);
tkp.PrivilegeCount=1;//一个要设置的权限
tkp.Privileges[0]。属性=SE_PRIVILEGE_ENABLED;
//获取此进程的关闭权限。
AdjustTokenPrivileges(hToken、FALSE和tkp、0、,
(PTOKEN_特权)NULL,0);
//无法测试AdjustTokenPrivileges的返回值。
性别(标志、原因);
如果(GetLastError()!=错误\u成功){
返回GetLastError();
}
返回0;
}
您不使用系统退出(int)有什么原因吗
Java试图控制应用程序的关闭,也许它试图阻止您以其他方式进行操作。请详细说明“不工作”是什么意思?崩溃API调用返回的错误代码(哪个?)没有错误/崩溃,但什么也看不到?
jlong
是64位的,我很确定int
在C中不会超过32位(这也不应该给您带来问题)1。我的函数返回0,这意味着GetLastError()
2不会返回任何错误。只要我知道我在类型转换3中没有错误。我所说的“不起作用”是指ExitWindowsEx
的工作方式就像它没有完全权限一样(注销工作关闭,重新启动不工作)。请尝试检查LookupPrivilegValue和AdjustTokenPrivileges返回的错误代码。可能是因为WindowsExitEx的用途不同:它关闭Windows或重新启动。