2 * PROJECT: ReactOS Service Control Manager
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/system/services/rpcserver.c
5 * PURPOSE: RPC server interface for the advapi32 calls
6 * COPYRIGHT: Copyright 2005-2006 Eric Kohl
7 * Copyright 2006-2007 Hervé Poussineau <hpoussin@reactos.org>
8 * Copyright 2007 Ged Murphy <gedmurphy@reactos.org>
11 /* INCLUDES ****************************************************************/
19 /* GLOBALS *****************************************************************/
21 #define MANAGER_TAG 0x72674D68 /* 'hMgr' */
22 #define SERVICE_TAG 0x63765368 /* 'hSvc' */
24 typedef struct _SCMGR_HANDLE
32 typedef struct _MANAGER_HANDLE
36 /* FIXME: Insert more data here */
38 WCHAR DatabaseName
[1];
39 } MANAGER_HANDLE
, *PMANAGER_HANDLE
;
42 typedef struct _SERVICE_HANDLE
47 PSERVICE ServiceEntry
;
49 /* FIXME: Insert more data here */
51 } SERVICE_HANDLE
, *PSERVICE_HANDLE
;
54 #define SC_MANAGER_READ \
55 (STANDARD_RIGHTS_READ | \
56 SC_MANAGER_QUERY_LOCK_STATUS | \
57 SC_MANAGER_ENUMERATE_SERVICE)
59 #define SC_MANAGER_WRITE \
60 (STANDARD_RIGHTS_WRITE | \
61 SC_MANAGER_MODIFY_BOOT_CONFIG | \
62 SC_MANAGER_CREATE_SERVICE)
64 #define SC_MANAGER_EXECUTE \
65 (STANDARD_RIGHTS_EXECUTE | \
67 SC_MANAGER_ENUMERATE_SERVICE | \
68 SC_MANAGER_CONNECT | \
69 SC_MANAGER_CREATE_SERVICE)
72 #define SERVICE_READ \
73 (STANDARD_RIGHTS_READ | \
74 SERVICE_INTERROGATE | \
75 SERVICE_ENUMERATE_DEPENDENTS | \
76 SERVICE_QUERY_STATUS | \
79 #define SERVICE_WRITE \
80 (STANDARD_RIGHTS_WRITE | \
81 SERVICE_CHANGE_CONFIG)
83 #define SERVICE_EXECUTE \
84 (STANDARD_RIGHTS_EXECUTE | \
85 SERVICE_USER_DEFINED_CONTROL | \
86 SERVICE_PAUSE_CONTINUE | \
91 /* VARIABLES ***************************************************************/
93 static GENERIC_MAPPING
94 ScmManagerMapping
= {SC_MANAGER_READ
,
97 SC_MANAGER_ALL_ACCESS
};
99 static GENERIC_MAPPING
100 ScmServiceMapping
= {SERVICE_READ
,
103 SC_MANAGER_ALL_ACCESS
};
106 /* FUNCTIONS ***************************************************************/
109 ScmStartRpcServer(VOID
)
113 DPRINT("ScmStartRpcServer() called\n");
115 Status
= RpcServerUseProtseqEpW(L
"ncacn_np",
119 if (Status
!= RPC_S_OK
)
121 DPRINT1("RpcServerUseProtseqEpW() failed (Status %lx)\n", Status
);
125 Status
= RpcServerRegisterIf(svcctl_v2_0_s_ifspec
,
128 if (Status
!= RPC_S_OK
)
130 DPRINT1("RpcServerRegisterIf() failed (Status %lx)\n", Status
);
134 Status
= RpcServerListen(1, 20, TRUE
);
135 if (Status
!= RPC_S_OK
)
137 DPRINT1("RpcServerListen() failed (Status %lx)\n", Status
);
141 DPRINT("ScmStartRpcServer() done\n");
146 ScmCreateManagerHandle(LPWSTR lpDatabaseName
,
151 if (lpDatabaseName
== NULL
)
152 lpDatabaseName
= SERVICES_ACTIVE_DATABASEW
;
154 if (_wcsicmp(lpDatabaseName
,SERVICES_FAILED_DATABASEW
)==0)
156 DPRINT1("Database %S, does not exist\n",lpDatabaseName
);
157 return ERROR_DATABASE_DOES_NOT_EXIST
;
159 else if (_wcsicmp(lpDatabaseName
, SERVICES_ACTIVE_DATABASEW
) != 0)
161 DPRINT1("Invalid Database name %S.\n",lpDatabaseName
);
162 return ERROR_INVALID_NAME
;
165 Ptr
= (MANAGER_HANDLE
*) HeapAlloc(GetProcessHeap(),
167 sizeof(MANAGER_HANDLE
) + (wcslen(lpDatabaseName
) + 1) * sizeof(WCHAR
));
169 return ERROR_NOT_ENOUGH_MEMORY
;
171 Ptr
->Handle
.Tag
= MANAGER_TAG
;
172 Ptr
->Handle
.RefCount
= 1;
174 /* FIXME: initialize more data here */
176 wcscpy(Ptr
->DatabaseName
, lpDatabaseName
);
178 *Handle
= (SC_HANDLE
)Ptr
;
180 return ERROR_SUCCESS
;
185 ScmCreateServiceHandle(PSERVICE lpServiceEntry
,
190 Ptr
= (SERVICE_HANDLE
*) HeapAlloc(GetProcessHeap(),
192 sizeof(SERVICE_HANDLE
));
194 return ERROR_NOT_ENOUGH_MEMORY
;
196 Ptr
->Handle
.Tag
= SERVICE_TAG
;
197 Ptr
->Handle
.RefCount
= 1;
199 /* FIXME: initialize more data here */
200 Ptr
->ServiceEntry
= lpServiceEntry
;
202 *Handle
= (SC_HANDLE
)Ptr
;
204 return ERROR_SUCCESS
;
209 ScmCheckAccess(SC_HANDLE Handle
,
210 DWORD dwDesiredAccess
)
212 PMANAGER_HANDLE hMgr
;
214 hMgr
= (PMANAGER_HANDLE
)Handle
;
215 if (hMgr
->Handle
.Tag
== MANAGER_TAG
)
217 RtlMapGenericMask(&dwDesiredAccess
,
220 hMgr
->Handle
.DesiredAccess
= dwDesiredAccess
;
222 return ERROR_SUCCESS
;
224 else if (hMgr
->Handle
.Tag
== SERVICE_TAG
)
226 RtlMapGenericMask(&dwDesiredAccess
,
229 hMgr
->Handle
.DesiredAccess
= dwDesiredAccess
;
231 return ERROR_SUCCESS
;
234 return ERROR_INVALID_HANDLE
;
239 ScmAssignNewTag(PSERVICE lpService
)
242 DPRINT("Assigning new tag to service %S\n", lpService
->lpServiceName
);
243 lpService
->dwTag
= 0;
244 return ERROR_SUCCESS
;
248 /* Internal recursive function */
249 /* Need to search for every dependency on every service */
251 Int_EnumDependentServicesW(HKEY hServicesKey
,
253 DWORD dwServiceState
,
254 PSERVICE
*lpServices
,
255 LPDWORD pcbBytesNeeded
,
256 LPDWORD lpServicesReturned
)
258 DWORD dwError
= ERROR_SUCCESS
;
259 WCHAR szNameBuf
[MAX_PATH
];
260 WCHAR szValueBuf
[MAX_PATH
];
261 WCHAR
*lpszNameBuf
= szNameBuf
;
262 WCHAR
*lpszValueBuf
= szValueBuf
;
266 PSERVICE lpCurrentService
;
267 HKEY hServiceEnumKey
;
268 DWORD dwCurrentServiceState
= SERVICE_ACTIVE
;
269 DWORD dwDependServiceStrPtr
= 0;
270 DWORD dwRequiredSize
= 0;
272 /* Get the number of service keys */
273 dwError
= RegQueryInfoKeyW(hServicesKey
,
285 if (dwError
!= ERROR_SUCCESS
)
287 DPRINT1("ERROR! Unable to get number of services keys.\n");
291 /* Iterate the service keys to see if another service depends on the this service */
292 for (dwIteration
= 0; dwIteration
< dwNumSubKeys
; dwIteration
++)
295 dwError
= RegEnumKeyExW(hServicesKey
,
303 if (dwError
!= ERROR_SUCCESS
)
306 /* Open the Service key */
307 dwError
= RegOpenKeyExW(hServicesKey
,
312 if (dwError
!= ERROR_SUCCESS
)
317 /* Check for the DependOnService Value */
318 dwError
= RegQueryValueExW(hServiceEnumKey
,
322 (LPBYTE
)lpszValueBuf
,
325 /* FIXME: Handle load order. */
327 /* If the service found has a DependOnService value */
328 if (dwError
== ERROR_SUCCESS
)
330 dwDependServiceStrPtr
= 0;
332 /* Can be more than one Dependencies in the DependOnService string */
333 while (wcslen(lpszValueBuf
+ dwDependServiceStrPtr
) > 0)
335 if (_wcsicmp(lpszValueBuf
+ dwDependServiceStrPtr
, lpService
->lpServiceName
) == 0)
337 /* Get the current enumed service pointer */
338 lpCurrentService
= ScmGetServiceEntryByName(lpszNameBuf
);
340 /* Check for valid Service */
341 if (!lpCurrentService
)
343 /* This should never happen! */
344 DPRINT1("This should not happen at this point, report to Developer\n");
345 return ERROR_NOT_FOUND
;
348 /* Determine state the service is in */
349 if (lpCurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
350 dwCurrentServiceState
= SERVICE_INACTIVE
;
352 /* If the ServiceState matches that requested or searching for SERVICE_STATE_ALL */
353 if ((dwCurrentServiceState
== dwServiceState
) ||
354 (dwServiceState
== SERVICE_STATE_ALL
))
356 /* Calculate the required size */
357 dwRequiredSize
+= sizeof(SERVICE_STATUS
);
358 dwRequiredSize
+= ((wcslen(lpCurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
));
359 dwRequiredSize
+= ((wcslen(lpCurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
361 /* Add the size for service name and display name pointers */
362 dwRequiredSize
+= (2 * sizeof(PVOID
));
364 /* increase the BytesNeeded size */
365 *pcbBytesNeeded
= *pcbBytesNeeded
+ dwRequiredSize
;
367 /* Don't fill callers buffer yet, as MSDN read that the last service with dependency
370 /* Recursive call to check for its dependencies */
371 Int_EnumDependentServicesW(hServicesKey
,
378 /* If the lpServices is valid set the service pointer */
380 lpServices
[*lpServicesReturned
] = lpCurrentService
;
382 *lpServicesReturned
= *lpServicesReturned
+ 1;
386 dwDependServiceStrPtr
+= (wcslen(lpszValueBuf
+ dwDependServiceStrPtr
) + 1);
389 else if (*pcbBytesNeeded
)
391 dwError
= ERROR_SUCCESS
;
394 RegCloseKey(hServiceEnumKey
);
402 DWORD
RCloseServiceHandle(
403 LPSC_RPC_HANDLE hSCObject
)
405 PMANAGER_HANDLE hManager
;
406 PSERVICE_HANDLE hService
;
410 DWORD pcbBytesNeeded
= 0;
411 DWORD dwServicesReturned
= 0;
413 DPRINT("RCloseServiceHandle() called\n");
415 DPRINT("hSCObject = %p\n", *hSCObject
);
418 return ERROR_INVALID_HANDLE
;
420 hManager
= (PMANAGER_HANDLE
)*hSCObject
;
421 hService
= (PSERVICE_HANDLE
)*hSCObject
;
422 if (hManager
->Handle
.Tag
== MANAGER_TAG
)
424 DPRINT("Found manager handle\n");
426 hManager
->Handle
.RefCount
--;
427 if (hManager
->Handle
.RefCount
== 0)
429 /* FIXME: add handle cleanup code */
431 HeapFree(GetProcessHeap(), 0, hManager
);
435 DPRINT("RCloseServiceHandle() done\n");
436 return ERROR_SUCCESS
;
438 else if (hService
->Handle
.Tag
== SERVICE_TAG
)
440 DPRINT("Found service handle\n");
442 /* Get the pointer to the service record */
443 lpService
= hService
->ServiceEntry
;
445 ASSERT(hService
->Handle
.RefCount
> 0);
447 hService
->Handle
.RefCount
--;
448 if (hService
->Handle
.RefCount
== 0)
450 /* FIXME: add handle cleanup code */
452 /* Free the handle */
453 HeapFree(GetProcessHeap(), 0, hService
);
457 ASSERT(lpService
->dwRefCount
> 0);
459 lpService
->dwRefCount
--;
460 DPRINT("CloseServiceHandle - lpService->dwRefCount %u\n",
461 lpService
->dwRefCount
);
463 if (lpService
->dwRefCount
== 0)
465 /* If this service has been marked for deletion */
466 if (lpService
->bDeleted
)
468 /* Open the Services Reg key */
469 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
470 L
"System\\CurrentControlSet\\Services",
472 KEY_SET_VALUE
| KEY_READ
,
474 if (dwError
!= ERROR_SUCCESS
)
476 DPRINT1("Failed to open services key\n");
480 /* Call the internal function with NULL, just to get bytes we need */
481 Int_EnumDependentServicesW(hServicesKey
,
486 &dwServicesReturned
);
488 /* if pcbBytesNeeded returned a value then there are services running that are dependent on this service*/
491 DPRINT1("Deletion failed due to running dependencies.\n");
492 RegCloseKey(hServicesKey
);
493 return ERROR_SUCCESS
;
496 /* There are no references and no runnning dependencies,
497 it is now safe to delete the service */
499 /* Delete the Service Key */
500 dwError
= RegDeleteKeyW(hServicesKey
,
501 lpService
->lpServiceName
);
503 RegCloseKey(hServicesKey
);
505 if (dwError
!= ERROR_SUCCESS
)
507 DPRINT1("Failed to Delete the Service Registry key\n");
511 /* Delete the Service */
512 ScmDeleteServiceRecord(lpService
);
516 DPRINT("RCloseServiceHandle() done\n");
517 return ERROR_SUCCESS
;
520 DPRINT1("Invalid handle tag (Tag %lx)\n", hManager
->Handle
.Tag
);
522 return ERROR_INVALID_HANDLE
;
527 DWORD
RControlService(
528 SC_RPC_HANDLE hService
,
530 LPSERVICE_STATUS lpServiceStatus
)
532 PSERVICE_HANDLE hSvc
;
534 ACCESS_MASK DesiredAccess
;
535 DWORD dwError
= ERROR_SUCCESS
;
536 DWORD pcbBytesNeeded
= 0;
537 DWORD dwServicesReturned
= 0;
538 HKEY hServicesKey
= NULL
;
540 DPRINT("RControlService() called\n");
543 return ERROR_SHUTDOWN_IN_PROGRESS
;
545 /* Check the service handle */
546 hSvc
= (PSERVICE_HANDLE
)hService
;
547 if (!hSvc
|| hSvc
->Handle
.Tag
!= SERVICE_TAG
)
549 DPRINT1("Invalid handle tag!\n");
550 return ERROR_INVALID_HANDLE
;
553 /* Check the service entry point */
554 lpService
= hSvc
->ServiceEntry
;
555 if (lpService
== NULL
)
557 DPRINT1("lpService == NULL!\n");
558 return ERROR_INVALID_HANDLE
;
561 /* Check access rights */
564 case SERVICE_CONTROL_STOP
:
565 DesiredAccess
= SERVICE_STOP
;
568 case SERVICE_CONTROL_PAUSE
:
569 case SERVICE_CONTROL_CONTINUE
:
570 DesiredAccess
= SERVICE_PAUSE_CONTINUE
;
573 case SERVICE_INTERROGATE
:
574 DesiredAccess
= SERVICE_INTERROGATE
;
578 if (dwControl
>= 128 && dwControl
<= 255)
579 DesiredAccess
= SERVICE_USER_DEFINED_CONTROL
;
581 DesiredAccess
= SERVICE_QUERY_CONFIG
|
582 SERVICE_CHANGE_CONFIG
|
583 SERVICE_QUERY_STATUS
|
585 SERVICE_PAUSE_CONTINUE
;
589 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
591 return ERROR_ACCESS_DENIED
;
593 if (dwControl
== SERVICE_CONTROL_STOP
)
595 /* Check if the service has dependencies running as windows
596 doesn't stop a service that does */
598 /* Open the Services Reg key */
599 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
600 L
"System\\CurrentControlSet\\Services",
604 if (dwError
!= ERROR_SUCCESS
)
606 DPRINT1("Failed to open services key\n");
610 /* Call the internal function with NULL, just to get bytes we need */
611 Int_EnumDependentServicesW(hServicesKey
,
616 &dwServicesReturned
);
618 RegCloseKey(hServicesKey
);
620 /* If pcbBytesNeeded is not zero then there are services running that
621 are dependent on this service */
622 if (pcbBytesNeeded
!= 0)
624 DPRINT("Service has running dependencies. Failed to stop service.\n");
625 return ERROR_DEPENDENT_SERVICES_RUNNING
;
629 if (lpService
->Status
.dwServiceType
& SERVICE_DRIVER
)
631 /* Send control code to the driver */
632 dwError
= ScmControlDriver(lpService
,
638 /* Send control code to the service */
639 dwError
= ScmControlService(lpService
,
644 if ((dwError
== ERROR_SUCCESS
) && (pcbBytesNeeded
))
645 dwError
= ERROR_DEPENDENT_SERVICES_RUNNING
;
647 /* Return service status information */
648 RtlCopyMemory(lpServiceStatus
,
650 sizeof(SERVICE_STATUS
));
657 DWORD
RDeleteService(
658 SC_RPC_HANDLE hService
)
660 PSERVICE_HANDLE hSvc
;
664 DPRINT("RDeleteService() called\n");
667 return ERROR_SHUTDOWN_IN_PROGRESS
;
669 hSvc
= (PSERVICE_HANDLE
)hService
;
670 if (!hSvc
|| hSvc
->Handle
.Tag
!= SERVICE_TAG
)
671 return ERROR_INVALID_HANDLE
;
673 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
675 return ERROR_ACCESS_DENIED
;
677 lpService
= hSvc
->ServiceEntry
;
678 if (lpService
== NULL
)
680 DPRINT1("lpService == NULL!\n");
681 return ERROR_INVALID_HANDLE
;
684 /* FIXME: Acquire service database lock exclusively */
686 if (lpService
->bDeleted
)
688 DPRINT1("The service has already been marked for delete!\n");
689 return ERROR_SERVICE_MARKED_FOR_DELETE
;
692 /* Mark service for delete */
693 lpService
->bDeleted
= TRUE
;
695 dwError
= ScmMarkServiceForDelete(lpService
);
697 /* FIXME: Release service database lock */
699 DPRINT("RDeleteService() done\n");
706 DWORD
RLockServiceDatabase(
707 SC_RPC_HANDLE hSCManager
,
708 LPSC_RPC_LOCK lpLock
)
710 PMANAGER_HANDLE hMgr
;
712 DPRINT("RLockServiceDatabase() called\n");
716 hMgr
= (PMANAGER_HANDLE
)hSCManager
;
717 if (!hMgr
|| hMgr
->Handle
.Tag
!= MANAGER_TAG
)
718 return ERROR_INVALID_HANDLE
;
720 if (!RtlAreAllAccessesGranted(hMgr
->Handle
.DesiredAccess
,
722 return ERROR_ACCESS_DENIED
;
724 // return ScmLockDatabase(0, hMgr->0xC, hLock);
726 /* FIXME: Lock the database */
727 *lpLock
= (SC_RPC_LOCK
)0x12345678; /* Dummy! */
729 return ERROR_SUCCESS
;
734 DWORD
RQueryServiceObjectSecurity(
735 SC_RPC_HANDLE hService
,
736 SECURITY_INFORMATION dwSecurityInformation
,
737 LPBYTE lpSecurityDescriptor
,
739 LPBOUNDED_DWORD_256K pcbBytesNeeded
)
741 PSERVICE_HANDLE hSvc
;
743 ULONG DesiredAccess
= 0;
749 SECURITY_DESCRIPTOR ObjectDescriptor
;
751 DPRINT("RQueryServiceObjectSecurity() called\n");
753 hSvc
= (PSERVICE_HANDLE
)hService
;
754 if (!hSvc
|| hSvc
->Handle
.Tag
!= SERVICE_TAG
)
756 DPRINT1("Invalid handle tag!\n");
757 return ERROR_INVALID_HANDLE
;
760 if (dwSecurityInformation
& (DACL_SECURITY_INFORMATION
||
761 GROUP_SECURITY_INFORMATION
||
762 OWNER_SECURITY_INFORMATION
))
763 DesiredAccess
|= READ_CONTROL
;
765 if (dwSecurityInformation
& SACL_SECURITY_INFORMATION
)
766 DesiredAccess
|= ACCESS_SYSTEM_SECURITY
;
768 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
771 DPRINT1("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
772 return ERROR_ACCESS_DENIED
;
775 lpService
= hSvc
->ServiceEntry
;
776 if (lpService
== NULL
)
778 DPRINT1("lpService == NULL!\n");
779 return ERROR_INVALID_HANDLE
;
782 /* FIXME: Lock the service list */
785 Status
= RtlCreateSecurityDescriptor(&ObjectDescriptor
, SECURITY_DESCRIPTOR_REVISION
);
787 Status
= RtlQuerySecurityObject(&ObjectDescriptor
/* lpService->lpSecurityDescriptor */,
788 dwSecurityInformation
,
789 (PSECURITY_DESCRIPTOR
)lpSecurityDescriptor
,
793 /* FIXME: Unlock the service list */
795 if (NT_SUCCESS(Status
))
797 *pcbBytesNeeded
= dwBytesNeeded
;
798 dwError
= STATUS_SUCCESS
;
800 else if (Status
== STATUS_BUFFER_TOO_SMALL
)
802 *pcbBytesNeeded
= dwBytesNeeded
;
803 dwError
= ERROR_INSUFFICIENT_BUFFER
;
805 else if (Status
== STATUS_BAD_DESCRIPTOR_FORMAT
)
807 dwError
= ERROR_GEN_FAILURE
;
811 dwError
= RtlNtStatusToDosError(Status
);
819 DWORD
RSetServiceObjectSecurity(
820 SC_RPC_HANDLE hService
,
821 DWORD dwSecurityInformation
,
822 LPBYTE lpSecurityDescriptor
,
823 DWORD dwSecuityDescriptorSize
)
825 PSERVICE_HANDLE hSvc
;
827 ULONG DesiredAccess
= 0;
828 /* HANDLE hToken = NULL; */
830 /* NTSTATUS Status; */
833 DPRINT1("RSetServiceObjectSecurity() called\n");
835 hSvc
= (PSERVICE_HANDLE
)hService
;
836 if (!hSvc
|| hSvc
->Handle
.Tag
!= SERVICE_TAG
)
838 DPRINT1("Invalid handle tag!\n");
839 return ERROR_INVALID_HANDLE
;
842 if (dwSecurityInformation
== 0 ||
843 dwSecurityInformation
& ~(OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
844 | DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
))
845 return ERROR_INVALID_PARAMETER
;
847 if (!RtlValidSecurityDescriptor((PSECURITY_DESCRIPTOR
)lpSecurityDescriptor
))
848 return ERROR_INVALID_PARAMETER
;
850 if (dwSecurityInformation
& SACL_SECURITY_INFORMATION
)
851 DesiredAccess
|= ACCESS_SYSTEM_SECURITY
;
853 if (dwSecurityInformation
& DACL_SECURITY_INFORMATION
)
854 DesiredAccess
|= WRITE_DAC
;
856 if (dwSecurityInformation
& (OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
))
857 DesiredAccess
|= WRITE_OWNER
;
859 if ((dwSecurityInformation
& OWNER_SECURITY_INFORMATION
) &&
860 (((PISECURITY_DESCRIPTOR
)lpSecurityDescriptor
)->Owner
== NULL
))
861 return ERROR_INVALID_PARAMETER
;
863 if ((dwSecurityInformation
& GROUP_SECURITY_INFORMATION
) &&
864 (((PISECURITY_DESCRIPTOR
)lpSecurityDescriptor
)->Group
== NULL
))
865 return ERROR_INVALID_PARAMETER
;
867 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
870 DPRINT1("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
871 return ERROR_ACCESS_DENIED
;
874 lpService
= hSvc
->ServiceEntry
;
875 if (lpService
== NULL
)
877 DPRINT1("lpService == NULL!\n");
878 return ERROR_INVALID_HANDLE
;
881 if (lpService
->bDeleted
)
882 return ERROR_SERVICE_MARKED_FOR_DELETE
;
885 RpcImpersonateClient(NULL
);
887 Status
= NtOpenThreadToken(NtCurrentThread(),
891 if (!NT_SUCCESS(Status
))
892 return RtlNtStatusToDosError(Status
);
896 /* FIXME: Lock service database */
898 Status
= RtlSetSecurityObject(dwSecurityInformation
,
899 (PSECURITY_DESCRIPTOR
)lpSecurityDescriptor
,
900 &lpService
->lpSecurityDescriptor
,
903 if (!NT_SUCCESS(Status
))
905 dwError
= RtlNtStatusToDosError(Status
);
910 dwError
= ScmOpenServiceKey(lpService
->lpServiceName
,
911 READ_CONTROL
| KEY_CREATE_SUB_KEY
| KEY_SET_VALUE
,
913 if (dwError
!= ERROR_SUCCESS
)
917 dwError
= ERROR_SUCCESS
;
918 // dwError = ScmWriteSecurityDescriptor(hServiceKey,
919 // lpService->lpSecurityDescriptor);
921 RegFlushKey(hServiceKey
);
922 RegCloseKey(hServiceKey
);
931 /* FIXME: Unlock service database */
933 DPRINT("RSetServiceObjectSecurity() done (Error %lu)\n", dwError
);
940 DWORD
RQueryServiceStatus(
941 SC_RPC_HANDLE hService
,
942 LPSERVICE_STATUS lpServiceStatus
)
944 PSERVICE_HANDLE hSvc
;
947 DPRINT("RQueryServiceStatus() called\n");
950 return ERROR_SHUTDOWN_IN_PROGRESS
;
952 hSvc
= (PSERVICE_HANDLE
)hService
;
953 if (!hSvc
|| hSvc
->Handle
.Tag
!= SERVICE_TAG
)
955 DPRINT1("Invalid handle tag!\n");
956 return ERROR_INVALID_HANDLE
;
959 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
960 SERVICE_QUERY_STATUS
))
962 DPRINT1("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
963 return ERROR_ACCESS_DENIED
;
966 lpService
= hSvc
->ServiceEntry
;
967 if (lpService
== NULL
)
969 DPRINT1("lpService == NULL!\n");
970 return ERROR_INVALID_HANDLE
;
973 /* Return service status information */
974 RtlCopyMemory(lpServiceStatus
,
976 sizeof(SERVICE_STATUS
));
978 return ERROR_SUCCESS
;
983 ScmIsValidServiceState(DWORD dwCurrentState
)
985 switch (dwCurrentState
)
987 case SERVICE_STOPPED
:
988 case SERVICE_START_PENDING
:
989 case SERVICE_STOP_PENDING
:
990 case SERVICE_RUNNING
:
991 case SERVICE_CONTINUE_PENDING
:
992 case SERVICE_PAUSE_PENDING
:
1003 DWORD
RSetServiceStatus(
1004 RPC_SERVICE_STATUS_HANDLE hServiceStatus
,
1005 LPSERVICE_STATUS lpServiceStatus
)
1009 DPRINT("RSetServiceStatus() called\n");
1010 DPRINT("hServiceStatus = %p\n", hServiceStatus
);
1011 DPRINT("dwServiceType = %lu\n", lpServiceStatus
->dwServiceType
);
1012 DPRINT("dwCurrentState = %lu\n", lpServiceStatus
->dwCurrentState
);
1013 DPRINT("dwControlsAccepted = %lu\n", lpServiceStatus
->dwControlsAccepted
);
1014 DPRINT("dwWin32ExitCode = %lu\n", lpServiceStatus
->dwWin32ExitCode
);
1015 DPRINT("dwServiceSpecificExitCode = %lu\n", lpServiceStatus
->dwServiceSpecificExitCode
);
1016 DPRINT("dwCheckPoint = %lu\n", lpServiceStatus
->dwCheckPoint
);
1017 DPRINT("dwWaitHint = %lu\n", lpServiceStatus
->dwWaitHint
);
1019 if (hServiceStatus
== 0)
1021 DPRINT1("hServiceStatus == NULL!\n");
1022 return ERROR_INVALID_HANDLE
;
1025 lpService
= ScmGetServiceEntryByClientHandle((HANDLE
)hServiceStatus
);
1026 if (lpService
== NULL
)
1028 DPRINT1("lpService == NULL!\n");
1029 return ERROR_INVALID_HANDLE
;
1032 /* Check current state */
1033 if (!ScmIsValidServiceState(lpServiceStatus
->dwCurrentState
))
1035 DPRINT1("Invalid service state!\n");
1036 return ERROR_INVALID_DATA
;
1039 /* Check service type */
1040 if (!(lpServiceStatus
->dwServiceType
& SERVICE_WIN32
) &&
1041 (lpServiceStatus
->dwServiceType
& SERVICE_DRIVER
))
1043 DPRINT1("Invalid service type!\n");
1044 return ERROR_INVALID_DATA
;
1047 /* Check accepted controls */
1048 if (lpServiceStatus
->dwControlsAccepted
& ~0xFF)
1050 DPRINT1("Invalid controls accepted!\n");
1051 return ERROR_INVALID_DATA
;
1055 RtlCopyMemory(&lpService
->Status
,
1057 sizeof(SERVICE_STATUS
));
1059 DPRINT("Set %S to %lu\n", lpService
->lpDisplayName
, lpService
->Status
.dwCurrentState
);
1060 DPRINT("RSetServiceStatus() done\n");
1062 return ERROR_SUCCESS
;
1067 DWORD
RUnlockServiceDatabase(
1071 return ERROR_SUCCESS
;
1076 DWORD
RNotifyBootConfigStatus(
1077 SVCCTL_HANDLEW lpMachineName
,
1078 DWORD BootAcceptable
)
1081 return ERROR_CALL_NOT_IMPLEMENTED
;
1086 DWORD
RI_ScSetServiceBitsW(
1087 RPC_SERVICE_STATUS_HANDLE hServiceStatus
,
1088 DWORD dwServiceBits
,
1090 int bUpdateImmediately
,
1094 return ERROR_CALL_NOT_IMPLEMENTED
;
1099 DWORD
RChangeServiceConfigW(
1100 SC_RPC_HANDLE hService
,
1101 DWORD dwServiceType
,
1103 DWORD dwErrorControl
,
1104 LPWSTR lpBinaryPathName
,
1105 LPWSTR lpLoadOrderGroup
,
1107 LPBYTE lpDependencies
,
1109 LPWSTR lpServiceStartName
,
1112 LPWSTR lpDisplayName
)
1114 DWORD dwError
= ERROR_SUCCESS
;
1115 PSERVICE_HANDLE hSvc
;
1116 PSERVICE lpService
= NULL
;
1117 HKEY hServiceKey
= NULL
;
1118 LPWSTR lpDisplayNameW
= NULL
;
1120 DPRINT("RChangeServiceConfigW() called\n");
1121 DPRINT("dwServiceType = %lu\n", dwServiceType
);
1122 DPRINT("dwStartType = %lu\n", dwStartType
);
1123 DPRINT("dwErrorControl = %lu\n", dwErrorControl
);
1124 DPRINT("lpBinaryPathName = %S\n", lpBinaryPathName
);
1125 DPRINT("lpLoadOrderGroup = %S\n", lpLoadOrderGroup
);
1126 DPRINT("lpDisplayName = %S\n", lpDisplayName
);
1129 return ERROR_SHUTDOWN_IN_PROGRESS
;
1131 hSvc
= (PSERVICE_HANDLE
)hService
;
1132 if (!hSvc
|| hSvc
->Handle
.Tag
!= SERVICE_TAG
)
1134 DPRINT1("Invalid handle tag!\n");
1135 return ERROR_INVALID_HANDLE
;
1138 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
1139 SERVICE_CHANGE_CONFIG
))
1141 DPRINT1("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
1142 return ERROR_ACCESS_DENIED
;
1145 lpService
= hSvc
->ServiceEntry
;
1146 if (lpService
== NULL
)
1148 DPRINT1("lpService == NULL!\n");
1149 return ERROR_INVALID_HANDLE
;
1152 /* FIXME: Lock database exclusively */
1154 if (lpService
->bDeleted
)
1156 /* FIXME: Unlock database */
1157 DPRINT1("The service has already been marked for delete!\n");
1158 return ERROR_SERVICE_MARKED_FOR_DELETE
;
1161 /* Open the service key */
1162 dwError
= ScmOpenServiceKey(lpService
->szServiceName
,
1165 if (dwError
!= ERROR_SUCCESS
)
1168 /* Write service data to the registry */
1169 /* Set the display name */
1170 if (lpDisplayName
!= NULL
&& *lpDisplayName
!= 0)
1172 RegSetValueExW(hServiceKey
,
1176 (LPBYTE
)lpDisplayName
,
1177 (wcslen(lpDisplayName
) + 1) * sizeof(WCHAR
));
1179 /* Update the display name */
1180 lpDisplayNameW
= (LPWSTR
)HeapAlloc(GetProcessHeap(),
1182 (wcslen(lpDisplayName
) + 1) * sizeof(WCHAR
));
1183 if (lpDisplayNameW
== NULL
)
1185 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
1189 if (lpService
->lpDisplayName
!= lpService
->lpServiceName
)
1190 HeapFree(GetProcessHeap(), 0, lpService
->lpDisplayName
);
1192 lpService
->lpDisplayName
= lpDisplayNameW
;
1195 if (dwServiceType
!= SERVICE_NO_CHANGE
)
1197 /* Set the service type */
1198 dwError
= RegSetValueExW(hServiceKey
,
1202 (LPBYTE
)&dwServiceType
,
1204 if (dwError
!= ERROR_SUCCESS
)
1207 lpService
->Status
.dwServiceType
= dwServiceType
;
1210 if (dwStartType
!= SERVICE_NO_CHANGE
)
1212 /* Set the start value */
1213 dwError
= RegSetValueExW(hServiceKey
,
1217 (LPBYTE
)&dwStartType
,
1219 if (dwError
!= ERROR_SUCCESS
)
1222 lpService
->dwStartType
= dwStartType
;
1225 if (dwErrorControl
!= SERVICE_NO_CHANGE
)
1227 /* Set the error control value */
1228 dwError
= RegSetValueExW(hServiceKey
,
1232 (LPBYTE
)&dwErrorControl
,
1234 if (dwError
!= ERROR_SUCCESS
)
1237 lpService
->dwErrorControl
= dwErrorControl
;
1241 /* FIXME: set the new ImagePath value */
1243 /* Set the image path */
1244 if (dwServiceType
& SERVICE_WIN32
)
1246 if (lpBinaryPathName
!= NULL
&& *lpBinaryPathName
!= 0)
1248 dwError
= RegSetValueExW(hServiceKey
,
1252 (LPBYTE
)lpBinaryPathName
,
1253 (wcslen(lpBinaryPathName
) + 1) * sizeof(WCHAR
));
1254 if (dwError
!= ERROR_SUCCESS
)
1258 else if (dwServiceType
& SERVICE_DRIVER
)
1260 if (lpImagePath
!= NULL
&& *lpImagePath
!= 0)
1262 dwError
= RegSetValueExW(hServiceKey
,
1266 (LPBYTE
)lpImagePath
,
1267 (wcslen(lpImagePath
) + 1) *sizeof(WCHAR
));
1268 if (dwError
!= ERROR_SUCCESS
)
1274 /* Set the group name */
1275 if (lpLoadOrderGroup
!= NULL
&& *lpLoadOrderGroup
!= 0)
1277 dwError
= RegSetValueExW(hServiceKey
,
1281 (LPBYTE
)lpLoadOrderGroup
,
1282 (wcslen(lpLoadOrderGroup
) + 1) * sizeof(WCHAR
));
1283 if (dwError
!= ERROR_SUCCESS
)
1286 dwError
= ScmSetServiceGroup(lpService
,
1288 if (dwError
!= ERROR_SUCCESS
)
1292 if (lpdwTagId
!= NULL
)
1294 dwError
= ScmAssignNewTag(lpService
);
1295 if (dwError
!= ERROR_SUCCESS
)
1298 dwError
= RegSetValueExW(hServiceKey
,
1302 (LPBYTE
)&lpService
->dwTag
,
1304 if (dwError
!= ERROR_SUCCESS
)
1307 *lpdwTagId
= lpService
->dwTag
;
1310 /* Write dependencies */
1311 if (lpDependencies
!= NULL
&& *lpDependencies
!= 0)
1313 dwError
= ScmWriteDependencies(hServiceKey
,
1314 (LPWSTR
)lpDependencies
,
1316 if (dwError
!= ERROR_SUCCESS
)
1320 if (lpPassword
!= NULL
)
1322 /* FIXME: Write password */
1325 /* FIXME: Unlock database */
1328 if (hServiceKey
!= NULL
)
1329 RegCloseKey(hServiceKey
);
1331 DPRINT("RChangeServiceConfigW() done (Error %lu)\n", dwError
);
1337 /* Create a path suitable for the bootloader out of the full path */
1339 ScmConvertToBootPathName(wchar_t *CanonName
, wchar_t **RelativeName
)
1341 DWORD ServiceNameLen
, BufferSize
, ExpandedLen
;
1344 UNICODE_STRING NtPathName
, SystemRoot
, LinkTarget
;
1345 OBJECT_ATTRIBUTES ObjectAttributes
;
1347 HANDLE SymbolicLinkHandle
;
1349 DPRINT("ScmConvertToBootPathName %S\n", CanonName
);
1351 ServiceNameLen
= wcslen(CanonName
);
1353 /* First check, if it's already good */
1354 if (ServiceNameLen
> 12 &&
1355 !_wcsnicmp(L
"\\SystemRoot\\", CanonName
, 12))
1357 *RelativeName
= LocalAlloc(LMEM_ZEROINIT
, ServiceNameLen
* sizeof(WCHAR
) + sizeof(WCHAR
));
1358 if (*RelativeName
== NULL
)
1360 DPRINT1("Error allocating memory for boot driver name!\n");
1361 return ERROR_NOT_ENOUGH_MEMORY
;
1365 wcscpy(*RelativeName
, CanonName
);
1367 DPRINT1("Bootdriver name %S\n", *RelativeName
);
1368 return ERROR_SUCCESS
;
1371 /* If it has %SystemRoot% prefix, substitute it to \System*/
1372 if (ServiceNameLen
> 13 &&
1373 !_wcsnicmp(L
"%SystemRoot%\\", CanonName
, 13))
1375 /* There is no +sizeof(wchar_t) because the name is less by 1 wchar */
1376 *RelativeName
= LocalAlloc(LMEM_ZEROINIT
, ServiceNameLen
* sizeof(WCHAR
));
1378 if (*RelativeName
== NULL
)
1380 DPRINT1("Error allocating memory for boot driver name!\n");
1381 return ERROR_NOT_ENOUGH_MEMORY
;
1385 wcscpy(*RelativeName
, L
"\\SystemRoot\\");
1386 wcscat(*RelativeName
, CanonName
+ 13);
1388 DPRINT1("Bootdriver name %S\n", *RelativeName
);
1389 return ERROR_SUCCESS
;
1392 /* Get buffer size needed for expanding env strings */
1393 BufferSize
= ExpandEnvironmentStringsW(L
"%SystemRoot%\\", &Dest
, 1);
1395 if (BufferSize
<= 1)
1397 DPRINT1("Error during a call to ExpandEnvironmentStringsW()\n");
1398 return ERROR_INVALID_ENVIRONMENT
;
1401 /* Allocate memory, since the size is known now */
1402 Expanded
= LocalAlloc(LMEM_ZEROINIT
, BufferSize
* sizeof(WCHAR
) + sizeof(WCHAR
));
1405 DPRINT1("Error allocating memory for boot driver name!\n");
1406 return ERROR_NOT_ENOUGH_MEMORY
;
1410 if (ExpandEnvironmentStringsW(L
"%SystemRoot%\\", Expanded
, BufferSize
) >
1413 DPRINT1("Error during a call to ExpandEnvironmentStringsW()\n");
1414 LocalFree(Expanded
);
1415 return ERROR_NOT_ENOUGH_MEMORY
;
1418 /* Convert to NY-style path */
1419 if (!RtlDosPathNameToNtPathName_U(Expanded
, &NtPathName
, NULL
, NULL
))
1421 DPRINT1("Error during a call to RtlDosPathNameToNtPathName_U()\n");
1422 return ERROR_INVALID_ENVIRONMENT
;
1425 DPRINT("Converted to NT-style %wZ\n", &NtPathName
);
1427 /* No need to keep the dos-path anymore */
1428 LocalFree(Expanded
);
1430 /* Copy it to the allocated place */
1431 Expanded
= LocalAlloc(LMEM_ZEROINIT
, NtPathName
.Length
+ sizeof(WCHAR
));
1434 DPRINT1("Error allocating memory for boot driver name!\n");
1435 return ERROR_NOT_ENOUGH_MEMORY
;
1438 ExpandedLen
= NtPathName
.Length
/ sizeof(WCHAR
);
1439 wcsncpy(Expanded
, NtPathName
.Buffer
, ExpandedLen
);
1440 Expanded
[ExpandedLen
] = 0;
1442 if (ServiceNameLen
> ExpandedLen
&&
1443 !_wcsnicmp(Expanded
, CanonName
, ExpandedLen
))
1445 /* Only \SystemRoot\ is missing */
1446 *RelativeName
= LocalAlloc(LMEM_ZEROINIT
,
1447 (ServiceNameLen
- ExpandedLen
) * sizeof(WCHAR
) + 13*sizeof(WCHAR
));
1448 if (*RelativeName
== NULL
)
1450 DPRINT1("Error allocating memory for boot driver name!\n");
1451 LocalFree(Expanded
);
1452 return ERROR_NOT_ENOUGH_MEMORY
;
1455 wcscpy(*RelativeName
, L
"\\SystemRoot\\");
1456 wcscat(*RelativeName
, CanonName
+ ExpandedLen
);
1458 RtlFreeUnicodeString(&NtPathName
);
1459 return ERROR_SUCCESS
;
1462 /* The most complex case starts here */
1463 RtlInitUnicodeString(&SystemRoot
, L
"\\SystemRoot");
1464 InitializeObjectAttributes(&ObjectAttributes
,
1466 OBJ_CASE_INSENSITIVE
,
1470 /* Open this symlink */
1471 Status
= NtOpenSymbolicLinkObject(&SymbolicLinkHandle
, SYMBOLIC_LINK_QUERY
, &ObjectAttributes
);
1473 if (NT_SUCCESS(Status
))
1475 LinkTarget
.Length
= 0;
1476 LinkTarget
.MaximumLength
= 0;
1478 DPRINT("Opened symbolic link object\n");
1480 Status
= NtQuerySymbolicLinkObject(SymbolicLinkHandle
, &LinkTarget
, &BufferSize
);
1481 if (NT_SUCCESS(Status
) || Status
== STATUS_BUFFER_TOO_SMALL
)
1483 /* Check if required buffer size is sane */
1484 if (BufferSize
> 0xFFFD)
1486 DPRINT1("Too large buffer required\n");
1489 if (SymbolicLinkHandle
) NtClose(SymbolicLinkHandle
);
1490 LocalFree(Expanded
);
1491 return ERROR_NOT_ENOUGH_MEMORY
;
1494 /* Alloc the string */
1495 LinkTarget
.Buffer
= LocalAlloc(LMEM_ZEROINIT
, BufferSize
+ sizeof(WCHAR
));
1496 if (!LinkTarget
.Buffer
)
1498 DPRINT1("Unable to alloc buffer\n");
1499 if (SymbolicLinkHandle
) NtClose(SymbolicLinkHandle
);
1500 LocalFree(Expanded
);
1501 return ERROR_NOT_ENOUGH_MEMORY
;
1504 /* Do a real query now */
1505 LinkTarget
.Length
= BufferSize
;
1506 LinkTarget
.MaximumLength
= LinkTarget
.Length
+ sizeof(WCHAR
);
1508 Status
= NtQuerySymbolicLinkObject(SymbolicLinkHandle
, &LinkTarget
, &BufferSize
);
1509 if (NT_SUCCESS(Status
))
1511 DPRINT("LinkTarget: %wZ\n", &LinkTarget
);
1513 ExpandedLen
= LinkTarget
.Length
/ sizeof(WCHAR
);
1514 if ((ServiceNameLen
> ExpandedLen
) &&
1515 !_wcsnicmp(LinkTarget
.Buffer
, CanonName
, ExpandedLen
))
1517 *RelativeName
= LocalAlloc(LMEM_ZEROINIT
,
1518 (ServiceNameLen
- ExpandedLen
) * sizeof(WCHAR
) + 13*sizeof(WCHAR
));
1520 if (*RelativeName
== NULL
)
1522 DPRINT1("Unable to alloc buffer\n");
1523 if (SymbolicLinkHandle
) NtClose(SymbolicLinkHandle
);
1524 LocalFree(Expanded
);
1525 RtlFreeUnicodeString(&NtPathName
);
1526 return ERROR_NOT_ENOUGH_MEMORY
;
1529 /* Copy it over, substituting the first part
1531 wcscpy(*RelativeName
, L
"\\SystemRoot\\");
1532 wcscat(*RelativeName
, CanonName
+ExpandedLen
+1);
1535 if (SymbolicLinkHandle
) NtClose(SymbolicLinkHandle
);
1536 LocalFree(Expanded
);
1537 RtlFreeUnicodeString(&NtPathName
);
1539 /* Return success */
1540 return ERROR_SUCCESS
;
1544 if (SymbolicLinkHandle
) NtClose(SymbolicLinkHandle
);
1545 LocalFree(Expanded
);
1546 RtlFreeUnicodeString(&NtPathName
);
1547 return ERROR_INVALID_PARAMETER
;
1552 DPRINT1("Error, Status = %08X\n", Status
);
1553 if (SymbolicLinkHandle
) NtClose(SymbolicLinkHandle
);
1554 LocalFree(Expanded
);
1555 RtlFreeUnicodeString(&NtPathName
);
1556 return ERROR_INVALID_PARAMETER
;
1561 DPRINT1("Error, Status = %08X\n", Status
);
1562 if (SymbolicLinkHandle
) NtClose(SymbolicLinkHandle
);
1563 LocalFree(Expanded
);
1564 RtlFreeUnicodeString(&NtPathName
);
1565 return ERROR_INVALID_PARAMETER
;
1570 DPRINT1("Error, Status = %08X\n", Status
);
1571 LocalFree(Expanded
);
1572 return ERROR_INVALID_PARAMETER
;
1576 *RelativeName
= NULL
;
1577 return ERROR_INVALID_PARAMETER
;
1581 ScmCanonDriverImagePath(DWORD dwStartType
,
1582 const wchar_t *lpServiceName
,
1583 wchar_t **lpCanonName
)
1585 DWORD ServiceNameLen
, Result
;
1586 UNICODE_STRING NtServiceName
;
1587 WCHAR
*RelativeName
;
1588 const WCHAR
*SourceName
= lpServiceName
;
1590 /* Calculate the length of the service's name */
1591 ServiceNameLen
= wcslen(lpServiceName
);
1593 /* 12 is wcslen(L"\\SystemRoot\\") */
1594 if (ServiceNameLen
> 12 &&
1595 !_wcsnicmp(L
"\\SystemRoot\\", lpServiceName
, 12))
1597 /* SystemRoot prefix is already included */
1599 *lpCanonName
= LocalAlloc(LMEM_ZEROINIT
, ServiceNameLen
* sizeof(WCHAR
) + sizeof(WCHAR
));
1601 if (*lpCanonName
== NULL
)
1603 DPRINT1("Error allocating memory for canonized service name!\n");
1604 return ERROR_NOT_ENOUGH_MEMORY
;
1607 /* If it's a boot-time driver, it must be systemroot relative */
1608 if (dwStartType
== SERVICE_BOOT_START
)
1612 wcscpy(*lpCanonName
, SourceName
);
1614 DPRINT("Canonicalized name %S\n", *lpCanonName
);
1618 /* Check if it has %SystemRoot% (len=13) */
1619 if (ServiceNameLen
> 13 &&
1620 !_wcsnicmp(L
"%%SystemRoot%%\\", lpServiceName
, 13))
1622 /* Substitute %SystemRoot% with \\SystemRoot\\ */
1623 *lpCanonName
= LocalAlloc(LMEM_ZEROINIT
, ServiceNameLen
* sizeof(WCHAR
) + sizeof(WCHAR
));
1625 if (*lpCanonName
== NULL
)
1627 DPRINT1("Error allocating memory for canonized service name!\n");
1628 return ERROR_NOT_ENOUGH_MEMORY
;
1631 /* If it's a boot-time driver, it must be systemroot relative */
1632 if (dwStartType
== SERVICE_BOOT_START
)
1633 wcscpy(*lpCanonName
, L
"\\SystemRoot\\");
1635 wcscat(*lpCanonName
, lpServiceName
+ 13);
1637 DPRINT("Canonicalized name %S\n", *lpCanonName
);
1641 /* Check if it's a relative path name */
1642 if (lpServiceName
[0] != L
'\\' && lpServiceName
[1] != L
':')
1644 *lpCanonName
= LocalAlloc(LMEM_ZEROINIT
, ServiceNameLen
* sizeof(WCHAR
) + sizeof(WCHAR
));
1646 if (*lpCanonName
== NULL
)
1648 DPRINT1("Error allocating memory for canonized service name!\n");
1649 return ERROR_NOT_ENOUGH_MEMORY
;
1652 /* Just copy it over without changing */
1653 wcscpy(*lpCanonName
, lpServiceName
);
1658 /* It seems to be a DOS path, convert it */
1659 if (!RtlDosPathNameToNtPathName_U(lpServiceName
, &NtServiceName
, NULL
, NULL
))
1661 DPRINT1("RtlDosPathNameToNtPathName_U() failed!\n");
1662 return ERROR_INVALID_PARAMETER
;
1665 *lpCanonName
= LocalAlloc(LMEM_ZEROINIT
, NtServiceName
.Length
+ sizeof(WCHAR
));
1667 if (*lpCanonName
== NULL
)
1669 DPRINT1("Error allocating memory for canonized service name!\n");
1670 RtlFreeUnicodeString(&NtServiceName
);
1671 return ERROR_NOT_ENOUGH_MEMORY
;
1674 /* Copy the string */
1675 wcsncpy(*lpCanonName
, NtServiceName
.Buffer
, NtServiceName
.Length
/ sizeof(WCHAR
));
1677 /* The unicode string is not needed anymore */
1678 RtlFreeUnicodeString(&NtServiceName
);
1680 if (dwStartType
!= SERVICE_BOOT_START
)
1682 DPRINT("Canonicalized name %S\n", *lpCanonName
);
1686 /* The service is boot-started, so must be relative */
1687 Result
= ScmConvertToBootPathName(*lpCanonName
, &RelativeName
);
1690 /* There is a problem, free name and return */
1691 LocalFree(*lpCanonName
);
1692 DPRINT1("Error converting named!\n");
1696 ASSERT(RelativeName
);
1698 /* Copy that string */
1699 wcscpy(*lpCanonName
, RelativeName
+ 12);
1701 /* Free the allocated buffer */
1702 LocalFree(RelativeName
);
1704 DPRINT("Canonicalized name %S\n", *lpCanonName
);
1712 DWORD
RCreateServiceW(
1713 SC_RPC_HANDLE hSCManager
,
1714 LPCWSTR lpServiceName
,
1715 LPCWSTR lpDisplayName
,
1716 DWORD dwDesiredAccess
,
1717 DWORD dwServiceType
,
1719 DWORD dwErrorControl
,
1720 LPCWSTR lpBinaryPathName
,
1721 LPCWSTR lpLoadOrderGroup
,
1723 LPBYTE lpDependencies
,
1725 LPCWSTR lpServiceStartName
,
1728 LPSC_RPC_HANDLE lpServiceHandle
)
1730 PMANAGER_HANDLE hManager
;
1731 DWORD dwError
= ERROR_SUCCESS
;
1732 PSERVICE lpService
= NULL
;
1733 SC_HANDLE hServiceHandle
= NULL
;
1734 LPWSTR lpImagePath
= NULL
;
1735 HKEY hServiceKey
= NULL
;
1737 DPRINT("RCreateServiceW() called\n");
1738 DPRINT("lpServiceName = %S\n", lpServiceName
);
1739 DPRINT("lpDisplayName = %S\n", lpDisplayName
);
1740 DPRINT("dwDesiredAccess = %lx\n", dwDesiredAccess
);
1741 DPRINT("dwServiceType = %lu\n", dwServiceType
);
1742 DPRINT("dwStartType = %lu\n", dwStartType
);
1743 DPRINT("dwErrorControl = %lu\n", dwErrorControl
);
1744 DPRINT("lpBinaryPathName = %S\n", lpBinaryPathName
);
1745 DPRINT("lpLoadOrderGroup = %S\n", lpLoadOrderGroup
);
1748 return ERROR_SHUTDOWN_IN_PROGRESS
;
1750 hManager
= (PMANAGER_HANDLE
)hSCManager
;
1751 if (!hManager
|| hManager
->Handle
.Tag
!= MANAGER_TAG
)
1753 DPRINT1("Invalid manager handle!\n");
1754 return ERROR_INVALID_HANDLE
;
1757 /* Check access rights */
1758 if (!RtlAreAllAccessesGranted(hManager
->Handle
.DesiredAccess
,
1759 SC_MANAGER_CREATE_SERVICE
))
1761 DPRINT1("Insufficient access rights! 0x%lx\n",
1762 hManager
->Handle
.DesiredAccess
);
1763 return ERROR_ACCESS_DENIED
;
1766 if (wcslen(lpServiceName
) == 0)
1768 return ERROR_INVALID_NAME
;
1771 if (wcslen(lpBinaryPathName
) == 0)
1773 return ERROR_INVALID_PARAMETER
;
1776 if ((dwServiceType
== (SERVICE_WIN32_OWN_PROCESS
| SERVICE_INTERACTIVE_PROCESS
)) &&
1777 (lpServiceStartName
))
1779 return ERROR_INVALID_PARAMETER
;
1782 if ((dwServiceType
> SERVICE_WIN32_SHARE_PROCESS
) &&
1783 (dwServiceType
!= (SERVICE_WIN32_OWN_PROCESS
| SERVICE_INTERACTIVE_PROCESS
)) &&
1784 (dwServiceType
!= (SERVICE_WIN32_SHARE_PROCESS
| SERVICE_INTERACTIVE_PROCESS
)))
1786 return ERROR_INVALID_PARAMETER
;
1789 if (dwStartType
> SERVICE_DISABLED
)
1791 return ERROR_INVALID_PARAMETER
;
1794 lpService
= ScmGetServiceEntryByName(lpServiceName
);
1797 /* check if it is marked for deletion */
1798 if (lpService
->bDeleted
)
1799 return ERROR_SERVICE_MARKED_FOR_DELETE
;
1800 /* Return Error exist */
1801 return ERROR_SERVICE_EXISTS
;
1804 if (lpDisplayName
!= NULL
&&
1805 ScmGetServiceEntryByDisplayName(lpDisplayName
) != NULL
)
1806 return ERROR_DUPLICATE_SERVICE_NAME
;
1808 if (dwServiceType
& SERVICE_DRIVER
)
1810 dwError
= ScmCanonDriverImagePath(dwStartType
,
1813 if (dwError
!= ERROR_SUCCESS
)
1818 if (dwStartType
== SERVICE_BOOT_START
||
1819 dwStartType
== SERVICE_SYSTEM_START
)
1821 return ERROR_INVALID_PARAMETER
;
1825 /* Allocate a new service entry */
1826 dwError
= ScmCreateNewServiceRecord(lpServiceName
,
1828 if (dwError
!= ERROR_SUCCESS
)
1831 /* Fill the new service entry */
1832 lpService
->Status
.dwServiceType
= dwServiceType
;
1833 lpService
->dwStartType
= dwStartType
;
1834 lpService
->dwErrorControl
= dwErrorControl
;
1836 /* Fill the display name */
1837 if (lpDisplayName
!= NULL
&&
1838 *lpDisplayName
!= 0 &&
1839 _wcsicmp(lpService
->lpDisplayName
, lpDisplayName
) != 0)
1841 lpService
->lpDisplayName
= (WCHAR
*) HeapAlloc(GetProcessHeap(), 0,
1842 (wcslen(lpDisplayName
) + 1) * sizeof(WCHAR
));
1843 if (lpService
->lpDisplayName
== NULL
)
1845 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
1848 wcscpy(lpService
->lpDisplayName
, lpDisplayName
);
1851 /* Assign the service to a group */
1852 if (lpLoadOrderGroup
!= NULL
&& *lpLoadOrderGroup
!= 0)
1854 dwError
= ScmSetServiceGroup(lpService
,
1856 if (dwError
!= ERROR_SUCCESS
)
1860 /* Assign a new tag */
1861 if (lpdwTagId
!= NULL
)
1863 dwError
= ScmAssignNewTag(lpService
);
1864 if (dwError
!= ERROR_SUCCESS
)
1868 /* Write service data to the registry */
1869 /* Create the service key */
1870 dwError
= ScmCreateServiceKey(lpServiceName
,
1873 if (dwError
!= ERROR_SUCCESS
)
1876 /* Set the display name */
1877 if (lpDisplayName
!= NULL
&& *lpDisplayName
!= 0)
1879 RegSetValueExW(hServiceKey
,
1883 (LPBYTE
)lpDisplayName
,
1884 (wcslen(lpDisplayName
) + 1) * sizeof(WCHAR
));
1887 /* Set the service type */
1888 dwError
= RegSetValueExW(hServiceKey
,
1892 (LPBYTE
)&dwServiceType
,
1894 if (dwError
!= ERROR_SUCCESS
)
1897 /* Set the start value */
1898 dwError
= RegSetValueExW(hServiceKey
,
1902 (LPBYTE
)&dwStartType
,
1904 if (dwError
!= ERROR_SUCCESS
)
1907 /* Set the error control value */
1908 dwError
= RegSetValueExW(hServiceKey
,
1912 (LPBYTE
)&dwErrorControl
,
1914 if (dwError
!= ERROR_SUCCESS
)
1917 /* Set the image path */
1918 if (dwServiceType
& SERVICE_WIN32
)
1920 dwError
= RegSetValueExW(hServiceKey
,
1924 (LPBYTE
)lpBinaryPathName
,
1925 (wcslen(lpBinaryPathName
) + 1) * sizeof(WCHAR
));
1926 if (dwError
!= ERROR_SUCCESS
)
1929 else if (dwServiceType
& SERVICE_DRIVER
)
1931 dwError
= RegSetValueExW(hServiceKey
,
1935 (LPBYTE
)lpImagePath
,
1936 (wcslen(lpImagePath
) + 1) * sizeof(WCHAR
));
1937 if (dwError
!= ERROR_SUCCESS
)
1941 /* Set the group name */
1942 if (lpLoadOrderGroup
!= NULL
&& *lpLoadOrderGroup
!= 0)
1944 dwError
= RegSetValueExW(hServiceKey
,
1948 (LPBYTE
)lpLoadOrderGroup
,
1949 (wcslen(lpLoadOrderGroup
) + 1) * sizeof(WCHAR
));
1950 if (dwError
!= ERROR_SUCCESS
)
1954 if (lpdwTagId
!= NULL
)
1956 dwError
= RegSetValueExW(hServiceKey
,
1960 (LPBYTE
)&lpService
->dwTag
,
1962 if (dwError
!= ERROR_SUCCESS
)
1966 /* Write dependencies */
1967 if (lpDependencies
!= NULL
&& *lpDependencies
!= 0)
1969 dwError
= ScmWriteDependencies(hServiceKey
,
1970 (LPWSTR
)lpDependencies
,
1972 if (dwError
!= ERROR_SUCCESS
)
1976 /* FIXME: Handle lpServiceStartName propertly! */
1977 /* If a non driver and NULL for lpServiceStartName, write ObjectName as LocalSystem */
1978 if ((dwServiceType
& SERVICE_WIN32
) && (!lpServiceStartName
))
1980 dwError
= RegSetValueExW(hServiceKey
,
1984 (LPBYTE
)L
"LocalSystem",
1986 if (dwError
!= ERROR_SUCCESS
)
1990 if (lpPassword
!= NULL
)
1992 /* FIXME: Write password */
1995 dwError
= ScmCreateServiceHandle(lpService
,
1997 if (dwError
!= ERROR_SUCCESS
)
2000 dwError
= ScmCheckAccess(hServiceHandle
,
2002 if (dwError
!= ERROR_SUCCESS
)
2005 lpService
->dwRefCount
= 1;
2006 DPRINT("CreateService - lpService->dwRefCount %u\n", lpService
->dwRefCount
);
2009 if (hServiceKey
!= NULL
)
2010 RegCloseKey(hServiceKey
);
2012 if (dwError
== ERROR_SUCCESS
)
2014 DPRINT("hService %p\n", hServiceHandle
);
2015 *lpServiceHandle
= (SC_RPC_HANDLE
)hServiceHandle
;
2017 if (lpdwTagId
!= NULL
)
2018 *lpdwTagId
= lpService
->dwTag
;
2022 /* Release the display name buffer */
2023 if (lpService
->lpServiceName
!= NULL
)
2024 HeapFree(GetProcessHeap(), 0, lpService
->lpDisplayName
);
2028 /* Remove the service handle */
2029 HeapFree(GetProcessHeap(), 0, hServiceHandle
);
2032 if (lpService
!= NULL
)
2034 /* FIXME: remove the service entry */
2038 if (lpImagePath
!= NULL
)
2039 HeapFree(GetProcessHeap(), 0, lpImagePath
);
2041 DPRINT("RCreateServiceW() done (Error %lu)\n", dwError
);
2048 DWORD
REnumDependentServicesW(
2049 SC_RPC_HANDLE hService
,
2050 DWORD dwServiceState
,
2053 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
2054 LPBOUNDED_DWORD_256K lpServicesReturned
)
2056 DWORD dwError
= ERROR_SUCCESS
;
2057 DWORD dwServicesReturned
= 0;
2058 DWORD dwServiceCount
;
2059 HKEY hServicesKey
= NULL
;
2060 LPSC_RPC_HANDLE hSCObject
;
2061 PSERVICE_HANDLE hSvc
;
2062 PSERVICE lpService
= NULL
;
2063 PSERVICE
*lpServicesArray
= NULL
;
2064 LPENUM_SERVICE_STATUSW lpServicesPtr
= NULL
;
2067 *pcbBytesNeeded
= 0;
2068 *lpServicesReturned
= 0;
2070 DPRINT("REnumDependentServicesW() called\n");
2072 hSCObject
= &hService
;
2073 hSvc
= (PSERVICE_HANDLE
) *hSCObject
;
2074 lpService
= hSvc
->ServiceEntry
;
2076 /* Check access rights */
2077 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
2078 SC_MANAGER_ENUMERATE_SERVICE
))
2080 DPRINT1("Insufficient access rights! 0x%lx\n",
2081 hSvc
->Handle
.DesiredAccess
);
2082 return ERROR_ACCESS_DENIED
;
2085 /* Open the Services Reg key */
2086 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
2087 L
"System\\CurrentControlSet\\Services",
2091 if (dwError
!= ERROR_SUCCESS
)
2094 /* First determine the bytes needed and get the number of dependent services */
2095 dwError
= Int_EnumDependentServicesW(hServicesKey
,
2100 &dwServicesReturned
);
2101 if (dwError
!= ERROR_SUCCESS
)
2104 /* If buffer size is less than the bytes needed or pointer is null */
2105 if ((!lpServices
) || (cbBufSize
< *pcbBytesNeeded
))
2107 dwError
= ERROR_MORE_DATA
;
2111 /* Allocate memory for array of service pointers */
2112 lpServicesArray
= HeapAlloc(GetProcessHeap(),
2114 (dwServicesReturned
+ 1) * sizeof(PSERVICE
));
2115 if (!lpServicesArray
)
2117 DPRINT1("Could not allocate a buffer!!\n");
2118 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
2122 dwServicesReturned
= 0;
2123 *pcbBytesNeeded
= 0;
2125 dwError
= Int_EnumDependentServicesW(hServicesKey
,
2130 &dwServicesReturned
);
2131 if (dwError
!= ERROR_SUCCESS
)
2136 lpServicesPtr
= (LPENUM_SERVICE_STATUSW
) lpServices
;
2137 lpStr
= (LPWSTR
)(lpServices
+ (dwServicesReturned
* sizeof(ENUM_SERVICE_STATUSW
)));
2139 /* Copy EnumDepenedentService to Buffer */
2140 for (dwServiceCount
= 0; dwServiceCount
< dwServicesReturned
; dwServiceCount
++)
2142 lpService
= lpServicesArray
[dwServiceCount
];
2144 /* Copy status info */
2145 memcpy(&lpServicesPtr
->ServiceStatus
,
2147 sizeof(SERVICE_STATUS
));
2149 /* Copy display name */
2150 wcscpy(lpStr
, lpService
->lpDisplayName
);
2151 lpServicesPtr
->lpDisplayName
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServices
);
2152 lpStr
+= (wcslen(lpService
->lpDisplayName
) + 1);
2154 /* Copy service name */
2155 wcscpy(lpStr
, lpService
->lpServiceName
);
2156 lpServicesPtr
->lpServiceName
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServices
);
2157 lpStr
+= (wcslen(lpService
->lpServiceName
) + 1);
2162 *lpServicesReturned
= dwServicesReturned
;
2165 if (lpServicesArray
!= NULL
)
2166 HeapFree(GetProcessHeap(), 0, lpServicesArray
);
2168 RegCloseKey(hServicesKey
);
2170 DPRINT("REnumDependentServicesW() done (Error %lu)\n", dwError
);
2177 DWORD
REnumServicesStatusW(
2178 SC_RPC_HANDLE hSCManager
,
2179 DWORD dwServiceType
,
2180 DWORD dwServiceState
,
2183 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
2184 LPBOUNDED_DWORD_256K lpServicesReturned
,
2185 LPBOUNDED_DWORD_256K lpResumeHandle
)
2187 PMANAGER_HANDLE hManager
;
2189 DWORD dwError
= ERROR_SUCCESS
;
2190 PLIST_ENTRY ServiceEntry
;
2191 PSERVICE CurrentService
;
2193 DWORD dwRequiredSize
;
2194 DWORD dwServiceCount
;
2196 DWORD dwLastResumeCount
= 0;
2197 LPENUM_SERVICE_STATUSW lpStatusPtr
;
2200 DPRINT("REnumServicesStatusW() called\n");
2203 return ERROR_SHUTDOWN_IN_PROGRESS
;
2205 hManager
= (PMANAGER_HANDLE
)hSCManager
;
2206 if (!hManager
|| hManager
->Handle
.Tag
!= MANAGER_TAG
)
2208 DPRINT1("Invalid manager handle!\n");
2209 return ERROR_INVALID_HANDLE
;
2212 *pcbBytesNeeded
= 0;
2213 *lpServicesReturned
= 0;
2215 if ((dwServiceType
!=SERVICE_DRIVER
) && (dwServiceType
!=SERVICE_WIN32
))
2217 DPRINT("Not a valid Service Type!\n");
2218 return ERROR_INVALID_PARAMETER
;
2221 if ((dwServiceState
<SERVICE_ACTIVE
) || (dwServiceState
>SERVICE_STATE_ALL
))
2223 DPRINT("Not a valid Service State!\n");
2224 return ERROR_INVALID_PARAMETER
;
2227 /* Check access rights */
2228 if (!RtlAreAllAccessesGranted(hManager
->Handle
.DesiredAccess
,
2229 SC_MANAGER_ENUMERATE_SERVICE
))
2231 DPRINT1("Insufficient access rights! 0x%lx\n",
2232 hManager
->Handle
.DesiredAccess
);
2233 return ERROR_ACCESS_DENIED
;
2237 dwLastResumeCount
= *lpResumeHandle
;
2239 /* FIXME: Lock the service list shared */
2241 lpService
= ScmGetServiceEntryByResumeCount(dwLastResumeCount
);
2242 if (lpService
== NULL
)
2244 dwError
= ERROR_SUCCESS
;
2251 for (ServiceEntry
= &lpService
->ServiceListEntry
;
2252 ServiceEntry
!= &ServiceListHead
;
2253 ServiceEntry
= ServiceEntry
->Flink
)
2255 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
2259 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
2262 dwState
= SERVICE_ACTIVE
;
2263 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
2264 dwState
= SERVICE_INACTIVE
;
2266 if ((dwState
& dwServiceState
) == 0)
2269 dwSize
= sizeof(ENUM_SERVICE_STATUSW
) +
2270 ((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
2271 ((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
2273 if (dwRequiredSize
+ dwSize
> dwBufSize
)
2275 DPRINT("Service name: %S no fit\n", CurrentService
->lpServiceName
);
2279 DPRINT("Service name: %S fit\n", CurrentService
->lpServiceName
);
2280 dwRequiredSize
+= dwSize
;
2282 dwLastResumeCount
= CurrentService
->dwResumeCount
;
2285 DPRINT("dwRequiredSize: %lu\n", dwRequiredSize
);
2286 DPRINT("dwServiceCount: %lu\n", dwServiceCount
);
2289 ServiceEntry
!= &ServiceListHead
;
2290 ServiceEntry
= ServiceEntry
->Flink
)
2292 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
2296 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
2299 dwState
= SERVICE_ACTIVE
;
2300 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
2301 dwState
= SERVICE_INACTIVE
;
2303 if ((dwState
& dwServiceState
) == 0)
2306 dwRequiredSize
+= (sizeof(ENUM_SERVICE_STATUSW
) +
2307 ((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
2308 ((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
)));
2310 dwError
= ERROR_MORE_DATA
;
2313 DPRINT("*pcbBytesNeeded: %lu\n", dwRequiredSize
);
2316 *lpResumeHandle
= dwLastResumeCount
;
2318 *lpServicesReturned
= dwServiceCount
;
2319 *pcbBytesNeeded
= dwRequiredSize
;
2321 lpStatusPtr
= (LPENUM_SERVICE_STATUSW
)lpBuffer
;
2322 lpStringPtr
= (LPWSTR
)((ULONG_PTR
)lpBuffer
+
2323 dwServiceCount
* sizeof(ENUM_SERVICE_STATUSW
));
2326 for (ServiceEntry
= &lpService
->ServiceListEntry
;
2327 ServiceEntry
!= &ServiceListHead
;
2328 ServiceEntry
= ServiceEntry
->Flink
)
2330 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
2334 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
2337 dwState
= SERVICE_ACTIVE
;
2338 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
2339 dwState
= SERVICE_INACTIVE
;
2341 if ((dwState
& dwServiceState
) == 0)
2344 dwSize
= sizeof(ENUM_SERVICE_STATUSW
) +
2345 ((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
2346 ((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
2348 if (dwRequiredSize
+ dwSize
> dwBufSize
)
2351 /* Copy the service name */
2352 wcscpy(lpStringPtr
, CurrentService
->lpServiceName
);
2353 lpStatusPtr
->lpServiceName
= (LPWSTR
)((ULONG_PTR
)lpStringPtr
- (ULONG_PTR
)lpBuffer
);
2354 lpStringPtr
+= (wcslen(CurrentService
->lpServiceName
) + 1);
2356 /* Copy the display name */
2357 wcscpy(lpStringPtr
, CurrentService
->lpDisplayName
);
2358 lpStatusPtr
->lpDisplayName
= (LPWSTR
)((ULONG_PTR
)lpStringPtr
- (ULONG_PTR
)lpBuffer
);
2359 lpStringPtr
+= (wcslen(CurrentService
->lpDisplayName
) + 1);
2361 /* Copy the status information */
2362 memcpy(&lpStatusPtr
->ServiceStatus
,
2363 &CurrentService
->Status
,
2364 sizeof(SERVICE_STATUS
));
2367 dwRequiredSize
+= dwSize
;
2372 *pcbBytesNeeded
= 0;
2373 if (lpResumeHandle
) *lpResumeHandle
= 0;
2377 /* FIXME: Unlock the service list */
2379 DPRINT("REnumServicesStatusW() done (Error %lu)\n", dwError
);
2386 DWORD
ROpenSCManagerW(
2387 LPWSTR lpMachineName
,
2388 LPWSTR lpDatabaseName
,
2389 DWORD dwDesiredAccess
,
2390 LPSC_RPC_HANDLE lpScHandle
)
2395 DPRINT("ROpenSCManagerW() called\n");
2396 DPRINT("lpMachineName = %p\n", lpMachineName
);
2397 DPRINT("lpMachineName: %S\n", lpMachineName
);
2398 DPRINT("lpDataBaseName = %p\n", lpDatabaseName
);
2399 DPRINT("lpDataBaseName: %S\n", lpDatabaseName
);
2400 DPRINT("dwDesiredAccess = %x\n", dwDesiredAccess
);
2403 return ERROR_SHUTDOWN_IN_PROGRESS
;
2406 return ERROR_INVALID_PARAMETER
;
2408 dwError
= ScmCreateManagerHandle(lpDatabaseName
,
2410 if (dwError
!= ERROR_SUCCESS
)
2412 DPRINT1("ScmCreateManagerHandle() failed (Error %lu)\n", dwError
);
2416 /* Check the desired access */
2417 dwError
= ScmCheckAccess(hHandle
,
2418 dwDesiredAccess
| SC_MANAGER_CONNECT
);
2419 if (dwError
!= ERROR_SUCCESS
)
2421 DPRINT1("ScmCheckAccess() failed (Error %lu)\n", dwError
);
2422 HeapFree(GetProcessHeap(), 0, hHandle
);
2426 *lpScHandle
= (SC_RPC_HANDLE
)hHandle
;
2427 DPRINT("*hScm = %p\n", *lpScHandle
);
2429 DPRINT("ROpenSCManagerW() done\n");
2431 return ERROR_SUCCESS
;
2436 DWORD
ROpenServiceW(
2437 SC_RPC_HANDLE hSCManager
,
2438 LPWSTR lpServiceName
,
2439 DWORD dwDesiredAccess
,
2440 LPSC_RPC_HANDLE lpServiceHandle
)
2443 PMANAGER_HANDLE hManager
;
2447 DPRINT("ROpenServiceW() called\n");
2448 DPRINT("hSCManager = %p\n", hSCManager
);
2449 DPRINT("lpServiceName = %p\n", lpServiceName
);
2450 DPRINT("lpServiceName: %S\n", lpServiceName
);
2451 DPRINT("dwDesiredAccess = %x\n", dwDesiredAccess
);
2454 return ERROR_SHUTDOWN_IN_PROGRESS
;
2456 hManager
= (PMANAGER_HANDLE
)hSCManager
;
2457 if (!hManager
|| hManager
->Handle
.Tag
!= MANAGER_TAG
)
2459 DPRINT1("Invalid manager handle!\n");
2460 return ERROR_INVALID_HANDLE
;
2463 if (!lpServiceHandle
)
2464 return ERROR_INVALID_PARAMETER
;
2467 return ERROR_INVALID_ADDRESS
;
2469 /* FIXME: Lock the service list */
2471 /* Get service database entry */
2472 lpService
= ScmGetServiceEntryByName(lpServiceName
);
2473 if (lpService
== NULL
)
2475 DPRINT("Could not find a service!\n");
2476 return ERROR_SERVICE_DOES_NOT_EXIST
;
2479 /* Create a service handle */
2480 dwError
= ScmCreateServiceHandle(lpService
,
2482 if (dwError
!= ERROR_SUCCESS
)
2484 DPRINT1("ScmCreateServiceHandle() failed (Error %lu)\n", dwError
);
2488 /* Check the desired access */
2489 dwError
= ScmCheckAccess(hHandle
,
2491 if (dwError
!= ERROR_SUCCESS
)
2493 DPRINT1("ScmCheckAccess() failed (Error %lu)\n", dwError
);
2494 HeapFree(GetProcessHeap(), 0, hHandle
);
2498 lpService
->dwRefCount
++;
2499 DPRINT("OpenService - lpService->dwRefCount %u\n",lpService
->dwRefCount
);
2501 *lpServiceHandle
= (SC_RPC_HANDLE
)hHandle
;
2502 DPRINT("*hService = %p\n", *lpServiceHandle
);
2504 DPRINT("ROpenServiceW() done\n");
2506 return ERROR_SUCCESS
;
2511 DWORD
RQueryServiceConfigW(
2512 SC_RPC_HANDLE hService
,
2513 LPBYTE lpBuf
, //LPQUERY_SERVICE_CONFIGW lpServiceConfig,
2515 LPBOUNDED_DWORD_8K pcbBytesNeeded
)
2517 LPQUERY_SERVICE_CONFIGW lpServiceConfig
= (LPQUERY_SERVICE_CONFIGW
)lpBuf
;
2518 DWORD dwError
= ERROR_SUCCESS
;
2519 PSERVICE_HANDLE hSvc
;
2520 PSERVICE lpService
= NULL
;
2521 HKEY hServiceKey
= NULL
;
2522 LPWSTR lpImagePath
= NULL
;
2523 LPWSTR lpServiceStartName
= NULL
;
2524 DWORD dwRequiredSize
;
2525 LPQUERY_SERVICE_CONFIGW lpConfig
= NULL
;
2526 WCHAR lpEmptyString
[] = {0,0};
2529 DPRINT("RQueryServiceConfigW() called\n");
2532 return ERROR_SHUTDOWN_IN_PROGRESS
;
2534 hSvc
= (PSERVICE_HANDLE
)hService
;
2535 if (!hSvc
|| hSvc
->Handle
.Tag
!= SERVICE_TAG
)
2537 DPRINT1("Invalid handle tag!\n");
2538 return ERROR_INVALID_HANDLE
;
2541 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
2542 SERVICE_QUERY_CONFIG
))
2544 DPRINT1("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
2545 return ERROR_ACCESS_DENIED
;
2548 lpService
= hSvc
->ServiceEntry
;
2549 if (lpService
== NULL
)
2551 DPRINT1("lpService == NULL!\n");
2552 return ERROR_INVALID_HANDLE
;
2555 /* FIXME: Lock the service database shared */
2557 dwError
= ScmOpenServiceKey(lpService
->lpServiceName
,
2560 if (dwError
!= ERROR_SUCCESS
)
2563 dwError
= ScmReadString(hServiceKey
,
2566 if (dwError
!= ERROR_SUCCESS
)
2569 ScmReadString(hServiceKey
,
2571 &lpServiceStartName
);
2573 dwRequiredSize
= sizeof(QUERY_SERVICE_CONFIGW
);
2575 if (lpImagePath
!= NULL
)
2576 dwRequiredSize
+= ((wcslen(lpImagePath
) + 1) * sizeof(WCHAR
));
2578 dwRequiredSize
+= 2 * sizeof(WCHAR
);
2580 if (lpService
->lpGroup
!= NULL
)
2581 dwRequiredSize
+= ((wcslen(lpService
->lpGroup
->lpGroupName
) + 1) * sizeof(WCHAR
));
2583 dwRequiredSize
+= 2 * sizeof(WCHAR
);
2585 /* FIXME: Add Dependencies length*/
2587 if (lpServiceStartName
!= NULL
)
2588 dwRequiredSize
+= ((wcslen(lpServiceStartName
) + 1) * sizeof(WCHAR
));
2590 dwRequiredSize
+= 2 * sizeof(WCHAR
);
2592 if (lpService
->lpDisplayName
!= NULL
)
2593 dwRequiredSize
+= ((wcslen(lpService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
2595 dwRequiredSize
+= 2 * sizeof(WCHAR
);
2597 if (lpServiceConfig
== NULL
|| cbBufSize
< dwRequiredSize
)
2599 dwError
= ERROR_INSUFFICIENT_BUFFER
;
2603 lpConfig
= (LPQUERY_SERVICE_CONFIGW
)lpServiceConfig
;
2604 lpConfig
->dwServiceType
= lpService
->Status
.dwServiceType
;
2605 lpConfig
->dwStartType
= lpService
->dwStartType
;
2606 lpConfig
->dwErrorControl
= lpService
->dwErrorControl
;
2607 lpConfig
->dwTagId
= lpService
->dwTag
;
2609 lpStr
= (LPWSTR
)(lpConfig
+ 1);
2611 if (lpImagePath
!= NULL
)
2613 wcscpy(lpStr
, lpImagePath
);
2617 wcscpy(lpStr
, lpEmptyString
);
2620 lpConfig
->lpBinaryPathName
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpConfig
);
2621 lpStr
+= (wcslen(lpStr
) + 1);
2623 if (lpService
->lpGroup
!= NULL
)
2625 wcscpy(lpStr
, lpService
->lpGroup
->lpGroupName
);
2629 wcscpy(lpStr
, lpEmptyString
);
2632 lpConfig
->lpLoadOrderGroup
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpConfig
);
2633 lpStr
+= (wcslen(lpStr
) + 1);
2635 /* FIXME: Append Dependencies */
2636 wcscpy(lpStr
, lpEmptyString
);
2638 lpStr
+= (wcslen(lpStr
) + 1);
2639 lpConfig
->lpDependencies
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpConfig
);
2641 if (lpServiceStartName
!= NULL
)
2643 wcscpy(lpStr
, lpServiceStartName
);
2647 wcscpy(lpStr
, lpEmptyString
);
2650 lpConfig
->lpServiceStartName
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpConfig
);
2651 lpStr
+= (wcslen(lpStr
) + 1);
2653 if (lpService
->lpDisplayName
!= NULL
)
2655 wcscpy(lpStr
, lpService
->lpDisplayName
);
2659 wcscpy(lpStr
, lpEmptyString
);
2662 lpConfig
->lpDisplayName
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpConfig
);
2665 if (pcbBytesNeeded
!= NULL
)
2666 *pcbBytesNeeded
= dwRequiredSize
;
2669 if (lpImagePath
!= NULL
)
2670 HeapFree(GetProcessHeap(), 0, lpImagePath
);
2672 if (lpServiceStartName
!= NULL
)
2673 HeapFree(GetProcessHeap(), 0, lpServiceStartName
);
2675 if (hServiceKey
!= NULL
)
2676 RegCloseKey(hServiceKey
);
2678 /* FIXME: Unlock the service database */
2680 DPRINT("RQueryServiceConfigW() done\n");
2687 DWORD
RQueryServiceLockStatusW(
2688 SC_RPC_HANDLE hSCManager
,
2689 LPQUERY_SERVICE_LOCK_STATUSW lpLockStatus
,
2691 LPBOUNDED_DWORD_4K pcbBytesNeeded
)
2694 return ERROR_CALL_NOT_IMPLEMENTED
;
2699 DWORD
RStartServiceW(
2700 SC_RPC_HANDLE hService
,
2702 LPSTRING_PTRSW argv
)
2704 DWORD dwError
= ERROR_SUCCESS
;
2705 PSERVICE_HANDLE hSvc
;
2706 PSERVICE lpService
= NULL
;
2708 DPRINT("RStartServiceW() called\n");
2711 return ERROR_SHUTDOWN_IN_PROGRESS
;
2713 hSvc
= (PSERVICE_HANDLE
)hService
;
2714 if (!hSvc
|| hSvc
->Handle
.Tag
!= SERVICE_TAG
)
2716 DPRINT1("Invalid handle tag!\n");
2717 return ERROR_INVALID_HANDLE
;
2720 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
2723 DPRINT1("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
2724 return ERROR_ACCESS_DENIED
;
2727 lpService
= hSvc
->ServiceEntry
;
2728 if (lpService
== NULL
)
2730 DPRINT1("lpService == NULL!\n");
2731 return ERROR_INVALID_HANDLE
;
2734 if (lpService
->dwStartType
== SERVICE_DISABLED
)
2735 return ERROR_SERVICE_DISABLED
;
2737 if (lpService
->bDeleted
)
2738 return ERROR_SERVICE_MARKED_FOR_DELETE
;
2745 /* Start the service */
2746 dwError
= ScmStartService(lpService
, argc
, (LPWSTR
*)argv
);
2753 DWORD
RGetServiceDisplayNameW(
2754 SC_RPC_HANDLE hSCManager
,
2755 LPCWSTR lpServiceName
,
2756 LPWSTR lpDisplayName
,
2759 // PMANAGER_HANDLE hManager;
2764 DPRINT("RGetServiceDisplayNameW() called\n");
2765 DPRINT("hSCManager = %p\n", hSCManager
);
2766 DPRINT("lpServiceName: %S\n", lpServiceName
);
2767 DPRINT("lpDisplayName: %p\n", lpDisplayName
);
2768 DPRINT("*lpcchBuffer: %lu\n", *lpcchBuffer
);
2770 // hManager = (PMANAGER_HANDLE)hSCManager;
2771 // if (hManager->Handle.Tag != MANAGER_TAG)
2773 // DPRINT1("Invalid manager handle!\n");
2774 // return ERROR_INVALID_HANDLE;
2777 /* Get service database entry */
2778 lpService
= ScmGetServiceEntryByName(lpServiceName
);
2779 if (lpService
== NULL
)
2781 DPRINT1("Could not find a service!\n");
2783 /* If the service could not be found and lpcchBuffer is less than 2, windows
2784 puts null in lpDisplayName and puts 2 in lpcchBuffer */
2785 if (*lpcchBuffer
< 2)
2788 if (lpDisplayName
!= NULL
)
2790 *lpDisplayName
= '\0';
2794 return ERROR_SERVICE_DOES_NOT_EXIST
;
2797 if (!lpService
->lpDisplayName
)
2799 dwLength
= wcslen(lpService
->lpServiceName
);
2801 if (lpDisplayName
!= NULL
&&
2802 *lpcchBuffer
> dwLength
)
2804 wcscpy(lpDisplayName
, lpService
->lpServiceName
);
2809 dwLength
= wcslen(lpService
->lpDisplayName
);
2811 if (lpDisplayName
!= NULL
&&
2812 *lpcchBuffer
> dwLength
)
2814 wcscpy(lpDisplayName
, lpService
->lpDisplayName
);
2818 dwError
= (*lpcchBuffer
> dwLength
) ? ERROR_SUCCESS
: ERROR_INSUFFICIENT_BUFFER
;
2820 *lpcchBuffer
= dwLength
;
2827 DWORD
RGetServiceKeyNameW(
2828 SC_RPC_HANDLE hSCManager
,
2829 LPCWSTR lpDisplayName
,
2830 LPWSTR lpServiceName
,
2833 // PMANAGER_HANDLE hManager;
2838 DPRINT("RGetServiceKeyNameW() called\n");
2839 DPRINT("hSCManager = %p\n", hSCManager
);
2840 DPRINT("lpDisplayName: %S\n", lpDisplayName
);
2841 DPRINT("lpServiceName: %p\n", lpServiceName
);
2842 DPRINT("*lpcchBuffer: %lu\n", *lpcchBuffer
);
2844 // hManager = (PMANAGER_HANDLE)hSCManager;
2845 // if (hManager->Handle.Tag != MANAGER_TAG)
2847 // DPRINT1("Invalid manager handle!\n");
2848 // return ERROR_INVALID_HANDLE;
2851 /* Get service database entry */
2852 lpService
= ScmGetServiceEntryByDisplayName(lpDisplayName
);
2853 if (lpService
== NULL
)
2855 DPRINT1("Could not find a service!\n");
2857 /* If the service could not be found and lpcchBuffer is less than 2, windows
2858 puts null in lpDisplayName and puts 2 in lpcchBuffer */
2859 if (*lpcchBuffer
< 2)
2862 if (lpServiceName
!= NULL
)
2864 *lpServiceName
= '\0';
2868 return ERROR_SERVICE_DOES_NOT_EXIST
;
2871 dwLength
= wcslen(lpService
->lpServiceName
);
2873 if (lpServiceName
!= NULL
&&
2874 *lpcchBuffer
> dwLength
)
2876 wcscpy(lpServiceName
, lpService
->lpServiceName
);
2877 *lpcchBuffer
= dwLength
;
2878 return ERROR_SUCCESS
;
2881 dwError
= (*lpcchBuffer
> dwLength
) ? ERROR_SUCCESS
: ERROR_INSUFFICIENT_BUFFER
;
2883 *lpcchBuffer
= dwLength
;
2890 DWORD
RI_ScSetServiceBitsA(
2891 RPC_SERVICE_STATUS_HANDLE hServiceStatus
,
2892 DWORD dwServiceBits
,
2894 int bUpdateImmediately
,
2898 return ERROR_CALL_NOT_IMPLEMENTED
;
2903 DWORD
RChangeServiceConfigA(
2904 SC_RPC_HANDLE hService
,
2905 DWORD dwServiceType
,
2907 DWORD dwErrorControl
,
2908 LPSTR lpBinaryPathName
,
2909 LPSTR lpLoadOrderGroup
,
2911 LPSTR lpDependencies
,
2913 LPSTR lpServiceStartName
,
2916 LPSTR lpDisplayName
)
2918 DWORD dwError
= ERROR_SUCCESS
;
2919 PSERVICE_HANDLE hSvc
;
2920 PSERVICE lpService
= NULL
;
2921 HKEY hServiceKey
= NULL
;
2922 LPWSTR lpDisplayNameW
= NULL
;
2923 // LPWSTR lpBinaryPathNameW = NULL;
2924 LPWSTR lpLoadOrderGroupW
= NULL
;
2925 LPWSTR lpDependenciesW
= NULL
;
2926 // LPWSTR lpPasswordW = NULL;
2928 DPRINT("RChangeServiceConfigA() called\n");
2929 DPRINT("dwServiceType = %lu\n", dwServiceType
);
2930 DPRINT("dwStartType = %lu\n", dwStartType
);
2931 DPRINT("dwErrorControl = %lu\n", dwErrorControl
);
2932 DPRINT("lpBinaryPathName = %s\n", lpBinaryPathName
);
2933 DPRINT("lpLoadOrderGroup = %s\n", lpLoadOrderGroup
);
2934 DPRINT("lpDisplayName = %s\n", lpDisplayName
);
2937 return ERROR_SHUTDOWN_IN_PROGRESS
;
2939 hSvc
= (PSERVICE_HANDLE
)hService
;
2940 if (!hSvc
|| hSvc
->Handle
.Tag
!= SERVICE_TAG
)
2942 DPRINT1("Invalid handle tag!\n");
2943 return ERROR_INVALID_HANDLE
;
2946 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
2947 SERVICE_CHANGE_CONFIG
))
2949 DPRINT1("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
2950 return ERROR_ACCESS_DENIED
;
2953 lpService
= hSvc
->ServiceEntry
;
2954 if (lpService
== NULL
)
2956 DPRINT1("lpService == NULL!\n");
2957 return ERROR_INVALID_HANDLE
;
2960 /* FIXME: Lock database exclusively */
2962 if (lpService
->bDeleted
)
2964 /* FIXME: Unlock database */
2965 DPRINT1("The service has already been marked for delete!\n");
2966 return ERROR_SERVICE_MARKED_FOR_DELETE
;
2969 /* Open the service key */
2970 dwError
= ScmOpenServiceKey(lpService
->szServiceName
,
2973 if (dwError
!= ERROR_SUCCESS
)
2976 /* Write service data to the registry */
2978 if (lpDisplayName
!= NULL
&& *lpDisplayName
!= 0)
2980 /* Set the display name */
2981 lpDisplayNameW
= HeapAlloc(GetProcessHeap(),
2983 (strlen(lpDisplayName
) + 1) * sizeof(WCHAR
));
2984 if (lpDisplayNameW
== NULL
)
2986 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
2990 MultiByteToWideChar(CP_ACP
,
2995 strlen(lpDisplayName
) + 1);
2997 RegSetValueExW(hServiceKey
,
3001 (LPBYTE
)lpDisplayNameW
,
3002 (wcslen(lpDisplayNameW
) + 1) * sizeof(WCHAR
));
3004 /* Update lpService->lpDisplayName */
3005 if (lpService
->lpDisplayName
)
3006 HeapFree(GetProcessHeap(), 0, lpService
->lpDisplayName
);
3008 lpService
->lpDisplayName
= lpDisplayNameW
;
3011 if (dwServiceType
!= SERVICE_NO_CHANGE
)
3013 /* Set the service type */
3014 dwError
= RegSetValueExW(hServiceKey
,
3018 (LPBYTE
)&dwServiceType
,
3020 if (dwError
!= ERROR_SUCCESS
)
3023 lpService
->Status
.dwServiceType
= dwServiceType
;
3026 if (dwStartType
!= SERVICE_NO_CHANGE
)
3028 /* Set the start value */
3029 dwError
= RegSetValueExW(hServiceKey
,
3033 (LPBYTE
)&dwStartType
,
3035 if (dwError
!= ERROR_SUCCESS
)
3038 lpService
->dwStartType
= dwStartType
;
3041 if (dwErrorControl
!= SERVICE_NO_CHANGE
)
3043 /* Set the error control value */
3044 dwError
= RegSetValueExW(hServiceKey
,
3048 (LPBYTE
)&dwErrorControl
,
3050 if (dwError
!= ERROR_SUCCESS
)
3053 lpService
->dwErrorControl
= dwErrorControl
;
3057 /* FIXME: set the new ImagePath value */
3059 /* Set the image path */
3060 if (dwServiceType
& SERVICE_WIN32
)
3062 if (lpBinaryPathName
!= NULL
&& *lpBinaryPathName
!= 0)
3064 lpBinaryPathNameW
=HeapAlloc(GetProcessHeap(),0, (strlen(lpBinaryPathName
)+1) * sizeof(WCHAR
));
3065 MultiByteToWideChar(CP_ACP
, 0, lpBinaryPathName
, -1, lpBinaryPathNameW
, strlen(lpBinaryPathName
)+1);
3066 dwError
= RegSetValueExW(hServiceKey
,
3070 (LPBYTE
)lpBinaryPathNameW
,
3071 (wcslen(lpBinaryPathNameW
) + 1) * sizeof(WCHAR
));
3072 if (dwError
!= ERROR_SUCCESS
)
3076 else if (dwServiceType
& SERVICE_DRIVER
)
3078 if (lpImagePath
!= NULL
&& *lpImagePath
!= 0)
3080 dwError
= RegSetValueExW(hServiceKey
,
3084 (LPBYTE
)lpImagePath
,
3085 (wcslen(lpImagePath
) + 1) *sizeof(WCHAR
));
3086 if (dwError
!= ERROR_SUCCESS
)
3092 /* Set the group name */
3093 if (lpLoadOrderGroup
!= NULL
&& *lpLoadOrderGroup
!= 0)
3095 lpLoadOrderGroupW
= HeapAlloc(GetProcessHeap(),
3097 (strlen(lpLoadOrderGroup
) + 1) * sizeof(WCHAR
));
3098 if (lpLoadOrderGroupW
== NULL
)
3100 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
3104 MultiByteToWideChar(CP_ACP
,
3109 strlen(lpLoadOrderGroup
) + 1);
3111 dwError
= RegSetValueExW(hServiceKey
,
3115 (LPBYTE
)lpLoadOrderGroupW
,
3116 (wcslen(lpLoadOrderGroupW
) + 1) * sizeof(WCHAR
));
3117 if (dwError
!= ERROR_SUCCESS
)
3119 HeapFree(GetProcessHeap(), 0, lpLoadOrderGroupW
);
3123 dwError
= ScmSetServiceGroup(lpService
,
3126 HeapFree(GetProcessHeap(), 0, lpLoadOrderGroupW
);
3128 if (dwError
!= ERROR_SUCCESS
)
3132 if (lpdwTagId
!= NULL
)
3134 dwError
= ScmAssignNewTag(lpService
);
3135 if (dwError
!= ERROR_SUCCESS
)
3138 dwError
= RegSetValueExW(hServiceKey
,
3142 (LPBYTE
)&lpService
->dwTag
,
3144 if (dwError
!= ERROR_SUCCESS
)
3147 *lpdwTagId
= lpService
->dwTag
;
3150 /* Write dependencies */
3151 if (lpDependencies
!= NULL
&& *lpDependencies
!= 0)
3153 lpDependenciesW
= HeapAlloc(GetProcessHeap(),
3155 (strlen(lpDependencies
) + 1) * sizeof(WCHAR
));
3156 if (lpDependenciesW
== NULL
)
3158 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
3162 MultiByteToWideChar(CP_ACP
,
3167 strlen(lpDependencies
) + 1);
3169 dwError
= ScmWriteDependencies(hServiceKey
,
3170 (LPWSTR
)lpDependenciesW
,
3173 HeapFree(GetProcessHeap(), 0, lpDependenciesW
);
3176 if (lpPassword
!= NULL
)
3178 /* FIXME: Write password */
3181 /* FIXME: Unlock database */
3184 if (hServiceKey
!= NULL
)
3185 RegCloseKey(hServiceKey
);
3187 DPRINT("RChangeServiceConfigA() done (Error %lu)\n", dwError
);
3194 DWORD
RCreateServiceA(
3195 SC_RPC_HANDLE hSCManager
,
3196 LPSTR lpServiceName
,
3197 LPSTR lpDisplayName
,
3198 DWORD dwDesiredAccess
,
3199 DWORD dwServiceType
,
3201 DWORD dwErrorControl
,
3202 LPSTR lpBinaryPathName
,
3203 LPSTR lpLoadOrderGroup
,
3205 LPBYTE lpDependencies
,
3207 LPSTR lpServiceStartName
,
3210 LPSC_RPC_HANDLE lpServiceHandle
)
3213 return ERROR_CALL_NOT_IMPLEMENTED
;
3218 DWORD
REnumDependentServicesA(
3219 SC_RPC_HANDLE hService
,
3220 DWORD dwServiceState
,
3223 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
3224 LPBOUNDED_DWORD_256K lpServicesReturned
)
3226 DWORD dwError
= ERROR_SUCCESS
;
3227 DWORD dwServicesReturned
= 0;
3228 DWORD dwServiceCount
;
3229 HKEY hServicesKey
= NULL
;
3230 LPSC_RPC_HANDLE hSCObject
;
3231 PSERVICE_HANDLE hSvc
;
3232 PSERVICE lpService
= NULL
;
3233 PSERVICE
*lpServicesArray
= NULL
;
3234 LPENUM_SERVICE_STATUSA lpServicesPtr
= NULL
;
3237 *pcbBytesNeeded
= 0;
3238 *lpServicesReturned
= 0;
3240 DPRINT("REnumDependentServicesA() called\n");
3242 hSCObject
= &hService
;
3243 hSvc
= (PSERVICE_HANDLE
) *hSCObject
;
3244 lpService
= hSvc
->ServiceEntry
;
3246 /* Check access rights */
3247 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
3248 SC_MANAGER_ENUMERATE_SERVICE
))
3250 DPRINT1("Insufficient access rights! 0x%lx\n",
3251 hSvc
->Handle
.DesiredAccess
);
3252 return ERROR_ACCESS_DENIED
;
3255 /* Open the Services Reg key */
3256 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
3257 L
"System\\CurrentControlSet\\Services",
3262 if (dwError
!= ERROR_SUCCESS
)
3265 /* NOTE: Windows calculates the pcbBytesNeeded based on WCHAR strings for
3266 both EnumDependentServicesA and EnumDependentServicesW. So returned pcbBytesNeeded
3267 are the same for both. Verified in WINXP. */
3269 /* First determine the bytes needed and get the number of dependent services*/
3270 dwError
= Int_EnumDependentServicesW(hServicesKey
,
3275 &dwServicesReturned
);
3276 if (dwError
!= ERROR_SUCCESS
)
3279 /* If buffer size is less than the bytes needed or pointer is null*/
3280 if ((!lpServices
) || (cbBufSize
< *pcbBytesNeeded
))
3282 dwError
= ERROR_MORE_DATA
;
3286 /* Allocate memory for array of service pointers */
3287 lpServicesArray
= HeapAlloc(GetProcessHeap(),
3289 (dwServicesReturned
+ 1) * sizeof(PSERVICE
));
3290 if (!lpServicesArray
)
3292 DPRINT1("Could not allocate a buffer!!\n");
3293 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
3297 dwServicesReturned
= 0;
3298 *pcbBytesNeeded
= 0;
3300 dwError
= Int_EnumDependentServicesW(hServicesKey
,
3305 &dwServicesReturned
);
3306 if (dwError
!= ERROR_SUCCESS
)
3311 lpServicesPtr
= (LPENUM_SERVICE_STATUSA
)lpServices
;
3312 lpStr
= (LPSTR
)(lpServices
+ (dwServicesReturned
* sizeof(ENUM_SERVICE_STATUSA
)));
3314 /* Copy EnumDepenedentService to Buffer */
3315 for (dwServiceCount
= 0; dwServiceCount
< dwServicesReturned
; dwServiceCount
++)
3317 lpService
= lpServicesArray
[dwServiceCount
];
3319 /* Copy the status info */
3320 memcpy(&lpServicesPtr
->ServiceStatus
,
3322 sizeof(SERVICE_STATUS
));
3324 /* Copy display name */
3325 WideCharToMultiByte(CP_ACP
,
3327 lpService
->lpDisplayName
,
3330 wcslen(lpService
->lpDisplayName
),
3333 lpServicesPtr
->lpDisplayName
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServices
);
3334 lpStr
+= strlen(lpStr
) + 1;
3336 /* Copy service name */
3337 WideCharToMultiByte(CP_ACP
,
3339 lpService
->lpServiceName
,
3342 wcslen(lpService
->lpServiceName
),
3345 lpServicesPtr
->lpServiceName
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServices
);
3346 lpStr
+= strlen(lpStr
) + 1;
3351 *lpServicesReturned
= dwServicesReturned
;
3354 if (lpServicesArray
)
3355 HeapFree(GetProcessHeap(), 0, lpServicesArray
);
3357 RegCloseKey(hServicesKey
);
3359 DPRINT("REnumDependentServicesA() done (Error %lu)\n", dwError
);
3366 DWORD
REnumServicesStatusA(
3367 SC_RPC_HANDLE hSCManager
,
3368 DWORD dwServiceType
,
3369 DWORD dwServiceState
,
3372 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
3373 LPBOUNDED_DWORD_256K lpServicesReturned
,
3374 LPBOUNDED_DWORD_256K lpResumeHandle
)
3376 LPENUM_SERVICE_STATUSW lpStatusPtrW
= NULL
;
3377 LPENUM_SERVICE_STATUSA lpStatusPtrA
= NULL
;
3378 LPWSTR lpStringPtrW
;
3381 DWORD dwServiceCount
;
3383 DPRINT("REnumServicesStatusA() called\n");
3385 if ((dwBufSize
> 0) && (lpBuffer
))
3387 lpStatusPtrW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, dwBufSize
);
3390 DPRINT1("Failed to allocate buffer!\n");
3391 return ERROR_NOT_ENOUGH_MEMORY
;
3395 dwError
= REnumServicesStatusW(hSCManager
,
3398 (LPBYTE
)lpStatusPtrW
,
3404 /* if no services were returned then we are Done */
3405 if (*lpServicesReturned
== 0)
3408 lpStatusPtrA
= (LPENUM_SERVICE_STATUSA
)lpBuffer
;
3409 lpStringPtrA
= (LPSTR
)((ULONG_PTR
)lpBuffer
+
3410 *lpServicesReturned
* sizeof(ENUM_SERVICE_STATUSA
));
3411 lpStringPtrW
= (LPWSTR
)((ULONG_PTR
)lpStatusPtrW
+
3412 *lpServicesReturned
* sizeof(ENUM_SERVICE_STATUSW
));
3414 for (dwServiceCount
= 0; dwServiceCount
< *lpServicesReturned
; dwServiceCount
++)
3416 /* Copy the service name */
3417 WideCharToMultiByte(CP_ACP
,
3422 wcslen(lpStringPtrW
),
3426 lpStatusPtrA
->lpServiceName
= (LPSTR
)((ULONG_PTR
)lpStringPtrA
- (ULONG_PTR
)lpBuffer
);
3427 lpStringPtrA
+= wcslen(lpStringPtrW
) + 1;
3428 lpStringPtrW
+= wcslen(lpStringPtrW
) + 1;
3430 /* Copy the display name */
3431 WideCharToMultiByte(CP_ACP
,
3436 wcslen(lpStringPtrW
),
3440 lpStatusPtrA
->lpDisplayName
= (LPSTR
)((ULONG_PTR
)lpStringPtrA
- (ULONG_PTR
)lpBuffer
);
3441 lpStringPtrA
+= wcslen(lpStringPtrW
) + 1;
3442 lpStringPtrW
+= wcslen(lpStringPtrW
) + 1;
3444 /* Copy the status information */
3445 memcpy(&lpStatusPtrA
->ServiceStatus
,
3446 &lpStatusPtrW
->ServiceStatus
,
3447 sizeof(SERVICE_STATUS
));
3454 HeapFree(GetProcessHeap(), 0, lpStatusPtrW
);
3456 DPRINT("REnumServicesStatusA() done (Error %lu)\n", dwError
);
3463 DWORD
ROpenSCManagerA(
3464 LPSTR lpMachineName
,
3465 LPSTR lpDatabaseName
,
3466 DWORD dwDesiredAccess
,
3467 LPSC_RPC_HANDLE lpScHandle
)
3469 UNICODE_STRING MachineName
;
3470 UNICODE_STRING DatabaseName
;
3473 DPRINT("ROpenSCManagerA() called\n");
3476 RtlCreateUnicodeStringFromAsciiz(&MachineName
,
3480 RtlCreateUnicodeStringFromAsciiz(&DatabaseName
,
3483 dwError
= ROpenSCManagerW(lpMachineName
? MachineName
.Buffer
: NULL
,
3484 lpDatabaseName
? DatabaseName
.Buffer
: NULL
,
3489 RtlFreeUnicodeString(&MachineName
);
3492 RtlFreeUnicodeString(&DatabaseName
);
3499 DWORD
ROpenServiceA(
3500 SC_RPC_HANDLE hSCManager
,
3501 LPSTR lpServiceName
,
3502 DWORD dwDesiredAccess
,
3503 LPSC_RPC_HANDLE lpServiceHandle
)
3505 UNICODE_STRING ServiceName
;
3508 DPRINT("ROpenServiceA() called\n");
3511 RtlCreateUnicodeStringFromAsciiz(&ServiceName
,
3514 dwError
= ROpenServiceW(hSCManager
,
3515 lpServiceName
? ServiceName
.Buffer
: NULL
,
3520 RtlFreeUnicodeString(&ServiceName
);
3527 DWORD
RQueryServiceConfigA(
3528 SC_RPC_HANDLE hService
,
3529 LPBYTE lpBuf
, //LPQUERY_SERVICE_CONFIGA lpServiceConfig,
3531 LPBOUNDED_DWORD_8K pcbBytesNeeded
)
3533 LPQUERY_SERVICE_CONFIGA lpServiceConfig
= (LPQUERY_SERVICE_CONFIGA
)lpBuf
;
3534 DWORD dwError
= ERROR_SUCCESS
;
3535 PSERVICE_HANDLE hSvc
;
3536 PSERVICE lpService
= NULL
;
3537 HKEY hServiceKey
= NULL
;
3538 LPWSTR lpImagePath
= NULL
;
3539 LPWSTR lpServiceStartName
= NULL
;
3540 DWORD dwRequiredSize
;
3541 LPQUERY_SERVICE_CONFIGA lpConfig
= NULL
;
3542 CHAR lpEmptyString
[]={0,0};
3545 DPRINT("RQueryServiceConfigA() called\n");
3548 return ERROR_SHUTDOWN_IN_PROGRESS
;
3550 hSvc
= (PSERVICE_HANDLE
)hService
;
3551 if (!hSvc
|| hSvc
->Handle
.Tag
!= SERVICE_TAG
)
3553 DPRINT1("Invalid handle tag!\n");
3554 return ERROR_INVALID_HANDLE
;
3557 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
3558 SERVICE_QUERY_CONFIG
))
3560 DPRINT1("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
3561 return ERROR_ACCESS_DENIED
;
3564 lpService
= hSvc
->ServiceEntry
;
3565 if (lpService
== NULL
)
3567 DPRINT1("lpService == NULL!\n");
3568 return ERROR_INVALID_HANDLE
;
3571 /* FIXME: Lock the service database shared */
3573 dwError
= ScmOpenServiceKey(lpService
->lpServiceName
,
3576 if (dwError
!= ERROR_SUCCESS
)
3579 dwError
= ScmReadString(hServiceKey
,
3582 if (dwError
!= ERROR_SUCCESS
)
3585 ScmReadString(hServiceKey
,
3587 &lpServiceStartName
);
3589 dwRequiredSize
= sizeof(QUERY_SERVICE_CONFIGW
);
3591 if (lpImagePath
!= NULL
)
3592 dwRequiredSize
+= wcslen(lpImagePath
) + 1;
3594 dwRequiredSize
+= 2;
3596 if ((lpService
->lpGroup
!= NULL
) && (lpService
->lpGroup
->lpGroupName
!= NULL
))
3597 dwRequiredSize
+= wcslen(lpService
->lpGroup
->lpGroupName
) + 1;
3599 dwRequiredSize
+= 2;
3601 /* FIXME: Add Dependencies length*/
3602 dwRequiredSize
+= 2;
3604 if (lpServiceStartName
!= NULL
)
3605 dwRequiredSize
+= wcslen(lpServiceStartName
) + 1;
3607 dwRequiredSize
+= 2;
3609 if (lpService
->lpDisplayName
!= NULL
)
3610 dwRequiredSize
+= wcslen(lpService
->lpDisplayName
) + 1;
3612 dwRequiredSize
+= 2;
3614 if (lpServiceConfig
== NULL
|| cbBufSize
< dwRequiredSize
)
3616 dwError
= ERROR_INSUFFICIENT_BUFFER
;
3620 lpConfig
= (LPQUERY_SERVICE_CONFIGA
)lpServiceConfig
;
3621 lpConfig
->dwServiceType
= lpService
->Status
.dwServiceType
;
3622 lpConfig
->dwStartType
= lpService
->dwStartType
;
3623 lpConfig
->dwErrorControl
= lpService
->dwErrorControl
;
3624 lpConfig
->dwTagId
= lpService
->dwTag
;
3626 lpStr
= (LPSTR
)(lpServiceConfig
+ 1);
3628 /* NOTE: Strings that are NULL for QUERY_SERVICE_CONFIG are pointers to empty strings.
3633 WideCharToMultiByte(CP_ACP
,
3638 wcslen(lpImagePath
)+1,
3644 strcpy(lpStr
, lpEmptyString
);
3647 lpConfig
->lpBinaryPathName
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpConfig
);
3648 lpStr
+= (strlen((LPSTR
)lpStr
) + 1);
3650 if (lpService
->lpGroup
&& lpService
->lpGroup
->lpGroupName
)
3652 WideCharToMultiByte(CP_ACP
,
3654 lpService
->lpGroup
->lpGroupName
,
3657 wcslen(lpService
->lpGroup
->lpGroupName
)+1,
3663 strcpy(lpStr
, lpEmptyString
);
3666 lpConfig
->lpLoadOrderGroup
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpConfig
);
3667 lpStr
+= (strlen(lpStr
) + 1);
3669 /* FIXME: Append Dependencies */
3670 strcpy(lpStr
, lpEmptyString
);
3672 lpConfig
->lpDependencies
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpConfig
);
3673 lpStr
+= (strlen(lpStr
) + 1);
3675 if (lpServiceStartName
)
3677 WideCharToMultiByte(CP_ACP
,
3682 wcslen(lpServiceStartName
)+1,
3688 strcpy(lpStr
, lpEmptyString
);
3691 lpConfig
->lpServiceStartName
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpConfig
);
3692 lpStr
+= (strlen(lpStr
) + 1);
3694 if (lpService
->lpDisplayName
)
3696 WideCharToMultiByte(CP_ACP
,
3698 lpService
->lpDisplayName
,
3701 wcslen(lpService
->lpDisplayName
)+1,
3707 strcpy(lpStr
, lpEmptyString
);
3710 lpConfig
->lpDisplayName
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpConfig
);
3713 if (pcbBytesNeeded
!= NULL
)
3714 *pcbBytesNeeded
= dwRequiredSize
;
3717 if (lpImagePath
!= NULL
)
3718 HeapFree(GetProcessHeap(), 0, lpImagePath
);
3720 if (lpServiceStartName
!= NULL
)
3721 HeapFree(GetProcessHeap(), 0, lpServiceStartName
);
3723 if (hServiceKey
!= NULL
)
3724 RegCloseKey(hServiceKey
);
3726 /* FIXME: Unlock the service database */
3728 DPRINT("RQueryServiceConfigA() done\n");
3735 DWORD
RQueryServiceLockStatusA(
3736 SC_RPC_HANDLE hSCManager
,
3737 LPQUERY_SERVICE_LOCK_STATUSA lpLockStatus
,
3739 LPBOUNDED_DWORD_4K pcbBytesNeeded
)
3742 return ERROR_CALL_NOT_IMPLEMENTED
;
3747 DWORD
RStartServiceA(
3748 SC_RPC_HANDLE hService
,
3750 LPSTRING_PTRSA argv
)
3752 DWORD dwError
= ERROR_SUCCESS
;
3753 PSERVICE_HANDLE hSvc
;
3754 PSERVICE lpService
= NULL
;
3756 DPRINT1("RStartServiceA() called\n");
3759 return ERROR_SHUTDOWN_IN_PROGRESS
;
3761 hSvc
= (PSERVICE_HANDLE
)hService
;
3762 if (!hSvc
|| hSvc
->Handle
.Tag
!= SERVICE_TAG
)
3764 DPRINT1("Invalid handle tag!\n");
3765 return ERROR_INVALID_HANDLE
;
3768 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
3771 DPRINT1("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
3772 return ERROR_ACCESS_DENIED
;
3775 lpService
= hSvc
->ServiceEntry
;
3776 if (lpService
== NULL
)
3778 DPRINT1("lpService == NULL!\n");
3779 return ERROR_INVALID_HANDLE
;
3782 if (lpService
->dwStartType
== SERVICE_DISABLED
)
3783 return ERROR_SERVICE_DISABLED
;
3785 if (lpService
->bDeleted
)
3786 return ERROR_SERVICE_MARKED_FOR_DELETE
;
3788 /* FIXME: Convert argument vector to Unicode */
3790 /* Start the service */
3791 dwError
= ScmStartService(lpService
, 0, NULL
);
3793 /* FIXME: Free argument vector */
3800 DWORD
RGetServiceDisplayNameA(
3801 SC_RPC_HANDLE hSCManager
,
3802 LPCSTR lpServiceName
,
3803 LPSTR lpDisplayName
,
3804 LPBOUNDED_DWORD_4K lpcchBuffer
)
3806 // PMANAGER_HANDLE hManager;
3807 PSERVICE lpService
= NULL
;
3810 LPWSTR lpServiceNameW
;
3812 DPRINT("RGetServiceDisplayNameA() called\n");
3813 DPRINT("hSCManager = %p\n", hSCManager
);
3814 DPRINT("lpServiceName: %s\n", lpServiceName
);
3815 DPRINT("lpDisplayName: %p\n", lpDisplayName
);
3816 DPRINT("*lpcchBuffer: %lu\n", *lpcchBuffer
);
3818 // hManager = (PMANAGER_HANDLE)hSCManager;
3819 // if (hManager->Handle.Tag != MANAGER_TAG)
3821 // DPRINT1("Invalid manager handle!\n");
3822 // return ERROR_INVALID_HANDLE;
3825 if (lpServiceName
!= NULL
)
3827 dwLength
= strlen(lpServiceName
) + 1;
3828 lpServiceNameW
= HeapAlloc(GetProcessHeap(),
3830 dwLength
* sizeof(WCHAR
));
3831 if (!lpServiceNameW
)
3832 return ERROR_NOT_ENOUGH_MEMORY
;
3834 MultiByteToWideChar(CP_ACP
,
3841 lpService
= ScmGetServiceEntryByName(lpServiceNameW
);
3843 HeapFree(GetProcessHeap(), 0, lpServiceNameW
);
3846 if (lpService
== NULL
)
3848 DPRINT1("Could not find a service!\n");
3850 /* If the service could not be found and lpcchBuffer is 0, windows
3851 puts null in lpDisplayName and puts 1 in lpcchBuffer */
3852 if (*lpcchBuffer
== 0)
3855 if (lpDisplayName
!= NULL
)
3857 *lpDisplayName
= '\0';
3860 return ERROR_SERVICE_DOES_NOT_EXIST
;
3863 if (!lpService
->lpDisplayName
)
3865 dwLength
= wcslen(lpService
->lpServiceName
);
3866 if (lpDisplayName
!= NULL
&&
3867 *lpcchBuffer
> dwLength
)
3869 WideCharToMultiByte(CP_ACP
,
3871 lpService
->lpServiceName
,
3872 wcslen(lpService
->lpServiceName
),
3877 return ERROR_SUCCESS
;
3882 dwLength
= wcslen(lpService
->lpDisplayName
);
3883 if (lpDisplayName
!= NULL
&&
3884 *lpcchBuffer
> dwLength
)
3886 WideCharToMultiByte(CP_ACP
,
3888 lpService
->lpDisplayName
,
3889 wcslen(lpService
->lpDisplayName
),
3894 return ERROR_SUCCESS
;
3898 dwError
= (*lpcchBuffer
> dwLength
) ? ERROR_SUCCESS
: ERROR_INSUFFICIENT_BUFFER
;
3900 *lpcchBuffer
= dwLength
* 2;
3907 DWORD
RGetServiceKeyNameA(
3908 SC_RPC_HANDLE hSCManager
,
3909 LPCSTR lpDisplayName
,
3910 LPSTR lpServiceName
,
3911 LPBOUNDED_DWORD_4K lpcchBuffer
)
3916 LPWSTR lpDisplayNameW
;
3918 DPRINT("RGetServiceKeyNameA() called\n");
3919 DPRINT("hSCManager = %p\n", hSCManager
);
3920 DPRINT("lpDisplayName: %s\n", lpDisplayName
);
3921 DPRINT("lpServiceName: %p\n", lpServiceName
);
3922 DPRINT("*lpcchBuffer: %lu\n", *lpcchBuffer
);
3924 dwLength
= strlen(lpDisplayName
) + 1;
3925 lpDisplayNameW
= HeapAlloc(GetProcessHeap(),
3927 dwLength
* sizeof(WCHAR
));
3928 if (!lpDisplayNameW
)
3929 return ERROR_NOT_ENOUGH_MEMORY
;
3931 MultiByteToWideChar(CP_ACP
,
3938 lpService
= ScmGetServiceEntryByDisplayName(lpDisplayNameW
);
3940 HeapFree(GetProcessHeap(), 0, lpDisplayNameW
);
3942 if (lpService
== NULL
)
3944 DPRINT1("Could not find the service!\n");
3946 /* If the service could not be found and lpcchBuffer is 0,
3947 put null in lpDisplayName and puts 1 in lpcchBuffer, verified WINXP. */
3948 if (*lpcchBuffer
== 0)
3951 if (lpServiceName
!= NULL
)
3953 *lpServiceName
= '\0';
3957 return ERROR_SERVICE_DOES_NOT_EXIST
;
3960 dwLength
= wcslen(lpService
->lpServiceName
);
3961 if (lpServiceName
!= NULL
&&
3962 *lpcchBuffer
> dwLength
)
3964 WideCharToMultiByte(CP_ACP
,
3966 lpService
->lpServiceName
,
3967 wcslen(lpService
->lpServiceName
),
3972 return ERROR_SUCCESS
;
3975 dwError
= (*lpcchBuffer
> dwLength
) ? ERROR_SUCCESS
: ERROR_INSUFFICIENT_BUFFER
;
3977 *lpcchBuffer
= dwLength
* 2;
3984 DWORD
RI_ScGetCurrentGroupStateW(
3985 SC_RPC_HANDLE hSCManager
,
3986 LPWSTR lpLoadOrderGroup
,
3990 return ERROR_CALL_NOT_IMPLEMENTED
;
3995 DWORD
REnumServiceGroupW(
3996 SC_RPC_HANDLE hSCManager
,
3997 DWORD dwServiceType
,
3998 DWORD dwServiceState
,
4001 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
4002 LPBOUNDED_DWORD_256K lpServicesReturned
,
4003 LPBOUNDED_DWORD_256K lpResumeIndex
,
4004 LPCWSTR pszGroupName
)
4007 return ERROR_CALL_NOT_IMPLEMENTED
;
4012 // WARNING: This function is untested
4015 DWORD
RChangeServiceConfig2A(
4016 SC_RPC_HANDLE hService
,
4017 SC_RPC_CONFIG_INFOA Info
)
4019 SC_RPC_CONFIG_INFOW InfoW
;
4020 DWORD dwRet
, dwLength
;
4023 DPRINT("RChangeServiceConfig2A() called\n");
4024 DPRINT("dwInfoLevel = %lu\n", Info
.dwInfoLevel
);
4026 InfoW
.dwInfoLevel
= Info
.dwInfoLevel
;
4028 if (InfoW
.dwInfoLevel
== SERVICE_CONFIG_DESCRIPTION
)
4030 LPSERVICE_DESCRIPTIONW lpServiceDescriptonW
;
4031 LPSERVICE_DESCRIPTIONA lpServiceDescriptonA
;
4033 lpServiceDescriptonA
= Info
.psd
;
4035 ///if (lpServiceDescriptonA &&
4036 ///lpServiceDescriptonA->lpDescription)
4038 dwLength
= (strlen(Info
.lpDescription
) + 1) * sizeof(WCHAR
);
4040 lpServiceDescriptonW
= HeapAlloc(GetProcessHeap(),
4042 dwLength
+ sizeof(SERVICE_DESCRIPTIONW
));
4043 if (!lpServiceDescriptonW
)
4045 return ERROR_NOT_ENOUGH_MEMORY
;
4048 lpServiceDescriptonW
->lpDescription
= (LPWSTR
)(lpServiceDescriptonW
+ 1);
4050 MultiByteToWideChar(CP_ACP
,
4054 lpServiceDescriptonW
->lpDescription
,
4057 ptr
= lpServiceDescriptonW
;
4058 InfoW
.psd
= lpServiceDescriptonW
;
4061 else if (Info
.dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
4063 LPSERVICE_FAILURE_ACTIONSW lpServiceFailureActionsW
;
4064 LPSERVICE_FAILURE_ACTIONSA lpServiceFailureActionsA
;
4065 DWORD dwRebootLen
= 0;
4066 DWORD dwCommandLen
= 0;
4068 lpServiceFailureActionsA
= Info
.psfa
;
4070 if (lpServiceFailureActionsA
)
4072 if (lpServiceFailureActionsA
->lpRebootMsg
)
4074 dwRebootLen
= (strlen(lpServiceFailureActionsA
->lpRebootMsg
) + 1) * sizeof(WCHAR
);
4076 if (lpServiceFailureActionsA
->lpCommand
)
4078 dwCommandLen
= (strlen(lpServiceFailureActionsA
->lpCommand
) + 1) * sizeof(WCHAR
);
4080 dwLength
= dwRebootLen
+ dwCommandLen
+ sizeof(SERVICE_FAILURE_ACTIONSW
);
4082 lpServiceFailureActionsW
= HeapAlloc(GetProcessHeap(),
4085 if (!lpServiceFailureActionsW
)
4087 return ERROR_NOT_ENOUGH_MEMORY
;
4090 lpServiceFailureActionsW
->cActions
= lpServiceFailureActionsA
->cActions
;
4091 lpServiceFailureActionsW
->dwResetPeriod
= lpServiceFailureActionsA
->dwResetPeriod
;
4092 CopyMemory(lpServiceFailureActionsW
->lpsaActions
, lpServiceFailureActionsA
->lpsaActions
, sizeof(SC_ACTION
));
4094 if (lpServiceFailureActionsA
->lpRebootMsg
)
4096 MultiByteToWideChar(CP_ACP
,
4098 lpServiceFailureActionsA
->lpRebootMsg
,
4100 lpServiceFailureActionsW
->lpRebootMsg
,
4104 if (lpServiceFailureActionsA
->lpCommand
)
4106 MultiByteToWideChar(CP_ACP
,
4108 lpServiceFailureActionsA
->lpCommand
,
4110 lpServiceFailureActionsW
->lpCommand
,
4114 ptr
= lpServiceFailureActionsW
;
4118 dwRet
= RChangeServiceConfig2W(hService
, InfoW
);
4120 HeapFree(GetProcessHeap(), 0, ptr
);
4127 DWORD
RChangeServiceConfig2W(
4128 SC_RPC_HANDLE hService
,
4129 SC_RPC_CONFIG_INFOW Info
)
4131 DWORD dwError
= ERROR_SUCCESS
;
4132 PSERVICE_HANDLE hSvc
;
4133 PSERVICE lpService
= NULL
;
4134 HKEY hServiceKey
= NULL
;
4136 DPRINT("RChangeServiceConfig2W() called\n");
4137 DPRINT("dwInfoLevel = %lu\n", Info
.dwInfoLevel
);
4140 return ERROR_SHUTDOWN_IN_PROGRESS
;
4142 hSvc
= (PSERVICE_HANDLE
)hService
;
4143 if (!hSvc
|| hSvc
->Handle
.Tag
!= SERVICE_TAG
)
4145 DPRINT1("Invalid handle tag!\n");
4146 return ERROR_INVALID_HANDLE
;
4149 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
4150 SERVICE_CHANGE_CONFIG
))
4152 DPRINT1("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
4153 return ERROR_ACCESS_DENIED
;
4156 lpService
= hSvc
->ServiceEntry
;
4157 if (lpService
== NULL
)
4159 DPRINT1("lpService == NULL!\n");
4160 return ERROR_INVALID_HANDLE
;
4163 /* FIXME: Lock database exclusively */
4165 if (lpService
->bDeleted
)
4167 /* FIXME: Unlock database */
4168 DPRINT1("The service has already been marked for delete!\n");
4169 return ERROR_SERVICE_MARKED_FOR_DELETE
;
4172 /* Open the service key */
4173 dwError
= ScmOpenServiceKey(lpService
->szServiceName
,
4176 if (dwError
!= ERROR_SUCCESS
)
4179 if (Info
.dwInfoLevel
== SERVICE_CONFIG_DESCRIPTION
)
4181 LPSERVICE_DESCRIPTIONW lpServiceDescription
;
4183 lpServiceDescription
= (LPSERVICE_DESCRIPTIONW
)Info
.psd
;
4184 lpServiceDescription
->lpDescription
= (LPWSTR
)((ULONG_PTR
)lpServiceDescription
+ sizeof(LPSERVICE_DESCRIPTIONW
));
4186 if (lpServiceDescription
!= NULL
&&
4187 lpServiceDescription
->lpDescription
!= NULL
)
4189 DPRINT1("Setting value %S\n", lpServiceDescription
->lpDescription
);
4190 RegSetValueExW(hServiceKey
,
4194 (LPBYTE
)lpServiceDescription
->lpDescription
,
4195 (wcslen(lpServiceDescription
->lpDescription
) + 1) * sizeof(WCHAR
));
4197 if (dwError
!= ERROR_SUCCESS
)
4201 else if (Info
.dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
4204 dwError
= ERROR_CALL_NOT_IMPLEMENTED
;
4209 /* FIXME: Unlock database */
4210 if (hServiceKey
!= NULL
)
4211 RegCloseKey(hServiceKey
);
4213 DPRINT("RChangeServiceConfig2W() done (Error %lu)\n", dwError
);
4220 DWORD
RQueryServiceConfig2A(
4221 SC_RPC_HANDLE hService
,
4225 LPBOUNDED_DWORD_8K pcbBytesNeeded
)
4227 DWORD dwError
= ERROR_SUCCESS
;
4228 PSERVICE_HANDLE hSvc
;
4229 PSERVICE lpService
= NULL
;
4230 HKEY hServiceKey
= NULL
;
4231 LPWSTR lpDescriptionW
= NULL
;
4232 LPSTR lpDescription
= NULL
;
4234 DPRINT1("RQueryServiceConfig2A() called hService %p dwInfoLevel %u, lpBuffer %p cbBufSize %u pcbBytesNeeded %p\n",hService
, dwInfoLevel
, lpBuffer
, cbBufSize
, pcbBytesNeeded
);
4237 return ERROR_INVALID_ADDRESS
;
4240 return ERROR_SHUTDOWN_IN_PROGRESS
;
4242 hSvc
= (PSERVICE_HANDLE
)hService
;
4243 if (!hSvc
|| hSvc
->Handle
.Tag
!= SERVICE_TAG
)
4245 DPRINT1("Invalid handle tag!\n");
4246 return ERROR_INVALID_HANDLE
;
4249 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
4250 SERVICE_QUERY_CONFIG
))
4252 DPRINT1("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
4253 return ERROR_ACCESS_DENIED
;
4256 lpService
= hSvc
->ServiceEntry
;
4257 if (lpService
== NULL
)
4259 DPRINT1("lpService == NULL!\n");
4260 return ERROR_INVALID_HANDLE
;
4263 /* FIXME: Lock the service database shared */
4265 dwError
= ScmOpenServiceKey(lpService
->lpServiceName
,
4268 if (dwError
!= ERROR_SUCCESS
)
4271 if (dwInfoLevel
& SERVICE_CONFIG_DESCRIPTION
)
4273 LPSERVICE_DESCRIPTIONA lpServiceDescription
= (LPSERVICE_DESCRIPTIONA
)lpBuffer
;
4276 *pcbBytesNeeded
= sizeof(SERVICE_DESCRIPTIONA
);
4278 dwError
= ScmReadString(hServiceKey
,
4281 if (dwError
== ERROR_SUCCESS
)
4283 *pcbBytesNeeded
+= ((wcslen(lpDescriptionW
) + 1) * sizeof(WCHAR
));
4286 if (cbBufSize
>= *pcbBytesNeeded
)
4289 if (dwError
== ERROR_SUCCESS
)
4291 lpStr
= (LPSTR
)(lpServiceDescription
+ 1);
4293 WideCharToMultiByte(CP_ACP
,
4298 wcslen(lpDescriptionW
),
4301 lpServiceDescription
->lpDescription
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceDescription
);
4305 lpServiceDescription
->lpDescription
= NULL
;
4311 dwError
= ERROR_INSUFFICIENT_BUFFER
;
4315 else if (dwInfoLevel
& SERVICE_CONFIG_FAILURE_ACTIONS
)
4318 dwError
= ERROR_CALL_NOT_IMPLEMENTED
;
4323 if (lpDescription
!= NULL
)
4324 HeapFree(GetProcessHeap(), 0, lpDescription
);
4326 if (hServiceKey
!= NULL
)
4327 RegCloseKey(hServiceKey
);
4329 /* FIXME: Unlock database */
4331 DPRINT("RQueryServiceConfig2W() done (Error %lu)\n", dwError
);
4338 DWORD
RQueryServiceConfig2W(
4339 SC_RPC_HANDLE hService
,
4343 LPBOUNDED_DWORD_8K pcbBytesNeeded
)
4345 DWORD dwError
= ERROR_SUCCESS
;
4346 PSERVICE_HANDLE hSvc
;
4347 PSERVICE lpService
= NULL
;
4348 HKEY hServiceKey
= NULL
;
4349 DWORD dwRequiredSize
;
4350 LPWSTR lpDescription
= NULL
;
4351 LPWSTR lpFailureCommand
= NULL
;
4352 LPWSTR lpRebootMessage
= NULL
;
4354 DPRINT("RQueryServiceConfig2W() called\n");
4357 return ERROR_INVALID_ADDRESS
;
4360 return ERROR_SHUTDOWN_IN_PROGRESS
;
4362 hSvc
= (PSERVICE_HANDLE
)hService
;
4363 if (!hSvc
|| hSvc
->Handle
.Tag
!= SERVICE_TAG
)
4365 DPRINT1("Invalid handle tag!\n");
4366 return ERROR_INVALID_HANDLE
;
4369 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
4370 SERVICE_QUERY_CONFIG
))
4372 DPRINT1("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
4373 return ERROR_ACCESS_DENIED
;
4376 lpService
= hSvc
->ServiceEntry
;
4377 if (lpService
== NULL
)
4379 DPRINT1("lpService == NULL!\n");
4380 return ERROR_INVALID_HANDLE
;
4383 /* FIXME: Lock the service database shared */
4385 dwError
= ScmOpenServiceKey(lpService
->lpServiceName
,
4388 if (dwError
!= ERROR_SUCCESS
)
4391 if (dwInfoLevel
& SERVICE_CONFIG_DESCRIPTION
)
4393 LPSERVICE_DESCRIPTIONW lpServiceDescription
= (LPSERVICE_DESCRIPTIONW
)lpBuffer
;
4396 dwError
= ScmReadString(hServiceKey
,
4399 if (dwError
!= ERROR_SUCCESS
)
4402 dwRequiredSize
= sizeof(SERVICE_DESCRIPTIONW
) + ((wcslen(lpDescription
) + 1) * sizeof(WCHAR
));
4404 if (cbBufSize
< dwRequiredSize
)
4406 *pcbBytesNeeded
= dwRequiredSize
;
4407 dwError
= ERROR_INSUFFICIENT_BUFFER
;
4411 lpStr
= (LPWSTR
)(lpServiceDescription
+ 1);
4412 wcscpy(lpStr
, lpDescription
);
4413 lpServiceDescription
->lpDescription
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceDescription
);
4415 else if (dwInfoLevel
& SERVICE_CONFIG_FAILURE_ACTIONS
)
4418 LPSERVICE_FAILURE_ACTIONSW lpFailureActions
= (LPSERVICE_FAILURE_ACTIONSW
)lpBuffer
;
4422 dwError
= ScmReadString(hServiceKey
,
4426 dwError
= ScmReadString(hServiceKey
,
4430 dwRequiredSize
= sizeof(SERVICE_FAILURE_ACTIONSW
);
4432 if (lpFailureCommand
)
4433 dwRequiredSize
+= (wcslen(lpFailureCommand
) + 1) * sizeof(WCHAR
);
4435 if (lpRebootMessage
)
4436 dwRequiredSize
+= (wcslen(lpRebootMessage
) + 1) * sizeof(WCHAR
);
4438 if (cbBufSize
< dwRequiredSize
)
4440 *pcbBytesNeeded
= dwRequiredSize
;
4441 dwError
= ERROR_INSUFFICIENT_BUFFER
;
4445 lpFailureActions
->cActions
= 0;
4446 lpFailureActions
->dwResetPeriod
= 0;
4447 lpFailureActions
->lpCommand
= NULL
;
4448 lpFailureActions
->lpRebootMsg
= NULL
;
4449 lpFailureActions
->lpsaActions
= NULL
;
4451 lpStr
= (LPWSTR
)(lpFailureActions
+ 1);
4452 if (lpRebootMessage
)
4454 wcscpy(lpStr
, lpRebootMessage
);
4455 lpFailureActions
->lpRebootMsg
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpRebootMessage
);
4456 lpStr
+= wcslen(lpRebootMessage
) + 1;
4459 if (lpFailureCommand
)
4461 wcscpy(lpStr
, lpFailureCommand
);
4462 lpFailureActions
->lpCommand
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpFailureCommand
);
4463 lpStr
+= wcslen(lpRebootMessage
) + 1;
4465 dwError
= STATUS_SUCCESS
;
4470 if (lpDescription
!= NULL
)
4471 HeapFree(GetProcessHeap(), 0, lpDescription
);
4473 if (lpRebootMessage
!= NULL
)
4474 HeapFree(GetProcessHeap(), 0, lpRebootMessage
);
4476 if (lpFailureCommand
!= NULL
)
4477 HeapFree(GetProcessHeap(), 0, lpFailureCommand
);
4479 if (hServiceKey
!= NULL
)
4480 RegCloseKey(hServiceKey
);
4482 /* FIXME: Unlock database */
4484 DPRINT("RQueryServiceConfig2W() done (Error %lu)\n", dwError
);
4491 DWORD
RQueryServiceStatusEx(
4492 SC_RPC_HANDLE hService
,
4493 SC_STATUS_TYPE InfoLevel
,
4496 LPBOUNDED_DWORD_8K pcbBytesNeeded
)
4498 LPSERVICE_STATUS_PROCESS lpStatus
;
4499 PSERVICE_HANDLE hSvc
;
4502 DPRINT("RQueryServiceStatusEx() called\n");
4505 return ERROR_SHUTDOWN_IN_PROGRESS
;
4507 if (InfoLevel
!= SC_STATUS_PROCESS_INFO
)
4508 return ERROR_INVALID_LEVEL
;
4510 *pcbBytesNeeded
= sizeof(SERVICE_STATUS_PROCESS
);
4512 if (cbBufSize
< sizeof(SERVICE_STATUS_PROCESS
))
4513 return ERROR_INSUFFICIENT_BUFFER
;
4515 hSvc
= (PSERVICE_HANDLE
)hService
;
4516 if (!hSvc
|| hSvc
->Handle
.Tag
!= SERVICE_TAG
)
4518 DPRINT1("Invalid handle tag!\n");
4519 return ERROR_INVALID_HANDLE
;
4522 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
4523 SERVICE_QUERY_STATUS
))
4525 DPRINT1("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
4526 return ERROR_ACCESS_DENIED
;
4529 lpService
= hSvc
->ServiceEntry
;
4530 if (lpService
== NULL
)
4532 DPRINT1("lpService == NULL!\n");
4533 return ERROR_INVALID_HANDLE
;
4536 lpStatus
= (LPSERVICE_STATUS_PROCESS
)lpBuffer
;
4538 /* Return service status information */
4539 RtlCopyMemory(lpStatus
,
4541 sizeof(SERVICE_STATUS
));
4543 lpStatus
->dwProcessId
= lpService
->ProcessId
; /* FIXME */
4544 lpStatus
->dwServiceFlags
= 0; /* FIXME */
4546 return ERROR_SUCCESS
;
4551 DWORD
REnumServicesStatusExA(
4552 SC_RPC_HANDLE hSCManager
,
4553 SC_ENUM_TYPE InfoLevel
,
4554 DWORD dwServiceType
,
4555 DWORD dwServiceState
,
4558 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
4559 LPBOUNDED_DWORD_256K lpServicesReturned
,
4560 LPBOUNDED_DWORD_256K lpResumeIndex
,
4561 LPCSTR pszGroupName
)
4563 LPENUM_SERVICE_STATUS_PROCESSW lpStatusPtrW
= NULL
;
4564 LPENUM_SERVICE_STATUS_PROCESSA lpStatusPtrA
= NULL
;
4565 LPWSTR lpStringPtrW
;
4567 LPWSTR pszGroupNameW
= NULL
;
4569 DWORD dwServiceCount
;
4571 DPRINT("REnumServicesStatusExA() called\n");
4575 pszGroupNameW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (strlen(pszGroupName
) + 1) * sizeof(WCHAR
));
4578 DPRINT1("Failed to allocate buffer!\n");
4579 return ERROR_NOT_ENOUGH_MEMORY
;
4581 MultiByteToWideChar(CP_ACP
,
4586 strlen(pszGroupName
) + 1);
4589 if ((cbBufSize
> 0) && (lpBuffer
))
4591 lpStatusPtrW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, cbBufSize
);
4594 DPRINT1("Failed to allocate buffer!\n");
4595 return ERROR_NOT_ENOUGH_MEMORY
;
4599 dwError
= REnumServicesStatusExW(hSCManager
,
4603 (LPBYTE
)lpStatusPtrW
,
4610 /* if no services were returned then we are Done */
4611 if (*lpServicesReturned
== 0) goto Done
;
4613 lpStatusPtrA
= (LPENUM_SERVICE_STATUS_PROCESSA
)lpBuffer
;
4614 lpStringPtrA
= (LPSTR
)((ULONG_PTR
)lpBuffer
+
4615 *lpServicesReturned
* sizeof(ENUM_SERVICE_STATUS_PROCESSA
));
4616 lpStringPtrW
= (LPWSTR
)((ULONG_PTR
)lpStatusPtrW
+
4617 *lpServicesReturned
* sizeof(ENUM_SERVICE_STATUS_PROCESSW
));
4619 for (dwServiceCount
= 0; dwServiceCount
< *lpServicesReturned
; dwServiceCount
++)
4621 /* Copy the service name */
4622 WideCharToMultiByte(CP_ACP
,
4627 wcslen(lpStringPtrW
),
4631 lpStatusPtrA
->lpServiceName
= (LPSTR
)((ULONG_PTR
)lpStringPtrA
- (ULONG_PTR
)lpBuffer
);
4632 lpStringPtrA
+= wcslen(lpStringPtrW
) + 1;
4633 lpStringPtrW
+= wcslen(lpStringPtrW
) + 1;
4635 /* Copy the display name */
4636 WideCharToMultiByte(CP_ACP
,
4641 wcslen(lpStringPtrW
),
4645 lpStatusPtrA
->lpDisplayName
= (LPSTR
)((ULONG_PTR
)lpStringPtrA
- (ULONG_PTR
)lpBuffer
);
4646 lpStringPtrA
+= wcslen(lpStringPtrW
) + 1;
4647 lpStringPtrW
+= wcslen(lpStringPtrW
) + 1;
4649 /* Copy the status information */
4650 memcpy(&lpStatusPtrA
->ServiceStatusProcess
,
4651 &lpStatusPtrW
->ServiceStatusProcess
,
4652 sizeof(SERVICE_STATUS
));
4654 lpStatusPtrA
->ServiceStatusProcess
.dwProcessId
= lpStatusPtrW
->ServiceStatusProcess
.dwProcessId
; /* FIXME */
4655 lpStatusPtrA
->ServiceStatusProcess
.dwServiceFlags
= 0; /* FIXME */
4660 if (pszGroupNameW
) HeapFree(GetProcessHeap(), 0, pszGroupNameW
);
4662 if (lpStatusPtrW
) HeapFree(GetProcessHeap(), 0, lpStatusPtrW
);
4664 DPRINT("REnumServicesStatusExA() done (Error %lu)\n", dwError
);
4671 DWORD
REnumServicesStatusExW(
4672 SC_RPC_HANDLE hSCManager
,
4673 SC_ENUM_TYPE InfoLevel
,
4674 DWORD dwServiceType
,
4675 DWORD dwServiceState
,
4678 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
4679 LPBOUNDED_DWORD_256K lpServicesReturned
,
4680 LPBOUNDED_DWORD_256K lpResumeIndex
,
4681 LPCWSTR pszGroupName
)
4683 PMANAGER_HANDLE hManager
;
4685 DWORD dwError
= ERROR_SUCCESS
;
4686 PLIST_ENTRY ServiceEntry
;
4687 PSERVICE CurrentService
;
4689 DWORD dwRequiredSize
;
4690 DWORD dwServiceCount
;
4692 DWORD dwLastResumeCount
= 0;
4693 LPENUM_SERVICE_STATUS_PROCESSW lpStatusPtr
;
4696 DPRINT("REnumServicesStatusExW() called\n");
4699 return ERROR_SHUTDOWN_IN_PROGRESS
;
4701 if (InfoLevel
!= SC_ENUM_PROCESS_INFO
)
4702 return ERROR_INVALID_LEVEL
;
4704 hManager
= (PMANAGER_HANDLE
)hSCManager
;
4705 if (!hManager
|| hManager
->Handle
.Tag
!= MANAGER_TAG
)
4707 DPRINT1("Invalid manager handle!\n");
4708 return ERROR_INVALID_HANDLE
;
4711 *pcbBytesNeeded
= 0;
4712 *lpServicesReturned
= 0;
4714 if ((dwServiceType
!=SERVICE_DRIVER
) && (dwServiceType
!=SERVICE_WIN32
))
4716 DPRINT1("Not a valid Service Type!\n");
4717 return ERROR_INVALID_PARAMETER
;
4720 if ((dwServiceState
<SERVICE_ACTIVE
) || (dwServiceState
>SERVICE_STATE_ALL
))
4722 DPRINT1("Not a valid Service State!\n");
4723 return ERROR_INVALID_PARAMETER
;
4726 /* Check access rights */
4727 if (!RtlAreAllAccessesGranted(hManager
->Handle
.DesiredAccess
,
4728 SC_MANAGER_ENUMERATE_SERVICE
))
4730 DPRINT1("Insufficient access rights! 0x%lx\n",
4731 hManager
->Handle
.DesiredAccess
);
4732 return ERROR_ACCESS_DENIED
;
4735 if (lpResumeIndex
) dwLastResumeCount
= *lpResumeIndex
;
4737 /* Lock the service list shared */
4739 lpService
= ScmGetServiceEntryByResumeCount(dwLastResumeCount
);
4740 if (lpService
== NULL
)
4742 dwError
= ERROR_SUCCESS
;
4749 for (ServiceEntry
= &lpService
->ServiceListEntry
;
4750 ServiceEntry
!= &ServiceListHead
;
4751 ServiceEntry
= ServiceEntry
->Flink
)
4753 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
4757 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
4760 dwState
= SERVICE_ACTIVE
;
4761 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
4762 dwState
= SERVICE_INACTIVE
;
4764 if ((dwState
& dwServiceState
) == 0)
4769 if (*pszGroupName
== 0)
4771 if (CurrentService
->lpGroup
!= NULL
)
4776 if ((CurrentService
->lpGroup
== NULL
) ||
4777 _wcsicmp(pszGroupName
, CurrentService
->lpGroup
->lpGroupName
))
4782 dwSize
= sizeof(ENUM_SERVICE_STATUS_PROCESSW
) +
4783 ((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
4784 ((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
4786 if (dwRequiredSize
+ dwSize
<= cbBufSize
)
4788 DPRINT("Service name: %S fit\n", CurrentService
->lpServiceName
);
4789 dwRequiredSize
+= dwSize
;
4791 dwLastResumeCount
= CurrentService
->dwResumeCount
;
4795 DPRINT("Service name: %S no fit\n", CurrentService
->lpServiceName
);
4801 DPRINT("dwRequiredSize: %lu\n", dwRequiredSize
);
4802 DPRINT("dwServiceCount: %lu\n", dwServiceCount
);
4805 ServiceEntry
!= &ServiceListHead
;
4806 ServiceEntry
= ServiceEntry
->Flink
)
4808 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
4812 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
4815 dwState
= SERVICE_ACTIVE
;
4816 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
4817 dwState
= SERVICE_INACTIVE
;
4819 if ((dwState
& dwServiceState
) == 0)
4824 if (*pszGroupName
== 0)
4826 if (CurrentService
->lpGroup
!= NULL
)
4831 if ((CurrentService
->lpGroup
== NULL
) ||
4832 _wcsicmp(pszGroupName
, CurrentService
->lpGroup
->lpGroupName
))
4837 dwRequiredSize
+= (sizeof(ENUM_SERVICE_STATUS_PROCESSW
) +
4838 ((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
4839 ((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
)));
4841 dwError
= ERROR_MORE_DATA
;
4844 DPRINT("*pcbBytesNeeded: %lu\n", dwRequiredSize
);
4846 if (lpResumeIndex
) *lpResumeIndex
= dwLastResumeCount
;
4847 *lpServicesReturned
= dwServiceCount
;
4848 *pcbBytesNeeded
= dwRequiredSize
;
4850 /* If there was no services that matched */
4851 if ((!dwServiceCount
) && (dwError
!= ERROR_MORE_DATA
))
4853 dwError
= ERROR_SERVICE_DOES_NOT_EXIST
;
4857 lpStatusPtr
= (LPENUM_SERVICE_STATUS_PROCESSW
)lpBuffer
;
4858 lpStringPtr
= (LPWSTR
)((ULONG_PTR
)lpBuffer
+
4859 dwServiceCount
* sizeof(ENUM_SERVICE_STATUS_PROCESSW
));
4862 for (ServiceEntry
= &lpService
->ServiceListEntry
;
4863 ServiceEntry
!= &ServiceListHead
;
4864 ServiceEntry
= ServiceEntry
->Flink
)
4866 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
4870 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
4873 dwState
= SERVICE_ACTIVE
;
4874 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
4875 dwState
= SERVICE_INACTIVE
;
4877 if ((dwState
& dwServiceState
) == 0)
4882 if (*pszGroupName
== 0)
4884 if (CurrentService
->lpGroup
!= NULL
)
4889 if ((CurrentService
->lpGroup
== NULL
) ||
4890 _wcsicmp(pszGroupName
, CurrentService
->lpGroup
->lpGroupName
))
4895 dwSize
= sizeof(ENUM_SERVICE_STATUS_PROCESSW
) +
4896 ((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
4897 ((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
4899 if (dwRequiredSize
+ dwSize
<= cbBufSize
)
4901 /* Copy the service name */
4903 CurrentService
->lpServiceName
);
4904 lpStatusPtr
->lpServiceName
= (LPWSTR
)((ULONG_PTR
)lpStringPtr
- (ULONG_PTR
)lpBuffer
);
4905 lpStringPtr
+= (wcslen(CurrentService
->lpServiceName
) + 1);
4907 /* Copy the display name */
4909 CurrentService
->lpDisplayName
);
4910 lpStatusPtr
->lpDisplayName
= (LPWSTR
)((ULONG_PTR
)lpStringPtr
- (ULONG_PTR
)lpBuffer
);
4911 lpStringPtr
+= (wcslen(CurrentService
->lpDisplayName
) + 1);
4913 /* Copy the status information */
4914 memcpy(&lpStatusPtr
->ServiceStatusProcess
,
4915 &CurrentService
->Status
,
4916 sizeof(SERVICE_STATUS
));
4917 lpStatusPtr
->ServiceStatusProcess
.dwProcessId
= CurrentService
->ProcessId
; /* FIXME */
4918 lpStatusPtr
->ServiceStatusProcess
.dwServiceFlags
= 0; /* FIXME */
4921 dwRequiredSize
+= dwSize
;
4931 *pcbBytesNeeded
= 0;
4932 if (lpResumeIndex
) *lpResumeIndex
= 0;
4936 /* Unlock the service list */
4938 DPRINT("REnumServicesStatusExW() done (Error %lu)\n", dwError
);
4945 DWORD
RSendTSMessage(
4946 handle_t BindingHandle
) /* FIXME */
4949 return ERROR_CALL_NOT_IMPLEMENTED
;
4954 DWORD
RCreateServiceWOW64A(
4955 handle_t BindingHandle
,
4956 LPSTR lpServiceName
,
4957 LPSTR lpDisplayName
,
4958 DWORD dwDesiredAccess
,
4959 DWORD dwServiceType
,
4961 DWORD dwErrorControl
,
4962 LPSTR lpBinaryPathName
,
4963 LPSTR lpLoadOrderGroup
,
4965 LPBYTE lpDependencies
,
4967 LPSTR lpServiceStartName
,
4970 LPSC_RPC_HANDLE lpServiceHandle
)
4973 return ERROR_CALL_NOT_IMPLEMENTED
;
4978 DWORD
RCreateServiceWOW64W(
4979 handle_t BindingHandle
,
4980 LPWSTR lpServiceName
,
4981 LPWSTR lpDisplayName
,
4982 DWORD dwDesiredAccess
,
4983 DWORD dwServiceType
,
4985 DWORD dwErrorControl
,
4986 LPWSTR lpBinaryPathName
,
4987 LPWSTR lpLoadOrderGroup
,
4989 LPBYTE lpDependencies
,
4991 LPWSTR lpServiceStartName
,
4994 LPSC_RPC_HANDLE lpServiceHandle
)
4997 return ERROR_CALL_NOT_IMPLEMENTED
;
5002 DWORD
RQueryServiceTagInfo(
5003 handle_t BindingHandle
) /* FIXME */
5006 return ERROR_CALL_NOT_IMPLEMENTED
;
5011 DWORD
RNotifyServiceStatusChange(
5012 SC_RPC_HANDLE hService
,
5013 SC_RPC_NOTIFY_PARAMS NotifyParams
,
5014 GUID
*pClientProcessGuid
,
5015 GUID
*pSCMProcessGuid
,
5016 PBOOL pfCreateRemoteQueue
,
5017 LPSC_NOTIFY_RPC_HANDLE phNotify
)
5020 return ERROR_CALL_NOT_IMPLEMENTED
;
5025 DWORD
RGetNotifyResults(
5026 SC_NOTIFY_RPC_HANDLE hNotify
,
5027 PSC_RPC_NOTIFY_PARAMS_LIST
*ppNotifyParams
)
5030 return ERROR_CALL_NOT_IMPLEMENTED
;
5035 DWORD
RCloseNotifyHandle(
5036 LPSC_NOTIFY_RPC_HANDLE phNotify
,
5040 return ERROR_CALL_NOT_IMPLEMENTED
;
5045 DWORD
RControlServiceExA(
5046 SC_RPC_HANDLE hService
,
5051 return ERROR_CALL_NOT_IMPLEMENTED
;
5056 DWORD
RControlServiceExW(
5057 SC_RPC_HANDLE hService
,
5062 return ERROR_CALL_NOT_IMPLEMENTED
;
5067 DWORD
RSendPnPMessage(
5068 handle_t BindingHandle
) /* FIXME */
5071 return ERROR_CALL_NOT_IMPLEMENTED
;
5076 DWORD
RValidatePnPService(
5077 handle_t BindingHandle
) /* FIXME */
5080 return ERROR_CALL_NOT_IMPLEMENTED
;
5085 DWORD
ROpenServiceStatusHandle(
5086 handle_t BindingHandle
) /* FIXME */
5089 return ERROR_CALL_NOT_IMPLEMENTED
;
5095 handle_t BindingHandle
) /* FIXME */
5098 return ERROR_CALL_NOT_IMPLEMENTED
;
5102 void __RPC_FAR
* __RPC_USER
midl_user_allocate(SIZE_T len
)
5104 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
);
5108 void __RPC_USER
midl_user_free(void __RPC_FAR
* ptr
)
5110 HeapFree(GetProcessHeap(), 0, ptr
);
5114 void __RPC_USER
SC_RPC_HANDLE_rundown(SC_RPC_HANDLE hSCObject
)
5119 void __RPC_USER
SC_RPC_LOCK_rundown(SC_RPC_LOCK Lock
)
5124 void __RPC_USER
SC_NOTIFY_RPC_HANDLE_rundown(SC_NOTIFY_RPC_HANDLE hNotify
)