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 ****************************************************************/
21 /* GLOBALS *****************************************************************/
23 #define MANAGER_TAG 0x72674D68 /* 'hMgr' */
24 #define SERVICE_TAG 0x63765368 /* 'hSvc' */
25 #define INVALID_TAG 0xAABBCCDD
27 typedef struct _SCMGR_HANDLE
34 typedef struct _MANAGER_HANDLE
37 WCHAR DatabaseName
[1];
38 } MANAGER_HANDLE
, *PMANAGER_HANDLE
;
41 typedef struct _SERVICE_HANDLE
44 PSERVICE ServiceEntry
;
45 } SERVICE_HANDLE
, *PSERVICE_HANDLE
;
48 #define SC_MANAGER_READ \
49 (STANDARD_RIGHTS_READ | \
50 SC_MANAGER_QUERY_LOCK_STATUS | \
51 SC_MANAGER_ENUMERATE_SERVICE)
53 #define SC_MANAGER_WRITE \
54 (STANDARD_RIGHTS_WRITE | \
55 SC_MANAGER_MODIFY_BOOT_CONFIG | \
56 SC_MANAGER_CREATE_SERVICE)
58 #define SC_MANAGER_EXECUTE \
59 (STANDARD_RIGHTS_EXECUTE | \
61 SC_MANAGER_ENUMERATE_SERVICE | \
62 SC_MANAGER_CONNECT | \
63 SC_MANAGER_CREATE_SERVICE)
66 #define SERVICE_READ \
67 (STANDARD_RIGHTS_READ | \
68 SERVICE_INTERROGATE | \
69 SERVICE_ENUMERATE_DEPENDENTS | \
70 SERVICE_QUERY_STATUS | \
73 #define SERVICE_WRITE \
74 (STANDARD_RIGHTS_WRITE | \
75 SERVICE_CHANGE_CONFIG)
77 #define SERVICE_EXECUTE \
78 (STANDARD_RIGHTS_EXECUTE | \
79 SERVICE_USER_DEFINED_CONTROL | \
80 SERVICE_PAUSE_CONTINUE | \
84 #define TAG_ARRAY_SIZE 32
86 /* VARIABLES ***************************************************************/
88 static GENERIC_MAPPING
89 ScmManagerMapping
= {SC_MANAGER_READ
,
92 SC_MANAGER_ALL_ACCESS
};
94 static GENERIC_MAPPING
95 ScmServiceMapping
= {SERVICE_READ
,
100 DWORD g_dwServiceBits
= 0;
102 /* FUNCTIONS ***************************************************************/
105 ScmStartRpcServer(VOID
)
109 DPRINT("ScmStartRpcServer() called\n");
111 Status
= RpcServerUseProtseqEpW(L
"ncacn_np",
115 if (Status
!= RPC_S_OK
)
117 DPRINT1("RpcServerUseProtseqEpW() failed (Status %lx)\n", Status
);
121 Status
= RpcServerRegisterIf(svcctl_v2_0_s_ifspec
,
124 if (Status
!= RPC_S_OK
)
126 DPRINT1("RpcServerRegisterIf() failed (Status %lx)\n", Status
);
130 Status
= RpcServerListen(1, 20, TRUE
);
131 if (Status
!= RPC_S_OK
)
133 DPRINT1("RpcServerListen() failed (Status %lx)\n", Status
);
137 DPRINT("ScmStartRpcServer() done\n");
142 ScmCreateManagerHandle(LPWSTR lpDatabaseName
,
147 if (lpDatabaseName
== NULL
)
148 lpDatabaseName
= SERVICES_ACTIVE_DATABASEW
;
150 if (_wcsicmp(lpDatabaseName
, SERVICES_FAILED_DATABASEW
) == 0)
152 DPRINT("Database %S, does not exist\n", lpDatabaseName
);
153 return ERROR_DATABASE_DOES_NOT_EXIST
;
155 else if (_wcsicmp(lpDatabaseName
, SERVICES_ACTIVE_DATABASEW
) != 0)
157 DPRINT("Invalid Database name %S.\n", lpDatabaseName
);
158 return ERROR_INVALID_NAME
;
161 Ptr
= HeapAlloc(GetProcessHeap(),
163 FIELD_OFFSET(MANAGER_HANDLE
, DatabaseName
[wcslen(lpDatabaseName
) + 1]));
165 return ERROR_NOT_ENOUGH_MEMORY
;
167 Ptr
->Handle
.Tag
= MANAGER_TAG
;
169 wcscpy(Ptr
->DatabaseName
, lpDatabaseName
);
171 *Handle
= (SC_HANDLE
)Ptr
;
173 return ERROR_SUCCESS
;
178 ScmCreateServiceHandle(PSERVICE lpServiceEntry
,
183 Ptr
= HeapAlloc(GetProcessHeap(),
185 sizeof(SERVICE_HANDLE
));
187 return ERROR_NOT_ENOUGH_MEMORY
;
189 Ptr
->Handle
.Tag
= SERVICE_TAG
;
191 Ptr
->ServiceEntry
= lpServiceEntry
;
193 *Handle
= (SC_HANDLE
)Ptr
;
195 return ERROR_SUCCESS
;
199 static PMANAGER_HANDLE
200 ScmGetServiceManagerFromHandle(SC_RPC_HANDLE Handle
)
202 PMANAGER_HANDLE pManager
= NULL
;
206 if (((PMANAGER_HANDLE
)Handle
)->Handle
.Tag
== MANAGER_TAG
)
207 pManager
= (PMANAGER_HANDLE
)Handle
;
209 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
211 DPRINT1("Exception: Invalid Service Manager handle!\n");
219 static PSERVICE_HANDLE
220 ScmGetServiceFromHandle(SC_RPC_HANDLE Handle
)
222 PSERVICE_HANDLE pService
= NULL
;
226 if (((PSERVICE_HANDLE
)Handle
)->Handle
.Tag
== SERVICE_TAG
)
227 pService
= (PSERVICE_HANDLE
)Handle
;
229 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
231 DPRINT1("Exception: Invalid Service handle!\n");
240 ScmCheckAccess(SC_HANDLE Handle
,
241 DWORD dwDesiredAccess
)
243 PMANAGER_HANDLE hMgr
;
245 hMgr
= (PMANAGER_HANDLE
)Handle
;
246 if (hMgr
->Handle
.Tag
== MANAGER_TAG
)
248 RtlMapGenericMask(&dwDesiredAccess
,
251 hMgr
->Handle
.DesiredAccess
= dwDesiredAccess
;
253 return ERROR_SUCCESS
;
255 else if (hMgr
->Handle
.Tag
== SERVICE_TAG
)
257 RtlMapGenericMask(&dwDesiredAccess
,
260 hMgr
->Handle
.DesiredAccess
= dwDesiredAccess
;
262 return ERROR_SUCCESS
;
265 return ERROR_INVALID_HANDLE
;
270 ScmAssignNewTag(PSERVICE lpService
)
274 DWORD dwGroupTagCount
= 0;
275 PDWORD pdwGroupTags
= NULL
;
277 DWORD dwTagUsedBase
= 1;
278 BOOLEAN TagUsed
[TAG_ARRAY_SIZE
];
282 PLIST_ENTRY ServiceEntry
;
283 PSERVICE CurrentService
;
285 ASSERT(lpService
!= NULL
);
286 ASSERT(lpService
->lpGroup
!= NULL
);
288 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
289 L
"System\\CurrentControlSet\\Control\\GroupOrderList",
294 if (dwError
!= ERROR_SUCCESS
)
297 /* query value length */
299 dwError
= RegQueryValueExW(hKey
,
300 lpService
->lpGroup
->szGroupName
,
306 if (dwError
!= ERROR_SUCCESS
&& dwError
!= ERROR_MORE_DATA
)
309 pdwGroupTags
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, cbDataSize
);
312 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
316 dwError
= RegQueryValueExW(hKey
,
317 lpService
->lpGroup
->szGroupName
,
320 (LPBYTE
)pdwGroupTags
,
323 if (dwError
!= ERROR_SUCCESS
)
326 if (cbDataSize
< sizeof(pdwGroupTags
[0]))
329 dwGroupTagCount
= min(pdwGroupTags
[0], cbDataSize
/ sizeof(pdwGroupTags
[0]) - 1);
334 /* mark all tags as unused */
335 for (i
= 0; i
< TAG_ARRAY_SIZE
; i
++)
338 /* mark tags in GroupOrderList as used */
339 for (i
= 1; i
<= dwGroupTagCount
; i
++)
341 nTagOffset
= pdwGroupTags
[i
] - dwTagUsedBase
;
342 if (nTagOffset
>= 0 && nTagOffset
< TAG_ARRAY_SIZE
)
343 TagUsed
[nTagOffset
] = TRUE
;
346 /* mark tags in service list as used */
347 ServiceEntry
= lpService
->ServiceListEntry
.Flink
;
348 while (ServiceEntry
!= &lpService
->ServiceListEntry
)
350 ASSERT(ServiceEntry
!= NULL
);
351 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
352 if (CurrentService
->lpGroup
== lpService
->lpGroup
)
354 nTagOffset
= CurrentService
->dwTag
- dwTagUsedBase
;
355 if (nTagOffset
>= 0 && nTagOffset
< TAG_ARRAY_SIZE
)
356 TagUsed
[nTagOffset
] = TRUE
;
359 ServiceEntry
= ServiceEntry
->Flink
;
362 /* find unused tag, if any */
363 for (i
= 0; i
< TAG_ARRAY_SIZE
; i
++)
367 dwFreeTag
= dwTagUsedBase
+ i
;
372 dwTagUsedBase
+= TAG_ARRAY_SIZE
;
373 } while (!dwFreeTag
);
377 HeapFree(GetProcessHeap(), 0, pdwGroupTags
);
384 lpService
->dwTag
= dwFreeTag
;
385 DPRINT("Assigning new tag %lu to service %S in group %S\n",
386 lpService
->dwTag
, lpService
->lpServiceName
, lpService
->lpGroup
->szGroupName
);
387 dwError
= ERROR_SUCCESS
;
391 DPRINT1("Failed to assign new tag to service %S, error=%lu\n",
392 lpService
->lpServiceName
, dwError
);
399 /* Create a path suitable for the bootloader out of the full path */
401 ScmConvertToBootPathName(wchar_t *CanonName
, wchar_t **RelativeName
)
403 SIZE_T ServiceNameLen
, ExpandedLen
;
407 UNICODE_STRING NtPathName
, SystemRoot
, LinkTarget
;
408 OBJECT_ATTRIBUTES ObjectAttributes
;
410 HANDLE SymbolicLinkHandle
;
412 DPRINT("ScmConvertToBootPathName %S\n", CanonName
);
415 return ERROR_INVALID_PARAMETER
;
417 *RelativeName
= NULL
;
419 ServiceNameLen
= wcslen(CanonName
);
421 /* First check, if it's already good */
422 if (ServiceNameLen
> 12 &&
423 !_wcsnicmp(L
"\\SystemRoot\\", CanonName
, 12))
425 *RelativeName
= HeapAlloc(GetProcessHeap(),
427 (ServiceNameLen
+ 1) * sizeof(WCHAR
));
428 if (*RelativeName
== NULL
)
430 DPRINT("Error allocating memory for boot driver name!\n");
431 return ERROR_NOT_ENOUGH_MEMORY
;
435 wcscpy(*RelativeName
, CanonName
);
437 DPRINT("Bootdriver name %S\n", *RelativeName
);
438 return ERROR_SUCCESS
;
441 /* If it has %SystemRoot% prefix, substitute it to \System*/
442 if (ServiceNameLen
> 13 &&
443 !_wcsnicmp(L
"%SystemRoot%\\", CanonName
, 13))
445 /* There is no +sizeof(wchar_t) because the name is less by 1 wchar */
446 *RelativeName
= HeapAlloc(GetProcessHeap(),
448 ServiceNameLen
* sizeof(WCHAR
));
450 if (*RelativeName
== NULL
)
452 DPRINT("Error allocating memory for boot driver name!\n");
453 return ERROR_NOT_ENOUGH_MEMORY
;
457 wcscpy(*RelativeName
, L
"\\SystemRoot\\");
458 wcscat(*RelativeName
, CanonName
+ 13);
460 DPRINT("Bootdriver name %S\n", *RelativeName
);
461 return ERROR_SUCCESS
;
464 /* Get buffer size needed for expanding env strings */
465 BufferSize
= ExpandEnvironmentStringsW(L
"%SystemRoot%\\", &Dest
, 1);
468 DPRINT("Error during a call to ExpandEnvironmentStringsW()\n");
469 return ERROR_INVALID_ENVIRONMENT
;
472 /* Allocate memory, since the size is known now */
473 Expanded
= HeapAlloc(GetProcessHeap(),
475 (BufferSize
+ 1) * sizeof(WCHAR
));
478 DPRINT("Error allocating memory for boot driver name!\n");
479 return ERROR_NOT_ENOUGH_MEMORY
;
483 if (ExpandEnvironmentStringsW(L
"%SystemRoot%\\", Expanded
, BufferSize
) >
486 DPRINT("Error during a call to ExpandEnvironmentStringsW()\n");
487 HeapFree(GetProcessHeap(), 0, Expanded
);
488 return ERROR_NOT_ENOUGH_MEMORY
;
491 /* Convert to NT-style path */
492 if (!RtlDosPathNameToNtPathName_U(Expanded
, &NtPathName
, NULL
, NULL
))
494 DPRINT("Error during a call to RtlDosPathNameToNtPathName_U()\n");
495 return ERROR_INVALID_ENVIRONMENT
;
498 DPRINT("Converted to NT-style %wZ\n", &NtPathName
);
500 /* No need to keep the dos-path anymore */
501 HeapFree(GetProcessHeap(), 0, Expanded
);
503 /* Copy it to the allocated place */
504 Expanded
= HeapAlloc(GetProcessHeap(),
506 NtPathName
.Length
+ sizeof(UNICODE_NULL
));
509 DPRINT("Error allocating memory for boot driver name!\n");
510 RtlFreeUnicodeString(&NtPathName
);
511 return ERROR_NOT_ENOUGH_MEMORY
;
514 ExpandedLen
= NtPathName
.Length
/ sizeof(WCHAR
);
515 wcsncpy(Expanded
, NtPathName
.Buffer
, ExpandedLen
);
516 Expanded
[ExpandedLen
] = UNICODE_NULL
;
517 RtlFreeUnicodeString(&NtPathName
);
519 if (ServiceNameLen
> ExpandedLen
&&
520 !_wcsnicmp(Expanded
, CanonName
, ExpandedLen
))
522 HeapFree(GetProcessHeap(), 0, Expanded
);
524 /* Only \SystemRoot\ is missing */
525 *RelativeName
= HeapAlloc(GetProcessHeap(),
527 (ServiceNameLen
- ExpandedLen
) * sizeof(WCHAR
) + 13*sizeof(WCHAR
));
528 if (*RelativeName
== NULL
)
530 DPRINT("Error allocating memory for boot driver name!\n");
531 return ERROR_NOT_ENOUGH_MEMORY
;
534 wcscpy(*RelativeName
, L
"\\SystemRoot\\");
535 wcscat(*RelativeName
, CanonName
+ ExpandedLen
);
537 return ERROR_SUCCESS
;
540 /* No longer need this */
541 HeapFree(GetProcessHeap(), 0, Expanded
);
543 /* The most complex case starts here */
544 RtlInitUnicodeString(&SystemRoot
, L
"\\SystemRoot");
545 InitializeObjectAttributes(&ObjectAttributes
,
547 OBJ_CASE_INSENSITIVE
,
551 /* Open this symlink */
552 Status
= NtOpenSymbolicLinkObject(&SymbolicLinkHandle
, SYMBOLIC_LINK_QUERY
, &ObjectAttributes
);
553 if (NT_SUCCESS(Status
))
555 DPRINT("Opened symbolic link object\n");
557 RtlInitEmptyUnicodeString(&LinkTarget
, NULL
, 0);
558 Status
= NtQuerySymbolicLinkObject(SymbolicLinkHandle
, &LinkTarget
, &BufferSize
);
559 if (NT_SUCCESS(Status
) || Status
== STATUS_BUFFER_TOO_SMALL
)
561 /* Check if required buffer size is sane */
562 if (BufferSize
> UNICODE_STRING_MAX_BYTES
- sizeof(UNICODE_NULL
))
564 DPRINT("Too large buffer required\n");
566 NtClose(SymbolicLinkHandle
);
567 return ERROR_NOT_ENOUGH_MEMORY
;
570 /* Alloc the string */
571 LinkTarget
.Length
= (USHORT
)BufferSize
;
572 LinkTarget
.MaximumLength
= LinkTarget
.Length
+ sizeof(UNICODE_NULL
);
573 LinkTarget
.Buffer
= HeapAlloc(GetProcessHeap(),
575 LinkTarget
.MaximumLength
);
576 if (!LinkTarget
.Buffer
)
578 DPRINT("Unable to alloc buffer\n");
579 NtClose(SymbolicLinkHandle
);
580 return ERROR_NOT_ENOUGH_MEMORY
;
583 /* Do a real query now */
584 Status
= NtQuerySymbolicLinkObject(SymbolicLinkHandle
, &LinkTarget
, &BufferSize
);
585 NtClose(SymbolicLinkHandle
);
586 if (NT_SUCCESS(Status
))
588 DPRINT("LinkTarget: %wZ\n", &LinkTarget
);
590 ExpandedLen
= LinkTarget
.Length
/ sizeof(WCHAR
);
591 if ((ServiceNameLen
> ExpandedLen
) &&
592 !_wcsnicmp(LinkTarget
.Buffer
, CanonName
, ExpandedLen
))
594 *RelativeName
= HeapAlloc(GetProcessHeap(),
596 (ServiceNameLen
- ExpandedLen
) * sizeof(WCHAR
) + 13*sizeof(WCHAR
));
598 if (*RelativeName
== NULL
)
600 DPRINT("Unable to alloc buffer\n");
601 return ERROR_NOT_ENOUGH_MEMORY
;
604 /* Copy it over, substituting the first part
606 wcscpy(*RelativeName
, L
"\\SystemRoot\\");
607 wcscat(*RelativeName
, CanonName
+ExpandedLen
+1);
610 return ERROR_SUCCESS
;
614 return ERROR_INVALID_PARAMETER
;
619 DPRINT("Error, Status = %08X\n", Status
);
620 return ERROR_INVALID_PARAMETER
;
625 DPRINT("Error, Status = %08X\n", Status
);
626 NtClose(SymbolicLinkHandle
);
627 return ERROR_INVALID_PARAMETER
;
633 DPRINT("Error, Status = %08X\n", Status
);
634 return ERROR_INVALID_PARAMETER
;
640 ScmCanonDriverImagePath(DWORD dwStartType
,
641 const wchar_t *lpServiceName
,
642 wchar_t **lpCanonName
)
645 SIZE_T ServiceNameLen
;
646 UNICODE_STRING NtServiceName
;
648 const WCHAR
*SourceName
= lpServiceName
;
650 /* Calculate the length of the service's name */
651 ServiceNameLen
= wcslen(lpServiceName
);
653 /* 12 is wcslen(L"\\SystemRoot\\") */
654 if (ServiceNameLen
> 12 &&
655 !_wcsnicmp(L
"\\SystemRoot\\", lpServiceName
, 12))
657 /* SystemRoot prefix is already included */
658 *lpCanonName
= HeapAlloc(GetProcessHeap(),
660 (ServiceNameLen
+ 1) * sizeof(WCHAR
));
662 if (*lpCanonName
== NULL
)
664 DPRINT("Error allocating memory for canonized service name!\n");
665 return ERROR_NOT_ENOUGH_MEMORY
;
668 /* If it's a boot-time driver, it must be systemroot relative */
669 if (dwStartType
== SERVICE_BOOT_START
)
673 wcscpy(*lpCanonName
, SourceName
);
675 DPRINT("Canonicalized name %S\n", *lpCanonName
);
679 /* Check if it has %SystemRoot% (len=13) */
680 if (ServiceNameLen
> 13 &&
681 !_wcsnicmp(L
"%SystemRoot%\\", lpServiceName
, 13))
683 /* Substitute %SystemRoot% with \\SystemRoot\\ */
684 *lpCanonName
= HeapAlloc(GetProcessHeap(),
686 (ServiceNameLen
+ 1) * sizeof(WCHAR
));
688 if (*lpCanonName
== NULL
)
690 DPRINT("Error allocating memory for canonized service name!\n");
691 return ERROR_NOT_ENOUGH_MEMORY
;
694 /* If it's a boot-time driver, it must be systemroot relative */
695 if (dwStartType
== SERVICE_BOOT_START
)
696 wcscpy(*lpCanonName
, L
"\\SystemRoot\\");
698 wcscat(*lpCanonName
, lpServiceName
+ 13);
700 DPRINT("Canonicalized name %S\n", *lpCanonName
);
704 /* Check if it's a relative path name */
705 if (lpServiceName
[0] != L
'\\' && lpServiceName
[1] != L
':')
707 *lpCanonName
= HeapAlloc(GetProcessHeap(),
709 (ServiceNameLen
+ 1) * sizeof(WCHAR
));
711 if (*lpCanonName
== NULL
)
713 DPRINT("Error allocating memory for canonized service name!\n");
714 return ERROR_NOT_ENOUGH_MEMORY
;
717 /* Just copy it over without changing */
718 wcscpy(*lpCanonName
, lpServiceName
);
723 /* It seems to be a DOS path, convert it */
724 if (!RtlDosPathNameToNtPathName_U(lpServiceName
, &NtServiceName
, NULL
, NULL
))
726 DPRINT("RtlDosPathNameToNtPathName_U() failed!\n");
727 return ERROR_INVALID_PARAMETER
;
730 *lpCanonName
= HeapAlloc(GetProcessHeap(),
732 NtServiceName
.Length
+ sizeof(WCHAR
));
734 if (*lpCanonName
== NULL
)
736 DPRINT("Error allocating memory for canonized service name!\n");
737 RtlFreeUnicodeString(&NtServiceName
);
738 return ERROR_NOT_ENOUGH_MEMORY
;
741 /* Copy the string */
742 wcsncpy(*lpCanonName
, NtServiceName
.Buffer
, NtServiceName
.Length
/ sizeof(WCHAR
));
744 /* The unicode string is not needed anymore */
745 RtlFreeUnicodeString(&NtServiceName
);
747 if (dwStartType
!= SERVICE_BOOT_START
)
749 DPRINT("Canonicalized name %S\n", *lpCanonName
);
753 /* The service is boot-started, so must be relative */
754 Result
= ScmConvertToBootPathName(*lpCanonName
, &RelativeName
);
757 /* There is a problem, free name and return */
758 HeapFree(GetProcessHeap(), 0, *lpCanonName
);
759 DPRINT("Error converting named!\n");
763 ASSERT(RelativeName
);
765 /* Copy that string */
766 wcscpy(*lpCanonName
, RelativeName
+ 12);
768 /* Free the allocated buffer */
769 HeapFree(GetProcessHeap(), 0, RelativeName
);
771 DPRINT("Canonicalized name %S\n", *lpCanonName
);
778 /* Internal recursive function */
779 /* Need to search for every dependency on every service */
781 Int_EnumDependentServicesW(HKEY hServicesKey
,
783 DWORD dwServiceState
,
784 PSERVICE
*lpServices
,
785 LPDWORD pcbBytesNeeded
,
786 LPDWORD lpServicesReturned
)
788 DWORD dwError
= ERROR_SUCCESS
;
789 WCHAR szNameBuf
[MAX_PATH
];
790 WCHAR szValueBuf
[MAX_PATH
];
791 WCHAR
*lpszNameBuf
= szNameBuf
;
792 WCHAR
*lpszValueBuf
= szValueBuf
;
796 PSERVICE lpCurrentService
;
797 HKEY hServiceEnumKey
;
798 DWORD dwCurrentServiceState
= SERVICE_ACTIVE
;
799 DWORD dwDependServiceStrPtr
= 0;
800 DWORD dwRequiredSize
= 0;
802 /* Get the number of service keys */
803 dwError
= RegQueryInfoKeyW(hServicesKey
,
815 if (dwError
!= ERROR_SUCCESS
)
817 DPRINT("ERROR! Unable to get number of services keys.\n");
821 /* Iterate the service keys to see if another service depends on the this service */
822 for (dwIteration
= 0; dwIteration
< dwNumSubKeys
; dwIteration
++)
825 dwError
= RegEnumKeyExW(hServicesKey
,
833 if (dwError
!= ERROR_SUCCESS
)
836 /* Open the Service key */
837 dwError
= RegOpenKeyExW(hServicesKey
,
842 if (dwError
!= ERROR_SUCCESS
)
845 dwSize
= MAX_PATH
* sizeof(WCHAR
);
847 /* Check for the DependOnService Value */
848 dwError
= RegQueryValueExW(hServiceEnumKey
,
852 (LPBYTE
)lpszValueBuf
,
855 /* FIXME: Handle load order. */
857 /* If the service found has a DependOnService value */
858 if (dwError
== ERROR_SUCCESS
)
860 dwDependServiceStrPtr
= 0;
862 /* Can be more than one Dependencies in the DependOnService string */
863 while (wcslen(lpszValueBuf
+ dwDependServiceStrPtr
) > 0)
865 if (_wcsicmp(lpszValueBuf
+ dwDependServiceStrPtr
, lpService
->lpServiceName
) == 0)
867 /* Get the current enumed service pointer */
868 lpCurrentService
= ScmGetServiceEntryByName(lpszNameBuf
);
870 /* Check for valid Service */
871 if (!lpCurrentService
)
873 /* This should never happen! */
874 DPRINT("This should not happen at this point, report to Developer\n");
875 return ERROR_NOT_FOUND
;
878 /* Determine state the service is in */
879 if (lpCurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
880 dwCurrentServiceState
= SERVICE_INACTIVE
;
882 /* If the ServiceState matches that requested or searching for SERVICE_STATE_ALL */
883 if ((dwCurrentServiceState
== dwServiceState
) ||
884 (dwServiceState
== SERVICE_STATE_ALL
))
886 /* Calculate the required size */
887 dwRequiredSize
+= sizeof(SERVICE_STATUS
);
888 dwRequiredSize
+= (DWORD
)((wcslen(lpCurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
));
889 dwRequiredSize
+= (DWORD
)((wcslen(lpCurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
891 /* Add the size for service name and display name pointers */
892 dwRequiredSize
+= (2 * sizeof(PVOID
));
894 /* increase the BytesNeeded size */
895 *pcbBytesNeeded
= *pcbBytesNeeded
+ dwRequiredSize
;
897 /* Don't fill callers buffer yet, as MSDN read that the last service with dependency
900 /* Recursive call to check for its dependencies */
901 Int_EnumDependentServicesW(hServicesKey
,
908 /* If the lpServices is valid set the service pointer */
910 lpServices
[*lpServicesReturned
] = lpCurrentService
;
912 *lpServicesReturned
= *lpServicesReturned
+ 1;
916 dwDependServiceStrPtr
+= (DWORD
)(wcslen(lpszValueBuf
+ dwDependServiceStrPtr
) + 1);
919 else if (*pcbBytesNeeded
)
921 dwError
= ERROR_SUCCESS
;
924 RegCloseKey(hServiceEnumKey
);
935 LPSC_RPC_HANDLE hSCObject
)
937 PMANAGER_HANDLE hManager
;
938 PSERVICE_HANDLE hService
;
942 DWORD pcbBytesNeeded
= 0;
943 DWORD dwServicesReturned
= 0;
945 DPRINT("RCloseServiceHandle() called\n");
947 DPRINT("hSCObject = %p\n", *hSCObject
);
950 return ERROR_INVALID_HANDLE
;
952 hManager
= ScmGetServiceManagerFromHandle(*hSCObject
);
953 hService
= ScmGetServiceFromHandle(*hSCObject
);
955 if (hManager
!= NULL
)
957 DPRINT("Found manager handle\n");
959 /* Make sure we don't access stale memory if someone tries to use this handle again. */
960 hManager
->Handle
.Tag
= INVALID_TAG
;
962 HeapFree(GetProcessHeap(), 0, hManager
);
967 DPRINT("RCloseServiceHandle() done\n");
968 return ERROR_SUCCESS
;
970 else if (hService
!= NULL
)
972 DPRINT("Found service handle\n");
974 /* Lock the service database exclusively */
975 ScmLockDatabaseExclusive();
977 /* Get the pointer to the service record */
978 lpService
= hService
->ServiceEntry
;
980 /* Make sure we don't access stale memory if someone tries to use this handle again. */
981 hService
->Handle
.Tag
= INVALID_TAG
;
983 /* Free the handle */
984 HeapFree(GetProcessHeap(), 0, hService
);
987 ASSERT(lpService
->dwRefCount
> 0);
989 lpService
->dwRefCount
--;
990 DPRINT("CloseServiceHandle - lpService->dwRefCount %u\n",
991 lpService
->dwRefCount
);
993 if (lpService
->dwRefCount
== 0)
995 /* If this service has been marked for deletion */
996 if (lpService
->bDeleted
&&
997 lpService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
999 /* Open the Services Reg key */
1000 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
1001 L
"System\\CurrentControlSet\\Services",
1003 KEY_SET_VALUE
| KEY_READ
,
1005 if (dwError
!= ERROR_SUCCESS
)
1007 DPRINT("Failed to open services key\n");
1008 ScmUnlockDatabase();
1012 /* Call the internal function with NULL, just to get bytes we need */
1013 Int_EnumDependentServicesW(hServicesKey
,
1018 &dwServicesReturned
);
1020 /* If pcbBytesNeeded returned a value then there are services running that are dependent on this service */
1023 DPRINT("Deletion failed due to running dependencies.\n");
1024 RegCloseKey(hServicesKey
);
1025 ScmUnlockDatabase();
1026 return ERROR_SUCCESS
;
1029 /* There are no references and no running dependencies,
1030 it is now safe to delete the service */
1032 /* Delete the Service Key */
1033 dwError
= ScmDeleteRegKey(hServicesKey
,
1034 lpService
->lpServiceName
);
1036 RegCloseKey(hServicesKey
);
1038 if (dwError
!= ERROR_SUCCESS
)
1040 DPRINT("Failed to Delete the Service Registry key\n");
1041 ScmUnlockDatabase();
1045 /* Delete the Service */
1046 ScmDeleteServiceRecord(lpService
);
1050 ScmUnlockDatabase();
1054 DPRINT("RCloseServiceHandle() done\n");
1055 return ERROR_SUCCESS
;
1058 DPRINT("Invalid handle tag (Tag %lx)\n", hManager
->Handle
.Tag
);
1060 return ERROR_INVALID_HANDLE
;
1068 SC_RPC_HANDLE hService
,
1070 LPSERVICE_STATUS lpServiceStatus
)
1072 PSERVICE_HANDLE hSvc
;
1074 ACCESS_MASK DesiredAccess
;
1075 DWORD dwError
= ERROR_SUCCESS
;
1076 DWORD pcbBytesNeeded
= 0;
1077 DWORD dwServicesReturned
= 0;
1078 DWORD dwControlsAccepted
;
1079 DWORD dwCurrentState
;
1080 HKEY hServicesKey
= NULL
;
1081 LPCWSTR lpLogStrings
[2];
1082 WCHAR szLogBuffer
[80];
1085 DPRINT("RControlService() called\n");
1088 return ERROR_SHUTDOWN_IN_PROGRESS
;
1090 /* Check the service handle */
1091 hSvc
= ScmGetServiceFromHandle(hService
);
1094 DPRINT1("Invalid service handle!\n");
1095 return ERROR_INVALID_HANDLE
;
1098 /* Check the service entry point */
1099 lpService
= hSvc
->ServiceEntry
;
1100 if (lpService
== NULL
)
1102 DPRINT1("lpService == NULL!\n");
1103 return ERROR_INVALID_HANDLE
;
1106 /* Check access rights */
1109 case SERVICE_CONTROL_STOP
:
1110 DesiredAccess
= SERVICE_STOP
;
1113 case SERVICE_CONTROL_PAUSE
:
1114 case SERVICE_CONTROL_CONTINUE
:
1115 case SERVICE_CONTROL_PARAMCHANGE
:
1116 case SERVICE_CONTROL_NETBINDADD
:
1117 case SERVICE_CONTROL_NETBINDREMOVE
:
1118 case SERVICE_CONTROL_NETBINDENABLE
:
1119 case SERVICE_CONTROL_NETBINDDISABLE
:
1120 DesiredAccess
= SERVICE_PAUSE_CONTINUE
;
1123 case SERVICE_CONTROL_INTERROGATE
:
1124 DesiredAccess
= SERVICE_INTERROGATE
;
1128 if (dwControl
>= 128 && dwControl
<= 255)
1129 DesiredAccess
= SERVICE_USER_DEFINED_CONTROL
;
1131 return ERROR_INVALID_PARAMETER
;
1135 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
1137 return ERROR_ACCESS_DENIED
;
1139 /* Return the current service status information */
1140 RtlCopyMemory(lpServiceStatus
,
1142 sizeof(SERVICE_STATUS
));
1144 if (dwControl
== SERVICE_CONTROL_STOP
)
1146 /* Check if the service has dependencies running as windows
1147 doesn't stop a service that does */
1149 /* Open the Services Reg key */
1150 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
1151 L
"System\\CurrentControlSet\\Services",
1155 if (dwError
!= ERROR_SUCCESS
)
1157 DPRINT("Failed to open services key\n");
1161 /* Call the internal function with NULL, just to get bytes we need */
1162 Int_EnumDependentServicesW(hServicesKey
,
1167 &dwServicesReturned
);
1169 RegCloseKey(hServicesKey
);
1171 /* If pcbBytesNeeded is not zero then there are services running that
1172 are dependent on this service */
1173 if (pcbBytesNeeded
!= 0)
1175 DPRINT("Service has running dependencies. Failed to stop service.\n");
1176 return ERROR_DEPENDENT_SERVICES_RUNNING
;
1180 if (lpService
->Status
.dwServiceType
& SERVICE_DRIVER
)
1182 /* Send control code to the driver */
1183 dwError
= ScmControlDriver(lpService
,
1189 dwControlsAccepted
= lpService
->Status
.dwControlsAccepted
;
1190 dwCurrentState
= lpService
->Status
.dwCurrentState
;
1192 /* Return ERROR_SERVICE_NOT_ACTIVE if the service has not been started */
1193 if (lpService
->lpImage
== NULL
|| dwCurrentState
== SERVICE_STOPPED
)
1194 return ERROR_SERVICE_NOT_ACTIVE
;
1196 /* Check the current state before sending a control request */
1197 switch (dwCurrentState
)
1199 case SERVICE_STOP_PENDING
:
1200 case SERVICE_STOPPED
:
1201 return ERROR_SERVICE_CANNOT_ACCEPT_CTRL
;
1203 case SERVICE_START_PENDING
:
1206 case SERVICE_CONTROL_STOP
:
1209 case SERVICE_CONTROL_INTERROGATE
:
1210 RtlCopyMemory(lpServiceStatus
,
1212 sizeof(SERVICE_STATUS
));
1213 return ERROR_SUCCESS
;
1216 return ERROR_SERVICE_CANNOT_ACCEPT_CTRL
;
1221 /* Check if the control code is acceptable to the service */
1224 case SERVICE_CONTROL_STOP
:
1225 if ((dwControlsAccepted
& SERVICE_ACCEPT_STOP
) == 0)
1226 return ERROR_INVALID_SERVICE_CONTROL
;
1229 case SERVICE_CONTROL_PAUSE
:
1230 case SERVICE_CONTROL_CONTINUE
:
1231 if ((dwControlsAccepted
& SERVICE_ACCEPT_PAUSE_CONTINUE
) == 0)
1232 return ERROR_INVALID_SERVICE_CONTROL
;
1235 case SERVICE_CONTROL_PARAMCHANGE
:
1236 if ((dwControlsAccepted
& SERVICE_ACCEPT_PARAMCHANGE
) == 0)
1237 return ERROR_INVALID_SERVICE_CONTROL
;
1240 case SERVICE_CONTROL_NETBINDADD
:
1241 case SERVICE_CONTROL_NETBINDREMOVE
:
1242 case SERVICE_CONTROL_NETBINDENABLE
:
1243 case SERVICE_CONTROL_NETBINDDISABLE
:
1244 if ((dwControlsAccepted
& SERVICE_ACCEPT_NETBINDCHANGE
) == 0)
1245 return ERROR_INVALID_SERVICE_CONTROL
;
1249 /* Send control code to the service */
1250 dwError
= ScmControlService(lpService
->lpImage
->hControlPipe
,
1251 lpService
->lpServiceName
,
1252 (SERVICE_STATUS_HANDLE
)lpService
,
1255 /* Return service status information */
1256 RtlCopyMemory(lpServiceStatus
,
1258 sizeof(SERVICE_STATUS
));
1261 if (dwError
== ERROR_SUCCESS
)
1263 if (dwControl
== SERVICE_CONTROL_STOP
||
1264 dwControl
== SERVICE_CONTROL_PAUSE
||
1265 dwControl
== SERVICE_CONTROL_CONTINUE
)
1267 /* Log a successful send control */
1271 case SERVICE_CONTROL_STOP
:
1272 uID
= IDS_SERVICE_STOP
;
1275 case SERVICE_CONTROL_PAUSE
:
1276 uID
= IDS_SERVICE_PAUSE
;
1279 case SERVICE_CONTROL_CONTINUE
:
1280 uID
= IDS_SERVICE_RESUME
;
1283 LoadStringW(GetModuleHandle(NULL
), uID
, szLogBuffer
, ARRAYSIZE(szLogBuffer
));
1285 lpLogStrings
[0] = lpService
->lpDisplayName
;
1286 lpLogStrings
[1] = szLogBuffer
;
1288 ScmLogEvent(EVENT_SERVICE_CONTROL_SUCCESS
,
1289 EVENTLOG_INFORMATION_TYPE
,
1303 SC_RPC_HANDLE hService
)
1305 PSERVICE_HANDLE hSvc
;
1309 DPRINT("RDeleteService() called\n");
1312 return ERROR_SHUTDOWN_IN_PROGRESS
;
1314 hSvc
= ScmGetServiceFromHandle(hService
);
1317 DPRINT1("Invalid service handle!\n");
1318 return ERROR_INVALID_HANDLE
;
1321 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
1323 return ERROR_ACCESS_DENIED
;
1325 lpService
= hSvc
->ServiceEntry
;
1326 if (lpService
== NULL
)
1328 DPRINT("lpService == NULL!\n");
1329 return ERROR_INVALID_HANDLE
;
1332 /* Lock the service database exclusively */
1333 ScmLockDatabaseExclusive();
1335 if (lpService
->bDeleted
)
1337 DPRINT("The service has already been marked for delete!\n");
1338 dwError
= ERROR_SERVICE_MARKED_FOR_DELETE
;
1342 /* Mark service for delete */
1343 lpService
->bDeleted
= TRUE
;
1345 dwError
= ScmMarkServiceForDelete(lpService
);
1348 /* Unlock the service database */
1349 ScmUnlockDatabase();
1351 DPRINT("RDeleteService() done\n");
1360 RLockServiceDatabase(
1361 SC_RPC_HANDLE hSCManager
,
1362 LPSC_RPC_LOCK lpLock
)
1364 PMANAGER_HANDLE hMgr
;
1366 DPRINT("RLockServiceDatabase() called\n");
1370 hMgr
= ScmGetServiceManagerFromHandle(hSCManager
);
1373 DPRINT1("Invalid service manager handle!\n");
1374 return ERROR_INVALID_HANDLE
;
1377 if (!RtlAreAllAccessesGranted(hMgr
->Handle
.DesiredAccess
,
1379 return ERROR_ACCESS_DENIED
;
1381 return ScmAcquireServiceStartLock(FALSE
, lpLock
);
1388 RQueryServiceObjectSecurity(
1389 SC_RPC_HANDLE hService
,
1390 SECURITY_INFORMATION dwSecurityInformation
,
1391 LPBYTE lpSecurityDescriptor
,
1393 LPBOUNDED_DWORD_256K pcbBytesNeeded
)
1395 PSERVICE_HANDLE hSvc
;
1397 ULONG DesiredAccess
= 0;
1399 DWORD dwBytesNeeded
;
1402 DPRINT("RQueryServiceObjectSecurity() called\n");
1404 hSvc
= ScmGetServiceFromHandle(hService
);
1407 DPRINT1("Invalid service handle!\n");
1408 return ERROR_INVALID_HANDLE
;
1411 if (dwSecurityInformation
& (DACL_SECURITY_INFORMATION
|
1412 GROUP_SECURITY_INFORMATION
|
1413 OWNER_SECURITY_INFORMATION
))
1414 DesiredAccess
|= READ_CONTROL
;
1416 if (dwSecurityInformation
& SACL_SECURITY_INFORMATION
)
1417 DesiredAccess
|= ACCESS_SYSTEM_SECURITY
;
1419 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
1422 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
1423 return ERROR_ACCESS_DENIED
;
1426 lpService
= hSvc
->ServiceEntry
;
1427 if (lpService
== NULL
)
1429 DPRINT("lpService == NULL!\n");
1430 return ERROR_INVALID_HANDLE
;
1433 /* Lock the service database */
1434 ScmLockDatabaseShared();
1436 /* Retrieve the security descriptor */
1437 Status
= RtlQuerySecurityObject(lpService
->pSecurityDescriptor
,
1438 dwSecurityInformation
,
1439 (PSECURITY_DESCRIPTOR
)lpSecurityDescriptor
,
1443 /* Unlock the service database */
1444 ScmUnlockDatabase();
1446 if (NT_SUCCESS(Status
))
1448 *pcbBytesNeeded
= dwBytesNeeded
;
1449 dwError
= STATUS_SUCCESS
;
1451 else if (Status
== STATUS_BUFFER_TOO_SMALL
)
1453 *pcbBytesNeeded
= dwBytesNeeded
;
1454 dwError
= ERROR_INSUFFICIENT_BUFFER
;
1456 else if (Status
== STATUS_BAD_DESCRIPTOR_FORMAT
)
1458 dwError
= ERROR_GEN_FAILURE
;
1462 dwError
= RtlNtStatusToDosError(Status
);
1472 RSetServiceObjectSecurity(
1473 SC_RPC_HANDLE hService
,
1474 DWORD dwSecurityInformation
,
1475 LPBYTE lpSecurityDescriptor
,
1476 DWORD dwSecurityDescriptorSize
)
1478 PSERVICE_HANDLE hSvc
;
1480 ACCESS_MASK DesiredAccess
= 0;
1481 HANDLE hToken
= NULL
;
1482 HKEY hServiceKey
= NULL
;
1483 BOOL bDatabaseLocked
= FALSE
;
1487 DPRINT("RSetServiceObjectSecurity() called\n");
1489 hSvc
= ScmGetServiceFromHandle(hService
);
1492 DPRINT1("Invalid service handle!\n");
1493 return ERROR_INVALID_HANDLE
;
1496 if (dwSecurityInformation
== 0 ||
1497 dwSecurityInformation
& ~(OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
1498 | DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
))
1500 return ERROR_INVALID_PARAMETER
;
1503 if (!RtlValidSecurityDescriptor((PSECURITY_DESCRIPTOR
)lpSecurityDescriptor
))
1504 return ERROR_INVALID_PARAMETER
;
1506 if (dwSecurityInformation
& SACL_SECURITY_INFORMATION
)
1507 DesiredAccess
|= ACCESS_SYSTEM_SECURITY
;
1509 if (dwSecurityInformation
& DACL_SECURITY_INFORMATION
)
1510 DesiredAccess
|= WRITE_DAC
;
1512 if (dwSecurityInformation
& (OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
))
1513 DesiredAccess
|= WRITE_OWNER
;
1515 if ((dwSecurityInformation
& OWNER_SECURITY_INFORMATION
) &&
1516 (((PISECURITY_DESCRIPTOR
)lpSecurityDescriptor
)->Owner
== NULL
))
1518 return ERROR_INVALID_PARAMETER
;
1521 if ((dwSecurityInformation
& GROUP_SECURITY_INFORMATION
) &&
1522 (((PISECURITY_DESCRIPTOR
)lpSecurityDescriptor
)->Group
== NULL
))
1524 return ERROR_INVALID_PARAMETER
;
1527 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
1530 DPRINT1("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
1531 return ERROR_ACCESS_DENIED
;
1534 lpService
= hSvc
->ServiceEntry
;
1535 if (lpService
== NULL
)
1537 DPRINT1("lpService == NULL!\n");
1538 return ERROR_INVALID_HANDLE
;
1541 if (lpService
->bDeleted
)
1542 return ERROR_SERVICE_MARKED_FOR_DELETE
;
1545 RpcImpersonateClient(NULL
);
1547 Status
= NtOpenThreadToken(NtCurrentThread(),
1551 if (!NT_SUCCESS(Status
))
1552 return RtlNtStatusToDosError(Status
);
1557 /* Build the new security descriptor */
1558 Status
= RtlSetSecurityObject(dwSecurityInformation
,
1559 (PSECURITY_DESCRIPTOR
)lpSecurityDescriptor
,
1560 &lpService
->pSecurityDescriptor
,
1563 if (!NT_SUCCESS(Status
))
1565 dwError
= RtlNtStatusToDosError(Status
);
1569 /* Lock the service database exclusive */
1570 ScmLockDatabaseExclusive();
1571 bDatabaseLocked
= TRUE
;
1573 /* Open the service key */
1574 dwError
= ScmOpenServiceKey(lpService
->lpServiceName
,
1575 READ_CONTROL
| KEY_CREATE_SUB_KEY
| KEY_SET_VALUE
,
1577 if (dwError
!= ERROR_SUCCESS
)
1580 /* Store the new security descriptor */
1581 dwError
= ScmWriteSecurityDescriptor(hServiceKey
,
1582 lpService
->pSecurityDescriptor
);
1584 RegFlushKey(hServiceKey
);
1587 if (hServiceKey
!= NULL
)
1588 RegCloseKey(hServiceKey
);
1590 /* Unlock service database */
1591 if (bDatabaseLocked
== TRUE
)
1592 ScmUnlockDatabase();
1597 DPRINT("RSetServiceObjectSecurity() done (Error %lu)\n", dwError
);
1606 RQueryServiceStatus(
1607 SC_RPC_HANDLE hService
,
1608 LPSERVICE_STATUS lpServiceStatus
)
1610 PSERVICE_HANDLE hSvc
;
1613 DPRINT("RQueryServiceStatus() called\n");
1616 return ERROR_SHUTDOWN_IN_PROGRESS
;
1618 hSvc
= ScmGetServiceFromHandle(hService
);
1621 DPRINT1("Invalid service handle!\n");
1622 return ERROR_INVALID_HANDLE
;
1625 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
1626 SERVICE_QUERY_STATUS
))
1628 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
1629 return ERROR_ACCESS_DENIED
;
1632 lpService
= hSvc
->ServiceEntry
;
1633 if (lpService
== NULL
)
1635 DPRINT("lpService == NULL!\n");
1636 return ERROR_INVALID_HANDLE
;
1639 /* Lock the service database shared */
1640 ScmLockDatabaseShared();
1642 /* Return service status information */
1643 RtlCopyMemory(lpServiceStatus
,
1645 sizeof(SERVICE_STATUS
));
1647 /* Unlock the service database */
1648 ScmUnlockDatabase();
1650 return ERROR_SUCCESS
;
1655 ScmIsValidServiceState(DWORD dwCurrentState
)
1657 switch (dwCurrentState
)
1659 case SERVICE_STOPPED
:
1660 case SERVICE_START_PENDING
:
1661 case SERVICE_STOP_PENDING
:
1662 case SERVICE_RUNNING
:
1663 case SERVICE_CONTINUE_PENDING
:
1664 case SERVICE_PAUSE_PENDING
:
1665 case SERVICE_PAUSED
:
1678 RPC_SERVICE_STATUS_HANDLE hServiceStatus
,
1679 LPSERVICE_STATUS lpServiceStatus
)
1682 DWORD dwPreviousState
;
1683 DWORD dwPreviousType
;
1684 LPCWSTR lpLogStrings
[2];
1685 WCHAR szLogBuffer
[80];
1688 DPRINT("RSetServiceStatus() called\n");
1689 DPRINT("hServiceStatus = %lu\n", hServiceStatus
);
1690 DPRINT("dwServiceType = 0x%lx\n", lpServiceStatus
->dwServiceType
);
1691 DPRINT("dwCurrentState = %lu\n", lpServiceStatus
->dwCurrentState
);
1692 DPRINT("dwControlsAccepted = %lu\n", lpServiceStatus
->dwControlsAccepted
);
1693 DPRINT("dwWin32ExitCode = %lu\n", lpServiceStatus
->dwWin32ExitCode
);
1694 DPRINT("dwServiceSpecificExitCode = %lu\n", lpServiceStatus
->dwServiceSpecificExitCode
);
1695 DPRINT("dwCheckPoint = %lu\n", lpServiceStatus
->dwCheckPoint
);
1696 DPRINT("dwWaitHint = %lu\n", lpServiceStatus
->dwWaitHint
);
1698 if (hServiceStatus
== 0)
1700 DPRINT("hServiceStatus == NULL!\n");
1701 return ERROR_INVALID_HANDLE
;
1704 lpService
= (PSERVICE
)hServiceStatus
;
1706 /* Check current state */
1707 if (!ScmIsValidServiceState(lpServiceStatus
->dwCurrentState
))
1709 DPRINT("Invalid service state!\n");
1710 return ERROR_INVALID_DATA
;
1713 /* Check service type */
1714 if (!(lpServiceStatus
->dwServiceType
& SERVICE_WIN32
) &&
1715 (lpServiceStatus
->dwServiceType
& SERVICE_DRIVER
))
1717 DPRINT("Invalid service type!\n");
1718 return ERROR_INVALID_DATA
;
1721 /* Check accepted controls */
1722 if (lpServiceStatus
->dwControlsAccepted
& ~0xFF)
1724 DPRINT("Invalid controls accepted!\n");
1725 return ERROR_INVALID_DATA
;
1728 /* Set the wait hint and check point only if the service is in a pending state,
1729 otherwise they should be 0 */
1730 if (lpServiceStatus
->dwCurrentState
== SERVICE_STOPPED
||
1731 lpServiceStatus
->dwCurrentState
== SERVICE_PAUSED
||
1732 lpServiceStatus
->dwCurrentState
== SERVICE_RUNNING
)
1734 lpServiceStatus
->dwWaitHint
= 0;
1735 lpServiceStatus
->dwCheckPoint
= 0;
1738 /* Lock the service database exclusively */
1739 ScmLockDatabaseExclusive();
1741 /* Save the current service state */
1742 dwPreviousState
= lpService
->Status
.dwCurrentState
;
1744 /* Save the current service type */
1745 dwPreviousType
= lpService
->Status
.dwServiceType
;
1747 /* Update the service status */
1748 RtlCopyMemory(&lpService
->Status
,
1750 sizeof(SERVICE_STATUS
));
1752 /* Restore the previous service type */
1753 lpService
->Status
.dwServiceType
= dwPreviousType
;
1755 /* Dereference a stopped service */
1756 if ((lpServiceStatus
->dwServiceType
& SERVICE_WIN32
) &&
1757 (lpServiceStatus
->dwCurrentState
== SERVICE_STOPPED
))
1759 /* Decrement the image run counter */
1760 lpService
->lpImage
->dwImageRunCount
--;
1762 /* If we just stopped the last running service... */
1763 if (lpService
->lpImage
->dwImageRunCount
== 0)
1765 /* Stop the dispatcher thread */
1766 ScmControlService(lpService
->lpImage
->hControlPipe
,
1768 (SERVICE_STATUS_HANDLE
)lpService
,
1769 SERVICE_CONTROL_STOP
);
1771 /* Remove the service image */
1772 ScmRemoveServiceImage(lpService
->lpImage
);
1773 lpService
->lpImage
= NULL
;
1777 /* Unlock the service database */
1778 ScmUnlockDatabase();
1780 if ((lpServiceStatus
->dwCurrentState
== SERVICE_STOPPED
) &&
1781 (dwPreviousState
!= SERVICE_STOPPED
) &&
1782 (lpServiceStatus
->dwWin32ExitCode
!= ERROR_SUCCESS
))
1784 /* Log a failed service stop */
1785 StringCchPrintfW(szLogBuffer
, ARRAYSIZE(szLogBuffer
),
1786 L
"%lu", lpServiceStatus
->dwWin32ExitCode
);
1787 lpLogStrings
[0] = lpService
->lpDisplayName
;
1788 lpLogStrings
[1] = szLogBuffer
;
1790 ScmLogEvent(EVENT_SERVICE_EXIT_FAILED
,
1791 EVENTLOG_ERROR_TYPE
,
1795 else if (lpServiceStatus
->dwCurrentState
!= dwPreviousState
&&
1796 (lpServiceStatus
->dwCurrentState
== SERVICE_STOPPED
||
1797 lpServiceStatus
->dwCurrentState
== SERVICE_RUNNING
||
1798 lpServiceStatus
->dwCurrentState
== SERVICE_PAUSED
))
1800 /* Log a successful service status change */
1801 switch(lpServiceStatus
->dwCurrentState
)
1803 case SERVICE_STOPPED
:
1804 uID
= IDS_SERVICE_STOPPED
;
1807 case SERVICE_RUNNING
:
1808 uID
= IDS_SERVICE_RUNNING
;
1811 case SERVICE_PAUSED
:
1812 uID
= IDS_SERVICE_PAUSED
;
1816 LoadStringW(GetModuleHandle(NULL
), uID
, szLogBuffer
, ARRAYSIZE(szLogBuffer
));
1817 lpLogStrings
[0] = lpService
->lpDisplayName
;
1818 lpLogStrings
[1] = szLogBuffer
;
1820 ScmLogEvent(EVENT_SERVICE_STATUS_SUCCESS
,
1821 EVENTLOG_INFORMATION_TYPE
,
1826 DPRINT("Set %S to %lu\n", lpService
->lpDisplayName
, lpService
->Status
.dwCurrentState
);
1827 DPRINT("RSetServiceStatus() done\n");
1829 return ERROR_SUCCESS
;
1836 RUnlockServiceDatabase(
1839 DPRINT("RUnlockServiceDatabase(%p)\n", Lock
);
1840 return ScmReleaseServiceStartLock(Lock
);
1847 RNotifyBootConfigStatus(
1848 SVCCTL_HANDLEW lpMachineName
,
1849 DWORD BootAcceptable
)
1851 DPRINT1("RNotifyBootConfigStatus(%p %lu) called\n", lpMachineName
, BootAcceptable
);
1852 return ERROR_SUCCESS
;
1855 // return ERROR_CALL_NOT_IMPLEMENTED;
1862 RI_ScSetServiceBitsW(
1863 RPC_SERVICE_STATUS_HANDLE hServiceStatus
,
1864 DWORD dwServiceBits
,
1866 int bUpdateImmediately
,
1871 DPRINT("RI_ScSetServiceBitsW(%p %lx %d %d %S)\n",
1872 hServiceStatus
, dwServiceBits
, bSetBitsOn
,
1873 bUpdateImmediately
, lpString
);
1876 return ERROR_SHUTDOWN_IN_PROGRESS
;
1878 if (lpString
!= NULL
)
1879 return ERROR_INVALID_PARAMETER
;
1881 if (hServiceStatus
== 0)
1883 DPRINT("hServiceStatus == NULL!\n");
1884 return ERROR_INVALID_HANDLE
;
1887 // FIXME: Validate the status handle
1888 pService
= (PSERVICE
)hServiceStatus
;
1892 DPRINT("Old service bits: %08lx\n", pService
->dwServiceBits
);
1893 DPRINT("Old global service bits: %08lx\n", g_dwServiceBits
);
1894 pService
->dwServiceBits
|= dwServiceBits
;
1895 g_dwServiceBits
|= dwServiceBits
;
1896 DPRINT("New service bits: %08lx\n", pService
->dwServiceBits
);
1897 DPRINT("New global service bits: %08lx\n", g_dwServiceBits
);
1901 DPRINT("Old service bits: %08lx\n", pService
->dwServiceBits
);
1902 DPRINT("Old global service bits: %08lx\n", g_dwServiceBits
);
1903 pService
->dwServiceBits
&= ~dwServiceBits
;
1904 g_dwServiceBits
&= ~dwServiceBits
;
1905 DPRINT("New service bits: %08lx\n", pService
->dwServiceBits
);
1906 DPRINT("New global service bits: %08lx\n", g_dwServiceBits
);
1909 return ERROR_SUCCESS
;
1916 RChangeServiceConfigW(
1917 SC_RPC_HANDLE hService
,
1918 DWORD dwServiceType
,
1920 DWORD dwErrorControl
,
1921 LPWSTR lpBinaryPathName
,
1922 LPWSTR lpLoadOrderGroup
,
1924 LPBYTE lpDependencies
,
1926 LPWSTR lpServiceStartName
,
1929 LPWSTR lpDisplayName
)
1931 DWORD dwError
= ERROR_SUCCESS
;
1932 PSERVICE_HANDLE hSvc
;
1933 PSERVICE lpService
= NULL
;
1934 HKEY hServiceKey
= NULL
;
1935 LPWSTR lpDisplayNameW
= NULL
;
1936 LPWSTR lpImagePathW
= NULL
;
1937 LPWSTR lpClearTextPassword
= NULL
;
1939 DPRINT("RChangeServiceConfigW() called\n");
1940 DPRINT("dwServiceType = 0x%lx\n", dwServiceType
);
1941 DPRINT("dwStartType = %lu\n", dwStartType
);
1942 DPRINT("dwErrorControl = %lu\n", dwErrorControl
);
1943 DPRINT("lpBinaryPathName = %S\n", lpBinaryPathName
);
1944 DPRINT("lpLoadOrderGroup = %S\n", lpLoadOrderGroup
);
1945 DPRINT("lpServiceStartName = %S\n", lpServiceStartName
);
1946 DPRINT("lpPassword = %p\n", lpPassword
);
1947 DPRINT("dwPwSite = %lu\n", dwPwSize
);
1948 DPRINT("lpDisplayName = %S\n", lpDisplayName
);
1951 return ERROR_SHUTDOWN_IN_PROGRESS
;
1953 hSvc
= ScmGetServiceFromHandle(hService
);
1956 DPRINT1("Invalid service handle!\n");
1957 return ERROR_INVALID_HANDLE
;
1960 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
1961 SERVICE_CHANGE_CONFIG
))
1963 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
1964 return ERROR_ACCESS_DENIED
;
1967 /* Check for invalid service type value */
1968 if ((dwServiceType
!= SERVICE_NO_CHANGE
) &&
1969 (dwServiceType
!= SERVICE_KERNEL_DRIVER
) &&
1970 (dwServiceType
!= SERVICE_FILE_SYSTEM_DRIVER
) &&
1971 ((dwServiceType
& ~SERVICE_INTERACTIVE_PROCESS
) != SERVICE_WIN32_OWN_PROCESS
) &&
1972 ((dwServiceType
& ~SERVICE_INTERACTIVE_PROCESS
) != SERVICE_WIN32_SHARE_PROCESS
))
1974 return ERROR_INVALID_PARAMETER
;
1977 /* Check for invalid start type value */
1978 if ((dwStartType
!= SERVICE_NO_CHANGE
) &&
1979 (dwStartType
!= SERVICE_BOOT_START
) &&
1980 (dwStartType
!= SERVICE_SYSTEM_START
) &&
1981 (dwStartType
!= SERVICE_AUTO_START
) &&
1982 (dwStartType
!= SERVICE_DEMAND_START
) &&
1983 (dwStartType
!= SERVICE_DISABLED
))
1985 return ERROR_INVALID_PARAMETER
;
1988 /* Only drivers can be boot start or system start services */
1989 if ((dwStartType
== SERVICE_BOOT_START
) ||
1990 (dwStartType
== SERVICE_SYSTEM_START
))
1992 if ((dwServiceType
!= SERVICE_KERNEL_DRIVER
) &&
1993 (dwServiceType
!= SERVICE_FILE_SYSTEM_DRIVER
))
1994 return ERROR_INVALID_PARAMETER
;
1997 /* Check for invalid error control value */
1998 if ((dwErrorControl
!= SERVICE_NO_CHANGE
) &&
1999 (dwErrorControl
!= SERVICE_ERROR_IGNORE
) &&
2000 (dwErrorControl
!= SERVICE_ERROR_NORMAL
) &&
2001 (dwErrorControl
!= SERVICE_ERROR_SEVERE
) &&
2002 (dwErrorControl
!= SERVICE_ERROR_CRITICAL
))
2004 return ERROR_INVALID_PARAMETER
;
2007 if (lpdwTagId
&& (!lpLoadOrderGroup
|| !*lpLoadOrderGroup
))
2009 return ERROR_INVALID_PARAMETER
;
2012 lpService
= hSvc
->ServiceEntry
;
2013 if (lpService
== NULL
)
2015 DPRINT("lpService == NULL!\n");
2016 return ERROR_INVALID_HANDLE
;
2019 /* Lock the service database exclusively */
2020 ScmLockDatabaseExclusive();
2022 if (lpService
->bDeleted
)
2024 DPRINT("The service has already been marked for delete!\n");
2025 dwError
= ERROR_SERVICE_MARKED_FOR_DELETE
;
2029 /* Open the service key */
2030 dwError
= ScmOpenServiceKey(lpService
->szServiceName
,
2033 if (dwError
!= ERROR_SUCCESS
)
2036 /* Write service data to the registry */
2038 /* Set the display name */
2039 if (lpDisplayName
!= NULL
&& *lpDisplayName
!= 0)
2041 RegSetValueExW(hServiceKey
,
2045 (LPBYTE
)lpDisplayName
,
2046 (DWORD
)((wcslen(lpDisplayName
) + 1) * sizeof(WCHAR
)));
2048 /* Update the display name */
2049 lpDisplayNameW
= HeapAlloc(GetProcessHeap(),
2051 (wcslen(lpDisplayName
) + 1) * sizeof(WCHAR
));
2052 if (lpDisplayNameW
== NULL
)
2054 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
2058 wcscpy(lpDisplayNameW
, lpDisplayName
);
2059 if (lpService
->lpDisplayName
!= lpService
->lpServiceName
)
2060 HeapFree(GetProcessHeap(), 0, lpService
->lpDisplayName
);
2062 lpService
->lpDisplayName
= lpDisplayNameW
;
2065 if (dwServiceType
!= SERVICE_NO_CHANGE
)
2067 /* Set the service type */
2068 dwError
= RegSetValueExW(hServiceKey
,
2072 (LPBYTE
)&dwServiceType
,
2074 if (dwError
!= ERROR_SUCCESS
)
2077 lpService
->Status
.dwServiceType
= dwServiceType
;
2080 if (dwStartType
!= SERVICE_NO_CHANGE
)
2082 /* Set the start value */
2083 dwError
= RegSetValueExW(hServiceKey
,
2087 (LPBYTE
)&dwStartType
,
2089 if (dwError
!= ERROR_SUCCESS
)
2092 lpService
->dwStartType
= dwStartType
;
2095 if (dwErrorControl
!= SERVICE_NO_CHANGE
)
2097 /* Set the error control value */
2098 dwError
= RegSetValueExW(hServiceKey
,
2102 (LPBYTE
)&dwErrorControl
,
2104 if (dwError
!= ERROR_SUCCESS
)
2107 lpService
->dwErrorControl
= dwErrorControl
;
2110 if (lpBinaryPathName
!= NULL
&& *lpBinaryPathName
!= 0)
2112 /* Set the image path */
2113 lpImagePathW
= lpBinaryPathName
;
2115 if (lpService
->Status
.dwServiceType
& SERVICE_DRIVER
)
2117 dwError
= ScmCanonDriverImagePath(lpService
->dwStartType
,
2121 if (dwError
!= ERROR_SUCCESS
)
2125 dwError
= RegSetValueExW(hServiceKey
,
2129 (LPBYTE
)lpImagePathW
,
2130 (DWORD
)((wcslen(lpImagePathW
) + 1) * sizeof(WCHAR
)));
2132 if (lpImagePathW
!= lpBinaryPathName
)
2133 HeapFree(GetProcessHeap(), 0, lpImagePathW
);
2135 if (dwError
!= ERROR_SUCCESS
)
2139 /* Set the group name */
2140 if (lpLoadOrderGroup
!= NULL
&& *lpLoadOrderGroup
!= 0)
2142 dwError
= RegSetValueExW(hServiceKey
,
2146 (LPBYTE
)lpLoadOrderGroup
,
2147 (DWORD
)((wcslen(lpLoadOrderGroup
) + 1) * sizeof(WCHAR
)));
2148 if (dwError
!= ERROR_SUCCESS
)
2151 dwError
= ScmSetServiceGroup(lpService
,
2153 if (dwError
!= ERROR_SUCCESS
)
2158 if (lpdwTagId
!= NULL
)
2160 dwError
= ScmAssignNewTag(lpService
);
2161 if (dwError
!= ERROR_SUCCESS
)
2164 dwError
= RegSetValueExW(hServiceKey
,
2168 (LPBYTE
)&lpService
->dwTag
,
2170 if (dwError
!= ERROR_SUCCESS
)
2173 *lpdwTagId
= lpService
->dwTag
;
2176 /* Write dependencies */
2177 if (lpDependencies
!= NULL
&& *lpDependencies
!= 0)
2179 dwError
= ScmWriteDependencies(hServiceKey
,
2180 (LPWSTR
)lpDependencies
,
2182 if (dwError
!= ERROR_SUCCESS
)
2186 /* Start name and password are only used by Win32 services */
2187 if (lpService
->Status
.dwServiceType
& SERVICE_WIN32
)
2189 /* Write service start name */
2190 if (lpServiceStartName
!= NULL
&& *lpServiceStartName
!= 0)
2192 dwError
= RegSetValueExW(hServiceKey
,
2196 (LPBYTE
)lpServiceStartName
,
2197 (DWORD
)((wcslen(lpServiceStartName
) + 1) * sizeof(WCHAR
)));
2198 if (dwError
!= ERROR_SUCCESS
)
2202 if (lpPassword
!= NULL
)
2204 if (*(LPWSTR
)lpPassword
!= 0)
2206 /* Decrypt the password */
2207 dwError
= ScmDecryptPassword(lpPassword
,
2209 &lpClearTextPassword
);
2210 if (dwError
!= ERROR_SUCCESS
)
2212 DPRINT1("ScmDecryptPassword failed (Error %lu)\n", dwError
);
2215 DPRINT1("Clear text password: %S\n", lpClearTextPassword
);
2217 /* Write the password */
2218 dwError
= ScmSetServicePassword(lpService
->szServiceName
,
2219 lpClearTextPassword
);
2220 if (dwError
!= ERROR_SUCCESS
)
2222 DPRINT1("ScmSetServicePassword failed (Error %lu)\n", dwError
);
2228 /* Delete the password */
2229 dwError
= ScmSetServicePassword(lpService
->szServiceName
,
2231 if (dwError
== ERROR_FILE_NOT_FOUND
)
2232 dwError
= ERROR_SUCCESS
;
2234 if (dwError
!= ERROR_SUCCESS
)
2236 DPRINT1("ScmSetServicePassword failed (Error %lu)\n", dwError
);
2244 if (lpClearTextPassword
!= NULL
)
2246 /* Wipe and release the password buffer */
2247 SecureZeroMemory(lpClearTextPassword
,
2248 (wcslen(lpClearTextPassword
) + 1) * sizeof(WCHAR
));
2249 HeapFree(GetProcessHeap(), 0, lpClearTextPassword
);
2252 if (hServiceKey
!= NULL
)
2253 RegCloseKey(hServiceKey
);
2255 /* Unlock the service database */
2256 ScmUnlockDatabase();
2258 DPRINT("RChangeServiceConfigW() done (Error %lu)\n", dwError
);
2268 SC_RPC_HANDLE hSCManager
,
2269 LPCWSTR lpServiceName
,
2270 LPCWSTR lpDisplayName
,
2271 DWORD dwDesiredAccess
,
2272 DWORD dwServiceType
,
2274 DWORD dwErrorControl
,
2275 LPCWSTR lpBinaryPathName
,
2276 LPCWSTR lpLoadOrderGroup
,
2278 LPBYTE lpDependencies
,
2280 LPCWSTR lpServiceStartName
,
2283 LPSC_RPC_HANDLE lpServiceHandle
)
2285 PMANAGER_HANDLE hManager
;
2286 DWORD dwError
= ERROR_SUCCESS
;
2287 PSERVICE lpService
= NULL
;
2288 SC_HANDLE hServiceHandle
= NULL
;
2289 LPWSTR lpImagePath
= NULL
;
2290 LPWSTR lpClearTextPassword
= NULL
;
2291 HKEY hServiceKey
= NULL
;
2292 LPWSTR lpObjectName
;
2294 DPRINT("RCreateServiceW() called\n");
2295 DPRINT("lpServiceName = %S\n", lpServiceName
);
2296 DPRINT("lpDisplayName = %S\n", lpDisplayName
);
2297 DPRINT("dwDesiredAccess = %lx\n", dwDesiredAccess
);
2298 DPRINT("dwServiceType = 0x%lx\n", dwServiceType
);
2299 DPRINT("dwStartType = %lu\n", dwStartType
);
2300 DPRINT("dwErrorControl = %lu\n", dwErrorControl
);
2301 DPRINT("lpBinaryPathName = %S\n", lpBinaryPathName
);
2302 DPRINT("lpLoadOrderGroup = %S\n", lpLoadOrderGroup
);
2303 DPRINT("lpdwTagId = %p\n", lpdwTagId
);
2306 return ERROR_SHUTDOWN_IN_PROGRESS
;
2308 hManager
= ScmGetServiceManagerFromHandle(hSCManager
);
2309 if (hManager
== NULL
)
2311 DPRINT1("Invalid service manager handle!\n");
2312 return ERROR_INVALID_HANDLE
;
2315 /* Check access rights */
2316 if (!RtlAreAllAccessesGranted(hManager
->Handle
.DesiredAccess
,
2317 SC_MANAGER_CREATE_SERVICE
))
2319 DPRINT("Insufficient access rights! 0x%lx\n",
2320 hManager
->Handle
.DesiredAccess
);
2321 return ERROR_ACCESS_DENIED
;
2324 if (*lpServiceName
== 0)
2325 return ERROR_INVALID_NAME
;
2327 if (*lpBinaryPathName
== 0)
2328 return ERROR_INVALID_PARAMETER
;
2330 /* Check for invalid service type value */
2331 if ((dwServiceType
!= SERVICE_KERNEL_DRIVER
) &&
2332 (dwServiceType
!= SERVICE_FILE_SYSTEM_DRIVER
) &&
2333 ((dwServiceType
& ~SERVICE_INTERACTIVE_PROCESS
) != SERVICE_WIN32_OWN_PROCESS
) &&
2334 ((dwServiceType
& ~SERVICE_INTERACTIVE_PROCESS
) != SERVICE_WIN32_SHARE_PROCESS
))
2336 return ERROR_INVALID_PARAMETER
;
2339 /* Check for invalid start type value */
2340 if ((dwStartType
!= SERVICE_BOOT_START
) &&
2341 (dwStartType
!= SERVICE_SYSTEM_START
) &&
2342 (dwStartType
!= SERVICE_AUTO_START
) &&
2343 (dwStartType
!= SERVICE_DEMAND_START
) &&
2344 (dwStartType
!= SERVICE_DISABLED
))
2346 return ERROR_INVALID_PARAMETER
;
2349 /* Only drivers can be boot start or system start services */
2350 if ((dwStartType
== SERVICE_BOOT_START
) ||
2351 (dwStartType
== SERVICE_SYSTEM_START
))
2353 if ((dwServiceType
!= SERVICE_KERNEL_DRIVER
) &&
2354 (dwServiceType
!= SERVICE_FILE_SYSTEM_DRIVER
))
2356 return ERROR_INVALID_PARAMETER
;
2360 /* Check for invalid error control value */
2361 if ((dwErrorControl
!= SERVICE_ERROR_IGNORE
) &&
2362 (dwErrorControl
!= SERVICE_ERROR_NORMAL
) &&
2363 (dwErrorControl
!= SERVICE_ERROR_SEVERE
) &&
2364 (dwErrorControl
!= SERVICE_ERROR_CRITICAL
))
2366 return ERROR_INVALID_PARAMETER
;
2369 if ((dwServiceType
== (SERVICE_WIN32_OWN_PROCESS
| SERVICE_INTERACTIVE_PROCESS
)) &&
2370 (lpServiceStartName
))
2372 /* We allow LocalSystem to run interactive. */
2373 if (wcsicmp(lpServiceStartName
, L
"LocalSystem"))
2375 return ERROR_INVALID_PARAMETER
;
2379 if (lpdwTagId
&& (!lpLoadOrderGroup
|| !*lpLoadOrderGroup
))
2381 return ERROR_INVALID_PARAMETER
;
2384 /* Lock the service database exclusively */
2385 ScmLockDatabaseExclusive();
2387 lpService
= ScmGetServiceEntryByName(lpServiceName
);
2390 /* Unlock the service database */
2391 ScmUnlockDatabase();
2393 /* Check if it is marked for deletion */
2394 if (lpService
->bDeleted
)
2395 return ERROR_SERVICE_MARKED_FOR_DELETE
;
2397 /* Return service-exists error */
2398 return ERROR_SERVICE_EXISTS
;
2401 if (lpDisplayName
!= NULL
&&
2402 ScmGetServiceEntryByDisplayName(lpDisplayName
) != NULL
)
2404 /* Unlock the service database */
2405 ScmUnlockDatabase();
2407 return ERROR_DUPLICATE_SERVICE_NAME
;
2410 if (dwServiceType
& SERVICE_DRIVER
)
2412 dwError
= ScmCanonDriverImagePath(dwStartType
,
2415 if (dwError
!= ERROR_SUCCESS
)
2420 if (dwStartType
== SERVICE_BOOT_START
||
2421 dwStartType
== SERVICE_SYSTEM_START
)
2423 /* Unlock the service database */
2424 ScmUnlockDatabase();
2426 return ERROR_INVALID_PARAMETER
;
2430 /* Allocate a new service entry */
2431 dwError
= ScmCreateNewServiceRecord(lpServiceName
,
2435 if (dwError
!= ERROR_SUCCESS
)
2438 /* Fill the new service entry */
2439 lpService
->dwErrorControl
= dwErrorControl
;
2441 /* Fill the display name */
2442 if (lpDisplayName
!= NULL
&&
2443 *lpDisplayName
!= 0 &&
2444 _wcsicmp(lpService
->lpDisplayName
, lpDisplayName
) != 0)
2446 lpService
->lpDisplayName
= HeapAlloc(GetProcessHeap(),
2448 (wcslen(lpDisplayName
) + 1) * sizeof(WCHAR
));
2449 if (lpService
->lpDisplayName
== NULL
)
2451 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
2454 wcscpy(lpService
->lpDisplayName
, lpDisplayName
);
2457 /* Assign the service to a group */
2458 if (lpLoadOrderGroup
!= NULL
&& *lpLoadOrderGroup
!= 0)
2460 dwError
= ScmSetServiceGroup(lpService
,
2462 if (dwError
!= ERROR_SUCCESS
)
2466 /* Assign a new tag */
2467 if (lpdwTagId
!= NULL
)
2469 dwError
= ScmAssignNewTag(lpService
);
2470 if (dwError
!= ERROR_SUCCESS
)
2474 /* Assign the default security descriptor */
2475 if (dwServiceType
& SERVICE_WIN32
)
2477 dwError
= ScmCreateDefaultServiceSD(&lpService
->pSecurityDescriptor
);
2478 if (dwError
!= ERROR_SUCCESS
)
2482 /* Write service data to the registry */
2483 /* Create the service key */
2484 dwError
= ScmCreateServiceKey(lpServiceName
,
2487 if (dwError
!= ERROR_SUCCESS
)
2490 /* Set the display name */
2491 if (lpDisplayName
!= NULL
&& *lpDisplayName
!= 0)
2493 RegSetValueExW(hServiceKey
,
2497 (LPBYTE
)lpDisplayName
,
2498 (DWORD
)((wcslen(lpDisplayName
) + 1) * sizeof(WCHAR
)));
2501 /* Set the service type */
2502 dwError
= RegSetValueExW(hServiceKey
,
2506 (LPBYTE
)&dwServiceType
,
2508 if (dwError
!= ERROR_SUCCESS
)
2511 /* Set the start value */
2512 dwError
= RegSetValueExW(hServiceKey
,
2516 (LPBYTE
)&dwStartType
,
2518 if (dwError
!= ERROR_SUCCESS
)
2521 /* Set the error control value */
2522 dwError
= RegSetValueExW(hServiceKey
,
2526 (LPBYTE
)&dwErrorControl
,
2528 if (dwError
!= ERROR_SUCCESS
)
2531 /* Set the image path */
2532 if (dwServiceType
& SERVICE_WIN32
)
2534 dwError
= RegSetValueExW(hServiceKey
,
2538 (LPBYTE
)lpBinaryPathName
,
2539 (DWORD
)((wcslen(lpBinaryPathName
) + 1) * sizeof(WCHAR
)));
2540 if (dwError
!= ERROR_SUCCESS
)
2543 else if (dwServiceType
& SERVICE_DRIVER
)
2545 dwError
= RegSetValueExW(hServiceKey
,
2549 (LPBYTE
)lpImagePath
,
2550 (DWORD
)((wcslen(lpImagePath
) + 1) * sizeof(WCHAR
)));
2551 if (dwError
!= ERROR_SUCCESS
)
2555 /* Set the group name */
2556 if (lpLoadOrderGroup
!= NULL
&& *lpLoadOrderGroup
!= 0)
2558 dwError
= RegSetValueExW(hServiceKey
,
2562 (LPBYTE
)lpLoadOrderGroup
,
2563 (DWORD
)((wcslen(lpLoadOrderGroup
) + 1) * sizeof(WCHAR
)));
2564 if (dwError
!= ERROR_SUCCESS
)
2568 /* Set the service tag */
2569 if (lpdwTagId
!= NULL
)
2571 dwError
= RegSetValueExW(hServiceKey
,
2575 (LPBYTE
)&lpService
->dwTag
,
2577 if (dwError
!= ERROR_SUCCESS
)
2581 /* Write dependencies */
2582 if (lpDependencies
!= NULL
&& *lpDependencies
!= 0)
2584 dwError
= ScmWriteDependencies(hServiceKey
,
2585 (LPCWSTR
)lpDependencies
,
2587 if (dwError
!= ERROR_SUCCESS
)
2591 /* Start name and password are only used by Win32 services */
2592 if (dwServiceType
& SERVICE_WIN32
)
2594 /* Write service start name */
2595 lpObjectName
= (lpServiceStartName
!= NULL
) ? (LPWSTR
)lpServiceStartName
: L
"LocalSystem";
2596 dwError
= RegSetValueExW(hServiceKey
,
2600 (LPBYTE
)lpObjectName
,
2601 (DWORD
)((wcslen(lpObjectName
) + 1) * sizeof(WCHAR
)));
2602 if (dwError
!= ERROR_SUCCESS
)
2605 if (lpPassword
!= NULL
&& *(LPWSTR
)lpPassword
!= 0)
2607 /* Decrypt the password */
2608 dwError
= ScmDecryptPassword(lpPassword
,
2610 &lpClearTextPassword
);
2611 if (dwError
!= ERROR_SUCCESS
)
2614 /* Write the password */
2615 dwError
= ScmSetServicePassword(lpServiceName
,
2616 lpClearTextPassword
);
2617 if (dwError
!= ERROR_SUCCESS
)
2621 /* Write the security descriptor */
2622 dwError
= ScmWriteSecurityDescriptor(hServiceKey
,
2623 lpService
->pSecurityDescriptor
);
2624 if (dwError
!= ERROR_SUCCESS
)
2628 dwError
= ScmCreateServiceHandle(lpService
,
2630 if (dwError
!= ERROR_SUCCESS
)
2633 dwError
= ScmCheckAccess(hServiceHandle
,
2635 if (dwError
!= ERROR_SUCCESS
)
2638 lpService
->dwRefCount
= 1;
2639 DPRINT("CreateService - lpService->dwRefCount %u\n", lpService
->dwRefCount
);
2642 /* Unlock the service database */
2643 ScmUnlockDatabase();
2645 if (hServiceKey
!= NULL
)
2646 RegCloseKey(hServiceKey
);
2648 if (lpClearTextPassword
!= NULL
)
2650 /* Wipe and release the password buffer */
2651 SecureZeroMemory(lpClearTextPassword
,
2652 (wcslen(lpClearTextPassword
) + 1) * sizeof(WCHAR
));
2653 HeapFree(GetProcessHeap(), 0, lpClearTextPassword
);
2656 if (dwError
== ERROR_SUCCESS
)
2658 DPRINT("hService %p\n", hServiceHandle
);
2659 *lpServiceHandle
= (SC_RPC_HANDLE
)hServiceHandle
;
2661 if (lpdwTagId
!= NULL
)
2662 *lpdwTagId
= lpService
->dwTag
;
2666 if (lpService
!= NULL
&&
2667 lpService
->lpServiceName
!= NULL
)
2669 /* Release the display name buffer */
2670 HeapFree(GetProcessHeap(), 0, lpService
->lpDisplayName
);
2675 /* Remove the service handle */
2676 HeapFree(GetProcessHeap(), 0, hServiceHandle
);
2679 if (lpService
!= NULL
)
2681 /* FIXME: remove the service entry */
2685 if (lpImagePath
!= NULL
)
2686 HeapFree(GetProcessHeap(), 0, lpImagePath
);
2688 DPRINT("RCreateServiceW() done (Error %lu)\n", dwError
);
2697 REnumDependentServicesW(
2698 SC_RPC_HANDLE hService
,
2699 DWORD dwServiceState
,
2702 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
2703 LPBOUNDED_DWORD_256K lpServicesReturned
)
2705 DWORD dwError
= ERROR_SUCCESS
;
2706 DWORD dwServicesReturned
= 0;
2707 DWORD dwServiceCount
;
2708 HKEY hServicesKey
= NULL
;
2709 PSERVICE_HANDLE hSvc
;
2710 PSERVICE lpService
= NULL
;
2711 PSERVICE
*lpServicesArray
= NULL
;
2712 LPENUM_SERVICE_STATUSW lpServicesPtr
= NULL
;
2715 *pcbBytesNeeded
= 0;
2716 *lpServicesReturned
= 0;
2718 DPRINT("REnumDependentServicesW() called\n");
2720 hSvc
= ScmGetServiceFromHandle(hService
);
2723 DPRINT1("Invalid service handle!\n");
2724 return ERROR_INVALID_HANDLE
;
2727 lpService
= hSvc
->ServiceEntry
;
2729 /* Check access rights */
2730 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
2731 SC_MANAGER_ENUMERATE_SERVICE
))
2733 DPRINT("Insufficient access rights! 0x%lx\n",
2734 hSvc
->Handle
.DesiredAccess
);
2735 return ERROR_ACCESS_DENIED
;
2738 /* Open the Services Reg key */
2739 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
2740 L
"System\\CurrentControlSet\\Services",
2744 if (dwError
!= ERROR_SUCCESS
)
2747 /* First determine the bytes needed and get the number of dependent services */
2748 dwError
= Int_EnumDependentServicesW(hServicesKey
,
2753 &dwServicesReturned
);
2754 if (dwError
!= ERROR_SUCCESS
)
2757 /* If buffer size is less than the bytes needed or pointer is null */
2758 if ((!lpServices
) || (cbBufSize
< *pcbBytesNeeded
))
2760 dwError
= ERROR_MORE_DATA
;
2764 /* Allocate memory for array of service pointers */
2765 lpServicesArray
= HeapAlloc(GetProcessHeap(),
2767 (dwServicesReturned
+ 1) * sizeof(PSERVICE
));
2768 if (!lpServicesArray
)
2770 DPRINT1("Could not allocate a buffer!!\n");
2771 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
2775 dwServicesReturned
= 0;
2776 *pcbBytesNeeded
= 0;
2778 dwError
= Int_EnumDependentServicesW(hServicesKey
,
2783 &dwServicesReturned
);
2784 if (dwError
!= ERROR_SUCCESS
)
2789 lpServicesPtr
= (LPENUM_SERVICE_STATUSW
)lpServices
;
2790 lpStr
= (LPWSTR
)(lpServices
+ (dwServicesReturned
* sizeof(ENUM_SERVICE_STATUSW
)));
2792 /* Copy EnumDepenedentService to Buffer */
2793 for (dwServiceCount
= 0; dwServiceCount
< dwServicesReturned
; dwServiceCount
++)
2795 lpService
= lpServicesArray
[dwServiceCount
];
2797 /* Copy status info */
2798 memcpy(&lpServicesPtr
->ServiceStatus
,
2800 sizeof(SERVICE_STATUS
));
2802 /* Copy display name */
2803 wcscpy(lpStr
, lpService
->lpDisplayName
);
2804 lpServicesPtr
->lpDisplayName
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServices
);
2805 lpStr
+= (wcslen(lpService
->lpDisplayName
) + 1);
2807 /* Copy service name */
2808 wcscpy(lpStr
, lpService
->lpServiceName
);
2809 lpServicesPtr
->lpServiceName
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServices
);
2810 lpStr
+= (wcslen(lpService
->lpServiceName
) + 1);
2815 *lpServicesReturned
= dwServicesReturned
;
2818 if (lpServicesArray
!= NULL
)
2819 HeapFree(GetProcessHeap(), 0, lpServicesArray
);
2821 RegCloseKey(hServicesKey
);
2823 DPRINT("REnumDependentServicesW() done (Error %lu)\n", dwError
);
2832 REnumServicesStatusW(
2833 SC_RPC_HANDLE hSCManager
,
2834 DWORD dwServiceType
,
2835 DWORD dwServiceState
,
2838 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
2839 LPBOUNDED_DWORD_256K lpServicesReturned
,
2840 LPBOUNDED_DWORD_256K lpResumeHandle
)
2842 /* Enumerate all the services, not regarding of their group */
2843 return REnumServiceGroupW(hSCManager
,
2859 LPWSTR lpMachineName
,
2860 LPWSTR lpDatabaseName
,
2861 DWORD dwDesiredAccess
,
2862 LPSC_RPC_HANDLE lpScHandle
)
2867 DPRINT("ROpenSCManagerW() called\n");
2868 DPRINT("lpMachineName = %p\n", lpMachineName
);
2869 DPRINT("lpMachineName: %S\n", lpMachineName
);
2870 DPRINT("lpDataBaseName = %p\n", lpDatabaseName
);
2871 DPRINT("lpDataBaseName: %S\n", lpDatabaseName
);
2872 DPRINT("dwDesiredAccess = %x\n", dwDesiredAccess
);
2875 return ERROR_SHUTDOWN_IN_PROGRESS
;
2878 return ERROR_INVALID_PARAMETER
;
2880 dwError
= ScmCreateManagerHandle(lpDatabaseName
,
2882 if (dwError
!= ERROR_SUCCESS
)
2884 DPRINT("ScmCreateManagerHandle() failed (Error %lu)\n", dwError
);
2888 /* Check the desired access */
2889 dwError
= ScmCheckAccess(hHandle
,
2890 dwDesiredAccess
| SC_MANAGER_CONNECT
);
2891 if (dwError
!= ERROR_SUCCESS
)
2893 DPRINT("ScmCheckAccess() failed (Error %lu)\n", dwError
);
2894 HeapFree(GetProcessHeap(), 0, hHandle
);
2898 *lpScHandle
= (SC_RPC_HANDLE
)hHandle
;
2899 DPRINT("*hScm = %p\n", *lpScHandle
);
2901 DPRINT("ROpenSCManagerW() done\n");
2903 return ERROR_SUCCESS
;
2911 SC_RPC_HANDLE hSCManager
,
2912 LPWSTR lpServiceName
,
2913 DWORD dwDesiredAccess
,
2914 LPSC_RPC_HANDLE lpServiceHandle
)
2917 PMANAGER_HANDLE hManager
;
2919 DWORD dwError
= ERROR_SUCCESS
;
2921 DPRINT("ROpenServiceW() called\n");
2922 DPRINT("hSCManager = %p\n", hSCManager
);
2923 DPRINT("lpServiceName = %p\n", lpServiceName
);
2924 DPRINT("lpServiceName: %S\n", lpServiceName
);
2925 DPRINT("dwDesiredAccess = %x\n", dwDesiredAccess
);
2928 return ERROR_SHUTDOWN_IN_PROGRESS
;
2930 hManager
= ScmGetServiceManagerFromHandle(hSCManager
);
2931 if (hManager
== NULL
)
2933 DPRINT1("Invalid service manager handle!\n");
2934 return ERROR_INVALID_HANDLE
;
2937 if (!lpServiceHandle
)
2938 return ERROR_INVALID_PARAMETER
;
2941 return ERROR_INVALID_ADDRESS
;
2943 /* Lock the service database exclusive */
2944 ScmLockDatabaseExclusive();
2946 /* Get service database entry */
2947 lpService
= ScmGetServiceEntryByName(lpServiceName
);
2948 if (lpService
== NULL
)
2950 DPRINT("Could not find the service!\n");
2951 dwError
= ERROR_SERVICE_DOES_NOT_EXIST
;
2955 /* Create a service handle */
2956 dwError
= ScmCreateServiceHandle(lpService
,
2958 if (dwError
!= ERROR_SUCCESS
)
2960 DPRINT("ScmCreateServiceHandle() failed (Error %lu)\n", dwError
);
2964 /* Check the desired access */
2965 dwError
= ScmCheckAccess(hHandle
,
2967 if (dwError
!= ERROR_SUCCESS
)
2969 DPRINT("ScmCheckAccess() failed (Error %lu)\n", dwError
);
2970 HeapFree(GetProcessHeap(), 0, hHandle
);
2974 lpService
->dwRefCount
++;
2975 DPRINT("OpenService - lpService->dwRefCount %u\n",lpService
->dwRefCount
);
2977 *lpServiceHandle
= (SC_RPC_HANDLE
)hHandle
;
2978 DPRINT("*hService = %p\n", *lpServiceHandle
);
2981 /* Unlock the service database */
2982 ScmUnlockDatabase();
2984 DPRINT("ROpenServiceW() done\n");
2993 RQueryServiceConfigW(
2994 SC_RPC_HANDLE hService
,
2995 LPBYTE lpBuf
, //LPQUERY_SERVICE_CONFIGW lpServiceConfig,
2997 LPBOUNDED_DWORD_8K pcbBytesNeeded
)
2999 LPQUERY_SERVICE_CONFIGW lpServiceConfig
= (LPQUERY_SERVICE_CONFIGW
)lpBuf
;
3000 DWORD dwError
= ERROR_SUCCESS
;
3001 PSERVICE_HANDLE hSvc
;
3002 PSERVICE lpService
= NULL
;
3003 HKEY hServiceKey
= NULL
;
3004 LPWSTR lpImagePath
= NULL
;
3005 LPWSTR lpServiceStartName
= NULL
;
3006 LPWSTR lpDependencies
= NULL
;
3007 DWORD dwDependenciesLength
= 0;
3008 DWORD dwRequiredSize
;
3011 DPRINT("RQueryServiceConfigW() called\n");
3014 return ERROR_SHUTDOWN_IN_PROGRESS
;
3016 hSvc
= ScmGetServiceFromHandle(hService
);
3019 DPRINT1("Invalid service handle!\n");
3020 return ERROR_INVALID_HANDLE
;
3023 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
3024 SERVICE_QUERY_CONFIG
))
3026 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
3027 return ERROR_ACCESS_DENIED
;
3030 lpService
= hSvc
->ServiceEntry
;
3031 if (lpService
== NULL
)
3033 DPRINT("lpService == NULL!\n");
3034 return ERROR_INVALID_HANDLE
;
3037 /* Lock the service database shared */
3038 ScmLockDatabaseShared();
3040 dwError
= ScmOpenServiceKey(lpService
->lpServiceName
,
3043 if (dwError
!= ERROR_SUCCESS
)
3046 /* Read the image path */
3047 dwError
= ScmReadString(hServiceKey
,
3050 if (dwError
!= ERROR_SUCCESS
)
3053 /* Read the service start name */
3054 ScmReadString(hServiceKey
,
3056 &lpServiceStartName
);
3058 /* Read the dependencies */
3059 ScmReadDependencies(hServiceKey
,
3061 &dwDependenciesLength
);
3063 dwRequiredSize
= sizeof(QUERY_SERVICE_CONFIGW
);
3065 if (lpImagePath
!= NULL
)
3066 dwRequiredSize
+= (DWORD
)((wcslen(lpImagePath
) + 1) * sizeof(WCHAR
));
3068 dwRequiredSize
+= 2 * sizeof(WCHAR
);
3070 if ((lpService
->lpGroup
!= NULL
) && (lpService
->lpGroup
->lpGroupName
!= NULL
))
3071 dwRequiredSize
+= (DWORD
)((wcslen(lpService
->lpGroup
->lpGroupName
) + 1) * sizeof(WCHAR
));
3073 dwRequiredSize
+= 2 * sizeof(WCHAR
);
3075 if (lpDependencies
!= NULL
)
3076 dwRequiredSize
+= dwDependenciesLength
* sizeof(WCHAR
);
3078 dwRequiredSize
+= 2 * sizeof(WCHAR
);
3080 if (lpServiceStartName
!= NULL
)
3081 dwRequiredSize
+= (DWORD
)((wcslen(lpServiceStartName
) + 1) * sizeof(WCHAR
));
3083 dwRequiredSize
+= 2 * sizeof(WCHAR
);
3085 if (lpService
->lpDisplayName
!= NULL
)
3086 dwRequiredSize
+= (DWORD
)((wcslen(lpService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
3088 dwRequiredSize
+= 2 * sizeof(WCHAR
);
3090 if (lpServiceConfig
== NULL
|| cbBufSize
< dwRequiredSize
)
3092 dwError
= ERROR_INSUFFICIENT_BUFFER
;
3096 lpServiceConfig
->dwServiceType
= lpService
->Status
.dwServiceType
;
3097 lpServiceConfig
->dwStartType
= lpService
->dwStartType
;
3098 lpServiceConfig
->dwErrorControl
= lpService
->dwErrorControl
;
3099 lpServiceConfig
->dwTagId
= lpService
->dwTag
;
3101 lpStr
= (LPWSTR
)(lpServiceConfig
+ 1);
3103 /* Append the image path */
3104 if (lpImagePath
!= NULL
)
3106 wcscpy(lpStr
, lpImagePath
);
3113 lpServiceConfig
->lpBinaryPathName
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
3114 lpStr
+= (wcslen(lpStr
) + 1);
3116 /* Append the group name */
3117 if ((lpService
->lpGroup
!= NULL
) && (lpService
->lpGroup
->lpGroupName
!= NULL
))
3119 wcscpy(lpStr
, lpService
->lpGroup
->lpGroupName
);
3126 lpServiceConfig
->lpLoadOrderGroup
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
3127 lpStr
+= (wcslen(lpStr
) + 1);
3129 /* Append Dependencies */
3130 if (lpDependencies
!= NULL
)
3134 dwDependenciesLength
* sizeof(WCHAR
));
3141 lpServiceConfig
->lpDependencies
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
3142 if (lpDependencies
!= NULL
)
3143 lpStr
+= dwDependenciesLength
;
3145 lpStr
+= (wcslen(lpStr
) + 1);
3147 /* Append the service start name */
3148 if (lpServiceStartName
!= NULL
)
3150 wcscpy(lpStr
, lpServiceStartName
);
3157 lpServiceConfig
->lpServiceStartName
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
3158 lpStr
+= (wcslen(lpStr
) + 1);
3160 /* Append the display name */
3161 if (lpService
->lpDisplayName
!= NULL
)
3163 wcscpy(lpStr
, lpService
->lpDisplayName
);
3170 lpServiceConfig
->lpDisplayName
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
3173 if (pcbBytesNeeded
!= NULL
)
3174 *pcbBytesNeeded
= dwRequiredSize
;
3177 /* Unlock the service database */
3178 ScmUnlockDatabase();
3180 if (lpImagePath
!= NULL
)
3181 HeapFree(GetProcessHeap(), 0, lpImagePath
);
3183 if (lpServiceStartName
!= NULL
)
3184 HeapFree(GetProcessHeap(), 0, lpServiceStartName
);
3186 if (lpDependencies
!= NULL
)
3187 HeapFree(GetProcessHeap(), 0, lpDependencies
);
3189 if (hServiceKey
!= NULL
)
3190 RegCloseKey(hServiceKey
);
3192 DPRINT("RQueryServiceConfigW() done\n");
3201 RQueryServiceLockStatusW(
3202 SC_RPC_HANDLE hSCManager
,
3203 LPBYTE lpBuf
, // LPQUERY_SERVICE_LOCK_STATUSW lpLockStatus,
3205 LPBOUNDED_DWORD_4K pcbBytesNeeded
)
3207 LPQUERY_SERVICE_LOCK_STATUSW lpLockStatus
= (LPQUERY_SERVICE_LOCK_STATUSW
)lpBuf
;
3208 PMANAGER_HANDLE hMgr
;
3209 DWORD dwRequiredSize
;
3211 if (!lpLockStatus
|| !pcbBytesNeeded
)
3212 return ERROR_INVALID_PARAMETER
;
3214 hMgr
= ScmGetServiceManagerFromHandle(hSCManager
);
3217 DPRINT1("Invalid service manager handle!\n");
3218 return ERROR_INVALID_HANDLE
;
3221 if (!RtlAreAllAccessesGranted(hMgr
->Handle
.DesiredAccess
,
3222 SC_MANAGER_QUERY_LOCK_STATUS
))
3224 DPRINT("Insufficient access rights! 0x%lx\n", hMgr
->Handle
.DesiredAccess
);
3225 return ERROR_ACCESS_DENIED
;
3228 /* FIXME: we need to compute instead the real length of the owner name */
3229 dwRequiredSize
= sizeof(QUERY_SERVICE_LOCK_STATUSW
) + sizeof(WCHAR
);
3230 *pcbBytesNeeded
= dwRequiredSize
;
3232 if (cbBufSize
< dwRequiredSize
)
3233 return ERROR_INSUFFICIENT_BUFFER
;
3235 ScmQueryServiceLockStatusW(lpLockStatus
);
3237 return ERROR_SUCCESS
;
3245 SC_RPC_HANDLE hService
,
3247 LPSTRING_PTRSW argv
)
3249 DWORD dwError
= ERROR_SUCCESS
;
3250 PSERVICE_HANDLE hSvc
;
3251 PSERVICE lpService
= NULL
;
3256 DPRINT("RStartServiceW(%p %lu %p) called\n", hService
, argc
, argv
);
3257 DPRINT(" argc: %lu\n", argc
);
3260 for (i
= 0; i
< argc
; i
++)
3262 DPRINT(" argv[%lu]: %S\n", i
, argv
[i
].StringPtr
);
3268 return ERROR_SHUTDOWN_IN_PROGRESS
;
3270 hSvc
= ScmGetServiceFromHandle(hService
);
3273 DPRINT1("Invalid service handle!\n");
3274 return ERROR_INVALID_HANDLE
;
3277 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
3280 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
3281 return ERROR_ACCESS_DENIED
;
3284 lpService
= hSvc
->ServiceEntry
;
3285 if (lpService
== NULL
)
3287 DPRINT("lpService == NULL!\n");
3288 return ERROR_INVALID_HANDLE
;
3291 if (lpService
->dwStartType
== SERVICE_DISABLED
)
3292 return ERROR_SERVICE_DISABLED
;
3294 if (lpService
->bDeleted
)
3295 return ERROR_SERVICE_MARKED_FOR_DELETE
;
3297 /* Start the service */
3298 dwError
= ScmStartService(lpService
, argc
, (LPWSTR
*)argv
);
3307 RGetServiceDisplayNameW(
3308 SC_RPC_HANDLE hSCManager
,
3309 LPCWSTR lpServiceName
,
3310 LPWSTR lpDisplayName
,
3313 // PMANAGER_HANDLE hManager;
3315 LPCWSTR lpSvcDisplayName
;
3319 DPRINT("RGetServiceDisplayNameW() called\n");
3320 DPRINT("hSCManager = %p\n", hSCManager
);
3321 DPRINT("lpServiceName: %S\n", lpServiceName
);
3322 DPRINT("lpDisplayName: %p\n", lpDisplayName
);
3323 DPRINT("*lpcchBuffer: %lu\n", *lpcchBuffer
);
3326 hManager
= (PMANAGER_HANDLE
)hSCManager
;
3327 if (hManager
->Handle
.Tag
!= MANAGER_TAG
)
3329 DPRINT("Invalid manager handle!\n");
3330 return ERROR_INVALID_HANDLE
;
3334 /* Get service database entry */
3335 lpService
= ScmGetServiceEntryByName(lpServiceName
);
3336 if (lpService
== NULL
)
3338 DPRINT("Could not find the service!\n");
3339 return ERROR_SERVICE_DOES_NOT_EXIST
;
3342 if (lpService
->lpDisplayName
)
3343 lpSvcDisplayName
= lpService
->lpDisplayName
;
3345 lpSvcDisplayName
= lpService
->lpServiceName
;
3347 dwLength
= (DWORD
)wcslen(lpSvcDisplayName
);
3349 if (*lpcchBuffer
> dwLength
)
3351 if (lpDisplayName
!= NULL
)
3352 wcscpy(lpDisplayName
, lpSvcDisplayName
);
3354 dwError
= ERROR_SUCCESS
;
3358 dwError
= ERROR_INSUFFICIENT_BUFFER
;
3361 *lpcchBuffer
= dwLength
;
3370 RGetServiceKeyNameW(
3371 SC_RPC_HANDLE hSCManager
,
3372 LPCWSTR lpDisplayName
,
3373 LPWSTR lpServiceName
,
3376 // PMANAGER_HANDLE hManager;
3381 DPRINT("RGetServiceKeyNameW() called\n");
3382 DPRINT("hSCManager = %p\n", hSCManager
);
3383 DPRINT("lpDisplayName: %S\n", lpDisplayName
);
3384 DPRINT("lpServiceName: %p\n", lpServiceName
);
3385 DPRINT("*lpcchBuffer: %lu\n", *lpcchBuffer
);
3388 hManager
= (PMANAGER_HANDLE
)hSCManager
;
3389 if (hManager
->Handle
.Tag
!= MANAGER_TAG
)
3391 DPRINT("Invalid manager handle!\n");
3392 return ERROR_INVALID_HANDLE
;
3396 /* Get service database entry */
3397 lpService
= ScmGetServiceEntryByDisplayName(lpDisplayName
);
3398 if (lpService
== NULL
)
3400 DPRINT("Could not find the service!\n");
3401 return ERROR_SERVICE_DOES_NOT_EXIST
;
3404 dwLength
= (DWORD
)wcslen(lpService
->lpServiceName
);
3406 if (*lpcchBuffer
> dwLength
)
3408 if (lpServiceName
!= NULL
)
3409 wcscpy(lpServiceName
, lpService
->lpServiceName
);
3411 dwError
= ERROR_SUCCESS
;
3415 dwError
= ERROR_INSUFFICIENT_BUFFER
;
3418 *lpcchBuffer
= dwLength
;
3427 RI_ScSetServiceBitsA(
3428 RPC_SERVICE_STATUS_HANDLE hServiceStatus
,
3429 DWORD dwServiceBits
,
3431 int bUpdateImmediately
,
3435 return ERROR_SHUTDOWN_IN_PROGRESS
;
3437 if (lpString
!= NULL
)
3438 return ERROR_INVALID_PARAMETER
;
3440 return RI_ScSetServiceBitsW(hServiceStatus
,
3451 RChangeServiceConfigA(
3452 SC_RPC_HANDLE hService
,
3453 DWORD dwServiceType
,
3455 DWORD dwErrorControl
,
3456 LPSTR lpBinaryPathName
,
3457 LPSTR lpLoadOrderGroup
,
3459 LPBYTE lpDependencies
,
3461 LPSTR lpServiceStartName
,
3464 LPSTR lpDisplayName
)
3466 DWORD dwError
= ERROR_SUCCESS
;
3467 LPWSTR lpBinaryPathNameW
= NULL
;
3468 LPWSTR lpLoadOrderGroupW
= NULL
;
3469 LPWSTR lpDependenciesW
= NULL
;
3470 LPWSTR lpServiceStartNameW
= NULL
;
3471 LPWSTR lpDisplayNameW
= NULL
;
3472 DWORD dwDependenciesLength
= 0;
3477 if (lpBinaryPathName
)
3479 len
= MultiByteToWideChar(CP_ACP
, 0, lpBinaryPathName
, -1, NULL
, 0);
3480 lpBinaryPathNameW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3481 if (!lpBinaryPathNameW
)
3483 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3486 MultiByteToWideChar(CP_ACP
, 0, lpBinaryPathName
, -1, lpBinaryPathNameW
, len
);
3489 if (lpLoadOrderGroup
)
3491 len
= MultiByteToWideChar(CP_ACP
, 0, lpLoadOrderGroup
, -1, NULL
, 0);
3492 lpLoadOrderGroupW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3493 if (!lpLoadOrderGroupW
)
3495 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3498 MultiByteToWideChar(CP_ACP
, 0, lpLoadOrderGroup
, -1, lpLoadOrderGroupW
, len
);
3503 lpStr
= (LPCSTR
)lpDependencies
;
3506 cchLength
= strlen(lpStr
) + 1;
3507 dwDependenciesLength
+= (DWORD
)cchLength
;
3508 lpStr
= lpStr
+ cchLength
;
3510 dwDependenciesLength
++;
3512 lpDependenciesW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, dwDependenciesLength
* sizeof(WCHAR
));
3513 if (!lpDependenciesW
)
3515 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3518 MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)lpDependencies
, dwDependenciesLength
, lpDependenciesW
, dwDependenciesLength
);
3521 if (lpServiceStartName
)
3523 len
= MultiByteToWideChar(CP_ACP
, 0, lpServiceStartName
, -1, NULL
, 0);
3524 lpServiceStartNameW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3525 if (!lpServiceStartNameW
)
3527 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3530 MultiByteToWideChar(CP_ACP
, 0, lpServiceStartName
, -1, lpServiceStartNameW
, len
);
3535 len
= MultiByteToWideChar(CP_ACP
, 0, lpDisplayName
, -1, NULL
, 0);
3536 lpDisplayNameW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3537 if (!lpDisplayNameW
)
3539 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3542 MultiByteToWideChar(CP_ACP
, 0, lpDisplayName
, -1, lpDisplayNameW
, len
);
3545 dwError
= RChangeServiceConfigW(hService
,
3552 (LPBYTE
)lpDependenciesW
,
3553 dwDependenciesLength
,
3554 lpServiceStartNameW
,
3560 if (lpBinaryPathNameW
!= NULL
)
3561 HeapFree(GetProcessHeap(), 0, lpBinaryPathNameW
);
3563 if (lpLoadOrderGroupW
!= NULL
)
3564 HeapFree(GetProcessHeap(), 0, lpLoadOrderGroupW
);
3566 if (lpDependenciesW
!= NULL
)
3567 HeapFree(GetProcessHeap(), 0, lpDependenciesW
);
3569 if (lpServiceStartNameW
!= NULL
)
3570 HeapFree(GetProcessHeap(), 0, lpServiceStartNameW
);
3572 if (lpDisplayNameW
!= NULL
)
3573 HeapFree(GetProcessHeap(), 0, lpDisplayNameW
);
3583 SC_RPC_HANDLE hSCManager
,
3584 LPSTR lpServiceName
,
3585 LPSTR lpDisplayName
,
3586 DWORD dwDesiredAccess
,
3587 DWORD dwServiceType
,
3589 DWORD dwErrorControl
,
3590 LPSTR lpBinaryPathName
,
3591 LPSTR lpLoadOrderGroup
,
3593 LPBYTE lpDependencies
,
3595 LPSTR lpServiceStartName
,
3598 LPSC_RPC_HANDLE lpServiceHandle
)
3600 DWORD dwError
= ERROR_SUCCESS
;
3601 LPWSTR lpServiceNameW
= NULL
;
3602 LPWSTR lpDisplayNameW
= NULL
;
3603 LPWSTR lpBinaryPathNameW
= NULL
;
3604 LPWSTR lpLoadOrderGroupW
= NULL
;
3605 LPWSTR lpDependenciesW
= NULL
;
3606 LPWSTR lpServiceStartNameW
= NULL
;
3607 DWORD dwDependenciesLength
= 0;
3614 len
= MultiByteToWideChar(CP_ACP
, 0, lpServiceName
, -1, NULL
, 0);
3615 lpServiceNameW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3616 if (!lpServiceNameW
)
3618 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3621 MultiByteToWideChar(CP_ACP
, 0, lpServiceName
, -1, lpServiceNameW
, len
);
3626 len
= MultiByteToWideChar(CP_ACP
, 0, lpDisplayName
, -1, NULL
, 0);
3627 lpDisplayNameW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3628 if (!lpDisplayNameW
)
3630 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3633 MultiByteToWideChar(CP_ACP
, 0, lpDisplayName
, -1, lpDisplayNameW
, len
);
3636 if (lpBinaryPathName
)
3638 len
= MultiByteToWideChar(CP_ACP
, 0, lpBinaryPathName
, -1, NULL
, 0);
3639 lpBinaryPathNameW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3640 if (!lpBinaryPathNameW
)
3642 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3645 MultiByteToWideChar(CP_ACP
, 0, lpBinaryPathName
, -1, lpBinaryPathNameW
, len
);
3648 if (lpLoadOrderGroup
)
3650 len
= MultiByteToWideChar(CP_ACP
, 0, lpLoadOrderGroup
, -1, NULL
, 0);
3651 lpLoadOrderGroupW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3652 if (!lpLoadOrderGroupW
)
3654 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3657 MultiByteToWideChar(CP_ACP
, 0, lpLoadOrderGroup
, -1, lpLoadOrderGroupW
, len
);
3662 lpStr
= (LPCSTR
)lpDependencies
;
3665 cchLength
= strlen(lpStr
) + 1;
3666 dwDependenciesLength
+= (DWORD
)cchLength
;
3667 lpStr
= lpStr
+ cchLength
;
3669 dwDependenciesLength
++;
3671 lpDependenciesW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, dwDependenciesLength
* sizeof(WCHAR
));
3672 if (!lpDependenciesW
)
3674 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3677 MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)lpDependencies
, dwDependenciesLength
, lpDependenciesW
, dwDependenciesLength
);
3680 if (lpServiceStartName
)
3682 len
= MultiByteToWideChar(CP_ACP
, 0, lpServiceStartName
, -1, NULL
, 0);
3683 lpServiceStartNameW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3684 if (!lpServiceStartNameW
)
3686 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3689 MultiByteToWideChar(CP_ACP
, 0, lpServiceStartName
, -1, lpServiceStartNameW
, len
);
3692 dwError
= RCreateServiceW(hSCManager
,
3702 (LPBYTE
)lpDependenciesW
,
3703 dwDependenciesLength
,
3704 lpServiceStartNameW
,
3710 if (lpServiceNameW
!=NULL
)
3711 HeapFree(GetProcessHeap(), 0, lpServiceNameW
);
3713 if (lpDisplayNameW
!= NULL
)
3714 HeapFree(GetProcessHeap(), 0, lpDisplayNameW
);
3716 if (lpBinaryPathNameW
!= NULL
)
3717 HeapFree(GetProcessHeap(), 0, lpBinaryPathNameW
);
3719 if (lpLoadOrderGroupW
!= NULL
)
3720 HeapFree(GetProcessHeap(), 0, lpLoadOrderGroupW
);
3722 if (lpDependenciesW
!= NULL
)
3723 HeapFree(GetProcessHeap(), 0, lpDependenciesW
);
3725 if (lpServiceStartNameW
!= NULL
)
3726 HeapFree(GetProcessHeap(), 0, lpServiceStartNameW
);
3735 REnumDependentServicesA(
3736 SC_RPC_HANDLE hService
,
3737 DWORD dwServiceState
,
3740 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
3741 LPBOUNDED_DWORD_256K lpServicesReturned
)
3743 DWORD dwError
= ERROR_SUCCESS
;
3744 DWORD dwServicesReturned
= 0;
3745 DWORD dwServiceCount
;
3746 HKEY hServicesKey
= NULL
;
3747 PSERVICE_HANDLE hSvc
;
3748 PSERVICE lpService
= NULL
;
3749 PSERVICE
*lpServicesArray
= NULL
;
3750 LPENUM_SERVICE_STATUSA lpServicesPtr
= NULL
;
3753 *pcbBytesNeeded
= 0;
3754 *lpServicesReturned
= 0;
3756 DPRINT("REnumDependentServicesA() called\n");
3758 hSvc
= ScmGetServiceFromHandle(hService
);
3761 DPRINT1("Invalid service handle!\n");
3762 return ERROR_INVALID_HANDLE
;
3765 lpService
= hSvc
->ServiceEntry
;
3767 /* Check access rights */
3768 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
3769 SC_MANAGER_ENUMERATE_SERVICE
))
3771 DPRINT("Insufficient access rights! 0x%lx\n",
3772 hSvc
->Handle
.DesiredAccess
);
3773 return ERROR_ACCESS_DENIED
;
3776 /* Open the Services Reg key */
3777 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
3778 L
"System\\CurrentControlSet\\Services",
3783 if (dwError
!= ERROR_SUCCESS
)
3786 /* NOTE: Windows calculates the pcbBytesNeeded based on WCHAR strings for
3787 both EnumDependentServicesA and EnumDependentServicesW. So returned pcbBytesNeeded
3788 are the same for both. Verified in WINXP. */
3790 /* First determine the bytes needed and get the number of dependent services*/
3791 dwError
= Int_EnumDependentServicesW(hServicesKey
,
3796 &dwServicesReturned
);
3797 if (dwError
!= ERROR_SUCCESS
)
3800 /* If buffer size is less than the bytes needed or pointer is null*/
3801 if ((!lpServices
) || (cbBufSize
< *pcbBytesNeeded
))
3803 dwError
= ERROR_MORE_DATA
;
3807 /* Allocate memory for array of service pointers */
3808 lpServicesArray
= HeapAlloc(GetProcessHeap(),
3810 (dwServicesReturned
+ 1) * sizeof(PSERVICE
));
3811 if (!lpServicesArray
)
3813 DPRINT("Could not allocate a buffer!!\n");
3814 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
3818 dwServicesReturned
= 0;
3819 *pcbBytesNeeded
= 0;
3821 dwError
= Int_EnumDependentServicesW(hServicesKey
,
3826 &dwServicesReturned
);
3827 if (dwError
!= ERROR_SUCCESS
)
3832 lpServicesPtr
= (LPENUM_SERVICE_STATUSA
)lpServices
;
3833 lpStr
= (LPSTR
)(lpServices
+ (dwServicesReturned
* sizeof(ENUM_SERVICE_STATUSA
)));
3835 /* Copy EnumDepenedentService to Buffer */
3836 for (dwServiceCount
= 0; dwServiceCount
< dwServicesReturned
; dwServiceCount
++)
3838 lpService
= lpServicesArray
[dwServiceCount
];
3840 /* Copy the status info */
3841 memcpy(&lpServicesPtr
->ServiceStatus
,
3843 sizeof(SERVICE_STATUS
));
3845 /* Copy display name */
3846 WideCharToMultiByte(CP_ACP
,
3848 lpService
->lpDisplayName
,
3851 (int)wcslen(lpService
->lpDisplayName
),
3854 lpServicesPtr
->lpDisplayName
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServices
);
3855 lpStr
+= strlen(lpStr
) + 1;
3857 /* Copy service name */
3858 WideCharToMultiByte(CP_ACP
,
3860 lpService
->lpServiceName
,
3863 (int)wcslen(lpService
->lpServiceName
),
3866 lpServicesPtr
->lpServiceName
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServices
);
3867 lpStr
+= strlen(lpStr
) + 1;
3872 *lpServicesReturned
= dwServicesReturned
;
3875 if (lpServicesArray
)
3876 HeapFree(GetProcessHeap(), 0, lpServicesArray
);
3878 RegCloseKey(hServicesKey
);
3880 DPRINT("REnumDependentServicesA() done (Error %lu)\n", dwError
);
3889 REnumServicesStatusA(
3890 SC_RPC_HANDLE hSCManager
,
3891 DWORD dwServiceType
,
3892 DWORD dwServiceState
,
3895 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
3896 LPBOUNDED_DWORD_256K lpServicesReturned
,
3897 LPBOUNDED_DWORD_256K lpResumeHandle
)
3899 LPENUM_SERVICE_STATUSW lpStatusPtrW
= NULL
;
3900 LPENUM_SERVICE_STATUSW lpStatusPtrIncrW
;
3901 LPENUM_SERVICE_STATUSA lpStatusPtrA
= NULL
;
3902 LPWSTR lpStringPtrW
;
3905 DWORD dwServiceCount
;
3907 DPRINT("REnumServicesStatusA() called\n");
3909 if (pcbBytesNeeded
== NULL
|| lpServicesReturned
== NULL
)
3911 return ERROR_INVALID_ADDRESS
;
3914 if ((dwBufSize
> 0) && (lpBuffer
))
3916 lpStatusPtrW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, dwBufSize
);
3919 DPRINT("Failed to allocate buffer!\n");
3920 return ERROR_NOT_ENOUGH_MEMORY
;
3924 dwError
= REnumServicesStatusW(hSCManager
,
3927 (LPBYTE
)lpStatusPtrW
,
3933 /* if no services were returned then we are Done */
3934 if (*lpServicesReturned
== 0)
3937 lpStatusPtrIncrW
= lpStatusPtrW
;
3938 lpStatusPtrA
= (LPENUM_SERVICE_STATUSA
)lpBuffer
;
3939 lpStringPtrA
= (LPSTR
)((ULONG_PTR
)lpBuffer
+
3940 *lpServicesReturned
* sizeof(ENUM_SERVICE_STATUSA
));
3941 lpStringPtrW
= (LPWSTR
)((ULONG_PTR
)lpStatusPtrW
+
3942 *lpServicesReturned
* sizeof(ENUM_SERVICE_STATUSW
));
3944 for (dwServiceCount
= 0; dwServiceCount
< *lpServicesReturned
; dwServiceCount
++)
3946 /* Copy the service name */
3947 WideCharToMultiByte(CP_ACP
,
3952 (int)wcslen(lpStringPtrW
),
3956 lpStatusPtrA
->lpServiceName
= (LPSTR
)((ULONG_PTR
)lpStringPtrA
- (ULONG_PTR
)lpBuffer
);
3957 lpStringPtrA
+= wcslen(lpStringPtrW
) + 1;
3958 lpStringPtrW
+= wcslen(lpStringPtrW
) + 1;
3960 /* Copy the display name */
3961 WideCharToMultiByte(CP_ACP
,
3966 (int)wcslen(lpStringPtrW
),
3970 lpStatusPtrA
->lpDisplayName
= (LPSTR
)((ULONG_PTR
)lpStringPtrA
- (ULONG_PTR
)lpBuffer
);
3971 lpStringPtrA
+= wcslen(lpStringPtrW
) + 1;
3972 lpStringPtrW
+= wcslen(lpStringPtrW
) + 1;
3974 /* Copy the status information */
3975 memcpy(&lpStatusPtrA
->ServiceStatus
,
3976 &lpStatusPtrIncrW
->ServiceStatus
,
3977 sizeof(SERVICE_STATUS
));
3985 HeapFree(GetProcessHeap(), 0, lpStatusPtrW
);
3987 DPRINT("REnumServicesStatusA() done (Error %lu)\n", dwError
);
3997 LPSTR lpMachineName
,
3998 LPSTR lpDatabaseName
,
3999 DWORD dwDesiredAccess
,
4000 LPSC_RPC_HANDLE lpScHandle
)
4002 UNICODE_STRING MachineName
;
4003 UNICODE_STRING DatabaseName
;
4006 DPRINT("ROpenSCManagerA() called\n");
4009 RtlCreateUnicodeStringFromAsciiz(&MachineName
,
4013 RtlCreateUnicodeStringFromAsciiz(&DatabaseName
,
4016 dwError
= ROpenSCManagerW(lpMachineName
? MachineName
.Buffer
: NULL
,
4017 lpDatabaseName
? DatabaseName
.Buffer
: NULL
,
4022 RtlFreeUnicodeString(&MachineName
);
4025 RtlFreeUnicodeString(&DatabaseName
);
4035 SC_RPC_HANDLE hSCManager
,
4036 LPSTR lpServiceName
,
4037 DWORD dwDesiredAccess
,
4038 LPSC_RPC_HANDLE lpServiceHandle
)
4040 UNICODE_STRING ServiceName
;
4043 DPRINT("ROpenServiceA() called\n");
4046 RtlCreateUnicodeStringFromAsciiz(&ServiceName
,
4049 dwError
= ROpenServiceW(hSCManager
,
4050 lpServiceName
? ServiceName
.Buffer
: NULL
,
4055 RtlFreeUnicodeString(&ServiceName
);
4064 RQueryServiceConfigA(
4065 SC_RPC_HANDLE hService
,
4066 LPBYTE lpBuf
, //LPQUERY_SERVICE_CONFIGA lpServiceConfig,
4068 LPBOUNDED_DWORD_8K pcbBytesNeeded
)
4070 LPQUERY_SERVICE_CONFIGA lpServiceConfig
= (LPQUERY_SERVICE_CONFIGA
)lpBuf
;
4071 DWORD dwError
= ERROR_SUCCESS
;
4072 PSERVICE_HANDLE hSvc
;
4073 PSERVICE lpService
= NULL
;
4074 HKEY hServiceKey
= NULL
;
4075 LPWSTR lpImagePath
= NULL
;
4076 LPWSTR lpServiceStartName
= NULL
;
4077 LPWSTR lpDependencies
= NULL
;
4078 DWORD dwDependenciesLength
= 0;
4079 DWORD dwRequiredSize
;
4082 DPRINT("RQueryServiceConfigA() called\n");
4085 return ERROR_SHUTDOWN_IN_PROGRESS
;
4087 hSvc
= ScmGetServiceFromHandle(hService
);
4090 DPRINT1("Invalid service handle!\n");
4091 return ERROR_INVALID_HANDLE
;
4094 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
4095 SERVICE_QUERY_CONFIG
))
4097 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
4098 return ERROR_ACCESS_DENIED
;
4101 lpService
= hSvc
->ServiceEntry
;
4102 if (lpService
== NULL
)
4104 DPRINT("lpService == NULL!\n");
4105 return ERROR_INVALID_HANDLE
;
4108 /* Lock the service database shared */
4109 ScmLockDatabaseShared();
4111 dwError
= ScmOpenServiceKey(lpService
->lpServiceName
,
4114 if (dwError
!= ERROR_SUCCESS
)
4117 /* Read the image path */
4118 dwError
= ScmReadString(hServiceKey
,
4121 if (dwError
!= ERROR_SUCCESS
)
4124 /* Read the service start name */
4125 ScmReadString(hServiceKey
,
4127 &lpServiceStartName
);
4129 /* Read the dependencies */
4130 ScmReadDependencies(hServiceKey
,
4132 &dwDependenciesLength
);
4134 dwRequiredSize
= sizeof(QUERY_SERVICE_CONFIGA
);
4136 if (lpImagePath
!= NULL
)
4137 dwRequiredSize
+= (DWORD
)(wcslen(lpImagePath
) + 1);
4139 dwRequiredSize
+= 2 * sizeof(CHAR
);
4141 if ((lpService
->lpGroup
!= NULL
) && (lpService
->lpGroup
->lpGroupName
!= NULL
))
4142 dwRequiredSize
+= (DWORD
)(wcslen(lpService
->lpGroup
->lpGroupName
) + 1);
4144 dwRequiredSize
+= 2 * sizeof(CHAR
);
4146 /* Add Dependencies length */
4147 if (lpDependencies
!= NULL
)
4148 dwRequiredSize
+= dwDependenciesLength
;
4150 dwRequiredSize
+= 2 * sizeof(CHAR
);
4152 if (lpServiceStartName
!= NULL
)
4153 dwRequiredSize
+= (DWORD
)(wcslen(lpServiceStartName
) + 1);
4155 dwRequiredSize
+= 2 * sizeof(CHAR
);
4157 if (lpService
->lpDisplayName
!= NULL
)
4158 dwRequiredSize
+= (DWORD
)(wcslen(lpService
->lpDisplayName
) + 1);
4160 dwRequiredSize
+= 2 * sizeof(CHAR
);
4162 if (lpServiceConfig
== NULL
|| cbBufSize
< dwRequiredSize
)
4164 dwError
= ERROR_INSUFFICIENT_BUFFER
;
4168 lpServiceConfig
->dwServiceType
= lpService
->Status
.dwServiceType
;
4169 lpServiceConfig
->dwStartType
= lpService
->dwStartType
;
4170 lpServiceConfig
->dwErrorControl
= lpService
->dwErrorControl
;
4171 lpServiceConfig
->dwTagId
= lpService
->dwTag
;
4173 lpStr
= (LPSTR
)(lpServiceConfig
+ 1);
4175 /* NOTE: Strings that are NULL for QUERY_SERVICE_CONFIG are pointers to empty strings.
4176 Verified in WINXP */
4180 WideCharToMultiByte(CP_ACP
,
4185 (int)(wcslen(lpImagePath
) + 1),
4194 lpServiceConfig
->lpBinaryPathName
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
4195 lpStr
+= (strlen((LPSTR
)lpStr
) + 1);
4197 if (lpService
->lpGroup
&& lpService
->lpGroup
->lpGroupName
)
4199 WideCharToMultiByte(CP_ACP
,
4201 lpService
->lpGroup
->lpGroupName
,
4204 (int)(wcslen(lpService
->lpGroup
->lpGroupName
) + 1),
4213 lpServiceConfig
->lpLoadOrderGroup
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
4214 lpStr
+= (strlen(lpStr
) + 1);
4216 /* Append Dependencies */
4219 WideCharToMultiByte(CP_ACP
,
4222 dwDependenciesLength
,
4224 dwDependenciesLength
,
4233 lpServiceConfig
->lpDependencies
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
4235 lpStr
+= dwDependenciesLength
;
4237 lpStr
+= (strlen(lpStr
) + 1);
4239 if (lpServiceStartName
)
4241 WideCharToMultiByte(CP_ACP
,
4246 (int)(wcslen(lpServiceStartName
) + 1),
4255 lpServiceConfig
->lpServiceStartName
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
4256 lpStr
+= (strlen(lpStr
) + 1);
4258 if (lpService
->lpDisplayName
)
4260 WideCharToMultiByte(CP_ACP
,
4262 lpService
->lpDisplayName
,
4265 (int)(wcslen(lpService
->lpDisplayName
) + 1),
4274 lpServiceConfig
->lpDisplayName
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
4277 if (pcbBytesNeeded
!= NULL
)
4278 *pcbBytesNeeded
= dwRequiredSize
;
4281 /* Unlock the service database */
4282 ScmUnlockDatabase();
4284 if (lpImagePath
!= NULL
)
4285 HeapFree(GetProcessHeap(), 0, lpImagePath
);
4287 if (lpServiceStartName
!= NULL
)
4288 HeapFree(GetProcessHeap(), 0, lpServiceStartName
);
4290 if (lpDependencies
!= NULL
)
4291 HeapFree(GetProcessHeap(), 0, lpDependencies
);
4293 if (hServiceKey
!= NULL
)
4294 RegCloseKey(hServiceKey
);
4296 DPRINT("RQueryServiceConfigA() done\n");
4305 RQueryServiceLockStatusA(
4306 SC_RPC_HANDLE hSCManager
,
4307 LPBYTE lpBuf
, // LPQUERY_SERVICE_LOCK_STATUSA lpLockStatus,
4309 LPBOUNDED_DWORD_4K pcbBytesNeeded
)
4311 LPQUERY_SERVICE_LOCK_STATUSA lpLockStatus
= (LPQUERY_SERVICE_LOCK_STATUSA
)lpBuf
;
4312 PMANAGER_HANDLE hMgr
;
4313 DWORD dwRequiredSize
;
4315 if (!lpLockStatus
|| !pcbBytesNeeded
)
4316 return ERROR_INVALID_PARAMETER
;
4318 hMgr
= ScmGetServiceManagerFromHandle(hSCManager
);
4321 DPRINT1("Invalid service manager handle!\n");
4322 return ERROR_INVALID_HANDLE
;
4325 if (!RtlAreAllAccessesGranted(hMgr
->Handle
.DesiredAccess
,
4326 SC_MANAGER_QUERY_LOCK_STATUS
))
4328 DPRINT("Insufficient access rights! 0x%lx\n", hMgr
->Handle
.DesiredAccess
);
4329 return ERROR_ACCESS_DENIED
;
4332 /* FIXME: we need to compute instead the real length of the owner name */
4333 dwRequiredSize
= sizeof(QUERY_SERVICE_LOCK_STATUSA
) + sizeof(CHAR
);
4334 *pcbBytesNeeded
= dwRequiredSize
;
4336 if (cbBufSize
< dwRequiredSize
)
4337 return ERROR_INSUFFICIENT_BUFFER
;
4339 ScmQueryServiceLockStatusA(lpLockStatus
);
4341 return ERROR_SUCCESS
;
4349 SC_RPC_HANDLE hService
,
4351 LPSTRING_PTRSA argv
)
4353 DWORD dwError
= ERROR_SUCCESS
;
4354 PSERVICE_HANDLE hSvc
;
4355 PSERVICE lpService
= NULL
;
4356 LPWSTR
*lpVector
= NULL
;
4360 DPRINT("RStartServiceA() called\n");
4363 return ERROR_SHUTDOWN_IN_PROGRESS
;
4365 hSvc
= ScmGetServiceFromHandle(hService
);
4368 DPRINT1("Invalid service handle!\n");
4369 return ERROR_INVALID_HANDLE
;
4372 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
4375 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
4376 return ERROR_ACCESS_DENIED
;
4379 lpService
= hSvc
->ServiceEntry
;
4380 if (lpService
== NULL
)
4382 DPRINT("lpService == NULL!\n");
4383 return ERROR_INVALID_HANDLE
;
4386 if (lpService
->dwStartType
== SERVICE_DISABLED
)
4387 return ERROR_SERVICE_DISABLED
;
4389 if (lpService
->bDeleted
)
4390 return ERROR_SERVICE_MARKED_FOR_DELETE
;
4392 /* Build a Unicode argument vector */
4395 lpVector
= HeapAlloc(GetProcessHeap(),
4397 argc
* sizeof(LPWSTR
));
4398 if (lpVector
== NULL
)
4399 return ERROR_NOT_ENOUGH_MEMORY
;
4401 for (i
= 0; i
< argc
; i
++)
4403 dwLength
= MultiByteToWideChar(CP_ACP
,
4410 lpVector
[i
] = HeapAlloc(GetProcessHeap(),
4412 dwLength
* sizeof(WCHAR
));
4413 if (lpVector
[i
] == NULL
)
4415 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
4419 MultiByteToWideChar(CP_ACP
,
4428 /* Start the service */
4429 dwError
= ScmStartService(lpService
, argc
, lpVector
);
4432 /* Free the Unicode argument vector */
4433 if (lpVector
!= NULL
)
4435 for (i
= 0; i
< argc
; i
++)
4437 if (lpVector
[i
] != NULL
)
4438 HeapFree(GetProcessHeap(), 0, lpVector
[i
]);
4440 HeapFree(GetProcessHeap(), 0, lpVector
);
4450 RGetServiceDisplayNameA(
4451 SC_RPC_HANDLE hSCManager
,
4452 LPCSTR lpServiceName
,
4453 LPSTR lpDisplayName
,
4454 LPBOUNDED_DWORD_4K lpcchBuffer
)
4456 // PMANAGER_HANDLE hManager;
4457 PSERVICE lpService
= NULL
;
4458 LPCWSTR lpSvcDisplayName
;
4459 LPWSTR lpServiceNameW
;
4462 DPRINT("RGetServiceDisplayNameA() called\n");
4463 DPRINT("hSCManager = %p\n", hSCManager
);
4464 DPRINT("lpServiceName: %s\n", lpServiceName
);
4465 DPRINT("lpDisplayName: %p\n", lpDisplayName
);
4466 DPRINT("*lpcchBuffer: %lu\n", *lpcchBuffer
);
4469 hManager
= (PMANAGER_HANDLE
)hSCManager
;
4470 if (hManager
->Handle
.Tag
!= MANAGER_TAG
)
4472 DPRINT("Invalid manager handle!\n");
4473 return ERROR_INVALID_HANDLE
;
4477 /* Get service database entry */
4478 if (lpServiceName
!= NULL
)
4480 dwLength
= (DWORD
)(strlen(lpServiceName
) + 1);
4481 lpServiceNameW
= HeapAlloc(GetProcessHeap(),
4483 dwLength
* sizeof(WCHAR
));
4484 if (!lpServiceNameW
)
4485 return ERROR_NOT_ENOUGH_MEMORY
;
4487 MultiByteToWideChar(CP_ACP
,
4494 lpService
= ScmGetServiceEntryByName(lpServiceNameW
);
4496 HeapFree(GetProcessHeap(), 0, lpServiceNameW
);
4499 if (lpService
== NULL
)
4501 DPRINT("Could not find the service!\n");
4502 return ERROR_SERVICE_DOES_NOT_EXIST
;
4505 if (lpService
->lpDisplayName
)
4506 lpSvcDisplayName
= lpService
->lpDisplayName
;
4508 lpSvcDisplayName
= lpService
->lpServiceName
;
4511 * NOTE: On Windows the comparison on *lpcchBuffer is made against
4512 * the number of (wide) characters of the UNICODE display name, and
4513 * not against the number of bytes needed to store the ANSI string.
4515 dwLength
= (DWORD
)wcslen(lpSvcDisplayName
);
4517 if (*lpcchBuffer
> dwLength
)
4519 if (lpDisplayName
!= NULL
&&
4520 WideCharToMultiByte(CP_ACP
,
4530 * But then, if *lpcchBuffer was greater than the number of
4531 * (wide) characters of the UNICODE display name, yet smaller
4532 * than the number of bytes needed due to the possible presence
4533 * of DBCS characters, the *exact* number of bytes is returned
4534 * (without the NULL terminator).
4536 dwLength
= (DWORD
)WideCharToMultiByte(CP_ACP
,
4545 *lpcchBuffer
= dwLength
;
4546 return ERROR_INSUFFICIENT_BUFFER
;
4550 * NOTE: On Windows, RGetServiceDisplayNameA() does not update
4551 * *lpcchBuffer on success, contrary to RGetServiceDisplayNameW().
4553 return ERROR_SUCCESS
;
4558 * NOTE: On Windows, if *lpcchBuffer is smaller than the number of
4559 * (wide) characters of the UNICODE display name, only an upper
4560 * estimation is returned by doubling the string length, to account
4561 * for the presence of any possible DBCS characters.
4563 *lpcchBuffer
= dwLength
* sizeof(WCHAR
);
4564 return ERROR_INSUFFICIENT_BUFFER
;
4572 RGetServiceKeyNameA(
4573 SC_RPC_HANDLE hSCManager
,
4574 LPCSTR lpDisplayName
,
4575 LPSTR lpServiceName
,
4576 LPBOUNDED_DWORD_4K lpcchBuffer
)
4578 // PMANAGER_HANDLE hManager;
4580 LPWSTR lpDisplayNameW
;
4583 DPRINT("RGetServiceKeyNameA() called\n");
4584 DPRINT("hSCManager = %p\n", hSCManager
);
4585 DPRINT("lpDisplayName: %s\n", lpDisplayName
);
4586 DPRINT("lpServiceName: %p\n", lpServiceName
);
4587 DPRINT("*lpcchBuffer: %lu\n", *lpcchBuffer
);
4590 hManager
= (PMANAGER_HANDLE
)hSCManager
;
4591 if (hManager
->Handle
.Tag
!= MANAGER_TAG
)
4593 DPRINT("Invalid manager handle!\n");
4594 return ERROR_INVALID_HANDLE
;
4598 /* Get service database entry */
4600 dwLength
= (DWORD
)(strlen(lpDisplayName
) + 1);
4601 lpDisplayNameW
= HeapAlloc(GetProcessHeap(),
4603 dwLength
* sizeof(WCHAR
));
4604 if (!lpDisplayNameW
)
4605 return ERROR_NOT_ENOUGH_MEMORY
;
4607 MultiByteToWideChar(CP_ACP
,
4614 lpService
= ScmGetServiceEntryByDisplayName(lpDisplayNameW
);
4616 HeapFree(GetProcessHeap(), 0, lpDisplayNameW
);
4618 if (lpService
== NULL
)
4620 DPRINT("Could not find the service!\n");
4621 return ERROR_SERVICE_DOES_NOT_EXIST
;
4625 * NOTE: On Windows the comparison on *lpcchBuffer is made against
4626 * the number of (wide) characters of the UNICODE service name, and
4627 * not against the number of bytes needed to store the ANSI string.
4629 dwLength
= (DWORD
)wcslen(lpService
->lpServiceName
);
4631 if (*lpcchBuffer
> dwLength
)
4633 if (lpServiceName
!= NULL
&&
4634 WideCharToMultiByte(CP_ACP
,
4636 lpService
->lpServiceName
,
4644 * But then, if *lpcchBuffer was greater than the number of
4645 * (wide) characters of the UNICODE service name, yet smaller
4646 * than the number of bytes needed due to the possible presence
4647 * of DBCS characters, the *exact* number of bytes is returned
4648 * (without the NULL terminator).
4650 dwLength
= (DWORD
)WideCharToMultiByte(CP_ACP
,
4652 lpService
->lpServiceName
,
4659 *lpcchBuffer
= dwLength
;
4660 return ERROR_INSUFFICIENT_BUFFER
;
4664 * NOTE: On Windows, RGetServiceKeyNameA() does not update
4665 * *lpcchBuffer on success, contrary to RGetServiceKeyNameW().
4667 return ERROR_SUCCESS
;
4672 * NOTE: On Windows, if *lpcchBuffer is smaller than the number of
4673 * (wide) characters of the UNICODE service name, only an upper
4674 * estimation is returned by doubling the string length, to account
4675 * for the presence of any possible DBCS characters.
4677 *lpcchBuffer
= dwLength
* sizeof(WCHAR
);
4678 return ERROR_INSUFFICIENT_BUFFER
;
4686 RI_ScGetCurrentGroupStateW(
4687 SC_RPC_HANDLE hSCManager
,
4688 LPWSTR lpLoadOrderGroup
,
4691 PMANAGER_HANDLE hManager
;
4692 PSERVICE_GROUP pServiceGroup
;
4693 DWORD dwError
= ERROR_SUCCESS
;
4695 DPRINT("RI_ScGetCurrentGroupStateW() called\n");
4698 return ERROR_SHUTDOWN_IN_PROGRESS
;
4700 hManager
= ScmGetServiceManagerFromHandle(hSCManager
);
4701 if (hManager
== NULL
)
4703 DPRINT1("Invalid service manager handle!\n");
4704 return ERROR_INVALID_HANDLE
;
4707 /* Check for SC_MANAGER_ENUMERATE_SERVICE access right */
4708 if (!RtlAreAllAccessesGranted(hManager
->Handle
.DesiredAccess
,
4709 SC_MANAGER_ENUMERATE_SERVICE
))
4711 DPRINT("Insufficient access rights! 0x%lx\n",
4712 hManager
->Handle
.DesiredAccess
);
4713 return ERROR_ACCESS_DENIED
;
4716 /* Lock the service database shared */
4717 ScmLockDatabaseShared();
4719 /* Get the group list entry */
4720 pServiceGroup
= ScmGetServiceGroupByName(lpLoadOrderGroup
);
4721 if (pServiceGroup
== NULL
)
4723 dwError
= ERROR_SERVICE_DOES_NOT_EXIST
;
4727 /* FIXME: Return the group state */
4731 /* Unlock the service database */
4732 ScmUnlockDatabase();
4734 DPRINT("RI_ScGetCurrentGroupStateW() done (Error %lu)\n", dwError
);
4744 SC_RPC_HANDLE hSCManager
,
4745 DWORD dwServiceType
,
4746 DWORD dwServiceState
,
4749 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
4750 LPBOUNDED_DWORD_256K lpServicesReturned
,
4751 LPBOUNDED_DWORD_256K lpResumeIndex
,
4752 LPCWSTR pszGroupName
)
4754 PMANAGER_HANDLE hManager
;
4756 DWORD dwError
= ERROR_SUCCESS
;
4757 PLIST_ENTRY ServiceEntry
;
4758 PSERVICE CurrentService
;
4760 DWORD dwRequiredSize
;
4761 DWORD dwServiceCount
;
4763 DWORD dwLastResumeCount
= 0;
4764 LPENUM_SERVICE_STATUSW lpStatusPtr
;
4767 DPRINT("REnumServiceGroupW() called\n");
4770 return ERROR_SHUTDOWN_IN_PROGRESS
;
4772 hManager
= ScmGetServiceManagerFromHandle(hSCManager
);
4773 if (hManager
== NULL
)
4775 DPRINT1("Invalid service manager handle!\n");
4776 return ERROR_INVALID_HANDLE
;
4779 if (pcbBytesNeeded
== NULL
|| lpServicesReturned
== NULL
)
4781 return ERROR_INVALID_ADDRESS
;
4784 *pcbBytesNeeded
= 0;
4785 *lpServicesReturned
= 0;
4787 if ((dwServiceType
== 0) ||
4788 ((dwServiceType
& ~SERVICE_TYPE_ALL
) != 0))
4790 DPRINT("Not a valid Service Type!\n");
4791 return ERROR_INVALID_PARAMETER
;
4794 if ((dwServiceState
== 0) ||
4795 ((dwServiceState
& ~SERVICE_STATE_ALL
) != 0))
4797 DPRINT("Not a valid Service State!\n");
4798 return ERROR_INVALID_PARAMETER
;
4801 /* Check access rights */
4802 if (!RtlAreAllAccessesGranted(hManager
->Handle
.DesiredAccess
,
4803 SC_MANAGER_ENUMERATE_SERVICE
))
4805 DPRINT("Insufficient access rights! 0x%lx\n",
4806 hManager
->Handle
.DesiredAccess
);
4807 return ERROR_ACCESS_DENIED
;
4811 dwLastResumeCount
= *lpResumeIndex
;
4813 /* Lock the service database shared */
4814 ScmLockDatabaseShared();
4816 lpService
= ScmGetServiceEntryByResumeCount(dwLastResumeCount
);
4817 if (lpService
== NULL
)
4819 dwError
= ERROR_SUCCESS
;
4826 for (ServiceEntry
= &lpService
->ServiceListEntry
;
4827 ServiceEntry
!= &ServiceListHead
;
4828 ServiceEntry
= ServiceEntry
->Flink
)
4830 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
4834 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
4837 dwState
= SERVICE_ACTIVE
;
4838 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
4839 dwState
= SERVICE_INACTIVE
;
4841 if ((dwState
& dwServiceState
) == 0)
4846 if (*pszGroupName
== 0)
4848 if (CurrentService
->lpGroup
!= NULL
)
4853 if ((CurrentService
->lpGroup
== NULL
) ||
4854 _wcsicmp(pszGroupName
, CurrentService
->lpGroup
->lpGroupName
) != 0)
4859 dwSize
= sizeof(ENUM_SERVICE_STATUSW
) +
4860 (DWORD
)((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
4861 (DWORD
)((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
4863 if (dwRequiredSize
+ dwSize
> cbBufSize
)
4865 DPRINT("Service name: %S no fit\n", CurrentService
->lpServiceName
);
4869 DPRINT("Service name: %S fit\n", CurrentService
->lpServiceName
);
4870 dwRequiredSize
+= dwSize
;
4872 dwLastResumeCount
= CurrentService
->dwResumeCount
;
4875 DPRINT("dwRequiredSize: %lu\n", dwRequiredSize
);
4876 DPRINT("dwServiceCount: %lu\n", dwServiceCount
);
4879 ServiceEntry
!= &ServiceListHead
;
4880 ServiceEntry
= ServiceEntry
->Flink
)
4882 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
4886 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
4889 dwState
= SERVICE_ACTIVE
;
4890 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
4891 dwState
= SERVICE_INACTIVE
;
4893 if ((dwState
& dwServiceState
) == 0)
4898 if (*pszGroupName
== 0)
4900 if (CurrentService
->lpGroup
!= NULL
)
4905 if ((CurrentService
->lpGroup
== NULL
) ||
4906 _wcsicmp(pszGroupName
, CurrentService
->lpGroup
->lpGroupName
) != 0)
4911 dwRequiredSize
+= (sizeof(ENUM_SERVICE_STATUSW
) +
4912 (DWORD
)((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
4913 (DWORD
)((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
)));
4915 dwError
= ERROR_MORE_DATA
;
4918 DPRINT("*pcbBytesNeeded: %lu\n", dwRequiredSize
);
4921 *lpResumeIndex
= dwLastResumeCount
;
4923 *lpServicesReturned
= dwServiceCount
;
4924 *pcbBytesNeeded
= dwRequiredSize
;
4926 lpStatusPtr
= (LPENUM_SERVICE_STATUSW
)lpBuffer
;
4927 lpStringPtr
= (LPWSTR
)((ULONG_PTR
)lpBuffer
+
4928 dwServiceCount
* sizeof(ENUM_SERVICE_STATUSW
));
4931 for (ServiceEntry
= &lpService
->ServiceListEntry
;
4932 ServiceEntry
!= &ServiceListHead
;
4933 ServiceEntry
= ServiceEntry
->Flink
)
4935 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
4939 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
4942 dwState
= SERVICE_ACTIVE
;
4943 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
4944 dwState
= SERVICE_INACTIVE
;
4946 if ((dwState
& dwServiceState
) == 0)
4951 if (*pszGroupName
== 0)
4953 if (CurrentService
->lpGroup
!= NULL
)
4958 if ((CurrentService
->lpGroup
== NULL
) ||
4959 _wcsicmp(pszGroupName
, CurrentService
->lpGroup
->lpGroupName
) != 0)
4964 dwSize
= sizeof(ENUM_SERVICE_STATUSW
) +
4965 (DWORD
)((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
4966 (DWORD
)((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
4968 if (dwRequiredSize
+ dwSize
> cbBufSize
)
4971 /* Copy the service name */
4972 wcscpy(lpStringPtr
, CurrentService
->lpServiceName
);
4973 lpStatusPtr
->lpServiceName
= (LPWSTR
)((ULONG_PTR
)lpStringPtr
- (ULONG_PTR
)lpBuffer
);
4974 lpStringPtr
+= (wcslen(CurrentService
->lpServiceName
) + 1);
4976 /* Copy the display name */
4977 wcscpy(lpStringPtr
, CurrentService
->lpDisplayName
);
4978 lpStatusPtr
->lpDisplayName
= (LPWSTR
)((ULONG_PTR
)lpStringPtr
- (ULONG_PTR
)lpBuffer
);
4979 lpStringPtr
+= (wcslen(CurrentService
->lpDisplayName
) + 1);
4981 /* Copy the status information */
4982 memcpy(&lpStatusPtr
->ServiceStatus
,
4983 &CurrentService
->Status
,
4984 sizeof(SERVICE_STATUS
));
4987 dwRequiredSize
+= dwSize
;
4990 if (dwError
== ERROR_SUCCESS
)
4992 *pcbBytesNeeded
= 0;
4993 if (lpResumeIndex
) *lpResumeIndex
= 0;
4997 /* Unlock the service database */
4998 ScmUnlockDatabase();
5000 DPRINT("REnumServiceGroupW() done (Error %lu)\n", dwError
);
5009 RChangeServiceConfig2A(
5010 SC_RPC_HANDLE hService
,
5011 SC_RPC_CONFIG_INFOA Info
)
5013 SC_RPC_CONFIG_INFOW InfoW
= { 0 };
5014 DWORD dwRet
, dwLength
;
5017 DPRINT("RChangeServiceConfig2A() called\n");
5018 DPRINT("dwInfoLevel = %lu\n", Info
.dwInfoLevel
);
5020 if ((Info
.dwInfoLevel
< SERVICE_CONFIG_DESCRIPTION
) ||
5021 (Info
.dwInfoLevel
> SERVICE_CONFIG_FAILURE_ACTIONS
))
5023 return ERROR_INVALID_LEVEL
;
5026 InfoW
.dwInfoLevel
= Info
.dwInfoLevel
;
5028 if (InfoW
.dwInfoLevel
== SERVICE_CONFIG_DESCRIPTION
)
5030 LPSERVICE_DESCRIPTIONW lpServiceDescriptionW
;
5031 LPSERVICE_DESCRIPTIONA lpServiceDescriptionA
;
5033 lpServiceDescriptionA
= Info
.psd
;
5035 if (lpServiceDescriptionA
&&
5036 lpServiceDescriptionA
->lpDescription
)
5038 dwLength
= (DWORD
)((strlen(lpServiceDescriptionA
->lpDescription
) + 1) * sizeof(WCHAR
));
5040 lpServiceDescriptionW
= HeapAlloc(GetProcessHeap(),
5042 dwLength
+ sizeof(SERVICE_DESCRIPTIONW
));
5043 if (!lpServiceDescriptionW
)
5045 return ERROR_NOT_ENOUGH_MEMORY
;
5048 lpServiceDescriptionW
->lpDescription
= (LPWSTR
)(lpServiceDescriptionW
+ 1);
5050 MultiByteToWideChar(CP_ACP
,
5052 lpServiceDescriptionA
->lpDescription
,
5054 lpServiceDescriptionW
->lpDescription
,
5057 ptr
= lpServiceDescriptionW
;
5058 InfoW
.psd
= lpServiceDescriptionW
;
5061 else if (Info
.dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
5063 LPSERVICE_FAILURE_ACTIONSW lpServiceFailureActionsW
;
5064 LPSERVICE_FAILURE_ACTIONSA lpServiceFailureActionsA
;
5065 DWORD dwRebootLen
= 0;
5066 DWORD dwCommandLen
= 0;
5067 DWORD dwActionArrayLen
= 0;
5068 LPWSTR lpStr
= NULL
;
5070 lpServiceFailureActionsA
= Info
.psfa
;
5072 if (lpServiceFailureActionsA
)
5075 * The following code is inspired by the
5076 * SERVICE_CONFIG_FAILURE_ACTIONS case of
5077 * the RQueryServiceConfig2W function.
5080 /* Retrieve the needed length for the two data strings */
5081 if (lpServiceFailureActionsA
->lpRebootMsg
)
5083 dwRebootLen
= (DWORD
)((strlen(lpServiceFailureActionsA
->lpRebootMsg
) + 1) * sizeof(WCHAR
));
5085 if (lpServiceFailureActionsA
->lpCommand
)
5087 dwCommandLen
= (DWORD
)((strlen(lpServiceFailureActionsA
->lpCommand
) + 1) * sizeof(WCHAR
));
5091 * Retrieve the size of the lpsaActions array if needed.
5092 * We will copy the lpsaActions array only if there is at
5093 * least one action AND that the original array is valid.
5095 if (lpServiceFailureActionsA
->cActions
> 0 && lpServiceFailureActionsA
->lpsaActions
)
5097 dwActionArrayLen
= lpServiceFailureActionsA
->cActions
* sizeof(SC_ACTION
);
5100 /* Compute the total length for the UNICODE structure, including data */
5101 dwLength
= sizeof(SERVICE_FAILURE_ACTIONSW
) +
5102 dwActionArrayLen
+ dwRebootLen
+ dwCommandLen
;
5104 /* Allocate the structure */
5105 lpServiceFailureActionsW
= HeapAlloc(GetProcessHeap(),
5108 if (!lpServiceFailureActionsW
)
5110 return ERROR_NOT_ENOUGH_MEMORY
;
5113 /* Copy the members */
5114 lpServiceFailureActionsW
->dwResetPeriod
= lpServiceFailureActionsA
->dwResetPeriod
;
5115 lpServiceFailureActionsW
->cActions
= lpServiceFailureActionsA
->cActions
;
5117 /* Copy the lpsaActions array if needed */
5118 if (dwActionArrayLen
> 0)
5120 /* The storage zone is just after the end of the SERVICE_FAILURE_ACTIONSW structure */
5121 lpServiceFailureActionsW
->lpsaActions
= (LPSC_ACTION
)((ULONG_PTR
)(lpServiceFailureActionsW
+ 1));
5123 /* dwActionArrayLen == lpServiceFailureActionsW->cActions * sizeof(SC_ACTION) */
5124 RtlCopyMemory(lpServiceFailureActionsW
->lpsaActions
,
5125 lpServiceFailureActionsA
->lpsaActions
,
5130 /* No lpsaActions array */
5131 lpServiceFailureActionsW
->lpsaActions
= NULL
;
5133 /* The data strings are stored just after the lpsaActions array */
5134 lpStr
= (LPWSTR
)((ULONG_PTR
)(lpServiceFailureActionsW
+ 1) + dwActionArrayLen
);
5137 * Convert the data strings to UNICODE
5140 lpServiceFailureActionsW
->lpRebootMsg
= NULL
;
5141 lpServiceFailureActionsW
->lpCommand
= NULL
;
5145 /* lpRebootMsg points just after the lpsaActions array */
5146 lpServiceFailureActionsW
->lpRebootMsg
= lpStr
;
5148 MultiByteToWideChar(CP_ACP
,
5150 lpServiceFailureActionsA
->lpRebootMsg
,
5152 lpServiceFailureActionsW
->lpRebootMsg
,
5155 lpStr
+= dwRebootLen
/ sizeof(WCHAR
);
5160 /* lpRebootMsg points just after the lpRebootMsg data string */
5161 lpServiceFailureActionsW
->lpCommand
= lpStr
;
5163 MultiByteToWideChar(CP_ACP
,
5165 lpServiceFailureActionsA
->lpCommand
,
5167 lpServiceFailureActionsW
->lpCommand
,
5171 /* Set the pointers */
5172 ptr
= lpServiceFailureActionsW
;
5173 InfoW
.psfa
= lpServiceFailureActionsW
;
5177 dwRet
= RChangeServiceConfig2W(hService
, InfoW
);
5179 HeapFree(GetProcessHeap(), 0, ptr
);
5186 ScmSetFailureActions(HKEY hServiceKey
,
5187 LPSERVICE_FAILURE_ACTIONSW lpFailureActions
)
5189 LPSERVICE_FAILURE_ACTIONSW lpReadBuffer
= NULL
;
5190 LPSERVICE_FAILURE_ACTIONSW lpWriteBuffer
= NULL
;
5191 DWORD dwRequiredSize
= 0;
5195 /* There is nothing to be done if we have no failure actions */
5196 if (lpFailureActions
== NULL
)
5197 return ERROR_SUCCESS
;
5200 * 1- Retrieve the original value of FailureActions.
5203 /* Query value length */
5204 dwError
= RegQueryValueExW(hServiceKey
,
5210 if (dwError
!= ERROR_SUCCESS
&&
5211 dwError
!= ERROR_MORE_DATA
&&
5212 dwError
!= ERROR_FILE_NOT_FOUND
)
5217 dwRequiredSize
= (dwType
== REG_BINARY
) ? max(sizeof(SERVICE_FAILURE_ACTIONSW
), dwRequiredSize
)
5218 : sizeof(SERVICE_FAILURE_ACTIONSW
);
5220 /* Initialize the read buffer */
5221 lpReadBuffer
= HeapAlloc(GetProcessHeap(),
5224 if (lpReadBuffer
== NULL
)
5225 return ERROR_NOT_ENOUGH_MEMORY
;
5227 /* Now we can fill the read buffer */
5228 if (dwError
!= ERROR_FILE_NOT_FOUND
&&
5229 dwType
== REG_BINARY
)
5231 dwError
= RegQueryValueExW(hServiceKey
,
5235 (LPBYTE
)lpReadBuffer
,
5237 if (dwError
!= ERROR_SUCCESS
&&
5238 dwError
!= ERROR_FILE_NOT_FOUND
)
5241 if (dwRequiredSize
< sizeof(SERVICE_FAILURE_ACTIONSW
))
5242 dwRequiredSize
= sizeof(SERVICE_FAILURE_ACTIONSW
);
5247 * The value of the error doesn't really matter, the only
5248 * important thing is that it must be != ERROR_SUCCESS.
5250 dwError
= ERROR_INVALID_DATA
;
5253 if (dwError
== ERROR_SUCCESS
)
5255 lpReadBuffer
->cActions
= min(lpReadBuffer
->cActions
, (dwRequiredSize
- sizeof(SERVICE_FAILURE_ACTIONSW
)) / sizeof(SC_ACTION
));
5256 lpReadBuffer
->lpsaActions
= (lpReadBuffer
->cActions
> 0 ? (LPSC_ACTION
)(lpReadBuffer
+ 1) : NULL
);
5260 lpReadBuffer
->dwResetPeriod
= 0;
5261 lpReadBuffer
->cActions
= 0;
5262 lpReadBuffer
->lpsaActions
= NULL
;
5265 lpReadBuffer
->lpRebootMsg
= NULL
;
5266 lpReadBuffer
->lpCommand
= NULL
;
5269 * 2- Initialize the new value to set.
5272 dwRequiredSize
= sizeof(SERVICE_FAILURE_ACTIONSW
);
5274 if (lpFailureActions
->lpsaActions
== NULL
)
5277 * lpFailureActions->cActions is ignored.
5278 * Therefore we use the original values
5279 * of cActions and lpsaActions.
5281 dwRequiredSize
+= lpReadBuffer
->cActions
* sizeof(SC_ACTION
);
5286 * The reset period and array of failure actions
5287 * are deleted if lpFailureActions->cActions == 0 .
5289 dwRequiredSize
+= lpFailureActions
->cActions
* sizeof(SC_ACTION
);
5292 lpWriteBuffer
= HeapAlloc(GetProcessHeap(),
5295 if (lpWriteBuffer
== NULL
)
5297 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
5301 /* Clean the pointers as they have no meaning when the structure is stored in the registry */
5302 lpWriteBuffer
->lpRebootMsg
= NULL
;
5303 lpWriteBuffer
->lpCommand
= NULL
;
5304 lpWriteBuffer
->lpsaActions
= NULL
;
5306 /* Set the members */
5307 if (lpFailureActions
->lpsaActions
== NULL
)
5310 * lpFailureActions->dwResetPeriod and lpFailureActions->cActions are ignored.
5311 * Therefore we use the original values of dwResetPeriod, cActions and lpsaActions.
5313 lpWriteBuffer
->dwResetPeriod
= lpReadBuffer
->dwResetPeriod
;
5314 lpWriteBuffer
->cActions
= lpReadBuffer
->cActions
;
5316 if (lpReadBuffer
->lpsaActions
!= NULL
)
5318 memmove(lpWriteBuffer
+ 1,
5319 lpReadBuffer
->lpsaActions
,
5320 lpReadBuffer
->cActions
* sizeof(SC_ACTION
));
5325 if (lpFailureActions
->cActions
> 0)
5327 lpWriteBuffer
->dwResetPeriod
= lpFailureActions
->dwResetPeriod
;
5328 lpWriteBuffer
->cActions
= lpFailureActions
->cActions
;
5330 memmove(lpWriteBuffer
+ 1,
5331 lpFailureActions
->lpsaActions
,
5332 lpFailureActions
->cActions
* sizeof(SC_ACTION
));
5336 /* The reset period and array of failure actions are deleted */
5337 lpWriteBuffer
->dwResetPeriod
= 0;
5338 lpWriteBuffer
->cActions
= 0;
5342 /* Save the new failure actions into the registry */
5343 dwError
= RegSetValueExW(hServiceKey
,
5347 (LPBYTE
)lpWriteBuffer
,
5350 /* We modify the strings only in case of success.*/
5351 if (dwError
== ERROR_SUCCESS
)
5353 /* Modify the Reboot Message value, if specified */
5354 if (lpFailureActions
->lpRebootMsg
!= NULL
)
5356 /* If the Reboot Message is "" then we delete it */
5357 if (*lpFailureActions
->lpRebootMsg
== 0)
5359 DPRINT("Delete Reboot Message value\n");
5360 RegDeleteValueW(hServiceKey
, L
"RebootMessage");
5364 DPRINT("Setting Reboot Message value %S\n", lpFailureActions
->lpRebootMsg
);
5365 RegSetValueExW(hServiceKey
,
5369 (LPBYTE
)lpFailureActions
->lpRebootMsg
,
5370 (DWORD
)((wcslen(lpFailureActions
->lpRebootMsg
) + 1) * sizeof(WCHAR
)));
5374 /* Modify the Failure Command value, if specified */
5375 if (lpFailureActions
->lpCommand
!= NULL
)
5377 /* If the FailureCommand string is an empty string, delete the value */
5378 if (*lpFailureActions
->lpCommand
== 0)
5380 DPRINT("Delete Failure Command value\n");
5381 RegDeleteValueW(hServiceKey
, L
"FailureCommand");
5385 DPRINT("Setting Failure Command value %S\n", lpFailureActions
->lpCommand
);
5386 RegSetValueExW(hServiceKey
,
5390 (LPBYTE
)lpFailureActions
->lpCommand
,
5391 (DWORD
)((wcslen(lpFailureActions
->lpCommand
) + 1) * sizeof(WCHAR
)));
5397 if (lpWriteBuffer
!= NULL
)
5398 HeapFree(GetProcessHeap(), 0, lpWriteBuffer
);
5400 if (lpReadBuffer
!= NULL
)
5401 HeapFree(GetProcessHeap(), 0, lpReadBuffer
);
5410 RChangeServiceConfig2W(
5411 SC_RPC_HANDLE hService
,
5412 SC_RPC_CONFIG_INFOW Info
)
5414 DWORD dwError
= ERROR_SUCCESS
;
5415 PSERVICE_HANDLE hSvc
;
5416 PSERVICE lpService
= NULL
;
5417 HKEY hServiceKey
= NULL
;
5418 ACCESS_MASK RequiredAccess
= SERVICE_CHANGE_CONFIG
;
5420 DPRINT("RChangeServiceConfig2W() called\n");
5421 DPRINT("dwInfoLevel = %lu\n", Info
.dwInfoLevel
);
5424 return ERROR_SHUTDOWN_IN_PROGRESS
;
5426 if ((Info
.dwInfoLevel
< SERVICE_CONFIG_DESCRIPTION
) ||
5427 (Info
.dwInfoLevel
> SERVICE_CONFIG_FAILURE_ACTIONS
))
5429 return ERROR_INVALID_LEVEL
;
5432 hSvc
= ScmGetServiceFromHandle(hService
);
5435 DPRINT("Invalid service handle!\n");
5436 return ERROR_INVALID_HANDLE
;
5439 if (Info
.dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
5440 RequiredAccess
|= SERVICE_START
;
5442 /* Check the access rights */
5443 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
5446 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
5447 return ERROR_ACCESS_DENIED
;
5450 if (Info
.dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
5452 /* FIXME: Check if the caller has the SE_SHUTDOWN_NAME privilege */
5456 lpService
= hSvc
->ServiceEntry
;
5457 if (lpService
== NULL
)
5459 DPRINT("lpService == NULL!\n");
5460 return ERROR_INVALID_HANDLE
;
5463 /* Failure actions can only be set for Win32 services, not for drivers */
5464 if (Info
.dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
5466 if (lpService
->Status
.dwServiceType
& SERVICE_DRIVER
)
5467 return ERROR_CANNOT_DETECT_DRIVER_FAILURE
;
5470 /* Lock the service database exclusively */
5471 ScmLockDatabaseExclusive();
5473 if (lpService
->bDeleted
)
5475 DPRINT("The service has already been marked for delete!\n");
5476 dwError
= ERROR_SERVICE_MARKED_FOR_DELETE
;
5480 /* Open the service key */
5481 dwError
= ScmOpenServiceKey(lpService
->szServiceName
,
5482 KEY_READ
| KEY_SET_VALUE
,
5484 if (dwError
!= ERROR_SUCCESS
)
5487 if (Info
.dwInfoLevel
== SERVICE_CONFIG_DESCRIPTION
)
5489 LPSERVICE_DESCRIPTIONW lpServiceDescription
= (LPSERVICE_DESCRIPTIONW
)Info
.psd
;
5491 /* Modify the service description, if specified */
5492 if (lpServiceDescription
!= NULL
&&
5493 lpServiceDescription
->lpDescription
!= NULL
)
5495 /* If the description is "" then we delete it */
5496 if (*lpServiceDescription
->lpDescription
== 0)
5498 DPRINT("Delete service description\n");
5499 dwError
= RegDeleteValueW(hServiceKey
, L
"Description");
5501 if (dwError
== ERROR_FILE_NOT_FOUND
)
5502 dwError
= ERROR_SUCCESS
;
5506 DPRINT("Setting service description value %S\n", lpServiceDescription
->lpDescription
);
5507 dwError
= RegSetValueExW(hServiceKey
,
5511 (LPBYTE
)lpServiceDescription
->lpDescription
,
5512 (DWORD
)((wcslen(lpServiceDescription
->lpDescription
) + 1) * sizeof(WCHAR
)));
5517 dwError
= ERROR_SUCCESS
;
5520 else if (Info
.dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
5522 dwError
= ScmSetFailureActions(hServiceKey
,
5523 (LPSERVICE_FAILURE_ACTIONSW
)Info
.psfa
);
5527 if (hServiceKey
!= NULL
)
5528 RegCloseKey(hServiceKey
);
5530 /* Unlock the service database */
5531 ScmUnlockDatabase();
5533 DPRINT("RChangeServiceConfig2W() done (Error %lu)\n", dwError
);
5542 RQueryServiceConfig2A(
5543 SC_RPC_HANDLE hService
,
5547 LPBOUNDED_DWORD_8K pcbBytesNeeded
)
5549 DWORD dwError
= ERROR_SUCCESS
;
5550 PSERVICE_HANDLE hSvc
;
5551 PSERVICE lpService
= NULL
;
5552 HKEY hServiceKey
= NULL
;
5553 DWORD dwRequiredSize
= 0;
5555 LPWSTR lpDescriptionW
= NULL
;
5556 LPWSTR lpRebootMessageW
= NULL
;
5557 LPWSTR lpFailureCommandW
= NULL
;
5559 DPRINT("RQueryServiceConfig2A() called hService %p dwInfoLevel %u, lpBuffer %p cbBufSize %u pcbBytesNeeded %p\n",
5560 hService
, dwInfoLevel
, lpBuffer
, cbBufSize
, pcbBytesNeeded
);
5563 return ERROR_INVALID_ADDRESS
;
5566 return ERROR_SHUTDOWN_IN_PROGRESS
;
5568 if ((dwInfoLevel
< SERVICE_CONFIG_DESCRIPTION
) ||
5569 (dwInfoLevel
> SERVICE_CONFIG_FAILURE_ACTIONS
))
5571 return ERROR_INVALID_LEVEL
;
5574 hSvc
= ScmGetServiceFromHandle(hService
);
5577 DPRINT1("Invalid service handle!\n");
5578 return ERROR_INVALID_HANDLE
;
5581 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
5582 SERVICE_QUERY_CONFIG
))
5584 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
5585 return ERROR_ACCESS_DENIED
;
5588 lpService
= hSvc
->ServiceEntry
;
5589 if (lpService
== NULL
)
5591 DPRINT("lpService == NULL!\n");
5592 return ERROR_INVALID_HANDLE
;
5595 /* Lock the service database shared */
5596 ScmLockDatabaseShared();
5598 dwError
= ScmOpenServiceKey(lpService
->lpServiceName
,
5601 if (dwError
!= ERROR_SUCCESS
)
5604 if (dwInfoLevel
== SERVICE_CONFIG_DESCRIPTION
)
5606 LPSERVICE_DESCRIPTIONA lpServiceDescription
= (LPSERVICE_DESCRIPTIONA
)lpBuffer
;
5609 dwError
= ScmReadString(hServiceKey
,
5612 if (dwError
!= ERROR_SUCCESS
&& dwError
!= ERROR_FILE_NOT_FOUND
)
5615 *pcbBytesNeeded
= sizeof(SERVICE_DESCRIPTIONA
);
5616 if (dwError
== ERROR_SUCCESS
)
5617 *pcbBytesNeeded
+= (DWORD
)((wcslen(lpDescriptionW
) + 1) * sizeof(WCHAR
));
5619 if (cbBufSize
< *pcbBytesNeeded
)
5621 dwError
= ERROR_INSUFFICIENT_BUFFER
;
5625 if (dwError
== ERROR_SUCCESS
)
5627 lpStr
= (LPSTR
)(lpServiceDescription
+ 1);
5629 WideCharToMultiByte(CP_ACP
,
5634 (int)wcslen(lpDescriptionW
),
5637 lpServiceDescription
->lpDescription
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceDescription
);
5641 lpServiceDescription
->lpDescription
= NULL
;
5642 dwError
= ERROR_SUCCESS
;
5645 else if (dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
5647 LPSERVICE_FAILURE_ACTIONSA lpFailureActions
= (LPSERVICE_FAILURE_ACTIONSA
)lpBuffer
;
5650 /* Query value length */
5651 dwError
= RegQueryValueExW(hServiceKey
,
5657 if (dwError
!= ERROR_SUCCESS
&&
5658 dwError
!= ERROR_MORE_DATA
&&
5659 dwError
!= ERROR_FILE_NOT_FOUND
)
5664 dwRequiredSize
= (dwType
== REG_BINARY
) ? max(sizeof(SERVICE_FAILURE_ACTIONSA
), dwRequiredSize
)
5665 : sizeof(SERVICE_FAILURE_ACTIONSA
);
5667 /* Get the strings */
5668 ScmReadString(hServiceKey
,
5670 &lpFailureCommandW
);
5672 ScmReadString(hServiceKey
,
5676 if (lpRebootMessageW
)
5677 dwRequiredSize
+= (DWORD
)((wcslen(lpRebootMessageW
) + 1) * sizeof(WCHAR
));
5679 if (lpFailureCommandW
)
5680 dwRequiredSize
+= (DWORD
)((wcslen(lpFailureCommandW
) + 1) * sizeof(WCHAR
));
5682 if (cbBufSize
< dwRequiredSize
)
5684 *pcbBytesNeeded
= dwRequiredSize
;
5685 dwError
= ERROR_INSUFFICIENT_BUFFER
;
5689 /* Now we can fill the buffer */
5690 if (dwError
!= ERROR_FILE_NOT_FOUND
&& dwType
== REG_BINARY
)
5692 dwError
= RegQueryValueExW(hServiceKey
,
5696 (LPBYTE
)lpFailureActions
,
5698 if (dwError
!= ERROR_SUCCESS
&& dwError
!= ERROR_FILE_NOT_FOUND
)
5701 if (dwRequiredSize
< sizeof(SERVICE_FAILURE_ACTIONSA
))
5702 dwRequiredSize
= sizeof(SERVICE_FAILURE_ACTIONSA
);
5707 * The value of the error doesn't really matter, the only
5708 * important thing is that it must be != ERROR_SUCCESS .
5710 dwError
= ERROR_INVALID_DATA
;
5713 if (dwError
== ERROR_SUCCESS
)
5715 lpFailureActions
->cActions
= min(lpFailureActions
->cActions
, (dwRequiredSize
- sizeof(SERVICE_FAILURE_ACTIONSA
)) / sizeof(SC_ACTION
));
5717 /* Here lpFailureActions->lpsaActions contains an offset. The conversion is done by the caller. */
5718 lpFailureActions
->lpsaActions
= (lpFailureActions
->cActions
> 0 ? (LPSC_ACTION
)(ULONG_PTR
)sizeof(SERVICE_FAILURE_ACTIONSA
) : NULL
);
5720 lpStr
= (LPSTR
)((ULONG_PTR
)(lpFailureActions
+ 1) + lpFailureActions
->cActions
* sizeof(SC_ACTION
));
5724 lpFailureActions
->dwResetPeriod
= 0;
5725 lpFailureActions
->cActions
= 0;
5726 lpFailureActions
->lpsaActions
= NULL
;
5727 lpStr
= (LPSTR
)(lpFailureActions
+ 1);
5730 lpFailureActions
->lpRebootMsg
= NULL
;
5731 lpFailureActions
->lpCommand
= NULL
;
5733 if (lpRebootMessageW
)
5735 WideCharToMultiByte(CP_ACP
,
5740 (int)wcslen(lpRebootMessageW
),
5743 lpFailureActions
->lpRebootMsg
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpFailureActions
);
5744 lpStr
+= strlen(lpStr
) + 1;
5747 if (lpFailureCommandW
)
5749 WideCharToMultiByte(CP_ACP
,
5754 (int)wcslen(lpFailureCommandW
),
5757 lpFailureActions
->lpCommand
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpFailureActions
);
5758 /* lpStr += strlen(lpStr) + 1; */
5761 dwError
= ERROR_SUCCESS
;
5765 /* Unlock the service database */
5766 ScmUnlockDatabase();
5768 if (lpDescriptionW
!= NULL
)
5769 HeapFree(GetProcessHeap(), 0, lpDescriptionW
);
5771 if (lpRebootMessageW
!= NULL
)
5772 HeapFree(GetProcessHeap(), 0, lpRebootMessageW
);
5774 if (lpFailureCommandW
!= NULL
)
5775 HeapFree(GetProcessHeap(), 0, lpFailureCommandW
);
5777 if (hServiceKey
!= NULL
)
5778 RegCloseKey(hServiceKey
);
5780 DPRINT("RQueryServiceConfig2A() done (Error %lu)\n", dwError
);
5789 RQueryServiceConfig2W(
5790 SC_RPC_HANDLE hService
,
5794 LPBOUNDED_DWORD_8K pcbBytesNeeded
)
5796 DWORD dwError
= ERROR_SUCCESS
;
5797 PSERVICE_HANDLE hSvc
;
5798 PSERVICE lpService
= NULL
;
5799 HKEY hServiceKey
= NULL
;
5800 DWORD dwRequiredSize
= 0;
5802 LPWSTR lpDescription
= NULL
;
5803 LPWSTR lpRebootMessage
= NULL
;
5804 LPWSTR lpFailureCommand
= NULL
;
5806 DPRINT("RQueryServiceConfig2W() called\n");
5809 return ERROR_INVALID_ADDRESS
;
5812 return ERROR_SHUTDOWN_IN_PROGRESS
;
5814 if ((dwInfoLevel
< SERVICE_CONFIG_DESCRIPTION
) ||
5815 (dwInfoLevel
> SERVICE_CONFIG_FAILURE_ACTIONS
))
5817 return ERROR_INVALID_LEVEL
;
5820 hSvc
= ScmGetServiceFromHandle(hService
);
5823 DPRINT1("Invalid service handle!\n");
5824 return ERROR_INVALID_HANDLE
;
5827 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
5828 SERVICE_QUERY_CONFIG
))
5830 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
5831 return ERROR_ACCESS_DENIED
;
5834 lpService
= hSvc
->ServiceEntry
;
5835 if (lpService
== NULL
)
5837 DPRINT("lpService == NULL!\n");
5838 return ERROR_INVALID_HANDLE
;
5841 /* Lock the service database shared */
5842 ScmLockDatabaseShared();
5844 dwError
= ScmOpenServiceKey(lpService
->lpServiceName
,
5847 if (dwError
!= ERROR_SUCCESS
)
5850 if (dwInfoLevel
== SERVICE_CONFIG_DESCRIPTION
)
5852 LPSERVICE_DESCRIPTIONW lpServiceDescription
= (LPSERVICE_DESCRIPTIONW
)lpBuffer
;
5855 dwError
= ScmReadString(hServiceKey
,
5858 if (dwError
!= ERROR_SUCCESS
&& dwError
!= ERROR_FILE_NOT_FOUND
)
5861 *pcbBytesNeeded
= sizeof(SERVICE_DESCRIPTIONW
);
5862 if (dwError
== ERROR_SUCCESS
)
5863 *pcbBytesNeeded
+= (DWORD
)((wcslen(lpDescription
) + 1) * sizeof(WCHAR
));
5865 if (cbBufSize
< *pcbBytesNeeded
)
5867 dwError
= ERROR_INSUFFICIENT_BUFFER
;
5871 if (dwError
== ERROR_SUCCESS
)
5873 lpStr
= (LPWSTR
)(lpServiceDescription
+ 1);
5874 wcscpy(lpStr
, lpDescription
);
5875 lpServiceDescription
->lpDescription
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceDescription
);
5879 lpServiceDescription
->lpDescription
= NULL
;
5880 dwError
= ERROR_SUCCESS
;
5883 else if (dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
5885 LPSERVICE_FAILURE_ACTIONSW lpFailureActions
= (LPSERVICE_FAILURE_ACTIONSW
)lpBuffer
;
5886 LPWSTR lpStr
= NULL
;
5888 /* Query value length */
5889 dwError
= RegQueryValueExW(hServiceKey
,
5895 if (dwError
!= ERROR_SUCCESS
&&
5896 dwError
!= ERROR_MORE_DATA
&&
5897 dwError
!= ERROR_FILE_NOT_FOUND
)
5902 dwRequiredSize
= (dwType
== REG_BINARY
) ? max(sizeof(SERVICE_FAILURE_ACTIONSW
), dwRequiredSize
)
5903 : sizeof(SERVICE_FAILURE_ACTIONSW
);
5905 /* Get the strings */
5906 ScmReadString(hServiceKey
,
5910 ScmReadString(hServiceKey
,
5914 if (lpRebootMessage
)
5915 dwRequiredSize
+= (DWORD
)((wcslen(lpRebootMessage
) + 1) * sizeof(WCHAR
));
5917 if (lpFailureCommand
)
5918 dwRequiredSize
+= (DWORD
)((wcslen(lpFailureCommand
) + 1) * sizeof(WCHAR
));
5920 if (cbBufSize
< dwRequiredSize
)
5922 *pcbBytesNeeded
= dwRequiredSize
;
5923 dwError
= ERROR_INSUFFICIENT_BUFFER
;
5927 /* Now we can fill the buffer */
5928 if (dwError
!= ERROR_FILE_NOT_FOUND
&& dwType
== REG_BINARY
)
5930 dwError
= RegQueryValueExW(hServiceKey
,
5934 (LPBYTE
)lpFailureActions
,
5936 if (dwError
!= ERROR_SUCCESS
&& dwError
!= ERROR_FILE_NOT_FOUND
)
5939 if (dwRequiredSize
< sizeof(SERVICE_FAILURE_ACTIONSW
))
5940 dwRequiredSize
= sizeof(SERVICE_FAILURE_ACTIONSW
);
5945 * The value of the error doesn't really matter, the only
5946 * important thing is that it must be != ERROR_SUCCESS .
5948 dwError
= ERROR_INVALID_DATA
;
5951 if (dwError
== ERROR_SUCCESS
)
5953 lpFailureActions
->cActions
= min(lpFailureActions
->cActions
, (dwRequiredSize
- sizeof(SERVICE_FAILURE_ACTIONSW
)) / sizeof(SC_ACTION
));
5955 /* Here lpFailureActions->lpsaActions contains an offset. The conversion is done by the caller. */
5956 lpFailureActions
->lpsaActions
= (lpFailureActions
->cActions
> 0 ? (LPSC_ACTION
)(ULONG_PTR
)sizeof(SERVICE_FAILURE_ACTIONSW
) : NULL
);
5958 lpStr
= (LPWSTR
)((ULONG_PTR
)(lpFailureActions
+ 1) + lpFailureActions
->cActions
* sizeof(SC_ACTION
));
5962 lpFailureActions
->dwResetPeriod
= 0;
5963 lpFailureActions
->cActions
= 0;
5964 lpFailureActions
->lpsaActions
= NULL
;
5965 lpStr
= (LPWSTR
)(lpFailureActions
+ 1);
5968 lpFailureActions
->lpRebootMsg
= NULL
;
5969 lpFailureActions
->lpCommand
= NULL
;
5971 if (lpRebootMessage
)
5973 wcscpy(lpStr
, lpRebootMessage
);
5974 lpFailureActions
->lpRebootMsg
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpFailureActions
);
5975 lpStr
+= wcslen(lpStr
) + 1;
5978 if (lpFailureCommand
)
5980 wcscpy(lpStr
, lpFailureCommand
);
5981 lpFailureActions
->lpCommand
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpFailureActions
);
5982 /* lpStr += wcslen(lpStr) + 1; */
5985 dwError
= ERROR_SUCCESS
;
5989 /* Unlock the service database */
5990 ScmUnlockDatabase();
5992 if (lpDescription
!= NULL
)
5993 HeapFree(GetProcessHeap(), 0, lpDescription
);
5995 if (lpRebootMessage
!= NULL
)
5996 HeapFree(GetProcessHeap(), 0, lpRebootMessage
);
5998 if (lpFailureCommand
!= NULL
)
5999 HeapFree(GetProcessHeap(), 0, lpFailureCommand
);
6001 if (hServiceKey
!= NULL
)
6002 RegCloseKey(hServiceKey
);
6004 DPRINT("RQueryServiceConfig2W() done (Error %lu)\n", dwError
);
6013 RQueryServiceStatusEx(
6014 SC_RPC_HANDLE hService
,
6015 SC_STATUS_TYPE InfoLevel
,
6018 LPBOUNDED_DWORD_8K pcbBytesNeeded
)
6020 LPSERVICE_STATUS_PROCESS lpStatus
;
6021 PSERVICE_HANDLE hSvc
;
6024 DPRINT("RQueryServiceStatusEx() called\n");
6027 return ERROR_SHUTDOWN_IN_PROGRESS
;
6029 if (InfoLevel
!= SC_STATUS_PROCESS_INFO
)
6030 return ERROR_INVALID_LEVEL
;
6032 *pcbBytesNeeded
= sizeof(SERVICE_STATUS_PROCESS
);
6034 if (cbBufSize
< sizeof(SERVICE_STATUS_PROCESS
))
6035 return ERROR_INSUFFICIENT_BUFFER
;
6037 hSvc
= ScmGetServiceFromHandle(hService
);
6040 DPRINT1("Invalid service handle!\n");
6041 return ERROR_INVALID_HANDLE
;
6044 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
6045 SERVICE_QUERY_STATUS
))
6047 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
6048 return ERROR_ACCESS_DENIED
;
6051 lpService
= hSvc
->ServiceEntry
;
6052 if (lpService
== NULL
)
6054 DPRINT("lpService == NULL!\n");
6055 return ERROR_INVALID_HANDLE
;
6058 /* Lock the service database shared */
6059 ScmLockDatabaseShared();
6061 lpStatus
= (LPSERVICE_STATUS_PROCESS
)lpBuffer
;
6063 /* Return service status information */
6064 RtlCopyMemory(lpStatus
,
6066 sizeof(SERVICE_STATUS
));
6068 /* Copy the service process ID */
6069 if ((lpService
->Status
.dwCurrentState
== SERVICE_STOPPED
) || (lpService
->lpImage
== NULL
))
6070 lpStatus
->dwProcessId
= 0;
6072 lpStatus
->dwProcessId
= lpService
->lpImage
->dwProcessId
;
6074 lpStatus
->dwServiceFlags
= 0; /* FIXME */
6076 /* Unlock the service database */
6077 ScmUnlockDatabase();
6079 return ERROR_SUCCESS
;
6086 REnumServicesStatusExA(
6087 SC_RPC_HANDLE hSCManager
,
6088 SC_ENUM_TYPE InfoLevel
,
6089 DWORD dwServiceType
,
6090 DWORD dwServiceState
,
6093 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
6094 LPBOUNDED_DWORD_256K lpServicesReturned
,
6095 LPBOUNDED_DWORD_256K lpResumeIndex
,
6096 LPCSTR pszGroupName
)
6098 LPENUM_SERVICE_STATUS_PROCESSW lpStatusPtrW
= NULL
;
6099 LPENUM_SERVICE_STATUS_PROCESSW lpStatusPtrIncrW
;
6100 LPENUM_SERVICE_STATUS_PROCESSA lpStatusPtrA
= NULL
;
6101 LPWSTR lpStringPtrW
;
6103 LPWSTR pszGroupNameW
= NULL
;
6105 DWORD dwServiceCount
;
6107 DPRINT("REnumServicesStatusExA() called\n");
6109 if (pcbBytesNeeded
== NULL
|| lpServicesReturned
== NULL
)
6111 return ERROR_INVALID_ADDRESS
;
6116 pszGroupNameW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (strlen(pszGroupName
) + 1) * sizeof(WCHAR
));
6119 DPRINT("Failed to allocate buffer!\n");
6120 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
6124 MultiByteToWideChar(CP_ACP
,
6129 (int)(strlen(pszGroupName
) + 1));
6132 if ((cbBufSize
> 0) && (lpBuffer
))
6134 lpStatusPtrW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, cbBufSize
);
6137 DPRINT("Failed to allocate buffer!\n");
6138 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
6143 dwError
= REnumServicesStatusExW(hSCManager
,
6147 (LPBYTE
)lpStatusPtrW
,
6154 /* if no services were returned then we are Done */
6155 if (*lpServicesReturned
== 0)
6158 lpStatusPtrIncrW
= lpStatusPtrW
;
6159 lpStatusPtrA
= (LPENUM_SERVICE_STATUS_PROCESSA
)lpBuffer
;
6160 lpStringPtrA
= (LPSTR
)((ULONG_PTR
)lpBuffer
+
6161 *lpServicesReturned
* sizeof(ENUM_SERVICE_STATUS_PROCESSA
));
6162 lpStringPtrW
= (LPWSTR
)((ULONG_PTR
)lpStatusPtrW
+
6163 *lpServicesReturned
* sizeof(ENUM_SERVICE_STATUS_PROCESSW
));
6165 for (dwServiceCount
= 0; dwServiceCount
< *lpServicesReturned
; dwServiceCount
++)
6167 /* Copy the service name */
6168 WideCharToMultiByte(CP_ACP
,
6173 (int)wcslen(lpStringPtrW
),
6177 lpStatusPtrA
->lpServiceName
= (LPSTR
)((ULONG_PTR
)lpStringPtrA
- (ULONG_PTR
)lpBuffer
);
6178 lpStringPtrA
+= wcslen(lpStringPtrW
) + 1;
6179 lpStringPtrW
+= wcslen(lpStringPtrW
) + 1;
6181 /* Copy the display name */
6182 WideCharToMultiByte(CP_ACP
,
6187 (int)wcslen(lpStringPtrW
),
6191 lpStatusPtrA
->lpDisplayName
= (LPSTR
)((ULONG_PTR
)lpStringPtrA
- (ULONG_PTR
)lpBuffer
);
6192 lpStringPtrA
+= wcslen(lpStringPtrW
) + 1;
6193 lpStringPtrW
+= wcslen(lpStringPtrW
) + 1;
6195 /* Copy the status information */
6196 memcpy(&lpStatusPtrA
->ServiceStatusProcess
,
6197 &lpStatusPtrIncrW
->ServiceStatusProcess
,
6198 sizeof(SERVICE_STATUS
));
6200 /* Copy the service process ID */
6201 lpStatusPtrA
->ServiceStatusProcess
.dwProcessId
= lpStatusPtrIncrW
->ServiceStatusProcess
.dwProcessId
;
6203 lpStatusPtrA
->ServiceStatusProcess
.dwServiceFlags
= 0; /* FIXME */
6211 HeapFree(GetProcessHeap(), 0, pszGroupNameW
);
6214 HeapFree(GetProcessHeap(), 0, lpStatusPtrW
);
6216 DPRINT("REnumServicesStatusExA() done (Error %lu)\n", dwError
);
6225 REnumServicesStatusExW(
6226 SC_RPC_HANDLE hSCManager
,
6227 SC_ENUM_TYPE InfoLevel
,
6228 DWORD dwServiceType
,
6229 DWORD dwServiceState
,
6232 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
6233 LPBOUNDED_DWORD_256K lpServicesReturned
,
6234 LPBOUNDED_DWORD_256K lpResumeIndex
,
6235 LPCWSTR pszGroupName
)
6237 PMANAGER_HANDLE hManager
;
6239 DWORD dwError
= ERROR_SUCCESS
;
6240 PLIST_ENTRY ServiceEntry
;
6241 PSERVICE CurrentService
;
6243 DWORD dwRequiredSize
;
6244 DWORD dwServiceCount
;
6246 DWORD dwLastResumeCount
= 0;
6247 LPENUM_SERVICE_STATUS_PROCESSW lpStatusPtr
;
6250 DPRINT("REnumServicesStatusExW() called\n");
6253 return ERROR_SHUTDOWN_IN_PROGRESS
;
6255 if (InfoLevel
!= SC_ENUM_PROCESS_INFO
)
6256 return ERROR_INVALID_LEVEL
;
6258 hManager
= ScmGetServiceManagerFromHandle(hSCManager
);
6259 if (hManager
== NULL
)
6261 DPRINT1("Invalid service manager handle!\n");
6262 return ERROR_INVALID_HANDLE
;
6265 if (pcbBytesNeeded
== NULL
|| lpServicesReturned
== NULL
)
6267 return ERROR_INVALID_ADDRESS
;
6270 *pcbBytesNeeded
= 0;
6271 *lpServicesReturned
= 0;
6273 if ((dwServiceType
== 0) ||
6274 ((dwServiceType
& ~SERVICE_TYPE_ALL
) != 0))
6276 DPRINT("Not a valid Service Type!\n");
6277 return ERROR_INVALID_PARAMETER
;
6280 if ((dwServiceState
== 0) ||
6281 ((dwServiceState
& ~SERVICE_STATE_ALL
) != 0))
6283 DPRINT("Not a valid Service State!\n");
6284 return ERROR_INVALID_PARAMETER
;
6287 /* Check access rights */
6288 if (!RtlAreAllAccessesGranted(hManager
->Handle
.DesiredAccess
,
6289 SC_MANAGER_ENUMERATE_SERVICE
))
6291 DPRINT("Insufficient access rights! 0x%lx\n",
6292 hManager
->Handle
.DesiredAccess
);
6293 return ERROR_ACCESS_DENIED
;
6297 dwLastResumeCount
= *lpResumeIndex
;
6299 /* Lock the service database shared */
6300 ScmLockDatabaseShared();
6302 lpService
= ScmGetServiceEntryByResumeCount(dwLastResumeCount
);
6303 if (lpService
== NULL
)
6305 dwError
= ERROR_SUCCESS
;
6312 for (ServiceEntry
= &lpService
->ServiceListEntry
;
6313 ServiceEntry
!= &ServiceListHead
;
6314 ServiceEntry
= ServiceEntry
->Flink
)
6316 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
6320 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
6323 dwState
= SERVICE_ACTIVE
;
6324 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
6325 dwState
= SERVICE_INACTIVE
;
6327 if ((dwState
& dwServiceState
) == 0)
6332 if (*pszGroupName
== 0)
6334 if (CurrentService
->lpGroup
!= NULL
)
6339 if ((CurrentService
->lpGroup
== NULL
) ||
6340 _wcsicmp(pszGroupName
, CurrentService
->lpGroup
->lpGroupName
) != 0)
6345 dwSize
= sizeof(ENUM_SERVICE_STATUS_PROCESSW
) +
6346 (DWORD
)((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
6347 (DWORD
)((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
6349 if (dwRequiredSize
+ dwSize
<= cbBufSize
)
6351 DPRINT("Service name: %S fit\n", CurrentService
->lpServiceName
);
6352 dwRequiredSize
+= dwSize
;
6354 dwLastResumeCount
= CurrentService
->dwResumeCount
;
6358 DPRINT("Service name: %S no fit\n", CurrentService
->lpServiceName
);
6364 DPRINT("dwRequiredSize: %lu\n", dwRequiredSize
);
6365 DPRINT("dwServiceCount: %lu\n", dwServiceCount
);
6368 ServiceEntry
!= &ServiceListHead
;
6369 ServiceEntry
= ServiceEntry
->Flink
)
6371 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
6375 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
6378 dwState
= SERVICE_ACTIVE
;
6379 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
6380 dwState
= SERVICE_INACTIVE
;
6382 if ((dwState
& dwServiceState
) == 0)
6387 if (*pszGroupName
== 0)
6389 if (CurrentService
->lpGroup
!= NULL
)
6394 if ((CurrentService
->lpGroup
== NULL
) ||
6395 _wcsicmp(pszGroupName
, CurrentService
->lpGroup
->lpGroupName
) != 0)
6400 dwRequiredSize
+= (sizeof(ENUM_SERVICE_STATUS_PROCESSW
) +
6401 (DWORD
)((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
6402 (DWORD
)((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
)));
6404 dwError
= ERROR_MORE_DATA
;
6407 DPRINT("*pcbBytesNeeded: %lu\n", dwRequiredSize
);
6410 *lpResumeIndex
= dwLastResumeCount
;
6412 *lpServicesReturned
= dwServiceCount
;
6413 *pcbBytesNeeded
= dwRequiredSize
;
6415 /* If there was no services that matched */
6416 if ((!dwServiceCount
) && (dwError
!= ERROR_MORE_DATA
))
6418 dwError
= ERROR_SERVICE_DOES_NOT_EXIST
;
6422 lpStatusPtr
= (LPENUM_SERVICE_STATUS_PROCESSW
)lpBuffer
;
6423 lpStringPtr
= (LPWSTR
)((ULONG_PTR
)lpBuffer
+
6424 dwServiceCount
* sizeof(ENUM_SERVICE_STATUS_PROCESSW
));
6427 for (ServiceEntry
= &lpService
->ServiceListEntry
;
6428 ServiceEntry
!= &ServiceListHead
;
6429 ServiceEntry
= ServiceEntry
->Flink
)
6431 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
6435 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
6438 dwState
= SERVICE_ACTIVE
;
6439 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
6440 dwState
= SERVICE_INACTIVE
;
6442 if ((dwState
& dwServiceState
) == 0)
6447 if (*pszGroupName
== 0)
6449 if (CurrentService
->lpGroup
!= NULL
)
6454 if ((CurrentService
->lpGroup
== NULL
) ||
6455 _wcsicmp(pszGroupName
, CurrentService
->lpGroup
->lpGroupName
) != 0)
6460 dwSize
= sizeof(ENUM_SERVICE_STATUS_PROCESSW
) +
6461 (DWORD
)((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
6462 (DWORD
)((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
6464 if (dwRequiredSize
+ dwSize
<= cbBufSize
)
6466 /* Copy the service name */
6468 CurrentService
->lpServiceName
);
6469 lpStatusPtr
->lpServiceName
= (LPWSTR
)((ULONG_PTR
)lpStringPtr
- (ULONG_PTR
)lpBuffer
);
6470 lpStringPtr
+= (wcslen(CurrentService
->lpServiceName
) + 1);
6472 /* Copy the display name */
6474 CurrentService
->lpDisplayName
);
6475 lpStatusPtr
->lpDisplayName
= (LPWSTR
)((ULONG_PTR
)lpStringPtr
- (ULONG_PTR
)lpBuffer
);
6476 lpStringPtr
+= (wcslen(CurrentService
->lpDisplayName
) + 1);
6478 /* Copy the status information */
6479 memcpy(&lpStatusPtr
->ServiceStatusProcess
,
6480 &CurrentService
->Status
,
6481 sizeof(SERVICE_STATUS
));
6483 /* Copy the service process ID */
6484 if ((CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
) || (CurrentService
->lpImage
== NULL
))
6485 lpStatusPtr
->ServiceStatusProcess
.dwProcessId
= 0;
6487 lpStatusPtr
->ServiceStatusProcess
.dwProcessId
= CurrentService
->lpImage
->dwProcessId
;
6489 lpStatusPtr
->ServiceStatusProcess
.dwServiceFlags
= 0; /* FIXME */
6492 dwRequiredSize
+= dwSize
;
6502 *pcbBytesNeeded
= 0;
6508 /* Unlock the service database */
6509 ScmUnlockDatabase();
6511 DPRINT("REnumServicesStatusExW() done (Error %lu)\n", dwError
);
6521 handle_t BindingHandle
) /* FIXME */
6524 return ERROR_CALL_NOT_IMPLEMENTED
;
6531 RCreateServiceWOW64A(
6532 handle_t BindingHandle
,
6533 LPSTR lpServiceName
,
6534 LPSTR lpDisplayName
,
6535 DWORD dwDesiredAccess
,
6536 DWORD dwServiceType
,
6538 DWORD dwErrorControl
,
6539 LPSTR lpBinaryPathName
,
6540 LPSTR lpLoadOrderGroup
,
6542 LPBYTE lpDependencies
,
6544 LPSTR lpServiceStartName
,
6547 LPSC_RPC_HANDLE lpServiceHandle
)
6550 return ERROR_CALL_NOT_IMPLEMENTED
;
6557 RCreateServiceWOW64W(
6558 handle_t BindingHandle
,
6559 LPWSTR lpServiceName
,
6560 LPWSTR lpDisplayName
,
6561 DWORD dwDesiredAccess
,
6562 DWORD dwServiceType
,
6564 DWORD dwErrorControl
,
6565 LPWSTR lpBinaryPathName
,
6566 LPWSTR lpLoadOrderGroup
,
6568 LPBYTE lpDependencies
,
6570 LPWSTR lpServiceStartName
,
6573 LPSC_RPC_HANDLE lpServiceHandle
)
6576 return ERROR_CALL_NOT_IMPLEMENTED
;
6583 RI_ScQueryServiceTagInfo(
6584 SC_RPC_HANDLE hSCManager
,
6585 TAG_INFO_LEVEL dwInfoLevel
,
6586 PTAG_INFO_NAME_FROM_TAG_IN_PARAMS
* lpInParams
,
6587 PTAG_INFO_NAME_FROM_TAG_OUT_PARAMS
* lpOutParams
)
6590 return ERROR_CALL_NOT_IMPLEMENTED
;
6597 RNotifyServiceStatusChange(
6598 SC_RPC_HANDLE hService
,
6599 SC_RPC_NOTIFY_PARAMS NotifyParams
,
6600 GUID
*pClientProcessGuid
,
6601 GUID
*pSCMProcessGuid
,
6602 PBOOL pfCreateRemoteQueue
,
6603 LPSC_NOTIFY_RPC_HANDLE phNotify
)
6606 return ERROR_CALL_NOT_IMPLEMENTED
;
6614 SC_NOTIFY_RPC_HANDLE hNotify
,
6615 PSC_RPC_NOTIFY_PARAMS_LIST
*ppNotifyParams
)
6618 return ERROR_CALL_NOT_IMPLEMENTED
;
6626 LPSC_NOTIFY_RPC_HANDLE phNotify
,
6630 return ERROR_CALL_NOT_IMPLEMENTED
;
6638 SC_RPC_HANDLE hService
,
6643 return ERROR_CALL_NOT_IMPLEMENTED
;
6651 SC_RPC_HANDLE hService
,
6656 return ERROR_CALL_NOT_IMPLEMENTED
;
6664 handle_t BindingHandle
) /* FIXME */
6667 return ERROR_CALL_NOT_IMPLEMENTED
;
6674 RValidatePnPService(
6675 handle_t BindingHandle
) /* FIXME */
6678 return ERROR_CALL_NOT_IMPLEMENTED
;
6685 ROpenServiceStatusHandle(
6686 handle_t BindingHandle
) /* FIXME */
6689 return ERROR_CALL_NOT_IMPLEMENTED
;
6697 handle_t BindingHandle
) /* FIXME */
6700 return ERROR_CALL_NOT_IMPLEMENTED
;
6704 void __RPC_FAR
* __RPC_USER
midl_user_allocate(SIZE_T len
)
6706 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
);
6710 void __RPC_USER
midl_user_free(void __RPC_FAR
* ptr
)
6712 HeapFree(GetProcessHeap(), 0, ptr
);
6716 void __RPC_USER
SC_RPC_HANDLE_rundown(SC_RPC_HANDLE hSCObject
)
6718 /* Close the handle */
6719 RCloseServiceHandle(&hSCObject
);
6723 void __RPC_USER
SC_RPC_LOCK_rundown(SC_RPC_LOCK Lock
)
6725 /* Unlock the database */
6726 RUnlockServiceDatabase(&Lock
);
6730 void __RPC_USER
SC_NOTIFY_RPC_HANDLE_rundown(SC_NOTIFY_RPC_HANDLE hNotify
)