Author: mturk
Date: Tue Jul 19 09:42:36 2011
New Revision: 1148260

URL: http://svn.apache.org/viewvc?rev=1148260&view=rev
Log:
Split access right to a separate class

Added:
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/AccessRights.java
   (with props)
Modified:
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Win32.java
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsMutex.java
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsSemaphore.java
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsShm.java
    commons/sandbox/runtime/trunk/src/main/native/os/win32/winapi.c

Added: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/AccessRights.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/AccessRights.java?rev=1148260&view=auto
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/AccessRights.java
 (added)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/AccessRights.java
 Tue Jul 19 09:42:36 2011
@@ -0,0 +1,119 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.runtime.platform.windows;
+
+import java.util.EnumSet;
+
+/**
+ * Win32 Access Rights.
+ */
+public final class AccessRights
+{
+    private AccessRights()
+    {
+        // Pure static class   
+    }
+
+    /* Standard access rights   */
+    public static final int         DELETE                  = 0x00010000;
+    public static final int         READ_CONTROL            = 0x00020000;
+    public static final int         SYNCHRONIZE             = 0x00100000;
+    public static final int         WRITE_DAC               = 0x00040000;
+    public static final int         WRITE_OWNER             = 0x00080000;
+    public static final int         MAXIMUM_ALLOWED         = 0x02000000;
+    public static final int         STANDARD_RIGHTS_REQUIRED= 0x000F0000;
+    public static final int         STANDARD_RIGHTS_READ    = READ_CONTROL;
+    public static final int         STANDARD_RIGHTS_WRITE   = READ_CONTROL;
+    public static final int         STANDARD_RIGHTS_EXECUTE = READ_CONTROL;
+    public static final int         STANDARD_RIGHTS_ALL     = 0x001F0000;
+    public static final int         SPECIFIC_RIGHTS_ALL     = 0x0000FFFF;
+
+    /* Generic rights           */
+    public static final int         GENERIC_READ            = 0x80000000;
+    public static final int         GENERIC_WRITE           = 0x40000000;
+    public static final int         GENERIC_EXECUTE         = 0x20000000;
+    public static final int         GENERIC_ALL             = 0x10000000;
+
+    /* Generic cumulative rights */
+    public static final int         GENERIC_RWX             = 0xe0000000;
+    public static final int         GENERIC_RWR             = 0xc0000000;
+
+    public static final int         SECTION_QUERY               = 0x0001;
+    public static final int         SECTION_MAP_WRITE           = 0x0002;
+    public static final int         SECTION_MAP_READ            = 0x0004;
+    public static final int         SECTION_MAP_EXECUTE         = 0x0008;
+    public static final int         SECTION_EXTEND_SIZE         = 0x0010;
+    /* not included in SECTION_ALL_ACCESS */
+    public static final int         SECTION_MAP_EXECUTE_EXPLICIT= 0x0020;
+    public static final int         SECTION_ALL_ACCESS = 
STANDARD_RIGHTS_REQUIRED|
+                                                         SECTION_QUERY |
+                                                         SECTION_MAP_WRITE |
+                                                         SECTION_MAP_READ |
+                                                         SECTION_MAP_EXECUTE |
+                                                         SECTION_EXTEND_SIZE;
+    /* Some of the file access rights */
+    public static final int         FILE_EXECUTE            = 0x00000020;
+    public static final int         FILE_READ_DATA          = 0x00000001;
+    public static final int         FILE_WRITE_DATA         = 0x00000002;
+    public static final int         FILE_READ_EA            = 0x00000008;
+    public static final int         FILE_WRITE_EA           = 0x00000010;
+    public static final int         FILE_READ_ATTRIBUTES    = 0x00000080;
+    public static final int         FILE_WRITE_ATTRIBUTES   = 0x00000100;
+
+
+    public static final int         FILE_MAP_COPY           = SECTION_QUERY;
+    public static final int         FILE_MAP_WRITE          = 
SECTION_MAP_WRITE;
+    public static final int         FILE_MAP_READ           = SECTION_MAP_READ;
+    public static final int         FILE_MAP_ALL_ACCESS     = 
SECTION_ALL_ACCESS;
+    /* not included in FILE_MAP_ALL_ACCESS */
+    public static final int         FILE_MAP_EXECUTE        = 
SECTION_MAP_EXECUTE_EXPLICIT;
+
+    /* Memory protection flags  */
+    public static final int         PAGE_EXECUTE                    = 
0x00000010;
+    public static final int         PAGE_EXECUTE_READ               = 
0x00000020;
+    public static final int         PAGE_EXECUTE_READWRITE          = 
0x00000040;
+    public static final int         PAGE_EXECUTE_WRITECOPY          = 
0x00000080;
+    public static final int         PAGE_NOACCESS                   = 
0x00000001;
+    public static final int         PAGE_READONLY                   = 
0x00000002;
+    public static final int         PAGE_READWRITE                  = 
0x00000004;
+    public static final int         PAGE_WRITECOPY                  = 
0x00000008;
+    public static final int         PAGE_GUARD                      = 
0x00000100;
+    public static final int         PAGE_NOCACHE                    = 
0x00000200;
+    public static final int         PAGE_WRITECOMBINE               = 
0x00000400;
+
+    /* ServiceManager Access rights */
+    public static final int         SC_MANAGER_CONNECT              = 0x0001;
+    public static final int         SC_MANAGER_CREATE_SERVICE       = 0x0002;
+    public static final int         SC_MANAGER_ENUMERATE_SERVICE    = 0x0004;
+    public static final int         SC_MANAGER_LOCK                 = 0x0008;
+    public static final int         SC_MANAGER_QUERY_LOCK_STATUS    = 0x0010;
+    public static final int         SC_MANAGER_MODIFY_BOOT_CONFIG   = 0x0020;
+    public static final int         SC_MANAGER_ALL_ACCESS           = 
STANDARD_RIGHTS_REQUIRED | 0x003F;
+
+    /* Service Access rights */
+    public static final int         SERVICE_ALL_ACCESS              = 0xF01FF;
+    public static final int         SERVICE_CHANGE_CONFIG           = 0x0002;
+    public static final int         SERVICE_ENUMERATE_DEPENDENTS    = 0x0008;
+    public static final int         SERVICE_INTERROGATE             = 0x0080;
+    public static final int         SERVICE_PAUSE_CONTINUE          = 0x0040;
+    public static final int         SERVICE_QUERY_CONFIG            = 0x0001;
+    public static final int         SERVICE_QUERY_STATUS            = 0x0004;
+    public static final int         SERVICE_START                   = 0x0010;
+    public static final int         SERVICE_STOP                    = 0x0020;
+    public static final int         SERVICE_USER_DEFINED_CONTROL    = 0x0100;
+
+}

