通过调用操作系统的API实现关闭系统。
Java端代码:
package com.kkoolerter;
public class CloseSys {
public native int closeSystem();
static{
System.load("H:/Workspaces/Eclipse-3.6/CPP/java-close-system/bin/closesys.dll");
}
}
C++端代码:
使用javah生成的头文件
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_kkoolerter_CloseSys */
#ifndef _Included_com_kkoolerter_CloseSys
#define _Included_com_kkoolerter_CloseSys
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_kkoolerter_CloseSys
* Method: closeSystem
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_kkoolerter_CloseSys_closeSystem
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
/*
* closesys.cpp
*
* Created on: 2010-11-12
* Author: Jenson
*/
/*************************
关机实现
win32, linux
*************************/
#ifdef WIN32
#include <windows.h>
#include <cstdio>
#else
#include <unistd.h>
#include <cstdlib>
#endif
#include <stdio.h>
#include "closesys.h"
JNIEXPORT jint JNICALL Java_com_kkoolerter_CloseSys_closeSystem(JNIEnv *,
jobject) {
#ifdef WIN32
// win2K,XP下需要权限
TOKEN_PRIVILEGES tkp;
HANDLE hToken;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY, &hToken)) {
printf("OpenProcessToken failed!\n");
}
//获得本地机唯一的标识
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0); //调整获得的权限
if (GetLastError() != ERROR_SUCCESS) {
printf("AdjustTokenPrivileges enable failed!\n");
return -1;
}
int fResult = InitiateSystemShutdown(NULL, // NULL表示关本机
NULL, // 显示的消息
10, // 关机所需的时间
FALSE, FALSE); //设为TRUE为重起,设为FALSE为关机
if (!fResult) {
printf("InitiateSystemShutdown failed.\n");
return -1;
}
tkp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0);
if (GetLastError() != ERROR_SUCCESS) {
printf("AdjustTokenPrivileges disable failed.\n");
return -1;
}
ExitWindowsEx(EWX_SHUTDOWN, 0); //开始关机
return 1;
#else
// Linux实现
system("poweroff");
return 1;
#endif
}
将上面的C++代码生成windows平台的dll或Linux平台的so文件即可。
Java端代码:
package com.kkoolerter;
public class CloseSys {
public native int closeSystem();
static{
System.load("H:/Workspaces/Eclipse-3.6/CPP/java-close-system/bin/closesys.dll");
}
}
C++端代码:
使用javah生成的头文件
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_kkoolerter_CloseSys */
#ifndef _Included_com_kkoolerter_CloseSys
#define _Included_com_kkoolerter_CloseSys
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_kkoolerter_CloseSys
* Method: closeSystem
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_kkoolerter_CloseSys_closeSystem
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
/*
* closesys.cpp
*
* Created on: 2010-11-12
* Author: Jenson
*/
/*************************
关机实现
win32, linux
*************************/
#ifdef WIN32
#include <windows.h>
#include <cstdio>
#else
#include <unistd.h>
#include <cstdlib>
#endif
#include <stdio.h>
#include "closesys.h"
JNIEXPORT jint JNICALL Java_com_kkoolerter_CloseSys_closeSystem(JNIEnv *,
jobject) {
#ifdef WIN32
// win2K,XP下需要权限
TOKEN_PRIVILEGES tkp;
HANDLE hToken;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY, &hToken)) {
printf("OpenProcessToken failed!\n");
}
//获得本地机唯一的标识
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0); //调整获得的权限
if (GetLastError() != ERROR_SUCCESS) {
printf("AdjustTokenPrivileges enable failed!\n");
return -1;
}
int fResult = InitiateSystemShutdown(NULL, // NULL表示关本机
NULL, // 显示的消息
10, // 关机所需的时间
FALSE, FALSE); //设为TRUE为重起,设为FALSE为关机
if (!fResult) {
printf("InitiateSystemShutdown failed.\n");
return -1;
}
tkp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0);
if (GetLastError() != ERROR_SUCCESS) {
printf("AdjustTokenPrivileges disable failed.\n");
return -1;
}
ExitWindowsEx(EWX_SHUTDOWN, 0); //开始关机
return 1;
#else
// Linux实现
system("poweroff");
return 1;
#endif
}
将上面的C++代码生成windows平台的dll或Linux平台的so文件即可。
转载于:https://blog.51cto.com/wujuxiang/421954