sigar/bindings/java/src/jni/win32/service.cpp

372 lines
10 KiB
C++
Raw Normal View History

2004-11-21 05:01:16 +08:00
#ifdef WIN32
#define UNICODE
#define _UNICODE
#include "win32bindings.h"
#include "javasigar.h"
#ifdef __cplusplus
extern "C" {
#endif
#define STRING_SIG "Ljava/lang/String;"
#define SERVICE_SetStringField(field, str) \
id = env->GetFieldID(cls, field, STRING_SIG); \
value = env->NewString((const jchar *)str, lstrlen(str)); \
env->SetObjectField(obj, id, value)
#define SERVICE_SetIntField(field, val) \
id = env->GetFieldID(cls, field, "I"); \
env->SetIntField(obj, id, val)
typedef DWORD (CALLBACK *ChangeServiceConfig2_func_t)(SC_HANDLE,
DWORD, LPVOID);
2004-11-21 05:01:16 +08:00
2005-06-26 10:18:05 +08:00
typedef DWORD (CALLBACK *QueryServiceConfig2_func_t)(SC_HANDLE,
DWORD,
LPSERVICE_DESCRIPTION,
DWORD,
LPDWORD);
2004-11-21 05:01:16 +08:00
JNIEXPORT jboolean SIGAR_JNI(win32_Service_ChangeServiceDescription)
(JNIEnv *env, jclass, jlong handle, jstring description)
{
jboolean result = FALSE;
2005-06-27 11:41:43 +08:00
SERVICE_DESCRIPTION desc;
HINSTANCE lib;
ChangeServiceConfig2_func_t change_config;
if ((lib = LoadLibrary(L"advapi32"))) {
change_config = (ChangeServiceConfig2_func_t)
GetProcAddress(lib, "ChangeServiceConfig2W");
if (change_config) {
2005-06-27 11:41:43 +08:00
desc.lpDescription =
(LPTSTR)env->GetStringChars(description, NULL);
result = change_config((SC_HANDLE)handle,
2005-06-27 11:41:43 +08:00
SERVICE_CONFIG_DESCRIPTION, &desc);
env->ReleaseStringChars(description,
2005-06-27 11:41:43 +08:00
(const jchar *)desc.lpDescription);
}
2004-11-21 05:01:16 +08:00
FreeLibrary(lib);
2004-11-21 05:01:16 +08:00
}
return result;
2004-11-21 05:01:16 +08:00
}
JNIEXPORT jboolean SIGAR_JNI(win32_Service_CloseServiceHandle)
(JNIEnv *, jclass, jlong handle)
{
return CloseServiceHandle((SC_HANDLE)handle);
}
JNIEXPORT void SIGAR_JNI(win32_Service_ControlService)
(JNIEnv *env, jclass, jlong handle, jint control)
2004-11-21 05:01:16 +08:00
{
BOOL retval;
2005-06-27 11:41:43 +08:00
SERVICE_STATUS status;
2005-06-26 13:27:35 +08:00
if (control == 0) {
retval = StartService((SC_HANDLE)handle, 0, NULL);
}
else {
retval = ControlService((SC_HANDLE)handle, control, &status);
}
2005-06-27 11:41:43 +08:00
if (!retval) {
win32_throw_last_error(env);
2005-06-26 13:27:35 +08:00
}
2004-11-21 05:01:16 +08:00
}
JNIEXPORT jlong SIGAR_JNI(win32_Service_CreateService)
2005-06-27 11:41:43 +08:00
(JNIEnv *env, jclass, jlong handle,
jstring j_name, jstring j_display, jint type,
jint startType, jint errorControl, jstring j_path,
jobjectArray dependencies,
jstring j_startName,
jstring j_password)
2004-11-21 05:01:16 +08:00
{
2005-06-27 11:41:43 +08:00
TCHAR buffer[8192];
LPCTSTR depend = NULL;
jlong result;
LPCTSTR startName;
LPCTSTR name = (LPCTSTR)env->GetStringChars(j_name, NULL);
LPCTSTR display = (LPCTSTR)env->GetStringChars(j_display, NULL);
LPCTSTR path = (LPCTSTR)env->GetStringChars(j_path, NULL);
LPCTSTR password = (LPCTSTR)env->GetStringChars(j_password, NULL);
if (j_startName != NULL) {
startName = (LPCTSTR)env->GetStringChars(j_startName, NULL);
}
else {
startName = NULL;
}
if (dependencies != NULL) {
2004-11-21 05:01:16 +08:00
// Build a buffer of a double null terminated array of
// null terminated service names
2005-06-27 11:41:43 +08:00
LPTSTR ptr = buffer;
jsize alen = env->GetArrayLength(dependencies);
depend = buffer;
2004-11-21 05:01:16 +08:00
2005-06-27 11:41:43 +08:00
for (int i=0; i<alen; i++) {
2004-11-21 05:01:16 +08:00
jstring str = (jstring)env->GetObjectArrayElement(dependencies, i);
2005-06-27 11:41:43 +08:00
LPCTSTR chars = (LPCTSTR)env->GetStringChars(str, NULL);
size_t len = lstrlen(chars);
2004-11-21 05:01:16 +08:00
// If we're going to overrun the buffer then break out of the loop
2005-06-27 11:41:43 +08:00
if ((ptr + len + 1) >=
(buffer + sizeof(buffer) / sizeof(TCHAR)))
{
2004-11-21 05:01:16 +08:00
break;
2005-06-27 11:41:43 +08:00
}
2004-11-21 05:01:16 +08:00
2005-06-27 11:41:43 +08:00
lstrcpy(ptr, chars);
env->ReleaseStringChars(str, (const jchar *)chars);
2004-11-21 05:01:16 +08:00
// Move the buffer to the byte beyond the current string
// null terminator
2005-06-27 11:41:43 +08:00
ptr = ptr + len + 1;
2004-11-21 05:01:16 +08:00
}
2005-06-27 11:41:43 +08:00
*ptr = 0; // Double null terminate the string
2004-11-21 05:01:16 +08:00
}
// Create the Service
2005-06-27 11:41:43 +08:00
result = (jlong)CreateService((SC_HANDLE)handle, name,
display, SERVICE_ALL_ACCESS,
type,
startType, errorControl, path,
NULL, NULL, depend, startName,
password);
if (startName != NULL) {
env->ReleaseStringChars(j_startName, (const jchar *)startName);
}
env->ReleaseStringChars(j_password, (const jchar *)password);
env->ReleaseStringChars(j_path, (const jchar *)path);
env->ReleaseStringChars(j_display, (const jchar *)display);
env->ReleaseStringChars(j_name, (const jchar *)name);
if (result == 0) {
win32_throw_last_error(env);
}
2005-06-27 11:41:43 +08:00
return result;
2004-11-21 05:01:16 +08:00
}
2005-06-27 06:10:28 +08:00
JNIEXPORT void SIGAR_JNI(win32_Service_DeleteService)
2004-11-21 05:01:16 +08:00
(JNIEnv *env, jclass, jlong handle)
{
2005-06-27 06:07:58 +08:00
if (!DeleteService((SC_HANDLE)handle)) {
win32_throw_last_error(env);
}
2004-11-21 05:01:16 +08:00
}
JNIEXPORT jlong SIGAR_JNI(win32_Service_OpenSCManager)
2005-06-27 11:41:43 +08:00
(JNIEnv *env, jclass, jstring jmachine, jint access)
2004-11-21 05:01:16 +08:00
{
2005-06-27 11:41:43 +08:00
LPCTSTR machine = (LPCTSTR)env->GetStringChars(jmachine, NULL);
jlong result = (jlong)OpenSCManager(machine, NULL, access);
2004-11-21 05:01:16 +08:00
2005-06-27 11:41:43 +08:00
env->ReleaseStringChars(jmachine, (const jchar *)machine);
2004-11-21 05:01:16 +08:00
2005-06-27 11:41:43 +08:00
if (!result) {
win32_throw_last_error(env);
}
2005-06-27 11:41:43 +08:00
return result;
2004-11-21 05:01:16 +08:00
}
JNIEXPORT jlong SIGAR_JNI(win32_Service_OpenService)
2005-06-27 11:41:43 +08:00
(JNIEnv *env, jclass, jlong handle,
jstring jservice, jint access)
2004-11-21 05:01:16 +08:00
{
2005-06-27 11:41:43 +08:00
LPCTSTR service = (LPCTSTR)env->GetStringChars(jservice, NULL);
jlong result =
(jlong)OpenService((SC_HANDLE)handle, service, access);
2004-11-21 05:01:16 +08:00
2005-06-27 11:41:43 +08:00
env->ReleaseStringChars(jservice, (const jchar *)service);
if (!result) {
win32_throw_last_error(env);
}
2005-06-27 11:41:43 +08:00
return result;
2004-11-21 05:01:16 +08:00
}
JNIEXPORT jint
SIGAR_JNI(win32_Service_QueryServiceStatus)
(JNIEnv *, jclass, jlong handle)
{
SERVICE_STATUS status;
2005-06-27 11:41:43 +08:00
int result;
2004-11-21 05:01:16 +08:00
2005-06-27 11:41:43 +08:00
if (QueryServiceStatus((SC_HANDLE)handle, &status) == TRUE) {
result = status.dwCurrentState;
}
else {
result = -1;
}
2004-11-21 05:01:16 +08:00
2005-06-27 11:41:43 +08:00
return result;
2004-11-21 05:01:16 +08:00
}
2004-12-12 10:48:39 +08:00
JNIEXPORT jobject SIGAR_JNI(win32_Service_getServiceNames)
(JNIEnv *env, jclass)
{
SC_HANDLE handle =
OpenSCManager(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
ENUM_SERVICE_STATUS status, *services;
BOOL retval;
DWORD bytes, count, resume=0;
DWORD type = SERVICE_WIN32, state = SERVICE_STATE_ALL;
jobject listobj;
jclass listclass =
env->FindClass("java/util/ArrayList");
jmethodID listid =
env->GetMethodID(listclass, "<init>", "()V");
jmethodID addid =
env->GetMethodID(listclass, "add",
"(Ljava/lang/Object;)"
"Z");
if (handle == NULL) {
return NULL;
}
retval = EnumServicesStatus(handle, type, state,
&status, sizeof(status),
&bytes, &count, &resume);
DWORD err = GetLastError();
2005-06-27 11:41:43 +08:00
if ((retval == FALSE) || (err == ERROR_MORE_DATA)) {
2004-12-12 10:48:39 +08:00
DWORD size = bytes + sizeof(ENUM_SERVICE_STATUS);
services = new ENUM_SERVICE_STATUS[size];
EnumServicesStatus(handle, type, state, services,
size, &bytes, &count, &resume);
}
listobj = env->NewObject(listclass, listid);
for (int i=0; i<count; i++) {
jstring name =
env->NewString((const jchar *)services[i].lpServiceName,
lstrlen(services[i].lpServiceName));
env->CallBooleanMethod(listobj, addid, name);
}
CloseServiceHandle(handle);
delete services;
return listobj;
}
/*
* convert:
* "RPCSS\0Tcpip\0IPSec\0\0"
* to:
* ["RPCSS", "Tcpip", "IPSec"]
*/
static int to_array(JNIEnv *env, LPTSTR str, jobjectArray array)
{
TCHAR *ptr = &str[0];
int offset=0, i=0;
while (*ptr != 0) {
int slen = _tcslen(ptr);
if (array) {
jstring jstr =
env->NewString((const jchar *)ptr, slen);
env->SetObjectArrayElement(array, i, jstr);
}
offset += slen + 1;
ptr = &str[offset];
i++;
}
return i;
}
JNIEXPORT jboolean SIGAR_JNI(win32_Service_QueryServiceConfig)
(JNIEnv *env, jclass, jlong handle, jobject obj)
{
2005-06-27 11:41:43 +08:00
char buffer[8192]; /* 8k is max size from mdsn docs */
LPQUERY_SERVICE_CONFIG config = (LPQUERY_SERVICE_CONFIG)buffer;
DWORD bytes;
jfieldID id;
jclass cls = env->GetObjectClass(obj);
jstring value;
2005-06-26 10:18:05 +08:00
HINSTANCE lib;
if (!QueryServiceConfig((SC_HANDLE)handle, config,
sizeof(buffer), &bytes))
{
win32_throw_last_error(env);
return JNI_FALSE;
}
SERVICE_SetIntField("type", config->dwServiceType);
SERVICE_SetIntField("startType", config->dwStartType);
SERVICE_SetIntField("errorControl", config->dwErrorControl);
2005-06-26 10:44:44 +08:00
SERVICE_SetStringField("path", config->lpBinaryPathName);
SERVICE_SetStringField("loadOrderGroup", config->lpLoadOrderGroup);
SERVICE_SetIntField("tagId", config->dwTagId);
if (config->lpDependencies) {
/* first pass just get num for NewObjectArray */
int num = to_array(env, config->lpDependencies, NULL);
jclass stringclass =
env->FindClass("java/lang/String");
jobjectArray dependencies =
env->NewObjectArray(num, stringclass, 0);
to_array(env, config->lpDependencies, dependencies);
id = env->GetFieldID(cls, "dependencies",
"[" STRING_SIG);
env->SetObjectField(obj, id, dependencies);
}
2005-06-27 09:20:59 +08:00
SERVICE_SetStringField("startName", config->lpServiceStartName);
SERVICE_SetStringField("displayName", config->lpDisplayName);
2005-06-26 10:18:05 +08:00
if ((lib = LoadLibrary(L"advapi32"))) {
LPSERVICE_DESCRIPTION desc =
(LPSERVICE_DESCRIPTION)buffer;
QueryServiceConfig2_func_t query_config =
(QueryServiceConfig2_func_t)
GetProcAddress(lib, "QueryServiceConfig2W");
if (query_config) {
BOOL retval =
query_config((SC_HANDLE)handle,
SERVICE_CONFIG_DESCRIPTION,
desc, sizeof(buffer), &bytes);
if (retval && (desc->lpDescription != NULL)) {
SERVICE_SetStringField("description",
desc->lpDescription);
}
}
FreeLibrary(lib);
}
return JNI_TRUE;
}
2004-11-21 05:01:16 +08:00
#ifdef __cplusplus
}
#endif
#endif /* WIN32 */