Propchange: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/AccessRights.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Win32.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Win32.java?rev=1148260&r1=1148259&r2=1148260&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Win32.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Win32.java
 Tue Jul 19 09:42:36 2011
@@ -43,67 +43,6 @@ final class Win32
     public static final int         WAIT_ABANDONED          = 0x00000080;
     public static final int         WAIT_TIMEOUT            = 0x00000102;
 
-    /* Standard access rights   */
-    public static final int         DELETE                  = 0x00010000;
-    public static final int         READ_CONTROL            = 0x00020000;
-    public static final int         SYNCHRONIZE             = 0x00100000;
-    public static final int         WRITE_DAC               = 0x00040000;
-    public static final int         WRITE_OWNER             = 0x00080000;
-    public static final int         MAXIMUM_ALLOWED         = 0x02000000;
-    public static final int         STANDARD_RIGHTS_REQUIRED= 0x000F0000;
-    public static final int         STANDARD_RIGHTS_READ    = READ_CONTROL;
-    public static final int         STANDARD_RIGHTS_WRITE   = READ_CONTROL;
-    public static final int         STANDARD_RIGHTS_EXECUTE = READ_CONTROL;
-    public static final int         STANDARD_RIGHTS_ALL     = 0x001F0000;
-    public static final int         SPECIFIC_RIGHTS_ALL     = 0x0000FFFF;
-
-    /* Generic rights           */
-    public static final int         GENERIC_READ            = 0x80000000;
-    public static final int         GENERIC_WRITE           = 0x40000000;
-    public static final int         GENERIC_EXECUTE         = 0x20000000;
-    public static final int         GENERIC_ALL             = 0x10000000;
-
-    /* Generic cumulative rights */
-    public static final int         GENERIC_RWX             = 0xe0000000;
-    public static final int         GENERIC_RWR             = 0xc0000000;
-
-    /* Mutex access rights      */
-    public static final int         MUTEX_ALL_ACCESS        = 0x001F0001;
-    public static final int         MUTEX_MODIFY_STATE      = 0x00000001;
-    /* semaphore access rights  */
-    public static final int         SEMAPHORE_ALL_ACCESS    = 0x001F0003;
-    public static final int         SEMAPHORE_MODIFY_STATE  = 0x00000002;
-
-    public static final int         SECTION_QUERY               = 0x0001;
-    public static final int         SECTION_MAP_WRITE           = 0x0002;
-    public static final int         SECTION_MAP_READ            = 0x0004;
-    public static final int         SECTION_MAP_EXECUTE         = 0x0008;
-    public static final int         SECTION_EXTEND_SIZE         = 0x0010;
-    /* not included in SECTION_ALL_ACCESS */
-    public static final int         SECTION_MAP_EXECUTE_EXPLICIT= 0x0020;
-    public static final int         SECTION_ALL_ACCESS = 
STANDARD_RIGHTS_REQUIRED|
-                                                         SECTION_QUERY |
-                                                         SECTION_MAP_WRITE |
-                                                         SECTION_MAP_READ |
-                                                         SECTION_MAP_EXECUTE |
-                                                         SECTION_EXTEND_SIZE;
-    /* Some of the file access rights */
-    public static final int         FILE_EXECUTE            = 0x00000020;
-    public static final int         FILE_READ_DATA          = 0x00000001;
-    public static final int         FILE_WRITE_DATA         = 0x00000002;
-    public static final int         FILE_READ_EA            = 0x00000008;
-    public static final int         FILE_WRITE_EA           = 0x00000010;
-    public static final int         FILE_READ_ATTRIBUTES    = 0x00000080;
-    public static final int         FILE_WRITE_ATTRIBUTES   = 0x00000100;
-
-
-    public static final int         FILE_MAP_COPY           = SECTION_QUERY;
-    public static final int         FILE_MAP_WRITE          = 
SECTION_MAP_WRITE;
-    public static final int         FILE_MAP_READ           = SECTION_MAP_READ;
-    public static final int         FILE_MAP_ALL_ACCESS     = 
SECTION_ALL_ACCESS;
-    /* not included in FILE_MAP_ALL_ACCESS */
-    public static final int         FILE_MAP_EXECUTE        = 
SECTION_MAP_EXECUTE_EXPLICIT;
-
     /* Memory allocation flags  */
     public static final int         MEM_COMMIT              = 0x00001000;
     public static final int         MEM_RESERVE             = 0x00002000;
