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;
2640 /* Get the service tag (if Win32) */
2641 ScmGenerateServiceTag(lpService
);
2643 DPRINT("CreateService - lpService->dwRefCount %u\n", lpService
->dwRefCount
);
2646 /* Unlock the service database */
2647 ScmUnlockDatabase();
2649 if (hServiceKey
!= NULL
)
2650 RegCloseKey(hServiceKey
);
2652 if (lpClearTextPassword
!= NULL
)
2654 /* Wipe and release the password buffer */
2655 SecureZeroMemory(lpClearTextPassword
,
2656 (wcslen(lpClearTextPassword
) + 1) * sizeof(WCHAR
));
2657 HeapFree(GetProcessHeap(), 0, lpClearTextPassword
);
2660 if (dwError
== ERROR_SUCCESS
)
2662 DPRINT("hService %p\n", hServiceHandle
);
2663 *lpServiceHandle
= (SC_RPC_HANDLE
)hServiceHandle
;
2665 if (lpdwTagId
!= NULL
)
2666 *lpdwTagId
= lpService
->dwTag
;
2670 if (lpService
!= NULL
&&
2671 lpService
->lpServiceName
!= NULL
)
2673 /* Release the display name buffer */
2674 HeapFree(GetProcessHeap(), 0, lpService
->lpDisplayName
);
2679 /* Remove the service handle */
2680 HeapFree(GetProcessHeap(), 0, hServiceHandle
);
2683 if (lpService
!= NULL
)
2685 /* FIXME: remove the service entry */
2689 if (lpImagePath
!= NULL
)
2690 HeapFree(GetProcessHeap(), 0, lpImagePath
);
2692 DPRINT("RCreateServiceW() done (Error %lu)\n", dwError
);
2701 REnumDependentServicesW(
2702 SC_RPC_HANDLE hService
,
2703 DWORD dwServiceState
,
2706 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
2707 LPBOUNDED_DWORD_256K lpServicesReturned
)
2709 DWORD dwError
= ERROR_SUCCESS
;
2710 DWORD dwServicesReturned
= 0;
2711 DWORD dwServiceCount
;
2712 HKEY hServicesKey
= NULL
;
2713 PSERVICE_HANDLE hSvc
;
2714 PSERVICE lpService
= NULL
;
2715 PSERVICE
*lpServicesArray
= NULL
;
2716 LPENUM_SERVICE_STATUSW lpServicesPtr
= NULL
;
2719 *pcbBytesNeeded
= 0;
2720 *lpServicesReturned
= 0;
2722 DPRINT("REnumDependentServicesW() called\n");
2724 hSvc
= ScmGetServiceFromHandle(hService
);
2727 DPRINT1("Invalid service handle!\n");
2728 return ERROR_INVALID_HANDLE
;
2731 lpService
= hSvc
->ServiceEntry
;
2733 /* Check access rights */
2734 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
2735 SC_MANAGER_ENUMERATE_SERVICE
))
2737 DPRINT("Insufficient access rights! 0x%lx\n",
2738 hSvc
->Handle
.DesiredAccess
);
2739 return ERROR_ACCESS_DENIED
;
2742 /* Open the Services Reg key */
2743 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
2744 L
"System\\CurrentControlSet\\Services",
2748 if (dwError
!= ERROR_SUCCESS
)
2751 /* First determine the bytes needed and get the number of dependent services */
2752 dwError
= Int_EnumDependentServicesW(hServicesKey
,
2757 &dwServicesReturned
);
2758 if (dwError
!= ERROR_SUCCESS
)
2761 /* If buffer size is less than the bytes needed or pointer is null */
2762 if ((!lpServices
) || (cbBufSize
< *pcbBytesNeeded
))
2764 dwError
= ERROR_MORE_DATA
;
2768 /* Allocate memory for array of service pointers */
2769 lpServicesArray
= HeapAlloc(GetProcessHeap(),
2771 (dwServicesReturned
+ 1) * sizeof(PSERVICE
));
2772 if (!lpServicesArray
)
2774 DPRINT1("Could not allocate a buffer!!\n");
2775 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
2779 dwServicesReturned
= 0;
2780 *pcbBytesNeeded
= 0;
2782 dwError
= Int_EnumDependentServicesW(hServicesKey
,
2787 &dwServicesReturned
);
2788 if (dwError
!= ERROR_SUCCESS
)
2793 lpServicesPtr
= (LPENUM_SERVICE_STATUSW
)lpServices
;
2794 lpStr
= (LPWSTR
)(lpServices
+ (dwServicesReturned
* sizeof(ENUM_SERVICE_STATUSW
)));
2796 /* Copy EnumDepenedentService to Buffer */
2797 for (dwServiceCount
= 0; dwServiceCount
< dwServicesReturned
; dwServiceCount
++)
2799 lpService
= lpServicesArray
[dwServiceCount
];
2801 /* Copy status info */
2802 memcpy(&lpServicesPtr
->ServiceStatus
,
2804 sizeof(SERVICE_STATUS
));
2806 /* Copy display name */
2807 wcscpy(lpStr
, lpService
->lpDisplayName
);
2808 lpServicesPtr
->lpDisplayName
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServices
);
2809 lpStr
+= (wcslen(lpService
->lpDisplayName
) + 1);
2811 /* Copy service name */
2812 wcscpy(lpStr
, lpService
->lpServiceName
);
2813 lpServicesPtr
->lpServiceName
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServices
);
2814 lpStr
+= (wcslen(lpService
->lpServiceName
) + 1);
2819 *lpServicesReturned
= dwServicesReturned
;
2822 if (lpServicesArray
!= NULL
)
2823 HeapFree(GetProcessHeap(), 0, lpServicesArray
);
2825 RegCloseKey(hServicesKey
);
2827 DPRINT("REnumDependentServicesW() done (Error %lu)\n", dwError
);
2836 REnumServicesStatusW(
2837 SC_RPC_HANDLE hSCManager
,
2838 DWORD dwServiceType
,
2839 DWORD dwServiceState
,
2842 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
2843 LPBOUNDED_DWORD_256K lpServicesReturned
,
2844 LPBOUNDED_DWORD_256K lpResumeHandle
)
2846 /* Enumerate all the services, not regarding of their group */
2847 return REnumServiceGroupW(hSCManager
,
2863 LPWSTR lpMachineName
,
2864 LPWSTR lpDatabaseName
,
2865 DWORD dwDesiredAccess
,
2866 LPSC_RPC_HANDLE lpScHandle
)
2871 DPRINT("ROpenSCManagerW() called\n");
2872 DPRINT("lpMachineName = %p\n", lpMachineName
);
2873 DPRINT("lpMachineName: %S\n", lpMachineName
);
2874 DPRINT("lpDataBaseName = %p\n", lpDatabaseName
);
2875 DPRINT("lpDataBaseName: %S\n", lpDatabaseName
);
2876 DPRINT("dwDesiredAccess = %x\n", dwDesiredAccess
);
2879 return ERROR_SHUTDOWN_IN_PROGRESS
;
2882 return ERROR_INVALID_PARAMETER
;
2884 dwError
= ScmCreateManagerHandle(lpDatabaseName
,
2886 if (dwError
!= ERROR_SUCCESS
)
2888 DPRINT("ScmCreateManagerHandle() failed (Error %lu)\n", dwError
);
2892 /* Check the desired access */
2893 dwError
= ScmCheckAccess(hHandle
,
2894 dwDesiredAccess
| SC_MANAGER_CONNECT
);
2895 if (dwError
!= ERROR_SUCCESS
)
2897 DPRINT("ScmCheckAccess() failed (Error %lu)\n", dwError
);
2898 HeapFree(GetProcessHeap(), 0, hHandle
);
2902 *lpScHandle
= (SC_RPC_HANDLE
)hHandle
;
2903 DPRINT("*hScm = %p\n", *lpScHandle
);
2905 DPRINT("ROpenSCManagerW() done\n");
2907 return ERROR_SUCCESS
;
2915 SC_RPC_HANDLE hSCManager
,
2916 LPWSTR lpServiceName
,
2917 DWORD dwDesiredAccess
,
2918 LPSC_RPC_HANDLE lpServiceHandle
)
2921 PMANAGER_HANDLE hManager
;
2923 DWORD dwError
= ERROR_SUCCESS
;
2925 DPRINT("ROpenServiceW() called\n");
2926 DPRINT("hSCManager = %p\n", hSCManager
);
2927 DPRINT("lpServiceName = %p\n", lpServiceName
);
2928 DPRINT("lpServiceName: %S\n", lpServiceName
);
2929 DPRINT("dwDesiredAccess = %x\n", dwDesiredAccess
);
2932 return ERROR_SHUTDOWN_IN_PROGRESS
;
2934 hManager
= ScmGetServiceManagerFromHandle(hSCManager
);
2935 if (hManager
== NULL
)
2937 DPRINT1("Invalid service manager handle!\n");
2938 return ERROR_INVALID_HANDLE
;
2941 if (!lpServiceHandle
)
2942 return ERROR_INVALID_PARAMETER
;
2945 return ERROR_INVALID_ADDRESS
;
2947 /* Lock the service database exclusive */
2948 ScmLockDatabaseExclusive();
2950 /* Get service database entry */
2951 lpService
= ScmGetServiceEntryByName(lpServiceName
);
2952 if (lpService
== NULL
)
2954 DPRINT("Could not find the service!\n");
2955 dwError
= ERROR_SERVICE_DOES_NOT_EXIST
;
2959 /* Create a service handle */
2960 dwError
= ScmCreateServiceHandle(lpService
,
2962 if (dwError
!= ERROR_SUCCESS
)
2964 DPRINT("ScmCreateServiceHandle() failed (Error %lu)\n", dwError
);
2968 /* Check the desired access */
2969 dwError
= ScmCheckAccess(hHandle
,
2971 if (dwError
!= ERROR_SUCCESS
)
2973 DPRINT("ScmCheckAccess() failed (Error %lu)\n", dwError
);
2974 HeapFree(GetProcessHeap(), 0, hHandle
);
2978 lpService
->dwRefCount
++;
2979 DPRINT("OpenService - lpService->dwRefCount %u\n",lpService
->dwRefCount
);
2981 *lpServiceHandle
= (SC_RPC_HANDLE
)hHandle
;
2982 DPRINT("*hService = %p\n", *lpServiceHandle
);
2985 /* Unlock the service database */
2986 ScmUnlockDatabase();
2988 DPRINT("ROpenServiceW() done\n");
2997 RQueryServiceConfigW(
2998 SC_RPC_HANDLE hService
,
2999 LPBYTE lpBuf
, //LPQUERY_SERVICE_CONFIGW lpServiceConfig,
3001 LPBOUNDED_DWORD_8K pcbBytesNeeded
)
3003 LPQUERY_SERVICE_CONFIGW lpServiceConfig
= (LPQUERY_SERVICE_CONFIGW
)lpBuf
;
3004 DWORD dwError
= ERROR_SUCCESS
;
3005 PSERVICE_HANDLE hSvc
;
3006 PSERVICE lpService
= NULL
;
3007 HKEY hServiceKey
= NULL
;
3008 LPWSTR lpImagePath
= NULL
;
3009 LPWSTR lpServiceStartName
= NULL
;
3010 LPWSTR lpDependencies
= NULL
;
3011 DWORD dwDependenciesLength
= 0;
3012 DWORD dwRequiredSize
;
3015 DPRINT("RQueryServiceConfigW() called\n");
3018 return ERROR_SHUTDOWN_IN_PROGRESS
;
3020 hSvc
= ScmGetServiceFromHandle(hService
);
3023 DPRINT1("Invalid service handle!\n");
3024 return ERROR_INVALID_HANDLE
;
3027 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
3028 SERVICE_QUERY_CONFIG
))
3030 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
3031 return ERROR_ACCESS_DENIED
;
3034 lpService
= hSvc
->ServiceEntry
;
3035 if (lpService
== NULL
)
3037 DPRINT("lpService == NULL!\n");
3038 return ERROR_INVALID_HANDLE
;
3041 /* Lock the service database shared */
3042 ScmLockDatabaseShared();
3044 dwError
= ScmOpenServiceKey(lpService
->lpServiceName
,
3047 if (dwError
!= ERROR_SUCCESS
)
3050 /* Read the image path */
3051 dwError
= ScmReadString(hServiceKey
,
3054 if (dwError
!= ERROR_SUCCESS
)
3057 /* Read the service start name */
3058 ScmReadString(hServiceKey
,
3060 &lpServiceStartName
);
3062 /* Read the dependencies */
3063 ScmReadDependencies(hServiceKey
,
3065 &dwDependenciesLength
);
3067 dwRequiredSize
= sizeof(QUERY_SERVICE_CONFIGW
);
3069 if (lpImagePath
!= NULL
)
3070 dwRequiredSize
+= (DWORD
)((wcslen(lpImagePath
) + 1) * sizeof(WCHAR
));
3072 dwRequiredSize
+= 2 * sizeof(WCHAR
);
3074 if ((lpService
->lpGroup
!= NULL
) && (lpService
->lpGroup
->lpGroupName
!= NULL
))
3075 dwRequiredSize
+= (DWORD
)((wcslen(lpService
->lpGroup
->lpGroupName
) + 1) * sizeof(WCHAR
));
3077 dwRequiredSize
+= 2 * sizeof(WCHAR
);
3079 if (lpDependencies
!= NULL
)
3080 dwRequiredSize
+= dwDependenciesLength
* sizeof(WCHAR
);
3082 dwRequiredSize
+= 2 * sizeof(WCHAR
);
3084 if (lpServiceStartName
!= NULL
)
3085 dwRequiredSize
+= (DWORD
)((wcslen(lpServiceStartName
) + 1) * sizeof(WCHAR
));
3087 dwRequiredSize
+= 2 * sizeof(WCHAR
);
3089 if (lpService
->lpDisplayName
!= NULL
)
3090 dwRequiredSize
+= (DWORD
)((wcslen(lpService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
3092 dwRequiredSize
+= 2 * sizeof(WCHAR
);
3094 if (lpServiceConfig
== NULL
|| cbBufSize
< dwRequiredSize
)
3096 dwError
= ERROR_INSUFFICIENT_BUFFER
;
3100 lpServiceConfig
->dwServiceType
= lpService
->Status
.dwServiceType
;
3101 lpServiceConfig
->dwStartType
= lpService
->dwStartType
;
3102 lpServiceConfig
->dwErrorControl
= lpService
->dwErrorControl
;
3103 lpServiceConfig
->dwTagId
= lpService
->dwTag
;
3105 lpStr
= (LPWSTR
)(lpServiceConfig
+ 1);
3107 /* Append the image path */
3108 if (lpImagePath
!= NULL
)
3110 wcscpy(lpStr
, lpImagePath
);
3117 lpServiceConfig
->lpBinaryPathName
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
3118 lpStr
+= (wcslen(lpStr
) + 1);
3120 /* Append the group name */
3121 if ((lpService
->lpGroup
!= NULL
) && (lpService
->lpGroup
->lpGroupName
!= NULL
))
3123 wcscpy(lpStr
, lpService
->lpGroup
->lpGroupName
);
3130 lpServiceConfig
->lpLoadOrderGroup
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
3131 lpStr
+= (wcslen(lpStr
) + 1);
3133 /* Append Dependencies */
3134 if (lpDependencies
!= NULL
)
3138 dwDependenciesLength
* sizeof(WCHAR
));
3145 lpServiceConfig
->lpDependencies
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
3146 if (lpDependencies
!= NULL
)
3147 lpStr
+= dwDependenciesLength
;
3149 lpStr
+= (wcslen(lpStr
) + 1);
3151 /* Append the service start name */
3152 if (lpServiceStartName
!= NULL
)
3154 wcscpy(lpStr
, lpServiceStartName
);
3161 lpServiceConfig
->lpServiceStartName
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
3162 lpStr
+= (wcslen(lpStr
) + 1);
3164 /* Append the display name */
3165 if (lpService
->lpDisplayName
!= NULL
)
3167 wcscpy(lpStr
, lpService
->lpDisplayName
);
3174 lpServiceConfig
->lpDisplayName
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
3177 if (pcbBytesNeeded
!= NULL
)
3178 *pcbBytesNeeded
= dwRequiredSize
;
3181 /* Unlock the service database */
3182 ScmUnlockDatabase();
3184 if (lpImagePath
!= NULL
)
3185 HeapFree(GetProcessHeap(), 0, lpImagePath
);
3187 if (lpServiceStartName
!= NULL
)
3188 HeapFree(GetProcessHeap(), 0, lpServiceStartName
);
3190 if (lpDependencies
!= NULL
)
3191 HeapFree(GetProcessHeap(), 0, lpDependencies
);
3193 if (hServiceKey
!= NULL
)
3194 RegCloseKey(hServiceKey
);
3196 DPRINT("RQueryServiceConfigW() done\n");
3205 RQueryServiceLockStatusW(
3206 SC_RPC_HANDLE hSCManager
,
3207 LPBYTE lpBuf
, // LPQUERY_SERVICE_LOCK_STATUSW lpLockStatus,
3209 LPBOUNDED_DWORD_4K pcbBytesNeeded
)
3211 LPQUERY_SERVICE_LOCK_STATUSW lpLockStatus
= (LPQUERY_SERVICE_LOCK_STATUSW
)lpBuf
;
3212 PMANAGER_HANDLE hMgr
;
3213 DWORD dwRequiredSize
;
3215 if (!lpLockStatus
|| !pcbBytesNeeded
)
3216 return ERROR_INVALID_PARAMETER
;
3218 hMgr
= ScmGetServiceManagerFromHandle(hSCManager
);
3221 DPRINT1("Invalid service manager handle!\n");
3222 return ERROR_INVALID_HANDLE
;
3225 if (!RtlAreAllAccessesGranted(hMgr
->Handle
.DesiredAccess
,
3226 SC_MANAGER_QUERY_LOCK_STATUS
))
3228 DPRINT("Insufficient access rights! 0x%lx\n", hMgr
->Handle
.DesiredAccess
);
3229 return ERROR_ACCESS_DENIED
;
3232 /* FIXME: we need to compute instead the real length of the owner name */
3233 dwRequiredSize
= sizeof(QUERY_SERVICE_LOCK_STATUSW
) + sizeof(WCHAR
);
3234 *pcbBytesNeeded
= dwRequiredSize
;
3236 if (cbBufSize
< dwRequiredSize
)
3237 return ERROR_INSUFFICIENT_BUFFER
;
3239 ScmQueryServiceLockStatusW(lpLockStatus
);
3241 return ERROR_SUCCESS
;
3249 SC_RPC_HANDLE hService
,
3251 LPSTRING_PTRSW argv
)
3253 DWORD dwError
= ERROR_SUCCESS
;
3254 PSERVICE_HANDLE hSvc
;
3255 PSERVICE lpService
= NULL
;
3260 DPRINT("RStartServiceW(%p %lu %p) called\n", hService
, argc
, argv
);
3261 DPRINT(" argc: %lu\n", argc
);
3264 for (i
= 0; i
< argc
; i
++)
3266 DPRINT(" argv[%lu]: %S\n", i
, argv
[i
].StringPtr
);
3272 return ERROR_SHUTDOWN_IN_PROGRESS
;
3274 hSvc
= ScmGetServiceFromHandle(hService
);
3277 DPRINT1("Invalid service handle!\n");
3278 return ERROR_INVALID_HANDLE
;
3281 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
3284 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
3285 return ERROR_ACCESS_DENIED
;
3288 lpService
= hSvc
->ServiceEntry
;
3289 if (lpService
== NULL
)
3291 DPRINT("lpService == NULL!\n");
3292 return ERROR_INVALID_HANDLE
;
3295 if (lpService
->dwStartType
== SERVICE_DISABLED
)
3296 return ERROR_SERVICE_DISABLED
;
3298 if (lpService
->bDeleted
)
3299 return ERROR_SERVICE_MARKED_FOR_DELETE
;
3301 /* Start the service */
3302 dwError
= ScmStartService(lpService
, argc
, (LPWSTR
*)argv
);
3311 RGetServiceDisplayNameW(
3312 SC_RPC_HANDLE hSCManager
,
3313 LPCWSTR lpServiceName
,
3314 LPWSTR lpDisplayName
,
3317 // PMANAGER_HANDLE hManager;
3319 LPCWSTR lpSvcDisplayName
;
3323 DPRINT("RGetServiceDisplayNameW() called\n");
3324 DPRINT("hSCManager = %p\n", hSCManager
);
3325 DPRINT("lpServiceName: %S\n", lpServiceName
);
3326 DPRINT("lpDisplayName: %p\n", lpDisplayName
);
3327 DPRINT("*lpcchBuffer: %lu\n", *lpcchBuffer
);
3330 hManager
= (PMANAGER_HANDLE
)hSCManager
;
3331 if (hManager
->Handle
.Tag
!= MANAGER_TAG
)
3333 DPRINT("Invalid manager handle!\n");
3334 return ERROR_INVALID_HANDLE
;
3338 /* Get service database entry */
3339 lpService
= ScmGetServiceEntryByName(lpServiceName
);
3340 if (lpService
== NULL
)
3342 DPRINT("Could not find the service!\n");
3343 return ERROR_SERVICE_DOES_NOT_EXIST
;
3346 if (lpService
->lpDisplayName
)
3347 lpSvcDisplayName
= lpService
->lpDisplayName
;
3349 lpSvcDisplayName
= lpService
->lpServiceName
;
3351 dwLength
= (DWORD
)wcslen(lpSvcDisplayName
);
3353 if (*lpcchBuffer
> dwLength
)
3355 if (lpDisplayName
!= NULL
)
3356 wcscpy(lpDisplayName
, lpSvcDisplayName
);
3358 dwError
= ERROR_SUCCESS
;
3362 dwError
= ERROR_INSUFFICIENT_BUFFER
;
3365 *lpcchBuffer
= dwLength
;
3374 RGetServiceKeyNameW(
3375 SC_RPC_HANDLE hSCManager
,
3376 LPCWSTR lpDisplayName
,
3377 LPWSTR lpServiceName
,
3380 // PMANAGER_HANDLE hManager;
3385 DPRINT("RGetServiceKeyNameW() called\n");
3386 DPRINT("hSCManager = %p\n", hSCManager
);
3387 DPRINT("lpDisplayName: %S\n", lpDisplayName
);
3388 DPRINT("lpServiceName: %p\n", lpServiceName
);
3389 DPRINT("*lpcchBuffer: %lu\n", *lpcchBuffer
);
3392 hManager
= (PMANAGER_HANDLE
)hSCManager
;
3393 if (hManager
->Handle
.Tag
!= MANAGER_TAG
)
3395 DPRINT("Invalid manager handle!\n");
3396 return ERROR_INVALID_HANDLE
;
3400 /* Get service database entry */
3401 lpService
= ScmGetServiceEntryByDisplayName(lpDisplayName
);
3402 if (lpService
== NULL
)
3404 DPRINT("Could not find the service!\n");
3405 return ERROR_SERVICE_DOES_NOT_EXIST
;
3408 dwLength
= (DWORD
)wcslen(lpService
->lpServiceName
);
3410 if (*lpcchBuffer
> dwLength
)
3412 if (lpServiceName
!= NULL
)
3413 wcscpy(lpServiceName
, lpService
->lpServiceName
);
3415 dwError
= ERROR_SUCCESS
;
3419 dwError
= ERROR_INSUFFICIENT_BUFFER
;
3422 *lpcchBuffer
= dwLength
;
3431 RI_ScSetServiceBitsA(
3432 RPC_SERVICE_STATUS_HANDLE hServiceStatus
,
3433 DWORD dwServiceBits
,
3435 int bUpdateImmediately
,
3439 return ERROR_SHUTDOWN_IN_PROGRESS
;
3441 if (lpString
!= NULL
)
3442 return ERROR_INVALID_PARAMETER
;
3444 return RI_ScSetServiceBitsW(hServiceStatus
,
3455 RChangeServiceConfigA(
3456 SC_RPC_HANDLE hService
,
3457 DWORD dwServiceType
,
3459 DWORD dwErrorControl
,
3460 LPSTR lpBinaryPathName
,
3461 LPSTR lpLoadOrderGroup
,
3463 LPBYTE lpDependencies
,
3465 LPSTR lpServiceStartName
,
3468 LPSTR lpDisplayName
)
3470 DWORD dwError
= ERROR_SUCCESS
;
3471 LPWSTR lpBinaryPathNameW
= NULL
;
3472 LPWSTR lpLoadOrderGroupW
= NULL
;
3473 LPWSTR lpDependenciesW
= NULL
;
3474 LPWSTR lpServiceStartNameW
= NULL
;
3475 LPWSTR lpDisplayNameW
= NULL
;
3476 DWORD dwDependenciesLength
= 0;
3481 if (lpBinaryPathName
)
3483 len
= MultiByteToWideChar(CP_ACP
, 0, lpBinaryPathName
, -1, NULL
, 0);
3484 lpBinaryPathNameW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3485 if (!lpBinaryPathNameW
)
3487 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3490 MultiByteToWideChar(CP_ACP
, 0, lpBinaryPathName
, -1, lpBinaryPathNameW
, len
);
3493 if (lpLoadOrderGroup
)
3495 len
= MultiByteToWideChar(CP_ACP
, 0, lpLoadOrderGroup
, -1, NULL
, 0);
3496 lpLoadOrderGroupW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3497 if (!lpLoadOrderGroupW
)
3499 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3502 MultiByteToWideChar(CP_ACP
, 0, lpLoadOrderGroup
, -1, lpLoadOrderGroupW
, len
);
3507 lpStr
= (LPCSTR
)lpDependencies
;
3510 cchLength
= strlen(lpStr
) + 1;
3511 dwDependenciesLength
+= (DWORD
)cchLength
;
3512 lpStr
= lpStr
+ cchLength
;
3514 dwDependenciesLength
++;
3516 lpDependenciesW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, dwDependenciesLength
* sizeof(WCHAR
));
3517 if (!lpDependenciesW
)
3519 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3522 MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)lpDependencies
, dwDependenciesLength
, lpDependenciesW
, dwDependenciesLength
);
3525 if (lpServiceStartName
)
3527 len
= MultiByteToWideChar(CP_ACP
, 0, lpServiceStartName
, -1, NULL
, 0);
3528 lpServiceStartNameW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3529 if (!lpServiceStartNameW
)
3531 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3534 MultiByteToWideChar(CP_ACP
, 0, lpServiceStartName
, -1, lpServiceStartNameW
, len
);
3539 len
= MultiByteToWideChar(CP_ACP
, 0, lpDisplayName
, -1, NULL
, 0);
3540 lpDisplayNameW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3541 if (!lpDisplayNameW
)
3543 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3546 MultiByteToWideChar(CP_ACP
, 0, lpDisplayName
, -1, lpDisplayNameW
, len
);
3549 dwError
= RChangeServiceConfigW(hService
,
3556 (LPBYTE
)lpDependenciesW
,
3557 dwDependenciesLength
,
3558 lpServiceStartNameW
,
3564 if (lpBinaryPathNameW
!= NULL
)
3565 HeapFree(GetProcessHeap(), 0, lpBinaryPathNameW
);
3567 if (lpLoadOrderGroupW
!= NULL
)
3568 HeapFree(GetProcessHeap(), 0, lpLoadOrderGroupW
);
3570 if (lpDependenciesW
!= NULL
)
3571 HeapFree(GetProcessHeap(), 0, lpDependenciesW
);
3573 if (lpServiceStartNameW
!= NULL
)
3574 HeapFree(GetProcessHeap(), 0, lpServiceStartNameW
);
3576 if (lpDisplayNameW
!= NULL
)
3577 HeapFree(GetProcessHeap(), 0, lpDisplayNameW
);
3587 SC_RPC_HANDLE hSCManager
,
3588 LPSTR lpServiceName
,
3589 LPSTR lpDisplayName
,
3590 DWORD dwDesiredAccess
,
3591 DWORD dwServiceType
,
3593 DWORD dwErrorControl
,
3594 LPSTR lpBinaryPathName
,
3595 LPSTR lpLoadOrderGroup
,
3597 LPBYTE lpDependencies
,
3599 LPSTR lpServiceStartName
,
3602 LPSC_RPC_HANDLE lpServiceHandle
)
3604 DWORD dwError
= ERROR_SUCCESS
;
3605 LPWSTR lpServiceNameW
= NULL
;
3606 LPWSTR lpDisplayNameW
= NULL
;
3607 LPWSTR lpBinaryPathNameW
= NULL
;
3608 LPWSTR lpLoadOrderGroupW
= NULL
;
3609 LPWSTR lpDependenciesW
= NULL
;
3610 LPWSTR lpServiceStartNameW
= NULL
;
3611 DWORD dwDependenciesLength
= 0;
3618 len
= MultiByteToWideChar(CP_ACP
, 0, lpServiceName
, -1, NULL
, 0);
3619 lpServiceNameW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3620 if (!lpServiceNameW
)
3622 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3625 MultiByteToWideChar(CP_ACP
, 0, lpServiceName
, -1, lpServiceNameW
, len
);
3630 len
= MultiByteToWideChar(CP_ACP
, 0, lpDisplayName
, -1, NULL
, 0);
3631 lpDisplayNameW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3632 if (!lpDisplayNameW
)
3634 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3637 MultiByteToWideChar(CP_ACP
, 0, lpDisplayName
, -1, lpDisplayNameW
, len
);
3640 if (lpBinaryPathName
)
3642 len
= MultiByteToWideChar(CP_ACP
, 0, lpBinaryPathName
, -1, NULL
, 0);
3643 lpBinaryPathNameW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3644 if (!lpBinaryPathNameW
)
3646 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3649 MultiByteToWideChar(CP_ACP
, 0, lpBinaryPathName
, -1, lpBinaryPathNameW
, len
);
3652 if (lpLoadOrderGroup
)
3654 len
= MultiByteToWideChar(CP_ACP
, 0, lpLoadOrderGroup
, -1, NULL
, 0);
3655 lpLoadOrderGroupW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3656 if (!lpLoadOrderGroupW
)
3658 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3661 MultiByteToWideChar(CP_ACP
, 0, lpLoadOrderGroup
, -1, lpLoadOrderGroupW
, len
);
3666 lpStr
= (LPCSTR
)lpDependencies
;
3669 cchLength
= strlen(lpStr
) + 1;
3670 dwDependenciesLength
+= (DWORD
)cchLength
;
3671 lpStr
= lpStr
+ cchLength
;
3673 dwDependenciesLength
++;
3675 lpDependenciesW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, dwDependenciesLength
* sizeof(WCHAR
));
3676 if (!lpDependenciesW
)
3678 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3681 MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)lpDependencies
, dwDependenciesLength
, lpDependenciesW
, dwDependenciesLength
);
3684 if (lpServiceStartName
)
3686 len
= MultiByteToWideChar(CP_ACP
, 0, lpServiceStartName
, -1, NULL
, 0);
3687 lpServiceStartNameW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3688 if (!lpServiceStartNameW
)
3690 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3693 MultiByteToWideChar(CP_ACP
, 0, lpServiceStartName
, -1, lpServiceStartNameW
, len
);
3696 dwError
= RCreateServiceW(hSCManager
,
3706 (LPBYTE
)lpDependenciesW
,
3707 dwDependenciesLength
,
3708 lpServiceStartNameW
,
3714 if (lpServiceNameW
!=NULL
)
3715 HeapFree(GetProcessHeap(), 0, lpServiceNameW
);
3717 if (lpDisplayNameW
!= NULL
)
3718 HeapFree(GetProcessHeap(), 0, lpDisplayNameW
);
3720 if (lpBinaryPathNameW
!= NULL
)
3721 HeapFree(GetProcessHeap(), 0, lpBinaryPathNameW
);
3723 if (lpLoadOrderGroupW
!= NULL
)
3724 HeapFree(GetProcessHeap(), 0, lpLoadOrderGroupW
);
3726 if (lpDependenciesW
!= NULL
)
3727 HeapFree(GetProcessHeap(), 0, lpDependenciesW
);
3729 if (lpServiceStartNameW
!= NULL
)
3730 HeapFree(GetProcessHeap(), 0, lpServiceStartNameW
);
3739 REnumDependentServicesA(
3740 SC_RPC_HANDLE hService
,
3741 DWORD dwServiceState
,
3744 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
3745 LPBOUNDED_DWORD_256K lpServicesReturned
)
3747 DWORD dwError
= ERROR_SUCCESS
;
3748 DWORD dwServicesReturned
= 0;
3749 DWORD dwServiceCount
;
3750 HKEY hServicesKey
= NULL
;
3751 PSERVICE_HANDLE hSvc
;
3752 PSERVICE lpService
= NULL
;
3753 PSERVICE
*lpServicesArray
= NULL
;
3754 LPENUM_SERVICE_STATUSA lpServicesPtr
= NULL
;
3757 *pcbBytesNeeded
= 0;
3758 *lpServicesReturned
= 0;
3760 DPRINT("REnumDependentServicesA() called\n");
3762 hSvc
= ScmGetServiceFromHandle(hService
);
3765 DPRINT1("Invalid service handle!\n");
3766 return ERROR_INVALID_HANDLE
;
3769 lpService
= hSvc
->ServiceEntry
;
3771 /* Check access rights */
3772 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
3773 SC_MANAGER_ENUMERATE_SERVICE
))
3775 DPRINT("Insufficient access rights! 0x%lx\n",
3776 hSvc
->Handle
.DesiredAccess
);
3777 return ERROR_ACCESS_DENIED
;
3780 /* Open the Services Reg key */
3781 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
3782 L
"System\\CurrentControlSet\\Services",
3787 if (dwError
!= ERROR_SUCCESS
)
3790 /* NOTE: Windows calculates the pcbBytesNeeded based on WCHAR strings for
3791 both EnumDependentServicesA and EnumDependentServicesW. So returned pcbBytesNeeded
3792 are the same for both. Verified in WINXP. */
3794 /* First determine the bytes needed and get the number of dependent services*/
3795 dwError
= Int_EnumDependentServicesW(hServicesKey
,
3800 &dwServicesReturned
);
3801 if (dwError
!= ERROR_SUCCESS
)
3804 /* If buffer size is less than the bytes needed or pointer is null*/
3805 if ((!lpServices
) || (cbBufSize
< *pcbBytesNeeded
))
3807 dwError
= ERROR_MORE_DATA
;
3811 /* Allocate memory for array of service pointers */
3812 lpServicesArray
= HeapAlloc(GetProcessHeap(),
3814 (dwServicesReturned
+ 1) * sizeof(PSERVICE
));
3815 if (!lpServicesArray
)
3817 DPRINT("Could not allocate a buffer!!\n");
3818 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
3822 dwServicesReturned
= 0;
3823 *pcbBytesNeeded
= 0;
3825 dwError
= Int_EnumDependentServicesW(hServicesKey
,
3830 &dwServicesReturned
);
3831 if (dwError
!= ERROR_SUCCESS
)
3836 lpServicesPtr
= (LPENUM_SERVICE_STATUSA
)lpServices
;
3837 lpStr
= (LPSTR
)(lpServices
+ (dwServicesReturned
* sizeof(ENUM_SERVICE_STATUSA
)));
3839 /* Copy EnumDepenedentService to Buffer */
3840 for (dwServiceCount
= 0; dwServiceCount
< dwServicesReturned
; dwServiceCount
++)
3842 lpService
= lpServicesArray
[dwServiceCount
];
3844 /* Copy the status info */
3845 memcpy(&lpServicesPtr
->ServiceStatus
,
3847 sizeof(SERVICE_STATUS
));
3849 /* Copy display name */
3850 WideCharToMultiByte(CP_ACP
,
3852 lpService
->lpDisplayName
,
3855 (int)wcslen(lpService
->lpDisplayName
),
3858 lpServicesPtr
->lpDisplayName
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServices
);
3859 lpStr
+= strlen(lpStr
) + 1;
3861 /* Copy service name */
3862 WideCharToMultiByte(CP_ACP
,
3864 lpService
->lpServiceName
,
3867 (int)wcslen(lpService
->lpServiceName
),
3870 lpServicesPtr
->lpServiceName
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServices
);
3871 lpStr
+= strlen(lpStr
) + 1;
3876 *lpServicesReturned
= dwServicesReturned
;
3879 if (lpServicesArray
)
3880 HeapFree(GetProcessHeap(), 0, lpServicesArray
);
3882 RegCloseKey(hServicesKey
);
3884 DPRINT("REnumDependentServicesA() done (Error %lu)\n", dwError
);
3893 REnumServicesStatusA(
3894 SC_RPC_HANDLE hSCManager
,
3895 DWORD dwServiceType
,
3896 DWORD dwServiceState
,
3899 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
3900 LPBOUNDED_DWORD_256K lpServicesReturned
,
3901 LPBOUNDED_DWORD_256K lpResumeHandle
)
3903 LPENUM_SERVICE_STATUSW lpStatusPtrW
= NULL
;
3904 LPENUM_SERVICE_STATUSW lpStatusPtrIncrW
;
3905 LPENUM_SERVICE_STATUSA lpStatusPtrA
= NULL
;
3906 LPWSTR lpStringPtrW
;
3909 DWORD dwServiceCount
;
3911 DPRINT("REnumServicesStatusA() called\n");
3913 if (pcbBytesNeeded
== NULL
|| lpServicesReturned
== NULL
)
3915 return ERROR_INVALID_ADDRESS
;
3918 if ((dwBufSize
> 0) && (lpBuffer
))
3920 lpStatusPtrW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, dwBufSize
);
3923 DPRINT("Failed to allocate buffer!\n");
3924 return ERROR_NOT_ENOUGH_MEMORY
;
3928 dwError
= REnumServicesStatusW(hSCManager
,
3931 (LPBYTE
)lpStatusPtrW
,
3937 /* if no services were returned then we are Done */
3938 if (*lpServicesReturned
== 0)
3941 lpStatusPtrIncrW
= lpStatusPtrW
;
3942 lpStatusPtrA
= (LPENUM_SERVICE_STATUSA
)lpBuffer
;
3943 lpStringPtrA
= (LPSTR
)((ULONG_PTR
)lpBuffer
+
3944 *lpServicesReturned
* sizeof(ENUM_SERVICE_STATUSA
));
3945 lpStringPtrW
= (LPWSTR
)((ULONG_PTR
)lpStatusPtrW
+
3946 *lpServicesReturned
* sizeof(ENUM_SERVICE_STATUSW
));
3948 for (dwServiceCount
= 0; dwServiceCount
< *lpServicesReturned
; dwServiceCount
++)
3950 /* Copy the service name */
3951 WideCharToMultiByte(CP_ACP
,
3956 (int)wcslen(lpStringPtrW
),
3960 lpStatusPtrA
->lpServiceName
= (LPSTR
)((ULONG_PTR
)lpStringPtrA
- (ULONG_PTR
)lpBuffer
);
3961 lpStringPtrA
+= wcslen(lpStringPtrW
) + 1;
3962 lpStringPtrW
+= wcslen(lpStringPtrW
) + 1;
3964 /* Copy the display name */
3965 WideCharToMultiByte(CP_ACP
,
3970 (int)wcslen(lpStringPtrW
),
3974 lpStatusPtrA
->lpDisplayName
= (LPSTR
)((ULONG_PTR
)lpStringPtrA
- (ULONG_PTR
)lpBuffer
);
3975 lpStringPtrA
+= wcslen(lpStringPtrW
) + 1;
3976 lpStringPtrW
+= wcslen(lpStringPtrW
) + 1;
3978 /* Copy the status information */
3979 memcpy(&lpStatusPtrA
->ServiceStatus
,
3980 &lpStatusPtrIncrW
->ServiceStatus
,
3981 sizeof(SERVICE_STATUS
));
3989 HeapFree(GetProcessHeap(), 0, lpStatusPtrW
);
3991 DPRINT("REnumServicesStatusA() done (Error %lu)\n", dwError
);
4001 LPSTR lpMachineName
,
4002 LPSTR lpDatabaseName
,
4003 DWORD dwDesiredAccess
,
4004 LPSC_RPC_HANDLE lpScHandle
)
4006 UNICODE_STRING MachineName
;
4007 UNICODE_STRING DatabaseName
;
4010 DPRINT("ROpenSCManagerA() called\n");
4013 RtlCreateUnicodeStringFromAsciiz(&MachineName
,
4017 RtlCreateUnicodeStringFromAsciiz(&DatabaseName
,
4020 dwError
= ROpenSCManagerW(lpMachineName
? MachineName
.Buffer
: NULL
,
4021 lpDatabaseName
? DatabaseName
.Buffer
: NULL
,
4026 RtlFreeUnicodeString(&MachineName
);
4029 RtlFreeUnicodeString(&DatabaseName
);
4039 SC_RPC_HANDLE hSCManager
,
4040 LPSTR lpServiceName
,
4041 DWORD dwDesiredAccess
,
4042 LPSC_RPC_HANDLE lpServiceHandle
)
4044 UNICODE_STRING ServiceName
;
4047 DPRINT("ROpenServiceA() called\n");
4050 RtlCreateUnicodeStringFromAsciiz(&ServiceName
,
4053 dwError
= ROpenServiceW(hSCManager
,
4054 lpServiceName
? ServiceName
.Buffer
: NULL
,
4059 RtlFreeUnicodeString(&ServiceName
);
4068 RQueryServiceConfigA(
4069 SC_RPC_HANDLE hService
,
4070 LPBYTE lpBuf
, //LPQUERY_SERVICE_CONFIGA lpServiceConfig,
4072 LPBOUNDED_DWORD_8K pcbBytesNeeded
)
4074 LPQUERY_SERVICE_CONFIGA lpServiceConfig
= (LPQUERY_SERVICE_CONFIGA
)lpBuf
;
4075 DWORD dwError
= ERROR_SUCCESS
;
4076 PSERVICE_HANDLE hSvc
;
4077 PSERVICE lpService
= NULL
;
4078 HKEY hServiceKey
= NULL
;
4079 LPWSTR lpImagePath
= NULL
;
4080 LPWSTR lpServiceStartName
= NULL
;
4081 LPWSTR lpDependencies
= NULL
;
4082 DWORD dwDependenciesLength
= 0;
4083 DWORD dwRequiredSize
;
4086 DPRINT("RQueryServiceConfigA() called\n");
4089 return ERROR_SHUTDOWN_IN_PROGRESS
;
4091 hSvc
= ScmGetServiceFromHandle(hService
);
4094 DPRINT1("Invalid service handle!\n");
4095 return ERROR_INVALID_HANDLE
;
4098 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
4099 SERVICE_QUERY_CONFIG
))
4101 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
4102 return ERROR_ACCESS_DENIED
;
4105 lpService
= hSvc
->ServiceEntry
;
4106 if (lpService
== NULL
)
4108 DPRINT("lpService == NULL!\n");
4109 return ERROR_INVALID_HANDLE
;
4112 /* Lock the service database shared */
4113 ScmLockDatabaseShared();
4115 dwError
= ScmOpenServiceKey(lpService
->lpServiceName
,
4118 if (dwError
!= ERROR_SUCCESS
)
4121 /* Read the image path */
4122 dwError
= ScmReadString(hServiceKey
,
4125 if (dwError
!= ERROR_SUCCESS
)
4128 /* Read the service start name */
4129 ScmReadString(hServiceKey
,
4131 &lpServiceStartName
);
4133 /* Read the dependencies */
4134 ScmReadDependencies(hServiceKey
,
4136 &dwDependenciesLength
);
4138 dwRequiredSize
= sizeof(QUERY_SERVICE_CONFIGA
);
4140 if (lpImagePath
!= NULL
)
4141 dwRequiredSize
+= (DWORD
)(wcslen(lpImagePath
) + 1);
4143 dwRequiredSize
+= 2 * sizeof(CHAR
);
4145 if ((lpService
->lpGroup
!= NULL
) && (lpService
->lpGroup
->lpGroupName
!= NULL
))
4146 dwRequiredSize
+= (DWORD
)(wcslen(lpService
->lpGroup
->lpGroupName
) + 1);
4148 dwRequiredSize
+= 2 * sizeof(CHAR
);
4150 /* Add Dependencies length */
4151 if (lpDependencies
!= NULL
)
4152 dwRequiredSize
+= dwDependenciesLength
;
4154 dwRequiredSize
+= 2 * sizeof(CHAR
);
4156 if (lpServiceStartName
!= NULL
)
4157 dwRequiredSize
+= (DWORD
)(wcslen(lpServiceStartName
) + 1);
4159 dwRequiredSize
+= 2 * sizeof(CHAR
);
4161 if (lpService
->lpDisplayName
!= NULL
)
4162 dwRequiredSize
+= (DWORD
)(wcslen(lpService
->lpDisplayName
) + 1);
4164 dwRequiredSize
+= 2 * sizeof(CHAR
);
4166 if (lpServiceConfig
== NULL
|| cbBufSize
< dwRequiredSize
)
4168 dwError
= ERROR_INSUFFICIENT_BUFFER
;
4172 lpServiceConfig
->dwServiceType
= lpService
->Status
.dwServiceType
;
4173 lpServiceConfig
->dwStartType
= lpService
->dwStartType
;
4174 lpServiceConfig
->dwErrorControl
= lpService
->dwErrorControl
;
4175 lpServiceConfig
->dwTagId
= lpService
->dwTag
;
4177 lpStr
= (LPSTR
)(lpServiceConfig
+ 1);
4179 /* NOTE: Strings that are NULL for QUERY_SERVICE_CONFIG are pointers to empty strings.
4180 Verified in WINXP */
4184 WideCharToMultiByte(CP_ACP
,
4189 (int)(wcslen(lpImagePath
) + 1),
4198 lpServiceConfig
->lpBinaryPathName
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
4199 lpStr
+= (strlen((LPSTR
)lpStr
) + 1);
4201 if (lpService
->lpGroup
&& lpService
->lpGroup
->lpGroupName
)
4203 WideCharToMultiByte(CP_ACP
,
4205 lpService
->lpGroup
->lpGroupName
,
4208 (int)(wcslen(lpService
->lpGroup
->lpGroupName
) + 1),
4217 lpServiceConfig
->lpLoadOrderGroup
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
4218 lpStr
+= (strlen(lpStr
) + 1);
4220 /* Append Dependencies */
4223 WideCharToMultiByte(CP_ACP
,
4226 dwDependenciesLength
,
4228 dwDependenciesLength
,
4237 lpServiceConfig
->lpDependencies
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
4239 lpStr
+= dwDependenciesLength
;
4241 lpStr
+= (strlen(lpStr
) + 1);
4243 if (lpServiceStartName
)
4245 WideCharToMultiByte(CP_ACP
,
4250 (int)(wcslen(lpServiceStartName
) + 1),
4259 lpServiceConfig
->lpServiceStartName
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
4260 lpStr
+= (strlen(lpStr
) + 1);
4262 if (lpService
->lpDisplayName
)
4264 WideCharToMultiByte(CP_ACP
,
4266 lpService
->lpDisplayName
,
4269 (int)(wcslen(lpService
->lpDisplayName
) + 1),
4278 lpServiceConfig
->lpDisplayName
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
4281 if (pcbBytesNeeded
!= NULL
)
4282 *pcbBytesNeeded
= dwRequiredSize
;
4285 /* Unlock the service database */
4286 ScmUnlockDatabase();
4288 if (lpImagePath
!= NULL
)
4289 HeapFree(GetProcessHeap(), 0, lpImagePath
);
4291 if (lpServiceStartName
!= NULL
)
4292 HeapFree(GetProcessHeap(), 0, lpServiceStartName
);
4294 if (lpDependencies
!= NULL
)
4295 HeapFree(GetProcessHeap(), 0, lpDependencies
);
4297 if (hServiceKey
!= NULL
)
4298 RegCloseKey(hServiceKey
);
4300 DPRINT("RQueryServiceConfigA() done\n");
4309 RQueryServiceLockStatusA(
4310 SC_RPC_HANDLE hSCManager
,
4311 LPBYTE lpBuf
, // LPQUERY_SERVICE_LOCK_STATUSA lpLockStatus,
4313 LPBOUNDED_DWORD_4K pcbBytesNeeded
)
4315 LPQUERY_SERVICE_LOCK_STATUSA lpLockStatus
= (LPQUERY_SERVICE_LOCK_STATUSA
)lpBuf
;
4316 PMANAGER_HANDLE hMgr
;
4317 DWORD dwRequiredSize
;
4319 if (!lpLockStatus
|| !pcbBytesNeeded
)
4320 return ERROR_INVALID_PARAMETER
;
4322 hMgr
= ScmGetServiceManagerFromHandle(hSCManager
);
4325 DPRINT1("Invalid service manager handle!\n");
4326 return ERROR_INVALID_HANDLE
;
4329 if (!RtlAreAllAccessesGranted(hMgr
->Handle
.DesiredAccess
,
4330 SC_MANAGER_QUERY_LOCK_STATUS
))
4332 DPRINT("Insufficient access rights! 0x%lx\n", hMgr
->Handle
.DesiredAccess
);
4333 return ERROR_ACCESS_DENIED
;
4336 /* FIXME: we need to compute instead the real length of the owner name */
4337 dwRequiredSize
= sizeof(QUERY_SERVICE_LOCK_STATUSA
) + sizeof(CHAR
);
4338 *pcbBytesNeeded
= dwRequiredSize
;
4340 if (cbBufSize
< dwRequiredSize
)
4341 return ERROR_INSUFFICIENT_BUFFER
;
4343 ScmQueryServiceLockStatusA(lpLockStatus
);
4345 return ERROR_SUCCESS
;
4353 SC_RPC_HANDLE hService
,
4355 LPSTRING_PTRSA argv
)
4357 DWORD dwError
= ERROR_SUCCESS
;
4358 PSERVICE_HANDLE hSvc
;
4359 PSERVICE lpService
= NULL
;
4360 LPWSTR
*lpVector
= NULL
;
4364 DPRINT("RStartServiceA() called\n");
4367 return ERROR_SHUTDOWN_IN_PROGRESS
;
4369 hSvc
= ScmGetServiceFromHandle(hService
);
4372 DPRINT1("Invalid service handle!\n");
4373 return ERROR_INVALID_HANDLE
;
4376 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
4379 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
4380 return ERROR_ACCESS_DENIED
;
4383 lpService
= hSvc
->ServiceEntry
;
4384 if (lpService
== NULL
)
4386 DPRINT("lpService == NULL!\n");
4387 return ERROR_INVALID_HANDLE
;
4390 if (lpService
->dwStartType
== SERVICE_DISABLED
)
4391 return ERROR_SERVICE_DISABLED
;
4393 if (lpService
->bDeleted
)
4394 return ERROR_SERVICE_MARKED_FOR_DELETE
;
4396 /* Build a Unicode argument vector */
4399 lpVector
= HeapAlloc(GetProcessHeap(),
4401 argc
* sizeof(LPWSTR
));
4402 if (lpVector
== NULL
)
4403 return ERROR_NOT_ENOUGH_MEMORY
;
4405 for (i
= 0; i
< argc
; i
++)
4407 dwLength
= MultiByteToWideChar(CP_ACP
,
4414 lpVector
[i
] = HeapAlloc(GetProcessHeap(),
4416 dwLength
* sizeof(WCHAR
));
4417 if (lpVector
[i
] == NULL
)
4419 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
4423 MultiByteToWideChar(CP_ACP
,
4432 /* Start the service */
4433 dwError
= ScmStartService(lpService
, argc
, lpVector
);
4436 /* Free the Unicode argument vector */
4437 if (lpVector
!= NULL
)
4439 for (i
= 0; i
< argc
; i
++)
4441 if (lpVector
[i
] != NULL
)
4442 HeapFree(GetProcessHeap(), 0, lpVector
[i
]);
4444 HeapFree(GetProcessHeap(), 0, lpVector
);
4454 RGetServiceDisplayNameA(
4455 SC_RPC_HANDLE hSCManager
,
4456 LPCSTR lpServiceName
,
4457 LPSTR lpDisplayName
,
4458 LPBOUNDED_DWORD_4K lpcchBuffer
)
4460 // PMANAGER_HANDLE hManager;
4461 PSERVICE lpService
= NULL
;
4462 LPCWSTR lpSvcDisplayName
;
4463 LPWSTR lpServiceNameW
;
4466 DPRINT("RGetServiceDisplayNameA() called\n");
4467 DPRINT("hSCManager = %p\n", hSCManager
);
4468 DPRINT("lpServiceName: %s\n", lpServiceName
);
4469 DPRINT("lpDisplayName: %p\n", lpDisplayName
);
4470 DPRINT("*lpcchBuffer: %lu\n", *lpcchBuffer
);
4473 hManager
= (PMANAGER_HANDLE
)hSCManager
;
4474 if (hManager
->Handle
.Tag
!= MANAGER_TAG
)
4476 DPRINT("Invalid manager handle!\n");
4477 return ERROR_INVALID_HANDLE
;
4481 /* Get service database entry */
4482 if (lpServiceName
!= NULL
)
4484 dwLength
= (DWORD
)(strlen(lpServiceName
) + 1);
4485 lpServiceNameW
= HeapAlloc(GetProcessHeap(),
4487 dwLength
* sizeof(WCHAR
));
4488 if (!lpServiceNameW
)
4489 return ERROR_NOT_ENOUGH_MEMORY
;
4491 MultiByteToWideChar(CP_ACP
,
4498 lpService
= ScmGetServiceEntryByName(lpServiceNameW
);
4500 HeapFree(GetProcessHeap(), 0, lpServiceNameW
);
4503 if (lpService
== NULL
)
4505 DPRINT("Could not find the service!\n");
4506 return ERROR_SERVICE_DOES_NOT_EXIST
;
4509 if (lpService
->lpDisplayName
)
4510 lpSvcDisplayName
= lpService
->lpDisplayName
;
4512 lpSvcDisplayName
= lpService
->lpServiceName
;
4515 * NOTE: On Windows the comparison on *lpcchBuffer is made against
4516 * the number of (wide) characters of the UNICODE display name, and
4517 * not against the number of bytes needed to store the ANSI string.
4519 dwLength
= (DWORD
)wcslen(lpSvcDisplayName
);
4521 if (*lpcchBuffer
> dwLength
)
4523 if (lpDisplayName
!= NULL
&&
4524 WideCharToMultiByte(CP_ACP
,
4534 * But then, if *lpcchBuffer was greater than the number of
4535 * (wide) characters of the UNICODE display name, yet smaller
4536 * than the number of bytes needed due to the possible presence
4537 * of DBCS characters, the *exact* number of bytes is returned
4538 * (without the NULL terminator).
4540 dwLength
= (DWORD
)WideCharToMultiByte(CP_ACP
,
4549 *lpcchBuffer
= dwLength
;
4550 return ERROR_INSUFFICIENT_BUFFER
;
4554 * NOTE: On Windows, RGetServiceDisplayNameA() does not update
4555 * *lpcchBuffer on success, contrary to RGetServiceDisplayNameW().
4557 return ERROR_SUCCESS
;
4562 * NOTE: On Windows, if *lpcchBuffer is smaller than the number of
4563 * (wide) characters of the UNICODE display name, only an upper
4564 * estimation is returned by doubling the string length, to account
4565 * for the presence of any possible DBCS characters.
4567 *lpcchBuffer
= dwLength
* sizeof(WCHAR
);
4568 return ERROR_INSUFFICIENT_BUFFER
;
4576 RGetServiceKeyNameA(
4577 SC_RPC_HANDLE hSCManager
,
4578 LPCSTR lpDisplayName
,
4579 LPSTR lpServiceName
,
4580 LPBOUNDED_DWORD_4K lpcchBuffer
)
4582 // PMANAGER_HANDLE hManager;
4584 LPWSTR lpDisplayNameW
;
4587 DPRINT("RGetServiceKeyNameA() called\n");
4588 DPRINT("hSCManager = %p\n", hSCManager
);
4589 DPRINT("lpDisplayName: %s\n", lpDisplayName
);
4590 DPRINT("lpServiceName: %p\n", lpServiceName
);
4591 DPRINT("*lpcchBuffer: %lu\n", *lpcchBuffer
);
4594 hManager
= (PMANAGER_HANDLE
)hSCManager
;
4595 if (hManager
->Handle
.Tag
!= MANAGER_TAG
)
4597 DPRINT("Invalid manager handle!\n");
4598 return ERROR_INVALID_HANDLE
;
4602 /* Get service database entry */
4604 dwLength
= (DWORD
)(strlen(lpDisplayName
) + 1);
4605 lpDisplayNameW
= HeapAlloc(GetProcessHeap(),
4607 dwLength
* sizeof(WCHAR
));
4608 if (!lpDisplayNameW
)
4609 return ERROR_NOT_ENOUGH_MEMORY
;
4611 MultiByteToWideChar(CP_ACP
,
4618 lpService
= ScmGetServiceEntryByDisplayName(lpDisplayNameW
);
4620 HeapFree(GetProcessHeap(), 0, lpDisplayNameW
);
4622 if (lpService
== NULL
)
4624 DPRINT("Could not find the service!\n");
4625 return ERROR_SERVICE_DOES_NOT_EXIST
;
4629 * NOTE: On Windows the comparison on *lpcchBuffer is made against
4630 * the number of (wide) characters of the UNICODE service name, and
4631 * not against the number of bytes needed to store the ANSI string.
4633 dwLength
= (DWORD
)wcslen(lpService
->lpServiceName
);
4635 if (*lpcchBuffer
> dwLength
)
4637 if (lpServiceName
!= NULL
&&
4638 WideCharToMultiByte(CP_ACP
,
4640 lpService
->lpServiceName
,
4648 * But then, if *lpcchBuffer was greater than the number of
4649 * (wide) characters of the UNICODE service name, yet smaller
4650 * than the number of bytes needed due to the possible presence
4651 * of DBCS characters, the *exact* number of bytes is returned
4652 * (without the NULL terminator).
4654 dwLength
= (DWORD
)WideCharToMultiByte(CP_ACP
,
4656 lpService
->lpServiceName
,
4663 *lpcchBuffer
= dwLength
;
4664 return ERROR_INSUFFICIENT_BUFFER
;
4668 * NOTE: On Windows, RGetServiceKeyNameA() does not update
4669 * *lpcchBuffer on success, contrary to RGetServiceKeyNameW().
4671 return ERROR_SUCCESS
;
4676 * NOTE: On Windows, if *lpcchBuffer is smaller than the number of
4677 * (wide) characters of the UNICODE service name, only an upper
4678 * estimation is returned by doubling the string length, to account
4679 * for the presence of any possible DBCS characters.
4681 *lpcchBuffer
= dwLength
* sizeof(WCHAR
);
4682 return ERROR_INSUFFICIENT_BUFFER
;
4690 RI_ScGetCurrentGroupStateW(
4691 SC_RPC_HANDLE hSCManager
,
4692 LPWSTR lpLoadOrderGroup
,
4695 PMANAGER_HANDLE hManager
;
4696 PSERVICE_GROUP pServiceGroup
;
4697 DWORD dwError
= ERROR_SUCCESS
;
4699 DPRINT("RI_ScGetCurrentGroupStateW() called\n");
4702 return ERROR_SHUTDOWN_IN_PROGRESS
;
4704 hManager
= ScmGetServiceManagerFromHandle(hSCManager
);
4705 if (hManager
== NULL
)
4707 DPRINT1("Invalid service manager handle!\n");
4708 return ERROR_INVALID_HANDLE
;
4711 /* Check for SC_MANAGER_ENUMERATE_SERVICE access right */
4712 if (!RtlAreAllAccessesGranted(hManager
->Handle
.DesiredAccess
,
4713 SC_MANAGER_ENUMERATE_SERVICE
))
4715 DPRINT("Insufficient access rights! 0x%lx\n",
4716 hManager
->Handle
.DesiredAccess
);
4717 return ERROR_ACCESS_DENIED
;
4720 /* Lock the service database shared */
4721 ScmLockDatabaseShared();
4723 /* Get the group list entry */
4724 pServiceGroup
= ScmGetServiceGroupByName(lpLoadOrderGroup
);
4725 if (pServiceGroup
== NULL
)
4727 dwError
= ERROR_SERVICE_DOES_NOT_EXIST
;
4731 /* FIXME: Return the group state */
4735 /* Unlock the service database */
4736 ScmUnlockDatabase();
4738 DPRINT("RI_ScGetCurrentGroupStateW() done (Error %lu)\n", dwError
);
4748 SC_RPC_HANDLE hSCManager
,
4749 DWORD dwServiceType
,
4750 DWORD dwServiceState
,
4753 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
4754 LPBOUNDED_DWORD_256K lpServicesReturned
,
4755 LPBOUNDED_DWORD_256K lpResumeIndex
,
4756 LPCWSTR pszGroupName
)
4758 PMANAGER_HANDLE hManager
;
4760 DWORD dwError
= ERROR_SUCCESS
;
4761 PLIST_ENTRY ServiceEntry
;
4762 PSERVICE CurrentService
;
4764 DWORD dwRequiredSize
;
4765 DWORD dwServiceCount
;
4767 DWORD dwLastResumeCount
= 0;
4768 LPENUM_SERVICE_STATUSW lpStatusPtr
;
4771 DPRINT("REnumServiceGroupW() called\n");
4774 return ERROR_SHUTDOWN_IN_PROGRESS
;
4776 hManager
= ScmGetServiceManagerFromHandle(hSCManager
);
4777 if (hManager
== NULL
)
4779 DPRINT1("Invalid service manager handle!\n");
4780 return ERROR_INVALID_HANDLE
;
4783 if (pcbBytesNeeded
== NULL
|| lpServicesReturned
== NULL
)
4785 return ERROR_INVALID_ADDRESS
;
4788 *pcbBytesNeeded
= 0;
4789 *lpServicesReturned
= 0;
4791 if ((dwServiceType
== 0) ||
4792 ((dwServiceType
& ~SERVICE_TYPE_ALL
) != 0))
4794 DPRINT("Not a valid Service Type!\n");
4795 return ERROR_INVALID_PARAMETER
;
4798 if ((dwServiceState
== 0) ||
4799 ((dwServiceState
& ~SERVICE_STATE_ALL
) != 0))
4801 DPRINT("Not a valid Service State!\n");
4802 return ERROR_INVALID_PARAMETER
;
4805 /* Check access rights */
4806 if (!RtlAreAllAccessesGranted(hManager
->Handle
.DesiredAccess
,
4807 SC_MANAGER_ENUMERATE_SERVICE
))
4809 DPRINT("Insufficient access rights! 0x%lx\n",
4810 hManager
->Handle
.DesiredAccess
);
4811 return ERROR_ACCESS_DENIED
;
4815 dwLastResumeCount
= *lpResumeIndex
;
4817 /* Lock the service database shared */
4818 ScmLockDatabaseShared();
4820 lpService
= ScmGetServiceEntryByResumeCount(dwLastResumeCount
);
4821 if (lpService
== NULL
)
4823 dwError
= ERROR_SUCCESS
;
4830 for (ServiceEntry
= &lpService
->ServiceListEntry
;
4831 ServiceEntry
!= &ServiceListHead
;
4832 ServiceEntry
= ServiceEntry
->Flink
)
4834 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
4838 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
4841 dwState
= SERVICE_ACTIVE
;
4842 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
4843 dwState
= SERVICE_INACTIVE
;
4845 if ((dwState
& dwServiceState
) == 0)
4850 if (*pszGroupName
== 0)
4852 if (CurrentService
->lpGroup
!= NULL
)
4857 if ((CurrentService
->lpGroup
== NULL
) ||
4858 _wcsicmp(pszGroupName
, CurrentService
->lpGroup
->lpGroupName
) != 0)
4863 dwSize
= sizeof(ENUM_SERVICE_STATUSW
) +
4864 (DWORD
)((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
4865 (DWORD
)((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
4867 if (dwRequiredSize
+ dwSize
> cbBufSize
)
4869 DPRINT("Service name: %S no fit\n", CurrentService
->lpServiceName
);
4873 DPRINT("Service name: %S fit\n", CurrentService
->lpServiceName
);
4874 dwRequiredSize
+= dwSize
;
4876 dwLastResumeCount
= CurrentService
->dwResumeCount
;
4879 DPRINT("dwRequiredSize: %lu\n", dwRequiredSize
);
4880 DPRINT("dwServiceCount: %lu\n", dwServiceCount
);
4883 ServiceEntry
!= &ServiceListHead
;
4884 ServiceEntry
= ServiceEntry
->Flink
)
4886 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
4890 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
4893 dwState
= SERVICE_ACTIVE
;
4894 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
4895 dwState
= SERVICE_INACTIVE
;
4897 if ((dwState
& dwServiceState
) == 0)
4902 if (*pszGroupName
== 0)
4904 if (CurrentService
->lpGroup
!= NULL
)
4909 if ((CurrentService
->lpGroup
== NULL
) ||
4910 _wcsicmp(pszGroupName
, CurrentService
->lpGroup
->lpGroupName
) != 0)
4915 dwRequiredSize
+= (sizeof(ENUM_SERVICE_STATUSW
) +
4916 (DWORD
)((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
4917 (DWORD
)((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
)));
4919 dwError
= ERROR_MORE_DATA
;
4922 DPRINT("*pcbBytesNeeded: %lu\n", dwRequiredSize
);
4925 *lpResumeIndex
= dwLastResumeCount
;
4927 *lpServicesReturned
= dwServiceCount
;
4928 *pcbBytesNeeded
= dwRequiredSize
;
4930 lpStatusPtr
= (LPENUM_SERVICE_STATUSW
)lpBuffer
;
4931 lpStringPtr
= (LPWSTR
)((ULONG_PTR
)lpBuffer
+
4932 dwServiceCount
* sizeof(ENUM_SERVICE_STATUSW
));
4935 for (ServiceEntry
= &lpService
->ServiceListEntry
;
4936 ServiceEntry
!= &ServiceListHead
;
4937 ServiceEntry
= ServiceEntry
->Flink
)
4939 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
4943 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
4946 dwState
= SERVICE_ACTIVE
;
4947 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
4948 dwState
= SERVICE_INACTIVE
;
4950 if ((dwState
& dwServiceState
) == 0)
4955 if (*pszGroupName
== 0)
4957 if (CurrentService
->lpGroup
!= NULL
)
4962 if ((CurrentService
->lpGroup
== NULL
) ||
4963 _wcsicmp(pszGroupName
, CurrentService
->lpGroup
->lpGroupName
) != 0)
4968 dwSize
= sizeof(ENUM_SERVICE_STATUSW
) +
4969 (DWORD
)((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
4970 (DWORD
)((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
4972 if (dwRequiredSize
+ dwSize
> cbBufSize
)
4975 /* Copy the service name */
4976 wcscpy(lpStringPtr
, CurrentService
->lpServiceName
);
4977 lpStatusPtr
->lpServiceName
= (LPWSTR
)((ULONG_PTR
)lpStringPtr
- (ULONG_PTR
)lpBuffer
);
4978 lpStringPtr
+= (wcslen(CurrentService
->lpServiceName
) + 1);
4980 /* Copy the display name */
4981 wcscpy(lpStringPtr
, CurrentService
->lpDisplayName
);
4982 lpStatusPtr
->lpDisplayName
= (LPWSTR
)((ULONG_PTR
)lpStringPtr
- (ULONG_PTR
)lpBuffer
);
4983 lpStringPtr
+= (wcslen(CurrentService
->lpDisplayName
) + 1);
4985 /* Copy the status information */
4986 memcpy(&lpStatusPtr
->ServiceStatus
,
4987 &CurrentService
->Status
,
4988 sizeof(SERVICE_STATUS
));
4991 dwRequiredSize
+= dwSize
;
4994 if (dwError
== ERROR_SUCCESS
)
4996 *pcbBytesNeeded
= 0;
4997 if (lpResumeIndex
) *lpResumeIndex
= 0;
5001 /* Unlock the service database */
5002 ScmUnlockDatabase();
5004 DPRINT("REnumServiceGroupW() done (Error %lu)\n", dwError
);
5013 RChangeServiceConfig2A(
5014 SC_RPC_HANDLE hService
,
5015 SC_RPC_CONFIG_INFOA Info
)
5017 SC_RPC_CONFIG_INFOW InfoW
= { 0 };
5018 DWORD dwRet
, dwLength
;
5021 DPRINT("RChangeServiceConfig2A() called\n");
5022 DPRINT("dwInfoLevel = %lu\n", Info
.dwInfoLevel
);
5024 if ((Info
.dwInfoLevel
< SERVICE_CONFIG_DESCRIPTION
) ||
5025 (Info
.dwInfoLevel
> SERVICE_CONFIG_FAILURE_ACTIONS
))
5027 return ERROR_INVALID_LEVEL
;
5030 InfoW
.dwInfoLevel
= Info
.dwInfoLevel
;
5032 if (InfoW
.dwInfoLevel
== SERVICE_CONFIG_DESCRIPTION
)
5034 LPSERVICE_DESCRIPTIONW lpServiceDescriptionW
;
5035 LPSERVICE_DESCRIPTIONA lpServiceDescriptionA
;
5037 lpServiceDescriptionA
= Info
.psd
;
5039 if (lpServiceDescriptionA
&&
5040 lpServiceDescriptionA
->lpDescription
)
5042 dwLength
= (DWORD
)((strlen(lpServiceDescriptionA
->lpDescription
) + 1) * sizeof(WCHAR
));
5044 lpServiceDescriptionW
= HeapAlloc(GetProcessHeap(),
5046 dwLength
+ sizeof(SERVICE_DESCRIPTIONW
));
5047 if (!lpServiceDescriptionW
)
5049 return ERROR_NOT_ENOUGH_MEMORY
;
5052 lpServiceDescriptionW
->lpDescription
= (LPWSTR
)(lpServiceDescriptionW
+ 1);
5054 MultiByteToWideChar(CP_ACP
,
5056 lpServiceDescriptionA
->lpDescription
,
5058 lpServiceDescriptionW
->lpDescription
,
5061 ptr
= lpServiceDescriptionW
;
5062 InfoW
.psd
= lpServiceDescriptionW
;
5065 else if (Info
.dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
5067 LPSERVICE_FAILURE_ACTIONSW lpServiceFailureActionsW
;
5068 LPSERVICE_FAILURE_ACTIONSA lpServiceFailureActionsA
;
5069 DWORD dwRebootLen
= 0;
5070 DWORD dwCommandLen
= 0;
5071 DWORD dwActionArrayLen
= 0;
5072 LPWSTR lpStr
= NULL
;
5074 lpServiceFailureActionsA
= Info
.psfa
;
5076 if (lpServiceFailureActionsA
)
5079 * The following code is inspired by the
5080 * SERVICE_CONFIG_FAILURE_ACTIONS case of
5081 * the RQueryServiceConfig2W function.
5084 /* Retrieve the needed length for the two data strings */
5085 if (lpServiceFailureActionsA
->lpRebootMsg
)
5087 dwRebootLen
= (DWORD
)((strlen(lpServiceFailureActionsA
->lpRebootMsg
) + 1) * sizeof(WCHAR
));
5089 if (lpServiceFailureActionsA
->lpCommand
)
5091 dwCommandLen
= (DWORD
)((strlen(lpServiceFailureActionsA
->lpCommand
) + 1) * sizeof(WCHAR
));
5095 * Retrieve the size of the lpsaActions array if needed.
5096 * We will copy the lpsaActions array only if there is at
5097 * least one action AND that the original array is valid.
5099 if (lpServiceFailureActionsA
->cActions
> 0 && lpServiceFailureActionsA
->lpsaActions
)
5101 dwActionArrayLen
= lpServiceFailureActionsA
->cActions
* sizeof(SC_ACTION
);
5104 /* Compute the total length for the UNICODE structure, including data */
5105 dwLength
= sizeof(SERVICE_FAILURE_ACTIONSW
) +
5106 dwActionArrayLen
+ dwRebootLen
+ dwCommandLen
;
5108 /* Allocate the structure */
5109 lpServiceFailureActionsW
= HeapAlloc(GetProcessHeap(),
5112 if (!lpServiceFailureActionsW
)
5114 return ERROR_NOT_ENOUGH_MEMORY
;
5117 /* Copy the members */
5118 lpServiceFailureActionsW
->dwResetPeriod
= lpServiceFailureActionsA
->dwResetPeriod
;
5119 lpServiceFailureActionsW
->cActions
= lpServiceFailureActionsA
->cActions
;
5121 /* Copy the lpsaActions array if needed */
5122 if (dwActionArrayLen
> 0)
5124 /* The storage zone is just after the end of the SERVICE_FAILURE_ACTIONSW structure */
5125 lpServiceFailureActionsW
->lpsaActions
= (LPSC_ACTION
)((ULONG_PTR
)(lpServiceFailureActionsW
+ 1));
5127 /* dwActionArrayLen == lpServiceFailureActionsW->cActions * sizeof(SC_ACTION) */
5128 RtlCopyMemory(lpServiceFailureActionsW
->lpsaActions
,
5129 lpServiceFailureActionsA
->lpsaActions
,
5134 /* No lpsaActions array */
5135 lpServiceFailureActionsW
->lpsaActions
= NULL
;
5137 /* The data strings are stored just after the lpsaActions array */
5138 lpStr
= (LPWSTR
)((ULONG_PTR
)(lpServiceFailureActionsW
+ 1) + dwActionArrayLen
);
5141 * Convert the data strings to UNICODE
5144 lpServiceFailureActionsW
->lpRebootMsg
= NULL
;
5145 lpServiceFailureActionsW
->lpCommand
= NULL
;
5149 /* lpRebootMsg points just after the lpsaActions array */
5150 lpServiceFailureActionsW
->lpRebootMsg
= lpStr
;
5152 MultiByteToWideChar(CP_ACP
,
5154 lpServiceFailureActionsA
->lpRebootMsg
,
5156 lpServiceFailureActionsW
->lpRebootMsg
,
5159 lpStr
+= dwRebootLen
/ sizeof(WCHAR
);
5164 /* lpRebootMsg points just after the lpRebootMsg data string */
5165 lpServiceFailureActionsW
->lpCommand
= lpStr
;
5167 MultiByteToWideChar(CP_ACP
,
5169 lpServiceFailureActionsA
->lpCommand
,
5171 lpServiceFailureActionsW
->lpCommand
,
5175 /* Set the pointers */
5176 ptr
= lpServiceFailureActionsW
;
5177 InfoW
.psfa
= lpServiceFailureActionsW
;
5181 dwRet
= RChangeServiceConfig2W(hService
, InfoW
);
5183 HeapFree(GetProcessHeap(), 0, ptr
);
5190 ScmSetFailureActions(HKEY hServiceKey
,
5191 LPSERVICE_FAILURE_ACTIONSW lpFailureActions
)
5193 LPSERVICE_FAILURE_ACTIONSW lpReadBuffer
= NULL
;
5194 LPSERVICE_FAILURE_ACTIONSW lpWriteBuffer
= NULL
;
5195 DWORD dwRequiredSize
= 0;
5199 /* There is nothing to be done if we have no failure actions */
5200 if (lpFailureActions
== NULL
)
5201 return ERROR_SUCCESS
;
5204 * 1- Retrieve the original value of FailureActions.
5207 /* Query value length */
5208 dwError
= RegQueryValueExW(hServiceKey
,
5214 if (dwError
!= ERROR_SUCCESS
&&
5215 dwError
!= ERROR_MORE_DATA
&&
5216 dwError
!= ERROR_FILE_NOT_FOUND
)
5221 dwRequiredSize
= (dwType
== REG_BINARY
) ? max(sizeof(SERVICE_FAILURE_ACTIONSW
), dwRequiredSize
)
5222 : sizeof(SERVICE_FAILURE_ACTIONSW
);
5224 /* Initialize the read buffer */
5225 lpReadBuffer
= HeapAlloc(GetProcessHeap(),
5228 if (lpReadBuffer
== NULL
)
5229 return ERROR_NOT_ENOUGH_MEMORY
;
5231 /* Now we can fill the read buffer */
5232 if (dwError
!= ERROR_FILE_NOT_FOUND
&&
5233 dwType
== REG_BINARY
)
5235 dwError
= RegQueryValueExW(hServiceKey
,
5239 (LPBYTE
)lpReadBuffer
,
5241 if (dwError
!= ERROR_SUCCESS
&&
5242 dwError
!= ERROR_FILE_NOT_FOUND
)
5245 if (dwRequiredSize
< sizeof(SERVICE_FAILURE_ACTIONSW
))
5246 dwRequiredSize
= sizeof(SERVICE_FAILURE_ACTIONSW
);
5251 * The value of the error doesn't really matter, the only
5252 * important thing is that it must be != ERROR_SUCCESS.
5254 dwError
= ERROR_INVALID_DATA
;
5257 if (dwError
== ERROR_SUCCESS
)
5259 lpReadBuffer
->cActions
= min(lpReadBuffer
->cActions
, (dwRequiredSize
- sizeof(SERVICE_FAILURE_ACTIONSW
)) / sizeof(SC_ACTION
));
5260 lpReadBuffer
->lpsaActions
= (lpReadBuffer
->cActions
> 0 ? (LPSC_ACTION
)(lpReadBuffer
+ 1) : NULL
);
5264 lpReadBuffer
->dwResetPeriod
= 0;
5265 lpReadBuffer
->cActions
= 0;
5266 lpReadBuffer
->lpsaActions
= NULL
;
5269 lpReadBuffer
->lpRebootMsg
= NULL
;
5270 lpReadBuffer
->lpCommand
= NULL
;
5273 * 2- Initialize the new value to set.
5276 dwRequiredSize
= sizeof(SERVICE_FAILURE_ACTIONSW
);
5278 if (lpFailureActions
->lpsaActions
== NULL
)
5281 * lpFailureActions->cActions is ignored.
5282 * Therefore we use the original values
5283 * of cActions and lpsaActions.
5285 dwRequiredSize
+= lpReadBuffer
->cActions
* sizeof(SC_ACTION
);
5290 * The reset period and array of failure actions
5291 * are deleted if lpFailureActions->cActions == 0 .
5293 dwRequiredSize
+= lpFailureActions
->cActions
* sizeof(SC_ACTION
);
5296 lpWriteBuffer
= HeapAlloc(GetProcessHeap(),
5299 if (lpWriteBuffer
== NULL
)
5301 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
5305 /* Clean the pointers as they have no meaning when the structure is stored in the registry */
5306 lpWriteBuffer
->lpRebootMsg
= NULL
;
5307 lpWriteBuffer
->lpCommand
= NULL
;
5308 lpWriteBuffer
->lpsaActions
= NULL
;
5310 /* Set the members */
5311 if (lpFailureActions
->lpsaActions
== NULL
)
5314 * lpFailureActions->dwResetPeriod and lpFailureActions->cActions are ignored.
5315 * Therefore we use the original values of dwResetPeriod, cActions and lpsaActions.
5317 lpWriteBuffer
->dwResetPeriod
= lpReadBuffer
->dwResetPeriod
;
5318 lpWriteBuffer
->cActions
= lpReadBuffer
->cActions
;
5320 if (lpReadBuffer
->lpsaActions
!= NULL
)
5322 memmove(lpWriteBuffer
+ 1,
5323 lpReadBuffer
->lpsaActions
,
5324 lpReadBuffer
->cActions
* sizeof(SC_ACTION
));
5329 if (lpFailureActions
->cActions
> 0)
5331 lpWriteBuffer
->dwResetPeriod
= lpFailureActions
->dwResetPeriod
;
5332 lpWriteBuffer
->cActions
= lpFailureActions
->cActions
;
5334 memmove(lpWriteBuffer
+ 1,
5335 lpFailureActions
->lpsaActions
,
5336 lpFailureActions
->cActions
* sizeof(SC_ACTION
));
5340 /* The reset period and array of failure actions are deleted */
5341 lpWriteBuffer
->dwResetPeriod
= 0;
5342 lpWriteBuffer
->cActions
= 0;
5346 /* Save the new failure actions into the registry */
5347 dwError
= RegSetValueExW(hServiceKey
,
5351 (LPBYTE
)lpWriteBuffer
,
5354 /* We modify the strings only in case of success.*/
5355 if (dwError
== ERROR_SUCCESS
)
5357 /* Modify the Reboot Message value, if specified */
5358 if (lpFailureActions
->lpRebootMsg
!= NULL
)
5360 /* If the Reboot Message is "" then we delete it */
5361 if (*lpFailureActions
->lpRebootMsg
== 0)
5363 DPRINT("Delete Reboot Message value\n");
5364 RegDeleteValueW(hServiceKey
, L
"RebootMessage");
5368 DPRINT("Setting Reboot Message value %S\n", lpFailureActions
->lpRebootMsg
);
5369 RegSetValueExW(hServiceKey
,
5373 (LPBYTE
)lpFailureActions
->lpRebootMsg
,
5374 (DWORD
)((wcslen(lpFailureActions
->lpRebootMsg
) + 1) * sizeof(WCHAR
)));
5378 /* Modify the Failure Command value, if specified */
5379 if (lpFailureActions
->lpCommand
!= NULL
)
5381 /* If the FailureCommand string is an empty string, delete the value */
5382 if (*lpFailureActions
->lpCommand
== 0)
5384 DPRINT("Delete Failure Command value\n");
5385 RegDeleteValueW(hServiceKey
, L
"FailureCommand");
5389 DPRINT("Setting Failure Command value %S\n", lpFailureActions
->lpCommand
);
5390 RegSetValueExW(hServiceKey
,
5394 (LPBYTE
)lpFailureActions
->lpCommand
,
5395 (DWORD
)((wcslen(lpFailureActions
->lpCommand
) + 1) * sizeof(WCHAR
)));
5401 if (lpWriteBuffer
!= NULL
)
5402 HeapFree(GetProcessHeap(), 0, lpWriteBuffer
);
5404 if (lpReadBuffer
!= NULL
)
5405 HeapFree(GetProcessHeap(), 0, lpReadBuffer
);
5414 RChangeServiceConfig2W(
5415 SC_RPC_HANDLE hService
,
5416 SC_RPC_CONFIG_INFOW Info
)
5418 DWORD dwError
= ERROR_SUCCESS
;
5419 PSERVICE_HANDLE hSvc
;
5420 PSERVICE lpService
= NULL
;
5421 HKEY hServiceKey
= NULL
;
5422 ACCESS_MASK RequiredAccess
= SERVICE_CHANGE_CONFIG
;
5424 DPRINT("RChangeServiceConfig2W() called\n");
5425 DPRINT("dwInfoLevel = %lu\n", Info
.dwInfoLevel
);
5428 return ERROR_SHUTDOWN_IN_PROGRESS
;
5430 if ((Info
.dwInfoLevel
< SERVICE_CONFIG_DESCRIPTION
) ||
5431 (Info
.dwInfoLevel
> SERVICE_CONFIG_FAILURE_ACTIONS
))
5433 return ERROR_INVALID_LEVEL
;
5436 hSvc
= ScmGetServiceFromHandle(hService
);
5439 DPRINT("Invalid service handle!\n");
5440 return ERROR_INVALID_HANDLE
;
5443 if (Info
.dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
5444 RequiredAccess
|= SERVICE_START
;
5446 /* Check the access rights */
5447 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
5450 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
5451 return ERROR_ACCESS_DENIED
;
5454 if (Info
.dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
5456 /* FIXME: Check if the caller has the SE_SHUTDOWN_NAME privilege */
5460 lpService
= hSvc
->ServiceEntry
;
5461 if (lpService
== NULL
)
5463 DPRINT("lpService == NULL!\n");
5464 return ERROR_INVALID_HANDLE
;
5467 /* Failure actions can only be set for Win32 services, not for drivers */
5468 if (Info
.dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
5470 if (lpService
->Status
.dwServiceType
& SERVICE_DRIVER
)
5471 return ERROR_CANNOT_DETECT_DRIVER_FAILURE
;
5474 /* Lock the service database exclusively */
5475 ScmLockDatabaseExclusive();
5477 if (lpService
->bDeleted
)
5479 DPRINT("The service has already been marked for delete!\n");
5480 dwError
= ERROR_SERVICE_MARKED_FOR_DELETE
;
5484 /* Open the service key */
5485 dwError
= ScmOpenServiceKey(lpService
->szServiceName
,
5486 KEY_READ
| KEY_SET_VALUE
,
5488 if (dwError
!= ERROR_SUCCESS
)
5491 if (Info
.dwInfoLevel
== SERVICE_CONFIG_DESCRIPTION
)
5493 LPSERVICE_DESCRIPTIONW lpServiceDescription
= (LPSERVICE_DESCRIPTIONW
)Info
.psd
;
5495 /* Modify the service description, if specified */
5496 if (lpServiceDescription
!= NULL
&&
5497 lpServiceDescription
->lpDescription
!= NULL
)
5499 /* If the description is "" then we delete it */
5500 if (*lpServiceDescription
->lpDescription
== 0)
5502 DPRINT("Delete service description\n");
5503 dwError
= RegDeleteValueW(hServiceKey
, L
"Description");
5505 if (dwError
== ERROR_FILE_NOT_FOUND
)
5506 dwError
= ERROR_SUCCESS
;
5510 DPRINT("Setting service description value %S\n", lpServiceDescription
->lpDescription
);
5511 dwError
= RegSetValueExW(hServiceKey
,
5515 (LPBYTE
)lpServiceDescription
->lpDescription
,
5516 (DWORD
)((wcslen(lpServiceDescription
->lpDescription
) + 1) * sizeof(WCHAR
)));
5521 dwError
= ERROR_SUCCESS
;
5524 else if (Info
.dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
5526 dwError
= ScmSetFailureActions(hServiceKey
,
5527 (LPSERVICE_FAILURE_ACTIONSW
)Info
.psfa
);
5531 if (hServiceKey
!= NULL
)
5532 RegCloseKey(hServiceKey
);
5534 /* Unlock the service database */
5535 ScmUnlockDatabase();
5537 DPRINT("RChangeServiceConfig2W() done (Error %lu)\n", dwError
);
5546 RQueryServiceConfig2A(
5547 SC_RPC_HANDLE hService
,
5551 LPBOUNDED_DWORD_8K pcbBytesNeeded
)
5553 DWORD dwError
= ERROR_SUCCESS
;
5554 PSERVICE_HANDLE hSvc
;
5555 PSERVICE lpService
= NULL
;
5556 HKEY hServiceKey
= NULL
;
5557 DWORD dwRequiredSize
= 0;
5559 LPWSTR lpDescriptionW
= NULL
;
5560 LPWSTR lpRebootMessageW
= NULL
;
5561 LPWSTR lpFailureCommandW
= NULL
;
5563 DPRINT("RQueryServiceConfig2A() called hService %p dwInfoLevel %u, lpBuffer %p cbBufSize %u pcbBytesNeeded %p\n",
5564 hService
, dwInfoLevel
, lpBuffer
, cbBufSize
, pcbBytesNeeded
);
5567 return ERROR_INVALID_ADDRESS
;
5570 return ERROR_SHUTDOWN_IN_PROGRESS
;
5572 if ((dwInfoLevel
< SERVICE_CONFIG_DESCRIPTION
) ||
5573 (dwInfoLevel
> SERVICE_CONFIG_FAILURE_ACTIONS
))
5575 return ERROR_INVALID_LEVEL
;
5578 hSvc
= ScmGetServiceFromHandle(hService
);
5581 DPRINT1("Invalid service handle!\n");
5582 return ERROR_INVALID_HANDLE
;
5585 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
5586 SERVICE_QUERY_CONFIG
))
5588 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
5589 return ERROR_ACCESS_DENIED
;
5592 lpService
= hSvc
->ServiceEntry
;
5593 if (lpService
== NULL
)
5595 DPRINT("lpService == NULL!\n");
5596 return ERROR_INVALID_HANDLE
;
5599 /* Lock the service database shared */
5600 ScmLockDatabaseShared();
5602 dwError
= ScmOpenServiceKey(lpService
->lpServiceName
,
5605 if (dwError
!= ERROR_SUCCESS
)
5608 if (dwInfoLevel
== SERVICE_CONFIG_DESCRIPTION
)
5610 LPSERVICE_DESCRIPTIONA lpServiceDescription
= (LPSERVICE_DESCRIPTIONA
)lpBuffer
;
5613 dwError
= ScmReadString(hServiceKey
,
5616 if (dwError
!= ERROR_SUCCESS
&& dwError
!= ERROR_FILE_NOT_FOUND
)
5619 *pcbBytesNeeded
= sizeof(SERVICE_DESCRIPTIONA
);
5620 if (dwError
== ERROR_SUCCESS
)
5621 *pcbBytesNeeded
+= (DWORD
)((wcslen(lpDescriptionW
) + 1) * sizeof(WCHAR
));
5623 if (cbBufSize
< *pcbBytesNeeded
)
5625 dwError
= ERROR_INSUFFICIENT_BUFFER
;
5629 if (dwError
== ERROR_SUCCESS
)
5631 lpStr
= (LPSTR
)(lpServiceDescription
+ 1);
5633 WideCharToMultiByte(CP_ACP
,
5638 (int)wcslen(lpDescriptionW
),
5641 lpServiceDescription
->lpDescription
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceDescription
);
5645 lpServiceDescription
->lpDescription
= NULL
;
5646 dwError
= ERROR_SUCCESS
;
5649 else if (dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
5651 LPSERVICE_FAILURE_ACTIONSA lpFailureActions
= (LPSERVICE_FAILURE_ACTIONSA
)lpBuffer
;
5654 /* Query value length */
5655 dwError
= RegQueryValueExW(hServiceKey
,
5661 if (dwError
!= ERROR_SUCCESS
&&
5662 dwError
!= ERROR_MORE_DATA
&&
5663 dwError
!= ERROR_FILE_NOT_FOUND
)
5668 dwRequiredSize
= (dwType
== REG_BINARY
) ? max(sizeof(SERVICE_FAILURE_ACTIONSA
), dwRequiredSize
)
5669 : sizeof(SERVICE_FAILURE_ACTIONSA
);
5671 /* Get the strings */
5672 ScmReadString(hServiceKey
,
5674 &lpFailureCommandW
);
5676 ScmReadString(hServiceKey
,
5680 if (lpRebootMessageW
)
5681 dwRequiredSize
+= (DWORD
)((wcslen(lpRebootMessageW
) + 1) * sizeof(WCHAR
));
5683 if (lpFailureCommandW
)
5684 dwRequiredSize
+= (DWORD
)((wcslen(lpFailureCommandW
) + 1) * sizeof(WCHAR
));
5686 if (cbBufSize
< dwRequiredSize
)
5688 *pcbBytesNeeded
= dwRequiredSize
;
5689 dwError
= ERROR_INSUFFICIENT_BUFFER
;
5693 /* Now we can fill the buffer */
5694 if (dwError
!= ERROR_FILE_NOT_FOUND
&& dwType
== REG_BINARY
)
5696 dwError
= RegQueryValueExW(hServiceKey
,
5700 (LPBYTE
)lpFailureActions
,
5702 if (dwError
!= ERROR_SUCCESS
&& dwError
!= ERROR_FILE_NOT_FOUND
)
5705 if (dwRequiredSize
< sizeof(SERVICE_FAILURE_ACTIONSA
))
5706 dwRequiredSize
= sizeof(SERVICE_FAILURE_ACTIONSA
);
5711 * The value of the error doesn't really matter, the only
5712 * important thing is that it must be != ERROR_SUCCESS .
5714 dwError
= ERROR_INVALID_DATA
;
5717 if (dwError
== ERROR_SUCCESS
)
5719 lpFailureActions
->cActions
= min(lpFailureActions
->cActions
, (dwRequiredSize
- sizeof(SERVICE_FAILURE_ACTIONSA
)) / sizeof(SC_ACTION
));
5721 /* Here lpFailureActions->lpsaActions contains an offset. The conversion is done by the caller. */
5722 lpFailureActions
->lpsaActions
= (lpFailureActions
->cActions
> 0 ? (LPSC_ACTION
)(ULONG_PTR
)sizeof(SERVICE_FAILURE_ACTIONSA
) : NULL
);
5724 lpStr
= (LPSTR
)((ULONG_PTR
)(lpFailureActions
+ 1) + lpFailureActions
->cActions
* sizeof(SC_ACTION
));
5728 lpFailureActions
->dwResetPeriod
= 0;
5729 lpFailureActions
->cActions
= 0;
5730 lpFailureActions
->lpsaActions
= NULL
;
5731 lpStr
= (LPSTR
)(lpFailureActions
+ 1);
5734 lpFailureActions
->lpRebootMsg
= NULL
;
5735 lpFailureActions
->lpCommand
= NULL
;
5737 if (lpRebootMessageW
)
5739 WideCharToMultiByte(CP_ACP
,
5744 (int)wcslen(lpRebootMessageW
),
5747 lpFailureActions
->lpRebootMsg
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpFailureActions
);
5748 lpStr
+= strlen(lpStr
) + 1;
5751 if (lpFailureCommandW
)
5753 WideCharToMultiByte(CP_ACP
,
5758 (int)wcslen(lpFailureCommandW
),
5761 lpFailureActions
->lpCommand
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpFailureActions
);
5762 /* lpStr += strlen(lpStr) + 1; */
5765 dwError
= ERROR_SUCCESS
;
5769 /* Unlock the service database */
5770 ScmUnlockDatabase();
5772 if (lpDescriptionW
!= NULL
)
5773 HeapFree(GetProcessHeap(), 0, lpDescriptionW
);
5775 if (lpRebootMessageW
!= NULL
)
5776 HeapFree(GetProcessHeap(), 0, lpRebootMessageW
);
5778 if (lpFailureCommandW
!= NULL
)
5779 HeapFree(GetProcessHeap(), 0, lpFailureCommandW
);
5781 if (hServiceKey
!= NULL
)
5782 RegCloseKey(hServiceKey
);
5784 DPRINT("RQueryServiceConfig2A() done (Error %lu)\n", dwError
);
5793 RQueryServiceConfig2W(
5794 SC_RPC_HANDLE hService
,
5798 LPBOUNDED_DWORD_8K pcbBytesNeeded
)
5800 DWORD dwError
= ERROR_SUCCESS
;
5801 PSERVICE_HANDLE hSvc
;
5802 PSERVICE lpService
= NULL
;
5803 HKEY hServiceKey
= NULL
;
5804 DWORD dwRequiredSize
= 0;
5806 LPWSTR lpDescription
= NULL
;
5807 LPWSTR lpRebootMessage
= NULL
;
5808 LPWSTR lpFailureCommand
= NULL
;
5810 DPRINT("RQueryServiceConfig2W() called\n");
5813 return ERROR_INVALID_ADDRESS
;
5816 return ERROR_SHUTDOWN_IN_PROGRESS
;
5818 if ((dwInfoLevel
< SERVICE_CONFIG_DESCRIPTION
) ||
5819 (dwInfoLevel
> SERVICE_CONFIG_FAILURE_ACTIONS
))
5821 return ERROR_INVALID_LEVEL
;
5824 hSvc
= ScmGetServiceFromHandle(hService
);
5827 DPRINT1("Invalid service handle!\n");
5828 return ERROR_INVALID_HANDLE
;
5831 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
5832 SERVICE_QUERY_CONFIG
))
5834 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
5835 return ERROR_ACCESS_DENIED
;
5838 lpService
= hSvc
->ServiceEntry
;
5839 if (lpService
== NULL
)
5841 DPRINT("lpService == NULL!\n");
5842 return ERROR_INVALID_HANDLE
;
5845 /* Lock the service database shared */
5846 ScmLockDatabaseShared();
5848 dwError
= ScmOpenServiceKey(lpService
->lpServiceName
,
5851 if (dwError
!= ERROR_SUCCESS
)
5854 if (dwInfoLevel
== SERVICE_CONFIG_DESCRIPTION
)
5856 LPSERVICE_DESCRIPTIONW lpServiceDescription
= (LPSERVICE_DESCRIPTIONW
)lpBuffer
;
5859 dwError
= ScmReadString(hServiceKey
,
5862 if (dwError
!= ERROR_SUCCESS
&& dwError
!= ERROR_FILE_NOT_FOUND
)
5865 *pcbBytesNeeded
= sizeof(SERVICE_DESCRIPTIONW
);
5866 if (dwError
== ERROR_SUCCESS
)
5867 *pcbBytesNeeded
+= (DWORD
)((wcslen(lpDescription
) + 1) * sizeof(WCHAR
));
5869 if (cbBufSize
< *pcbBytesNeeded
)
5871 dwError
= ERROR_INSUFFICIENT_BUFFER
;
5875 if (dwError
== ERROR_SUCCESS
)
5877 lpStr
= (LPWSTR
)(lpServiceDescription
+ 1);
5878 wcscpy(lpStr
, lpDescription
);
5879 lpServiceDescription
->lpDescription
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceDescription
);
5883 lpServiceDescription
->lpDescription
= NULL
;
5884 dwError
= ERROR_SUCCESS
;
5887 else if (dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
5889 LPSERVICE_FAILURE_ACTIONSW lpFailureActions
= (LPSERVICE_FAILURE_ACTIONSW
)lpBuffer
;
5890 LPWSTR lpStr
= NULL
;
5892 /* Query value length */
5893 dwError
= RegQueryValueExW(hServiceKey
,
5899 if (dwError
!= ERROR_SUCCESS
&&
5900 dwError
!= ERROR_MORE_DATA
&&
5901 dwError
!= ERROR_FILE_NOT_FOUND
)
5906 dwRequiredSize
= (dwType
== REG_BINARY
) ? max(sizeof(SERVICE_FAILURE_ACTIONSW
), dwRequiredSize
)
5907 : sizeof(SERVICE_FAILURE_ACTIONSW
);
5909 /* Get the strings */
5910 ScmReadString(hServiceKey
,
5914 ScmReadString(hServiceKey
,
5918 if (lpRebootMessage
)
5919 dwRequiredSize
+= (DWORD
)((wcslen(lpRebootMessage
) + 1) * sizeof(WCHAR
));
5921 if (lpFailureCommand
)
5922 dwRequiredSize
+= (DWORD
)((wcslen(lpFailureCommand
) + 1) * sizeof(WCHAR
));
5924 if (cbBufSize
< dwRequiredSize
)
5926 *pcbBytesNeeded
= dwRequiredSize
;
5927 dwError
= ERROR_INSUFFICIENT_BUFFER
;
5931 /* Now we can fill the buffer */
5932 if (dwError
!= ERROR_FILE_NOT_FOUND
&& dwType
== REG_BINARY
)
5934 dwError
= RegQueryValueExW(hServiceKey
,
5938 (LPBYTE
)lpFailureActions
,
5940 if (dwError
!= ERROR_SUCCESS
&& dwError
!= ERROR_FILE_NOT_FOUND
)
5943 if (dwRequiredSize
< sizeof(SERVICE_FAILURE_ACTIONSW
))
5944 dwRequiredSize
= sizeof(SERVICE_FAILURE_ACTIONSW
);
5949 * The value of the error doesn't really matter, the only
5950 * important thing is that it must be != ERROR_SUCCESS .
5952 dwError
= ERROR_INVALID_DATA
;
5955 if (dwError
== ERROR_SUCCESS
)
5957 lpFailureActions
->cActions
= min(lpFailureActions
->cActions
, (dwRequiredSize
- sizeof(SERVICE_FAILURE_ACTIONSW
)) / sizeof(SC_ACTION
));
5959 /* Here lpFailureActions->lpsaActions contains an offset. The conversion is done by the caller. */
5960 lpFailureActions
->lpsaActions
= (lpFailureActions
->cActions
> 0 ? (LPSC_ACTION
)(ULONG_PTR
)sizeof(SERVICE_FAILURE_ACTIONSW
) : NULL
);
5962 lpStr
= (LPWSTR
)((ULONG_PTR
)(lpFailureActions
+ 1) + lpFailureActions
->cActions
* sizeof(SC_ACTION
));
5966 lpFailureActions
->dwResetPeriod
= 0;
5967 lpFailureActions
->cActions
= 0;
5968 lpFailureActions
->lpsaActions
= NULL
;
5969 lpStr
= (LPWSTR
)(lpFailureActions
+ 1);
5972 lpFailureActions
->lpRebootMsg
= NULL
;
5973 lpFailureActions
->lpCommand
= NULL
;
5975 if (lpRebootMessage
)
5977 wcscpy(lpStr
, lpRebootMessage
);
5978 lpFailureActions
->lpRebootMsg
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpFailureActions
);
5979 lpStr
+= wcslen(lpStr
) + 1;
5982 if (lpFailureCommand
)
5984 wcscpy(lpStr
, lpFailureCommand
);
5985 lpFailureActions
->lpCommand
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpFailureActions
);
5986 /* lpStr += wcslen(lpStr) + 1; */
5989 dwError
= ERROR_SUCCESS
;
5993 /* Unlock the service database */
5994 ScmUnlockDatabase();
5996 if (lpDescription
!= NULL
)
5997 HeapFree(GetProcessHeap(), 0, lpDescription
);
5999 if (lpRebootMessage
!= NULL
)
6000 HeapFree(GetProcessHeap(), 0, lpRebootMessage
);
6002 if (lpFailureCommand
!= NULL
)
6003 HeapFree(GetProcessHeap(), 0, lpFailureCommand
);
6005 if (hServiceKey
!= NULL
)
6006 RegCloseKey(hServiceKey
);
6008 DPRINT("RQueryServiceConfig2W() done (Error %lu)\n", dwError
);
6017 RQueryServiceStatusEx(
6018 SC_RPC_HANDLE hService
,
6019 SC_STATUS_TYPE InfoLevel
,
6022 LPBOUNDED_DWORD_8K pcbBytesNeeded
)
6024 LPSERVICE_STATUS_PROCESS lpStatus
;
6025 PSERVICE_HANDLE hSvc
;
6028 DPRINT("RQueryServiceStatusEx() called\n");
6031 return ERROR_SHUTDOWN_IN_PROGRESS
;
6033 if (InfoLevel
!= SC_STATUS_PROCESS_INFO
)
6034 return ERROR_INVALID_LEVEL
;
6036 *pcbBytesNeeded
= sizeof(SERVICE_STATUS_PROCESS
);
6038 if (cbBufSize
< sizeof(SERVICE_STATUS_PROCESS
))
6039 return ERROR_INSUFFICIENT_BUFFER
;
6041 hSvc
= ScmGetServiceFromHandle(hService
);
6044 DPRINT1("Invalid service handle!\n");
6045 return ERROR_INVALID_HANDLE
;
6048 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
6049 SERVICE_QUERY_STATUS
))
6051 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
6052 return ERROR_ACCESS_DENIED
;
6055 lpService
= hSvc
->ServiceEntry
;
6056 if (lpService
== NULL
)
6058 DPRINT("lpService == NULL!\n");
6059 return ERROR_INVALID_HANDLE
;
6062 /* Lock the service database shared */
6063 ScmLockDatabaseShared();
6065 lpStatus
= (LPSERVICE_STATUS_PROCESS
)lpBuffer
;
6067 /* Return service status information */
6068 RtlCopyMemory(lpStatus
,
6070 sizeof(SERVICE_STATUS
));
6072 /* Copy the service process ID */
6073 if ((lpService
->Status
.dwCurrentState
== SERVICE_STOPPED
) || (lpService
->lpImage
== NULL
))
6074 lpStatus
->dwProcessId
= 0;
6076 lpStatus
->dwProcessId
= lpService
->lpImage
->dwProcessId
;
6078 lpStatus
->dwServiceFlags
= 0; /* FIXME */
6080 /* Unlock the service database */
6081 ScmUnlockDatabase();
6083 return ERROR_SUCCESS
;
6090 REnumServicesStatusExA(
6091 SC_RPC_HANDLE hSCManager
,
6092 SC_ENUM_TYPE InfoLevel
,
6093 DWORD dwServiceType
,
6094 DWORD dwServiceState
,
6097 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
6098 LPBOUNDED_DWORD_256K lpServicesReturned
,
6099 LPBOUNDED_DWORD_256K lpResumeIndex
,
6100 LPCSTR pszGroupName
)
6102 LPENUM_SERVICE_STATUS_PROCESSW lpStatusPtrW
= NULL
;
6103 LPENUM_SERVICE_STATUS_PROCESSW lpStatusPtrIncrW
;
6104 LPENUM_SERVICE_STATUS_PROCESSA lpStatusPtrA
= NULL
;
6105 LPWSTR lpStringPtrW
;
6107 LPWSTR pszGroupNameW
= NULL
;
6109 DWORD dwServiceCount
;
6111 DPRINT("REnumServicesStatusExA() called\n");
6113 if (pcbBytesNeeded
== NULL
|| lpServicesReturned
== NULL
)
6115 return ERROR_INVALID_ADDRESS
;
6120 pszGroupNameW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (strlen(pszGroupName
) + 1) * sizeof(WCHAR
));
6123 DPRINT("Failed to allocate buffer!\n");
6124 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
6128 MultiByteToWideChar(CP_ACP
,
6133 (int)(strlen(pszGroupName
) + 1));
6136 if ((cbBufSize
> 0) && (lpBuffer
))
6138 lpStatusPtrW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, cbBufSize
);
6141 DPRINT("Failed to allocate buffer!\n");
6142 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
6147 dwError
= REnumServicesStatusExW(hSCManager
,
6151 (LPBYTE
)lpStatusPtrW
,
6158 /* if no services were returned then we are Done */
6159 if (*lpServicesReturned
== 0)
6162 lpStatusPtrIncrW
= lpStatusPtrW
;
6163 lpStatusPtrA
= (LPENUM_SERVICE_STATUS_PROCESSA
)lpBuffer
;
6164 lpStringPtrA
= (LPSTR
)((ULONG_PTR
)lpBuffer
+
6165 *lpServicesReturned
* sizeof(ENUM_SERVICE_STATUS_PROCESSA
));
6166 lpStringPtrW
= (LPWSTR
)((ULONG_PTR
)lpStatusPtrW
+
6167 *lpServicesReturned
* sizeof(ENUM_SERVICE_STATUS_PROCESSW
));
6169 for (dwServiceCount
= 0; dwServiceCount
< *lpServicesReturned
; dwServiceCount
++)
6171 /* Copy the service name */
6172 WideCharToMultiByte(CP_ACP
,
6177 (int)wcslen(lpStringPtrW
),
6181 lpStatusPtrA
->lpServiceName
= (LPSTR
)((ULONG_PTR
)lpStringPtrA
- (ULONG_PTR
)lpBuffer
);
6182 lpStringPtrA
+= wcslen(lpStringPtrW
) + 1;
6183 lpStringPtrW
+= wcslen(lpStringPtrW
) + 1;
6185 /* Copy the display name */
6186 WideCharToMultiByte(CP_ACP
,
6191 (int)wcslen(lpStringPtrW
),
6195 lpStatusPtrA
->lpDisplayName
= (LPSTR
)((ULONG_PTR
)lpStringPtrA
- (ULONG_PTR
)lpBuffer
);
6196 lpStringPtrA
+= wcslen(lpStringPtrW
) + 1;
6197 lpStringPtrW
+= wcslen(lpStringPtrW
) + 1;
6199 /* Copy the status information */
6200 memcpy(&lpStatusPtrA
->ServiceStatusProcess
,
6201 &lpStatusPtrIncrW
->ServiceStatusProcess
,
6202 sizeof(SERVICE_STATUS
));
6204 /* Copy the service process ID */
6205 lpStatusPtrA
->ServiceStatusProcess
.dwProcessId
= lpStatusPtrIncrW
->ServiceStatusProcess
.dwProcessId
;
6207 lpStatusPtrA
->ServiceStatusProcess
.dwServiceFlags
= 0; /* FIXME */
6215 HeapFree(GetProcessHeap(), 0, pszGroupNameW
);
6218 HeapFree(GetProcessHeap(), 0, lpStatusPtrW
);
6220 DPRINT("REnumServicesStatusExA() done (Error %lu)\n", dwError
);
6229 REnumServicesStatusExW(
6230 SC_RPC_HANDLE hSCManager
,
6231 SC_ENUM_TYPE InfoLevel
,
6232 DWORD dwServiceType
,
6233 DWORD dwServiceState
,
6236 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
6237 LPBOUNDED_DWORD_256K lpServicesReturned
,
6238 LPBOUNDED_DWORD_256K lpResumeIndex
,
6239 LPCWSTR pszGroupName
)
6241 PMANAGER_HANDLE hManager
;
6243 DWORD dwError
= ERROR_SUCCESS
;
6244 PLIST_ENTRY ServiceEntry
;
6245 PSERVICE CurrentService
;
6247 DWORD dwRequiredSize
;
6248 DWORD dwServiceCount
;
6250 DWORD dwLastResumeCount
= 0;
6251 LPENUM_SERVICE_STATUS_PROCESSW lpStatusPtr
;
6254 DPRINT("REnumServicesStatusExW() called\n");
6257 return ERROR_SHUTDOWN_IN_PROGRESS
;
6259 if (InfoLevel
!= SC_ENUM_PROCESS_INFO
)
6260 return ERROR_INVALID_LEVEL
;
6262 hManager
= ScmGetServiceManagerFromHandle(hSCManager
);
6263 if (hManager
== NULL
)
6265 DPRINT1("Invalid service manager handle!\n");
6266 return ERROR_INVALID_HANDLE
;
6269 if (pcbBytesNeeded
== NULL
|| lpServicesReturned
== NULL
)
6271 return ERROR_INVALID_ADDRESS
;
6274 *pcbBytesNeeded
= 0;
6275 *lpServicesReturned
= 0;
6277 if ((dwServiceType
== 0) ||
6278 ((dwServiceType
& ~SERVICE_TYPE_ALL
) != 0))
6280 DPRINT("Not a valid Service Type!\n");
6281 return ERROR_INVALID_PARAMETER
;
6284 if ((dwServiceState
== 0) ||
6285 ((dwServiceState
& ~SERVICE_STATE_ALL
) != 0))
6287 DPRINT("Not a valid Service State!\n");
6288 return ERROR_INVALID_PARAMETER
;
6291 /* Check access rights */
6292 if (!RtlAreAllAccessesGranted(hManager
->Handle
.DesiredAccess
,
6293 SC_MANAGER_ENUMERATE_SERVICE
))
6295 DPRINT("Insufficient access rights! 0x%lx\n",
6296 hManager
->Handle
.DesiredAccess
);
6297 return ERROR_ACCESS_DENIED
;
6301 dwLastResumeCount
= *lpResumeIndex
;
6303 /* Lock the service database shared */
6304 ScmLockDatabaseShared();
6306 lpService
= ScmGetServiceEntryByResumeCount(dwLastResumeCount
);
6307 if (lpService
== NULL
)
6309 dwError
= ERROR_SUCCESS
;
6316 for (ServiceEntry
= &lpService
->ServiceListEntry
;
6317 ServiceEntry
!= &ServiceListHead
;
6318 ServiceEntry
= ServiceEntry
->Flink
)
6320 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
6324 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
6327 dwState
= SERVICE_ACTIVE
;
6328 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
6329 dwState
= SERVICE_INACTIVE
;
6331 if ((dwState
& dwServiceState
) == 0)
6336 if (*pszGroupName
== 0)
6338 if (CurrentService
->lpGroup
!= NULL
)
6343 if ((CurrentService
->lpGroup
== NULL
) ||
6344 _wcsicmp(pszGroupName
, CurrentService
->lpGroup
->lpGroupName
) != 0)
6349 dwSize
= sizeof(ENUM_SERVICE_STATUS_PROCESSW
) +
6350 (DWORD
)((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
6351 (DWORD
)((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
6353 if (dwRequiredSize
+ dwSize
<= cbBufSize
)
6355 DPRINT("Service name: %S fit\n", CurrentService
->lpServiceName
);
6356 dwRequiredSize
+= dwSize
;
6358 dwLastResumeCount
= CurrentService
->dwResumeCount
;
6362 DPRINT("Service name: %S no fit\n", CurrentService
->lpServiceName
);
6368 DPRINT("dwRequiredSize: %lu\n", dwRequiredSize
);
6369 DPRINT("dwServiceCount: %lu\n", dwServiceCount
);
6372 ServiceEntry
!= &ServiceListHead
;
6373 ServiceEntry
= ServiceEntry
->Flink
)
6375 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
6379 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
6382 dwState
= SERVICE_ACTIVE
;
6383 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
6384 dwState
= SERVICE_INACTIVE
;
6386 if ((dwState
& dwServiceState
) == 0)
6391 if (*pszGroupName
== 0)
6393 if (CurrentService
->lpGroup
!= NULL
)
6398 if ((CurrentService
->lpGroup
== NULL
) ||
6399 _wcsicmp(pszGroupName
, CurrentService
->lpGroup
->lpGroupName
) != 0)
6404 dwRequiredSize
+= (sizeof(ENUM_SERVICE_STATUS_PROCESSW
) +
6405 (DWORD
)((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
6406 (DWORD
)((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
)));
6408 dwError
= ERROR_MORE_DATA
;
6411 DPRINT("*pcbBytesNeeded: %lu\n", dwRequiredSize
);
6414 *lpResumeIndex
= dwLastResumeCount
;
6416 *lpServicesReturned
= dwServiceCount
;
6417 *pcbBytesNeeded
= dwRequiredSize
;
6419 /* If there was no services that matched */
6420 if ((!dwServiceCount
) && (dwError
!= ERROR_MORE_DATA
))
6422 dwError
= ERROR_SERVICE_DOES_NOT_EXIST
;
6426 lpStatusPtr
= (LPENUM_SERVICE_STATUS_PROCESSW
)lpBuffer
;
6427 lpStringPtr
= (LPWSTR
)((ULONG_PTR
)lpBuffer
+
6428 dwServiceCount
* sizeof(ENUM_SERVICE_STATUS_PROCESSW
));
6431 for (ServiceEntry
= &lpService
->ServiceListEntry
;
6432 ServiceEntry
!= &ServiceListHead
;
6433 ServiceEntry
= ServiceEntry
->Flink
)
6435 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
6439 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
6442 dwState
= SERVICE_ACTIVE
;
6443 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
6444 dwState
= SERVICE_INACTIVE
;
6446 if ((dwState
& dwServiceState
) == 0)
6451 if (*pszGroupName
== 0)
6453 if (CurrentService
->lpGroup
!= NULL
)
6458 if ((CurrentService
->lpGroup
== NULL
) ||
6459 _wcsicmp(pszGroupName
, CurrentService
->lpGroup
->lpGroupName
) != 0)
6464 dwSize
= sizeof(ENUM_SERVICE_STATUS_PROCESSW
) +
6465 (DWORD
)((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
6466 (DWORD
)((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
6468 if (dwRequiredSize
+ dwSize
<= cbBufSize
)
6470 /* Copy the service name */
6472 CurrentService
->lpServiceName
);
6473 lpStatusPtr
->lpServiceName
= (LPWSTR
)((ULONG_PTR
)lpStringPtr
- (ULONG_PTR
)lpBuffer
);
6474 lpStringPtr
+= (wcslen(CurrentService
->lpServiceName
) + 1);
6476 /* Copy the display name */
6478 CurrentService
->lpDisplayName
);
6479 lpStatusPtr
->lpDisplayName
= (LPWSTR
)((ULONG_PTR
)lpStringPtr
- (ULONG_PTR
)lpBuffer
);
6480 lpStringPtr
+= (wcslen(CurrentService
->lpDisplayName
) + 1);
6482 /* Copy the status information */
6483 memcpy(&lpStatusPtr
->ServiceStatusProcess
,
6484 &CurrentService
->Status
,
6485 sizeof(SERVICE_STATUS
));
6487 /* Copy the service process ID */
6488 if ((CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
) || (CurrentService
->lpImage
== NULL
))
6489 lpStatusPtr
->ServiceStatusProcess
.dwProcessId
= 0;
6491 lpStatusPtr
->ServiceStatusProcess
.dwProcessId
= CurrentService
->lpImage
->dwProcessId
;
6493 lpStatusPtr
->ServiceStatusProcess
.dwServiceFlags
= 0; /* FIXME */
6496 dwRequiredSize
+= dwSize
;
6506 *pcbBytesNeeded
= 0;
6512 /* Unlock the service database */
6513 ScmUnlockDatabase();
6515 DPRINT("REnumServicesStatusExW() done (Error %lu)\n", dwError
);
6525 handle_t BindingHandle
) /* FIXME */
6528 return ERROR_CALL_NOT_IMPLEMENTED
;
6535 RCreateServiceWOW64A(
6536 handle_t BindingHandle
,
6537 LPSTR lpServiceName
,
6538 LPSTR lpDisplayName
,
6539 DWORD dwDesiredAccess
,
6540 DWORD dwServiceType
,
6542 DWORD dwErrorControl
,
6543 LPSTR lpBinaryPathName
,
6544 LPSTR lpLoadOrderGroup
,
6546 LPBYTE lpDependencies
,
6548 LPSTR lpServiceStartName
,
6551 LPSC_RPC_HANDLE lpServiceHandle
)
6554 return ERROR_CALL_NOT_IMPLEMENTED
;
6561 RCreateServiceWOW64W(
6562 handle_t BindingHandle
,
6563 LPWSTR lpServiceName
,
6564 LPWSTR lpDisplayName
,
6565 DWORD dwDesiredAccess
,
6566 DWORD dwServiceType
,
6568 DWORD dwErrorControl
,
6569 LPWSTR lpBinaryPathName
,
6570 LPWSTR lpLoadOrderGroup
,
6572 LPBYTE lpDependencies
,
6574 LPWSTR lpServiceStartName
,
6577 LPSC_RPC_HANDLE lpServiceHandle
)
6580 return ERROR_CALL_NOT_IMPLEMENTED
;
6587 RI_ScQueryServiceTagInfo(
6588 SC_RPC_HANDLE hSCManager
,
6589 TAG_INFO_LEVEL dwInfoLevel
,
6590 PTAG_INFO_NAME_FROM_TAG_IN_PARAMS
* lpInParams
,
6591 PTAG_INFO_NAME_FROM_TAG_OUT_PARAMS
* lpOutParams
)
6593 PMANAGER_HANDLE hManager
;
6595 /* Validate handle */
6596 hManager
= ScmGetServiceManagerFromHandle(hSCManager
);
6597 if (hManager
== NULL
)
6599 return ERROR_INVALID_HANDLE
;
6602 /* FIXME: should check whether client is local */
6604 /* Check access rights */
6605 if (!RtlAreAllAccessesGranted(hManager
->Handle
.DesiredAccess
,
6606 SC_MANAGER_ENUMERATE_SERVICE
))
6608 return ERROR_ACCESS_DENIED
;
6611 /* Check parameters */
6612 if (lpInParams
== NULL
|| lpOutParams
== NULL
)
6614 return ERROR_INVALID_PARAMETER
;
6617 /* Check info level */
6618 if (dwInfoLevel
!= TagInfoLevelNameFromTag
)
6623 /* Call internal helper */
6624 return ScmGetServiceNameFromTag(*lpInParams
, lpOutParams
);
6631 RNotifyServiceStatusChange(
6632 SC_RPC_HANDLE hService
,
6633 SC_RPC_NOTIFY_PARAMS NotifyParams
,
6634 GUID
*pClientProcessGuid
,
6635 GUID
*pSCMProcessGuid
,
6636 PBOOL pfCreateRemoteQueue
,
6637 LPSC_NOTIFY_RPC_HANDLE phNotify
)
6640 return ERROR_CALL_NOT_IMPLEMENTED
;
6648 SC_NOTIFY_RPC_HANDLE hNotify
,
6649 PSC_RPC_NOTIFY_PARAMS_LIST
*ppNotifyParams
)
6652 return ERROR_CALL_NOT_IMPLEMENTED
;
6660 LPSC_NOTIFY_RPC_HANDLE phNotify
,
6664 return ERROR_CALL_NOT_IMPLEMENTED
;
6672 SC_RPC_HANDLE hService
,
6677 return ERROR_CALL_NOT_IMPLEMENTED
;
6685 SC_RPC_HANDLE hService
,
6690 return ERROR_CALL_NOT_IMPLEMENTED
;
6698 handle_t BindingHandle
) /* FIXME */
6701 return ERROR_CALL_NOT_IMPLEMENTED
;
6708 RValidatePnPService(
6709 handle_t BindingHandle
) /* FIXME */
6712 return ERROR_CALL_NOT_IMPLEMENTED
;
6719 ROpenServiceStatusHandle(
6720 handle_t BindingHandle
) /* FIXME */
6723 return ERROR_CALL_NOT_IMPLEMENTED
;
6731 handle_t BindingHandle
) /* FIXME */
6734 return ERROR_CALL_NOT_IMPLEMENTED
;
6738 void __RPC_FAR
* __RPC_USER
midl_user_allocate(SIZE_T len
)
6740 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
);
6744 void __RPC_USER
midl_user_free(void __RPC_FAR
* ptr
)
6746 HeapFree(GetProcessHeap(), 0, ptr
);
6750 void __RPC_USER
SC_RPC_HANDLE_rundown(SC_RPC_HANDLE hSCObject
)
6752 /* Close the handle */
6753 RCloseServiceHandle(&hSCObject
);
6757 void __RPC_USER
SC_RPC_LOCK_rundown(SC_RPC_LOCK Lock
)
6759 /* Unlock the database */
6760 RUnlockServiceDatabase(&Lock
);
6764 void __RPC_USER
SC_NOTIFY_RPC_HANDLE_rundown(SC_NOTIFY_RPC_HANDLE hNotify
)