#include "C:\Documents and Settings\Joe Chen\桌面\c源文件\sdfds\sdfds\org_hexun_wangchong_admin_dll_Dll.h";
#include <string.h>
#include <Windows.h>
#include <iostream>
using namespace std ;
bool modifyServiceConfig(LPCWSTR user ,LPCWSTR pass,LPCWSTR lservice);
JNIEXPORT jboolean JNICALL Java_org_hexun_wangchong_admin_dll_Dll_modifyTomcatService
(JNIEnv *env, jobject, jstring username, jstring password, jstring serviceName)
{
//windows系统的用户名
LPCWSTR user = (LPCWSTR)env->GetStringChars(username,0);
//window系统的密码
LPCWSTR pass = (LPCWSTR)env->GetStringChars(password,0);
//window系统的服务名
LPCWSTR lservice = (LPCWSTR)env->GetStringChars(serviceName,0);
bool bools = false;
// modifyServiceConfig(LPCWSTR user ,LPCWSTR pass,LPCWSTR service);
SC_HANDLE scm;
SC_HANDLE service;//句柄
//获得scm
if((scm=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS))==NULL)
wcout<<"OpenSCManager Error\n";
//获得lservice句柄
service=OpenService(scm,lservice,SERVICE_ALL_ACCESS);
//调用window系统的api,重新设置service的拥有者
bool bl=0;
bl= ChangeServiceConfig( service, // handle to service
SERVICE_NO_CHANGE, // type of service
SERVICE_NO_CHANGE , // when to start service
SERVICE_NO_CHANGE , // severity of start failure
NULL, // service binary file name
NULL, // load ordering group name
NULL, // tag identifier
NULL, // array of dependency names
user, // account name -was using serviceName
pass, // account password
NULL); // display name
return bl;//返回BOOL类型
}
/*
注: L"Tomcat6" 是将tomcat字符串改变成LPCWSTR类型的
*/
例子:
int main(){
SC_HANDLE scm;
SC_HANDLE service;//句柄
if((scm=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS))==NULL)
cout<<"OpenSCManager Error\n";
//获得tomcat6 服务的句柄
service=OpenService(scm,L"Tomcat6",SERVICE_ALL_ACCESS);
cout<<service;
//getchar();
//设置tomcat6 服务的的拥有者
ChangeServiceConfig(service, // handle to service
SERVICE_NO_CHANGE, // type of service
SERVICE_NO_CHANGE , // when to start service
SERVICE_NO_CHANGE , // severity of start failure
NULL, // service binary file name
NULL, // load ordering group name
NULL, // tag identifier
NULL, // array of dependency names
L".\\jonw111", // account name -was using serviceName
L"12345678", // account password
NULL); // display name
cout<<111;
getchar();
}