@@ -111,31 +50,11 @@ final class Win32
     /* Memory free flags  */
     public static final int         MEM_DECOMMIT            = 0x00004000;
     public static final int         MEM_RELEASE             = 0x00008000;
-    /* Memory protection flags  */
-    public static final int         PAGE_EXECUTE            = 0x00000010;
-    public static final int         PAGE_EXECUTE_READ       = 0x00000020;
-    public static final int         PAGE_EXECUTE_READWRITE  = 0x00000040;
-    public static final int         PAGE_EXECUTE_WRITECOPY  = 0x00000080;
-    public static final int         PAGE_NOACCESS           = 0x00000001;
-    public static final int         PAGE_READONLY           = 0x00000002;
-    public static final int         PAGE_READWRITE          = 0x00000004;
-    public static final int         PAGE_WRITECOPY          = 0x00000008;
-    public static final int         PAGE_GUARD              = 0x00000100;
-    public static final int         PAGE_NOCACHE            = 0x00000200;
-    public static final int         PAGE_WRITECOMBINE       = 0x00000400;
 
     public static final int         INVALID_HANDLE_VALUE    = 0xFFFFFFFF;
     public static final String      SERVICES_ACTIVE_DATABASE        = 
"ServicesActive";
     public static final String      SERVICES_FAILED_DATABASE        = 
"ServicesFailed";
 
