这个是要被打包成的代码,就这样就可以了吗? 或者有人帮我打包为System.dll#include "stdio.h"
//#include "Winbase.h"
#include "Windows.h"
#include <iostream>
#include "jni.h"
#include "reconserver_SystemDll.h"
JNIEXPORT jboolean JNICALL Java_reconserver_SystemDll_newprocess(JNIEnv *, jclass, jstring str){
STARTUPINFO si;
PROCESS_INFORMATION pi; //新建进程 ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) ); // Start the child process.
if( !CreateProcess( NULL, // No module name (use command line).
str, // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
)
{
return false;
}
//cout<<π
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
return true;
}
JNIEXPORT jboolean JNICALL Java_reconserver_SystemDll_killprocess(JNIEnv *, jclass, jint id){HANDLE hProcessSnap = NULL; //结束进程
BOOL bRet = FALSE;
int pid=id;
hProcessSnap=OpenProcess(PROCESS_ALL_ACCESS,false,pid);
bRet=TerminateProcess(hProcessSnap,1);
return bRet;
}
JNIEXPORT jboolean JNICALL Java_reconserver_SystemDll_poweroff(JNIEnv *, jclass){ //关机
HANDLE hToken; // handle to process token
TOKEN_PRIVILEGES tkp; // pointer to token structure
BOOL fResult; // system shutdown flag
// Get the current process token handle so we can get shutdown
// privilege.
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return FALSE;
// Get the LUID for 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 shutdown privilege for this process.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES) NULL, 0);
// Cannot test the return value of AdjustTokenPrivileges.
if (GetLastError() != ERROR_SUCCESS)
return FALSE;
// Display the shutdown dialog box and start the countdown.
fResult = InitiateSystemShutdown(
NULL, // shut down local computer
NULL, // message for user
5, // time-out period
FALSE, // ask user to close apps
FALSE); // reboot after shutdown
if (!fResult)
return FALSE;
// Disable shutdown privilege.
tkp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES) NULL, 0);
return TRUE;
}JNIEXPORT jboolean JNICALL Java_reconserver_SystemDll_restart(JNIEnv *, jclass){ //重启
HANDLE hToken; // handle to process token
TOKEN_PRIVILEGES tkp; // pointer to token structure
BOOL fResult; // system shutdown flag
// Get the current process token handle so we can get shutdown
// privilege.
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return FALSE;
// Get the LUID for 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 shutdown privilege for this process.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES) NULL, 0);
// Cannot test the return value of AdjustTokenPrivileges.
if (GetLastError() != ERROR_SUCCESS)
return FALSE;
// Display the shutdown dialog box and start the countdown.
fResult = InitiateSystemShutdown(
NULL, // shut down local computer
NULL, // message for user
5, // time-out period
FALSE, // ask user to close apps
TRUE); // reboot after shutdown
if (!fResult)
return FALSE;
// Disable shutdown privilege.
tkp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES) NULL, 0);
return TRUE;
}
void main(int argc, char *argv[]) //这个main要不要
{}reconserver_SystemDll.h/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class reconserver_SystemDll */#ifndef _Included_reconserver_SystemDll
#define _Included_reconserver_SystemDll
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: reconserver_SystemDll
* Method: newprocess
* Signature: (Ljava/lang/String;)Z
*/
JNIEXPORT jboolean JNICALL Java_reconserver_SystemDll_newprocess
(JNIEnv *, jclass, jstring);/*
* Class: reconserver_SystemDll
* Method: killprocess
* Signature: (I)Z
*/
JNIEXPORT jboolean JNICALL Java_reconserver_SystemDll_killprocess
(JNIEnv *, jclass, jint);/*
* Class: reconserver_SystemDll
* Method: poweroff
* Signature: ()Z
*/
JNIEXPORT jboolean JNICALL Java_reconserver_SystemDll_poweroff
(JNIEnv *, jclass);/*
* Class: reconserver_SystemDll
* Method: restart
* Signature: ()Z
*/
JNIEXPORT jboolean JNICALL Java_reconserver_SystemDll_restart
(JNIEnv *, jclass);#ifdef __cplusplus
}
#endif
#endif
SystemDll.javapackage reconserver;public class SystemDll { static{
System.loadLibrary("System");
}
public native static boolean newprocess(String s);
public native static boolean killprocess(int s);
public native static boolean poweroff();
public native static boolean restart();
}
//#include "Winbase.h"
#include "Windows.h"
#include <iostream>
#include "jni.h"
#include "reconserver_SystemDll.h"
JNIEXPORT jboolean JNICALL Java_reconserver_SystemDll_newprocess(JNIEnv *, jclass, jstring str){
STARTUPINFO si;
PROCESS_INFORMATION pi; //新建进程 ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) ); // Start the child process.
if( !CreateProcess( NULL, // No module name (use command line).
str, // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
)
{
return false;
}
//cout<<π
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
return true;
}
JNIEXPORT jboolean JNICALL Java_reconserver_SystemDll_killprocess(JNIEnv *, jclass, jint id){HANDLE hProcessSnap = NULL; //结束进程
BOOL bRet = FALSE;
int pid=id;
hProcessSnap=OpenProcess(PROCESS_ALL_ACCESS,false,pid);
bRet=TerminateProcess(hProcessSnap,1);
return bRet;
}
JNIEXPORT jboolean JNICALL Java_reconserver_SystemDll_poweroff(JNIEnv *, jclass){ //关机
HANDLE hToken; // handle to process token
TOKEN_PRIVILEGES tkp; // pointer to token structure
BOOL fResult; // system shutdown flag
// Get the current process token handle so we can get shutdown
// privilege.
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return FALSE;
// Get the LUID for 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 shutdown privilege for this process.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES) NULL, 0);
// Cannot test the return value of AdjustTokenPrivileges.
if (GetLastError() != ERROR_SUCCESS)
return FALSE;
// Display the shutdown dialog box and start the countdown.
fResult = InitiateSystemShutdown(
NULL, // shut down local computer
NULL, // message for user
5, // time-out period
FALSE, // ask user to close apps
FALSE); // reboot after shutdown
if (!fResult)
return FALSE;
// Disable shutdown privilege.
tkp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES) NULL, 0);
return TRUE;
}JNIEXPORT jboolean JNICALL Java_reconserver_SystemDll_restart(JNIEnv *, jclass){ //重启
HANDLE hToken; // handle to process token
TOKEN_PRIVILEGES tkp; // pointer to token structure
BOOL fResult; // system shutdown flag
// Get the current process token handle so we can get shutdown
// privilege.
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return FALSE;
// Get the LUID for 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 shutdown privilege for this process.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES) NULL, 0);
// Cannot test the return value of AdjustTokenPrivileges.
if (GetLastError() != ERROR_SUCCESS)
return FALSE;
// Display the shutdown dialog box and start the countdown.
fResult = InitiateSystemShutdown(
NULL, // shut down local computer
NULL, // message for user
5, // time-out period
FALSE, // ask user to close apps
TRUE); // reboot after shutdown
if (!fResult)
return FALSE;
// Disable shutdown privilege.
tkp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES) NULL, 0);
return TRUE;
}
void main(int argc, char *argv[]) //这个main要不要
{}reconserver_SystemDll.h/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class reconserver_SystemDll */#ifndef _Included_reconserver_SystemDll
#define _Included_reconserver_SystemDll
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: reconserver_SystemDll
* Method: newprocess
* Signature: (Ljava/lang/String;)Z
*/
JNIEXPORT jboolean JNICALL Java_reconserver_SystemDll_newprocess
(JNIEnv *, jclass, jstring);/*
* Class: reconserver_SystemDll
* Method: killprocess
* Signature: (I)Z
*/
JNIEXPORT jboolean JNICALL Java_reconserver_SystemDll_killprocess
(JNIEnv *, jclass, jint);/*
* Class: reconserver_SystemDll
* Method: poweroff
* Signature: ()Z
*/
JNIEXPORT jboolean JNICALL Java_reconserver_SystemDll_poweroff
(JNIEnv *, jclass);/*
* Class: reconserver_SystemDll
* Method: restart
* Signature: ()Z
*/
JNIEXPORT jboolean JNICALL Java_reconserver_SystemDll_restart
(JNIEnv *, jclass);#ifdef __cplusplus
}
#endif
#endif
SystemDll.javapackage reconserver;public class SystemDll { static{
System.loadLibrary("System");
}
public native static boolean newprocess(String s);
public native static boolean killprocess(int s);
public native static boolean poweroff();
public native static boolean restart();
}
你程序有错呢,不是要帮你把程序也改了吧,老兄对C我也不熟
我是vs2005
//#include "Winbase.h"
#include "Windows.h"
#include <iostream>
#include "jni.h"
#include "reconserver_SystemDll.h"
JNIEXPORT jboolean JNICALL Java_reconserver_SystemDll_newprocess(JNIEnv *, jobject, jstring str){
STARTUPINFO si;
PROCESS_INFORMATION pi;
char* message;
message=(char*)(*env)->GetStringUTFChars(env, str, NULL); //改了这里 ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) ); // Start the child process.
if( !CreateProcess( NULL, // No module name (use command line).
message, // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
)
{
return false;
}
//cout<<π
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
return true;
}
JNIEXPORT jboolean JNICALL Java_reconserver_SystemDll_killprocess(JNIEnv *, jobject, jint id){HANDLE hProcessSnap = NULL;
BOOL bRet = FALSE;
int pid=id;
hProcessSnap=OpenProcess(PROCESS_ALL_ACCESS,false,pid);
bRet=TerminateProcess(hProcessSnap,1);
return bRet;
}
JNIEXPORT jboolean JNICALL Java_reconserver_SystemDll_poweroff(JNIEnv *, jobject){
HANDLE hToken; // handle to process token
TOKEN_PRIVILEGES tkp; // pointer to token structure
BOOL fResult; // system shutdown flag
// Get the current process token handle so we can get shutdown
// privilege.
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return FALSE;
// Get the LUID for 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 shutdown privilege for this process.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES) NULL, 0);
// Cannot test the return value of AdjustTokenPrivileges.
if (GetLastError() != ERROR_SUCCESS)
return FALSE;
// Display the shutdown dialog box and start the countdown.
fResult = InitiateSystemShutdown(
NULL, // shut down local computer
NULL, // message for user
5, // time-out period
FALSE, // ask user to close apps
FALSE); // reboot after shutdown
if (!fResult)
return FALSE;
// Disable shutdown privilege.
tkp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES) NULL, 0);
return TRUE;
}JNIEXPORT jboolean JNICALL Java_reconserver_SystemDll_restart(JNIEnv *, jobject){
HANDLE hToken; // handle to process token
TOKEN_PRIVILEGES tkp; // pointer to token structure
BOOL fResult; // system shutdown flag
// Get the current process token handle so we can get shutdown
// privilege.
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return FALSE;
// Get the LUID for 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 shutdown privilege for this process.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES) NULL, 0);
// Cannot test the return value of AdjustTokenPrivileges.
if (GetLastError() != ERROR_SUCCESS)
return FALSE;
// Display the shutdown dialog box and start the countdown.
fResult = InitiateSystemShutdown(
NULL, // shut down local computer
NULL, // message for user
5, // time-out period
FALSE, // ask user to close apps
TRUE); // reboot after shutdown
if (!fResult)
return FALSE;
// Disable shutdown privilege.
tkp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES) NULL, 0);
return TRUE;
}
void main(int argc, char *argv[])
{}
还有应该写.def的文件就来申明导出函数列表放到哪里。
是不是把以上我不会的那两点搞好后,编译,执行在debug里面生成那个.dll文件就是我要的