-    public static final int         SC_MANAGER_CONNECT              = 0x0001;
-    public static final int         SC_MANAGER_CREATE_SERVICE       = 0x0002;
-    public static final int         SC_MANAGER_ENUMERATE_SERVICE    = 0x0004;
-    public static final int         SC_MANAGER_LOCK                 = 0x0008;
-    public static final int         SC_MANAGER_QUERY_LOCK_STATUS    = 0x0010;
-    public static final int         SC_MANAGER_MODIFY_BOOT_CONFIG   = 0x0020;
-    public static final int         SC_MANAGER_ALL_ACCESS           = 
STANDARD_RIGHTS_REQUIRED | 0x003F;
-
     public static native int        CloseHandle(int handle);
     public static native int        LocalFree(long ptr);
     public static native int        WaitForSingleObject(int handle, int 
timeout);
@@ -150,8 +69,13 @@ final class Win32
     public static native int        VirtualProtect(long addr, long size, int 
protect);
     public static native int        VirtualFree(long addr, long size, int 
type);
 
+    /* Service API */
     public static native long       OpenSCManager(String machine, String 
database, int mode);
     public static native int        CloseServiceHandle(long handle);
+    public static native long       OpenService(long scm, String name, int 
access);
+    public static native int        QueryServiceStatusEx(long service, int[] 
SERVICE_STATUS_PROCESS);
+    public static native int        StartService(long service, int nargs, 
String[] args);
+    public static native int        ControlService(long service, int ctl, 
int[] SERVICE_STATUS);
 
     public static final int         HEAP_POINTER            = 1;
     public static final int         SLICE_POINTER           = 2;

Modified: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsMutex.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsMutex.java?rev=1148260&r1=1148259&r2=1148260&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsMutex.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsMutex.java
 Tue Jul 19 09:42:36 2011
@@ -33,6 +33,9 @@ import java.io.IOException;
  */
 final class WindowsMutex extends Mutex
 {
+    /* Mutex access rights      */
+    public static final int         MUTEX_ALL_ACCESS        = 0x001F0001;
+    public static final int         MUTEX_MODIFY_STATE      = 0x00000001;
 
     private WindowsMutex()
     {
@@ -61,9 +64,9 @@ final class WindowsMutex extends Mutex
             throw new InvalidArgumentException();
         this.name = "Global\\" + name.replace('\\', '_');
         if (owner) {
-            long s = Security.getSecurityDescriptor(Win32.MUTEX_ALL_ACCESS   | 
Win32.GENERIC_RWX,
-                                                    Win32.MUTEX_ALL_ACCESS   | 
Win32.GENERIC_RWX,
-                                                    Win32.MUTEX_MODIFY_STATE | 
Win32.GENERIC_RWR);
+            long s = Security.getSecurityDescriptor(MUTEX_ALL_ACCESS   | 
AccessRights.GENERIC_RWX,
+                                                    MUTEX_ALL_ACCESS   | 
AccessRights.GENERIC_RWX,
+                                                    MUTEX_MODIFY_STATE | 
AccessRights.GENERIC_RWR);
             handle = create0(this.name, s);
         }
         else {

Modified: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsSemaphore.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsSemaphore.java?rev=1148260&r1=1148259&r2=1148260&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsSemaphore.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsSemaphore.java
 Tue Jul 19 09:42:36 2011
@@ -34,6 +34,10 @@ import java.io.IOException;
 final class WindowsSemaphore extends Semaphore
 {
 
+    /* semaphore access rights  */
+    public static final int         SEMAPHORE_ALL_ACCESS    = 0x001F0003;
+    public static final int         SEMAPHORE_MODIFY_STATE  = 0x00000002;
+
     private WindowsSemaphore()
     {
         // No Instance
@@ -60,9 +64,9 @@ final class WindowsSemaphore extends Sem
         if (name == null)
             throw new InvalidArgumentException();
         this.name = "Global\\" + name.replace('\\', '_');
-        long sa = Security.getSecurityDescriptor(Win32.SEMAPHORE_ALL_ACCESS   
| Win32.GENERIC_RWX,
-                                                 Win32.SEMAPHORE_ALL_ACCESS   
| Win32.GENERIC_RWX,
-                                                 Win32.SEMAPHORE_MODIFY_STATE 
| Win32.GENERIC_RWR);
+        long sa = Security.getSecurityDescriptor(SEMAPHORE_ALL_ACCESS   | 
AccessRights.GENERIC_RWX,
+                                                 SEMAPHORE_ALL_ACCESS   | 
AccessRights.GENERIC_RWX,
+                                                 SEMAPHORE_MODIFY_STATE | 
AccessRights.GENERIC_RWR);
         handle = create0(this.name, value, sa);
         owner = true;
     }

Modified: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsShm.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsShm.java?rev=1148260&r1=1148259&r2=1148260&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsShm.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsShm.java
 Tue Jul 19 09:42:36 2011
@@ -62,7 +62,7 @@ final class WindowsShm extends Shm
         this.size = size;
         handle = Win32.CreateFileMapping(Win32.INVALID_HANDLE_VALUE,
                                          0L,
-                                         Win32.PAGE_READWRITE,
+                                         AccessRights.PAGE_READWRITE,
                                          this.size + 16,
                                          this.name);
         if (handle == 0) {
@@ -73,7 +73,7 @@ final class WindowsShm extends Shm
                 throw new SystemException(Status.describe(rc));
         }
         base = Win32.MapViewOfFile(handle,
-                                   Win32.FILE_MAP_ALL_ACCESS,
+                                   AccessRights.FILE_MAP_ALL_ACCESS,
                                    0L, this.size + 16);
         if (base == 0L) {
             Win32.CloseHandle(handle);
@@ -91,7 +91,7 @@ final class WindowsShm extends Shm
         if (name == null)
             throw new InvalidArgumentException();
         this.name = "Global\\" + name.replace('\\', '_');
-        handle = Win32.OpenFileMapping(Win32.FILE_MAP_READ | 
Win32.FILE_MAP_WRITE,
+        handle = Win32.OpenFileMapping(AccessRights.FILE_MAP_READ | 
AccessRights.FILE_MAP_WRITE,
                                        false,
                                        this.name);
         if (handle == 0) {
@@ -101,7 +101,7 @@ final class WindowsShm extends Shm
             else
                 throw new SystemException(Status.describe(rc));
         }
-        long header = Win32.MapViewOfFile(handle, Win32.FILE_MAP_READ, 0L, 64);
+        long header = Win32.MapViewOfFile(handle, AccessRights.FILE_MAP_READ, 
0L, 64);
         if (header == 0L) {
             Win32.CloseHandle(handle);
             throw new SystemException(Errno.msg());
@@ -113,7 +113,7 @@ final class WindowsShm extends Shm
             throw new SystemException(Errno.msg());
         }
         base = Win32.MapViewOfFile(handle,
-                                   Win32.FILE_MAP_READ | Win32.FILE_MAP_WRITE,
+                                   AccessRights.FILE_MAP_READ | 
AccessRights.FILE_MAP_WRITE,
                                    0L, size + 16);
         if (base == 0L) {
             Win32.CloseHandle(handle);

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/winapi.c
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/winapi.c?rev=1148260&r1=1148259&r2=1148260&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/winapi.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/winapi.c Tue Jul 19 
09:42:36 2011
@@ -187,17 +187,123 @@ ACR_WIN_EXPORT(jlong, Win32, OpenSCManag
     WITH_WSTR(database) {
         scm = OpenSCManager(J2S(machine), J2S(database), mode);
         if (scm == 0)
-            ACR_SAVE_OS_ERROR();
+            rc = ACR_GET_OS_ERROR();
     } DONE_WITH_STR(database);
     } DONE_WITH_STR(machine);
 
+    if (scm == 0)
+        ACR_SAVE_ERROR(rc);
     return P2J(scm);
 }
 
 ACR_WIN_EXPORT(jint, Win32, CloseServiceHandle)(JNI_STDARGS, jlong handle)
 {
+    if (handle == 0LL) {
+        /* XXX: Do nothing ? */
+        return 0;
+    }
     if (CloseServiceHandle(J2P(handle, SC_HANDLE)))
         return 0;
     else
         return ACR_GET_OS_ERROR();
 }
+
+ACR_WIN_EXPORT(jlong, Win32, OpenService)(JNI_STDARGS, jlong hscm, jstring 
name, jint acc)
+{
+    DWORD     rc  = ACR_EINVAL;
+    SC_HANDLE scm = J2P(hscm, SC_HANDLE);
+    SC_HANDLE svc = 0;
+
+    if (IS_INVALID_HANDLE(scm)) {
+        ACR_SAVE_ERROR(ACR_EINVAL);
+        return 0;
+    }
+    WITH_WSTR(name) {
+        svc = OpenServiceW(scm, J2S(name), acc);
+        if (svc == 0)
+            rc = ACR_GET_OS_ERROR();
+    } DONE_WITH_STR(name);
+
+    if (svc == 0)
+        ACR_SAVE_ERROR(rc);
+    return P2J(svc);
+}
+
+ACR_WIN_EXPORT(jint, Win32, QueryServiceStatusEx)(JNI_STDARGS, jlong hsvc, 
jintArray stat)
+{
+    SC_HANDLE svc = J2P(hsvc, SC_HANDLE);
+    SERVICE_STATUS_PROCESS ss;
+    DWORD     sz;
+
+    if (IS_INVALID_HANDLE(svc) || stat == 0)
+        return ACR_EINVAL;
+
+    if (QueryServiceStatusEx(svc, SC_STATUS_PROCESS_INFO, (LPBYTE)&ss,
+                             ISIZEOF(SERVICE_STATUS_PROCESS), &sz)) {
+        /* We could just use
+         * (*env)->SetIntArrayRegion(env, stat, 0, 9, (jint *)&ss);
+         * instead copying the members.
+         */
+        jint ja[9];
+        ja[0] = ss.dwServiceType;
+        ja[1] = ss.dwCurrentState;
+        ja[2] = ss.dwControlsAccepted;
+        ja[3] = ss.dwWin32ExitCode;
+        ja[4] = ss.dwServiceSpecificExitCode;
+        ja[5] = ss.dwCheckPoint;
+        ja[6] = ss.dwWaitHint;
+        ja[7] = ss.dwProcessId;
+        ja[8] = ss.dwServiceFlags;
+        (*env)->SetIntArrayRegion(env, stat, 0, 9, ja);
+        return 0;
+    }
+    else
+        return ACR_GET_OS_ERROR();
+}
+
+ACR_WIN_EXPORT(jint, Win32, StartService)(JNI_STDARGS, jlong hsvc, jint nargs, 
jobjectArray args)
+{
+    DWORD     rc    = 0;
+    SC_HANDLE svc   = J2P(hsvc, SC_HANDLE);
+    wchar_t **wargs = 0;
+
+    if (IS_INVALID_HANDLE(svc))
+        return ACR_EINVAL;
+    if (args != 0) {
+        wargs = AcrGetJavaStringArrayW(env, args);
+        if (wargs == 0)
+            return ACR_GET_OS_ERROR();
+    }
+    if (!StartServiceW(svc, nargs, wargs))
+        rc = ACR_GET_OS_ERROR();
+    AcrFree(wargs);
+    return rc;
+}
+
+ACR_WIN_EXPORT(jint, Win32, ControlService)(JNI_STDARGS, jlong hsvc, jint ctl, 
jintArray stat)
+{
+    SC_HANDLE svc = J2P(hsvc, SC_HANDLE);
+    SERVICE_STATUS ss;
+
+    if (IS_INVALID_HANDLE(svc) || stat == 0)
+        return ACR_EINVAL;
+
+    if (ControlService(svc, ctl, &ss)) {
+        /* We could just use
+         * (*env)->SetIntArrayRegion(env, stat, 0, 7, (jint *)&ss);
+         * instead copying the members.
+         */
+        jint ja[7];
+        ja[0] = ss.dwServiceType;
+        ja[1] = ss.dwCurrentState;
+        ja[2] = ss.dwControlsAccepted;
+        ja[3] = ss.dwWin32ExitCode;
+        ja[4] = ss.dwServiceSpecificExitCode;
+        ja[5] = ss.dwCheckPoint;
+        ja[6] = ss.dwWaitHint;
+        (*env)->SetIntArrayRegion(env, stat, 0, 7, ja);
+        return 0;
+    }
+    else
+        return ACR_GET_OS_ERROR();
+}


Reply via email to