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
;
1938 DPRINT("RChangeServiceConfigW() called\n");
1939 DPRINT("dwServiceType = 0x%lx\n", dwServiceType
);
1940 DPRINT("dwStartType = %lu\n", dwStartType
);
1941 DPRINT("dwErrorControl = %lu\n", dwErrorControl
);
1942 DPRINT("lpBinaryPathName = %S\n", lpBinaryPathName
);
1943 DPRINT("lpLoadOrderGroup = %S\n", lpLoadOrderGroup
);
1944 DPRINT("lpDisplayName = %S\n", lpDisplayName
);
1947 return ERROR_SHUTDOWN_IN_PROGRESS
;
1949 hSvc
= ScmGetServiceFromHandle(hService
);
1952 DPRINT1("Invalid service handle!\n");
1953 return ERROR_INVALID_HANDLE
;
1956 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
1957 SERVICE_CHANGE_CONFIG
))
1959 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
1960 return ERROR_ACCESS_DENIED
;
1963 /* Check for invalid service type value */
1964 if ((dwServiceType
!= SERVICE_NO_CHANGE
) &&
1965 (dwServiceType
!= SERVICE_KERNEL_DRIVER
) &&
1966 (dwServiceType
!= SERVICE_FILE_SYSTEM_DRIVER
) &&
1967 ((dwServiceType
& ~SERVICE_INTERACTIVE_PROCESS
) != SERVICE_WIN32_OWN_PROCESS
) &&
1968 ((dwServiceType
& ~SERVICE_INTERACTIVE_PROCESS
) != SERVICE_WIN32_SHARE_PROCESS
))
1970 return ERROR_INVALID_PARAMETER
;
1973 /* Check for invalid start type value */
1974 if ((dwStartType
!= SERVICE_NO_CHANGE
) &&
1975 (dwStartType
!= SERVICE_BOOT_START
) &&
1976 (dwStartType
!= SERVICE_SYSTEM_START
) &&
1977 (dwStartType
!= SERVICE_AUTO_START
) &&
1978 (dwStartType
!= SERVICE_DEMAND_START
) &&
1979 (dwStartType
!= SERVICE_DISABLED
))
1981 return ERROR_INVALID_PARAMETER
;
1984 /* Only drivers can be boot start or system start services */
1985 if ((dwStartType
== SERVICE_BOOT_START
) ||
1986 (dwStartType
== SERVICE_SYSTEM_START
))
1988 if ((dwServiceType
!= SERVICE_KERNEL_DRIVER
) &&
1989 (dwServiceType
!= SERVICE_FILE_SYSTEM_DRIVER
))
1990 return ERROR_INVALID_PARAMETER
;
1993 /* Check for invalid error control value */
1994 if ((dwErrorControl
!= SERVICE_NO_CHANGE
) &&
1995 (dwErrorControl
!= SERVICE_ERROR_IGNORE
) &&
1996 (dwErrorControl
!= SERVICE_ERROR_NORMAL
) &&
1997 (dwErrorControl
!= SERVICE_ERROR_SEVERE
) &&
1998 (dwErrorControl
!= SERVICE_ERROR_CRITICAL
))
2000 return ERROR_INVALID_PARAMETER
;
2003 if (lpdwTagId
&& (!lpLoadOrderGroup
|| !*lpLoadOrderGroup
))
2005 return ERROR_INVALID_PARAMETER
;
2008 lpService
= hSvc
->ServiceEntry
;
2009 if (lpService
== NULL
)
2011 DPRINT("lpService == NULL!\n");
2012 return ERROR_INVALID_HANDLE
;
2015 /* Lock the service database exclusively */
2016 ScmLockDatabaseExclusive();
2018 if (lpService
->bDeleted
)
2020 DPRINT("The service has already been marked for delete!\n");
2021 dwError
= ERROR_SERVICE_MARKED_FOR_DELETE
;
2025 /* Open the service key */
2026 dwError
= ScmOpenServiceKey(lpService
->szServiceName
,
2029 if (dwError
!= ERROR_SUCCESS
)
2032 /* Write service data to the registry */
2034 /* Set the display name */
2035 if (lpDisplayName
!= NULL
&& *lpDisplayName
!= 0)
2037 RegSetValueExW(hServiceKey
,
2041 (LPBYTE
)lpDisplayName
,
2042 (DWORD
)((wcslen(lpDisplayName
) + 1) * sizeof(WCHAR
)));
2044 /* Update the display name */
2045 lpDisplayNameW
= HeapAlloc(GetProcessHeap(),
2047 (wcslen(lpDisplayName
) + 1) * sizeof(WCHAR
));
2048 if (lpDisplayNameW
== NULL
)
2050 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
2054 wcscpy(lpDisplayNameW
, lpDisplayName
);
2055 if (lpService
->lpDisplayName
!= lpService
->lpServiceName
)
2056 HeapFree(GetProcessHeap(), 0, lpService
->lpDisplayName
);
2058 lpService
->lpDisplayName
= lpDisplayNameW
;
2061 if (dwServiceType
!= SERVICE_NO_CHANGE
)
2063 /* Set the service type */
2064 dwError
= RegSetValueExW(hServiceKey
,
2068 (LPBYTE
)&dwServiceType
,
2070 if (dwError
!= ERROR_SUCCESS
)
2073 lpService
->Status
.dwServiceType
= dwServiceType
;
2076 if (dwStartType
!= SERVICE_NO_CHANGE
)
2078 /* Set the start value */
2079 dwError
= RegSetValueExW(hServiceKey
,
2083 (LPBYTE
)&dwStartType
,
2085 if (dwError
!= ERROR_SUCCESS
)
2088 lpService
->dwStartType
= dwStartType
;
2091 if (dwErrorControl
!= SERVICE_NO_CHANGE
)
2093 /* Set the error control value */
2094 dwError
= RegSetValueExW(hServiceKey
,
2098 (LPBYTE
)&dwErrorControl
,
2100 if (dwError
!= ERROR_SUCCESS
)
2103 lpService
->dwErrorControl
= dwErrorControl
;
2106 if (lpBinaryPathName
!= NULL
&& *lpBinaryPathName
!= 0)
2108 /* Set the image path */
2109 lpImagePathW
= lpBinaryPathName
;
2111 if (lpService
->Status
.dwServiceType
& SERVICE_DRIVER
)
2113 dwError
= ScmCanonDriverImagePath(lpService
->dwStartType
,
2117 if (dwError
!= ERROR_SUCCESS
)
2121 dwError
= RegSetValueExW(hServiceKey
,
2125 (LPBYTE
)lpImagePathW
,
2126 (DWORD
)((wcslen(lpImagePathW
) + 1) * sizeof(WCHAR
)));
2128 if (lpImagePathW
!= lpBinaryPathName
)
2129 HeapFree(GetProcessHeap(), 0, lpImagePathW
);
2131 if (dwError
!= ERROR_SUCCESS
)
2135 /* Set the group name */
2136 if (lpLoadOrderGroup
!= NULL
&& *lpLoadOrderGroup
!= 0)
2138 dwError
= RegSetValueExW(hServiceKey
,
2142 (LPBYTE
)lpLoadOrderGroup
,
2143 (DWORD
)((wcslen(lpLoadOrderGroup
) + 1) * sizeof(WCHAR
)));
2144 if (dwError
!= ERROR_SUCCESS
)
2147 dwError
= ScmSetServiceGroup(lpService
,
2149 if (dwError
!= ERROR_SUCCESS
)
2154 if (lpdwTagId
!= NULL
)
2156 dwError
= ScmAssignNewTag(lpService
);
2157 if (dwError
!= ERROR_SUCCESS
)
2160 dwError
= RegSetValueExW(hServiceKey
,
2164 (LPBYTE
)&lpService
->dwTag
,
2166 if (dwError
!= ERROR_SUCCESS
)
2169 *lpdwTagId
= lpService
->dwTag
;
2172 /* Write dependencies */
2173 if (lpDependencies
!= NULL
&& *lpDependencies
!= 0)
2175 dwError
= ScmWriteDependencies(hServiceKey
,
2176 (LPWSTR
)lpDependencies
,
2178 if (dwError
!= ERROR_SUCCESS
)
2182 if (lpPassword
!= NULL
)
2184 if (*(LPWSTR
)lpPassword
!= 0)
2186 /* FIXME: Decrypt the password */
2188 /* Write the password */
2189 dwError
= ScmSetServicePassword(lpService
->szServiceName
,
2190 (LPCWSTR
)lpPassword
);
2191 if (dwError
!= ERROR_SUCCESS
)
2196 /* Delete the password */
2197 dwError
= ScmSetServicePassword(lpService
->szServiceName
,
2199 if (dwError
== ERROR_FILE_NOT_FOUND
)
2200 dwError
= ERROR_SUCCESS
;
2202 if (dwError
!= ERROR_SUCCESS
)
2208 if (hServiceKey
!= NULL
)
2209 RegCloseKey(hServiceKey
);
2211 /* Unlock the service database */
2212 ScmUnlockDatabase();
2214 DPRINT("RChangeServiceConfigW() done (Error %lu)\n", dwError
);
2224 SC_RPC_HANDLE hSCManager
,
2225 LPCWSTR lpServiceName
,
2226 LPCWSTR lpDisplayName
,
2227 DWORD dwDesiredAccess
,
2228 DWORD dwServiceType
,
2230 DWORD dwErrorControl
,
2231 LPCWSTR lpBinaryPathName
,
2232 LPCWSTR lpLoadOrderGroup
,
2234 LPBYTE lpDependencies
,
2236 LPCWSTR lpServiceStartName
,
2239 LPSC_RPC_HANDLE lpServiceHandle
)
2241 PMANAGER_HANDLE hManager
;
2242 DWORD dwError
= ERROR_SUCCESS
;
2243 PSERVICE lpService
= NULL
;
2244 SC_HANDLE hServiceHandle
= NULL
;
2245 LPWSTR lpImagePath
= NULL
;
2246 HKEY hServiceKey
= NULL
;
2247 LPWSTR lpObjectName
;
2249 DPRINT("RCreateServiceW() called\n");
2250 DPRINT("lpServiceName = %S\n", lpServiceName
);
2251 DPRINT("lpDisplayName = %S\n", lpDisplayName
);
2252 DPRINT("dwDesiredAccess = %lx\n", dwDesiredAccess
);
2253 DPRINT("dwServiceType = 0x%lx\n", dwServiceType
);
2254 DPRINT("dwStartType = %lu\n", dwStartType
);
2255 DPRINT("dwErrorControl = %lu\n", dwErrorControl
);
2256 DPRINT("lpBinaryPathName = %S\n", lpBinaryPathName
);
2257 DPRINT("lpLoadOrderGroup = %S\n", lpLoadOrderGroup
);
2258 DPRINT("lpdwTagId = %p\n", lpdwTagId
);
2261 return ERROR_SHUTDOWN_IN_PROGRESS
;
2263 hManager
= ScmGetServiceManagerFromHandle(hSCManager
);
2264 if (hManager
== NULL
)
2266 DPRINT1("Invalid service manager handle!\n");
2267 return ERROR_INVALID_HANDLE
;
2270 /* Check access rights */
2271 if (!RtlAreAllAccessesGranted(hManager
->Handle
.DesiredAccess
,
2272 SC_MANAGER_CREATE_SERVICE
))
2274 DPRINT("Insufficient access rights! 0x%lx\n",
2275 hManager
->Handle
.DesiredAccess
);
2276 return ERROR_ACCESS_DENIED
;
2279 if (*lpServiceName
== 0)
2280 return ERROR_INVALID_NAME
;
2282 if (*lpBinaryPathName
== 0)
2283 return ERROR_INVALID_PARAMETER
;
2285 /* Check for invalid service type value */
2286 if ((dwServiceType
!= SERVICE_KERNEL_DRIVER
) &&
2287 (dwServiceType
!= SERVICE_FILE_SYSTEM_DRIVER
) &&
2288 ((dwServiceType
& ~SERVICE_INTERACTIVE_PROCESS
) != SERVICE_WIN32_OWN_PROCESS
) &&
2289 ((dwServiceType
& ~SERVICE_INTERACTIVE_PROCESS
) != SERVICE_WIN32_SHARE_PROCESS
))
2291 return ERROR_INVALID_PARAMETER
;
2294 /* Check for invalid start type value */
2295 if ((dwStartType
!= SERVICE_BOOT_START
) &&
2296 (dwStartType
!= SERVICE_SYSTEM_START
) &&
2297 (dwStartType
!= SERVICE_AUTO_START
) &&
2298 (dwStartType
!= SERVICE_DEMAND_START
) &&
2299 (dwStartType
!= SERVICE_DISABLED
))
2301 return ERROR_INVALID_PARAMETER
;
2304 /* Only drivers can be boot start or system start services */
2305 if ((dwStartType
== SERVICE_BOOT_START
) ||
2306 (dwStartType
== SERVICE_SYSTEM_START
))
2308 if ((dwServiceType
!= SERVICE_KERNEL_DRIVER
) &&
2309 (dwServiceType
!= SERVICE_FILE_SYSTEM_DRIVER
))
2311 return ERROR_INVALID_PARAMETER
;
2315 /* Check for invalid error control value */
2316 if ((dwErrorControl
!= SERVICE_ERROR_IGNORE
) &&
2317 (dwErrorControl
!= SERVICE_ERROR_NORMAL
) &&
2318 (dwErrorControl
!= SERVICE_ERROR_SEVERE
) &&
2319 (dwErrorControl
!= SERVICE_ERROR_CRITICAL
))
2321 return ERROR_INVALID_PARAMETER
;
2324 if ((dwServiceType
== (SERVICE_WIN32_OWN_PROCESS
| SERVICE_INTERACTIVE_PROCESS
)) &&
2325 (lpServiceStartName
))
2327 /* We allow LocalSystem to run interactive. */
2328 if (wcsicmp(lpServiceStartName
, L
"LocalSystem"))
2330 return ERROR_INVALID_PARAMETER
;
2334 if (lpdwTagId
&& (!lpLoadOrderGroup
|| !*lpLoadOrderGroup
))
2336 return ERROR_INVALID_PARAMETER
;
2339 /* Lock the service database exclusively */
2340 ScmLockDatabaseExclusive();
2342 lpService
= ScmGetServiceEntryByName(lpServiceName
);
2345 /* Unlock the service database */
2346 ScmUnlockDatabase();
2348 /* Check if it is marked for deletion */
2349 if (lpService
->bDeleted
)
2350 return ERROR_SERVICE_MARKED_FOR_DELETE
;
2352 /* Return service-exists error */
2353 return ERROR_SERVICE_EXISTS
;
2356 if (lpDisplayName
!= NULL
&&
2357 ScmGetServiceEntryByDisplayName(lpDisplayName
) != NULL
)
2359 /* Unlock the service database */
2360 ScmUnlockDatabase();
2362 return ERROR_DUPLICATE_SERVICE_NAME
;
2365 if (dwServiceType
& SERVICE_DRIVER
)
2367 dwError
= ScmCanonDriverImagePath(dwStartType
,
2370 if (dwError
!= ERROR_SUCCESS
)
2375 if (dwStartType
== SERVICE_BOOT_START
||
2376 dwStartType
== SERVICE_SYSTEM_START
)
2378 /* Unlock the service database */
2379 ScmUnlockDatabase();
2381 return ERROR_INVALID_PARAMETER
;
2385 /* Allocate a new service entry */
2386 dwError
= ScmCreateNewServiceRecord(lpServiceName
,
2390 if (dwError
!= ERROR_SUCCESS
)
2393 /* Fill the new service entry */
2394 lpService
->dwErrorControl
= dwErrorControl
;
2396 /* Fill the display name */
2397 if (lpDisplayName
!= NULL
&&
2398 *lpDisplayName
!= 0 &&
2399 _wcsicmp(lpService
->lpDisplayName
, lpDisplayName
) != 0)
2401 lpService
->lpDisplayName
= HeapAlloc(GetProcessHeap(),
2403 (wcslen(lpDisplayName
) + 1) * sizeof(WCHAR
));
2404 if (lpService
->lpDisplayName
== NULL
)
2406 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
2409 wcscpy(lpService
->lpDisplayName
, lpDisplayName
);
2412 /* Assign the service to a group */
2413 if (lpLoadOrderGroup
!= NULL
&& *lpLoadOrderGroup
!= 0)
2415 dwError
= ScmSetServiceGroup(lpService
,
2417 if (dwError
!= ERROR_SUCCESS
)
2421 /* Assign a new tag */
2422 if (lpdwTagId
!= NULL
)
2424 dwError
= ScmAssignNewTag(lpService
);
2425 if (dwError
!= ERROR_SUCCESS
)
2429 /* Assign the default security descriptor */
2430 if (dwServiceType
& SERVICE_WIN32
)
2432 dwError
= ScmCreateDefaultServiceSD(&lpService
->pSecurityDescriptor
);
2433 if (dwError
!= ERROR_SUCCESS
)
2437 /* Write service data to the registry */
2438 /* Create the service key */
2439 dwError
= ScmCreateServiceKey(lpServiceName
,
2442 if (dwError
!= ERROR_SUCCESS
)
2445 /* Set the display name */
2446 if (lpDisplayName
!= NULL
&& *lpDisplayName
!= 0)
2448 RegSetValueExW(hServiceKey
,
2452 (LPBYTE
)lpDisplayName
,
2453 (DWORD
)((wcslen(lpDisplayName
) + 1) * sizeof(WCHAR
)));
2456 /* Set the service type */
2457 dwError
= RegSetValueExW(hServiceKey
,
2461 (LPBYTE
)&dwServiceType
,
2463 if (dwError
!= ERROR_SUCCESS
)
2466 /* Set the start value */
2467 dwError
= RegSetValueExW(hServiceKey
,
2471 (LPBYTE
)&dwStartType
,
2473 if (dwError
!= ERROR_SUCCESS
)
2476 /* Set the error control value */
2477 dwError
= RegSetValueExW(hServiceKey
,
2481 (LPBYTE
)&dwErrorControl
,
2483 if (dwError
!= ERROR_SUCCESS
)
2486 /* Set the image path */
2487 if (dwServiceType
& SERVICE_WIN32
)
2489 dwError
= RegSetValueExW(hServiceKey
,
2493 (LPBYTE
)lpBinaryPathName
,
2494 (DWORD
)((wcslen(lpBinaryPathName
) + 1) * sizeof(WCHAR
)));
2495 if (dwError
!= ERROR_SUCCESS
)
2498 else if (dwServiceType
& SERVICE_DRIVER
)
2500 dwError
= RegSetValueExW(hServiceKey
,
2504 (LPBYTE
)lpImagePath
,
2505 (DWORD
)((wcslen(lpImagePath
) + 1) * sizeof(WCHAR
)));
2506 if (dwError
!= ERROR_SUCCESS
)
2510 /* Set the group name */
2511 if (lpLoadOrderGroup
!= NULL
&& *lpLoadOrderGroup
!= 0)
2513 dwError
= RegSetValueExW(hServiceKey
,
2517 (LPBYTE
)lpLoadOrderGroup
,
2518 (DWORD
)((wcslen(lpLoadOrderGroup
) + 1) * sizeof(WCHAR
)));
2519 if (dwError
!= ERROR_SUCCESS
)
2523 /* Set the service tag */
2524 if (lpdwTagId
!= NULL
)
2526 dwError
= RegSetValueExW(hServiceKey
,
2530 (LPBYTE
)&lpService
->dwTag
,
2532 if (dwError
!= ERROR_SUCCESS
)
2536 /* Write dependencies */
2537 if (lpDependencies
!= NULL
&& *lpDependencies
!= 0)
2539 dwError
= ScmWriteDependencies(hServiceKey
,
2540 (LPCWSTR
)lpDependencies
,
2542 if (dwError
!= ERROR_SUCCESS
)
2546 /* Start name and password are only used by Win32 services */
2547 if (dwServiceType
& SERVICE_WIN32
)
2549 /* Write service start name */
2550 lpObjectName
= (lpServiceStartName
!= NULL
) ? (LPWSTR
)lpServiceStartName
: L
"LocalSystem";
2551 dwError
= RegSetValueExW(hServiceKey
,
2555 (LPBYTE
)lpObjectName
,
2556 (DWORD
)((wcslen(lpObjectName
) + 1) * sizeof(WCHAR
)));
2557 if (dwError
!= ERROR_SUCCESS
)
2560 if (lpPassword
!= NULL
&& *(LPWSTR
)lpPassword
!= 0)
2562 /* FIXME: Decrypt the password */
2564 /* Write the password */
2565 dwError
= ScmSetServicePassword(lpServiceName
,
2566 (LPCWSTR
)lpPassword
);
2567 if (dwError
!= ERROR_SUCCESS
)
2571 /* Write the security descriptor */
2572 dwError
= ScmWriteSecurityDescriptor(hServiceKey
,
2573 lpService
->pSecurityDescriptor
);
2574 if (dwError
!= ERROR_SUCCESS
)
2578 dwError
= ScmCreateServiceHandle(lpService
,
2580 if (dwError
!= ERROR_SUCCESS
)
2583 dwError
= ScmCheckAccess(hServiceHandle
,
2585 if (dwError
!= ERROR_SUCCESS
)
2588 lpService
->dwRefCount
= 1;
2589 DPRINT("CreateService - lpService->dwRefCount %u\n", lpService
->dwRefCount
);
2592 /* Unlock the service database */
2593 ScmUnlockDatabase();
2595 if (hServiceKey
!= NULL
)
2596 RegCloseKey(hServiceKey
);
2598 if (dwError
== ERROR_SUCCESS
)
2600 DPRINT("hService %p\n", hServiceHandle
);
2601 *lpServiceHandle
= (SC_RPC_HANDLE
)hServiceHandle
;
2603 if (lpdwTagId
!= NULL
)
2604 *lpdwTagId
= lpService
->dwTag
;
2608 if (lpService
!= NULL
&&
2609 lpService
->lpServiceName
!= NULL
)
2611 /* Release the display name buffer */
2612 HeapFree(GetProcessHeap(), 0, lpService
->lpDisplayName
);
2617 /* Remove the service handle */
2618 HeapFree(GetProcessHeap(), 0, hServiceHandle
);
2621 if (lpService
!= NULL
)
2623 /* FIXME: remove the service entry */
2627 if (lpImagePath
!= NULL
)
2628 HeapFree(GetProcessHeap(), 0, lpImagePath
);
2630 DPRINT("RCreateServiceW() done (Error %lu)\n", dwError
);
2639 REnumDependentServicesW(
2640 SC_RPC_HANDLE hService
,
2641 DWORD dwServiceState
,
2644 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
2645 LPBOUNDED_DWORD_256K lpServicesReturned
)
2647 DWORD dwError
= ERROR_SUCCESS
;
2648 DWORD dwServicesReturned
= 0;
2649 DWORD dwServiceCount
;
2650 HKEY hServicesKey
= NULL
;
2651 PSERVICE_HANDLE hSvc
;
2652 PSERVICE lpService
= NULL
;
2653 PSERVICE
*lpServicesArray
= NULL
;
2654 LPENUM_SERVICE_STATUSW lpServicesPtr
= NULL
;
2657 *pcbBytesNeeded
= 0;
2658 *lpServicesReturned
= 0;
2660 DPRINT("REnumDependentServicesW() called\n");
2662 hSvc
= ScmGetServiceFromHandle(hService
);
2665 DPRINT1("Invalid service handle!\n");
2666 return ERROR_INVALID_HANDLE
;
2669 lpService
= hSvc
->ServiceEntry
;
2671 /* Check access rights */
2672 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
2673 SC_MANAGER_ENUMERATE_SERVICE
))
2675 DPRINT("Insufficient access rights! 0x%lx\n",
2676 hSvc
->Handle
.DesiredAccess
);
2677 return ERROR_ACCESS_DENIED
;
2680 /* Open the Services Reg key */
2681 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
2682 L
"System\\CurrentControlSet\\Services",
2686 if (dwError
!= ERROR_SUCCESS
)
2689 /* First determine the bytes needed and get the number of dependent services */
2690 dwError
= Int_EnumDependentServicesW(hServicesKey
,
2695 &dwServicesReturned
);
2696 if (dwError
!= ERROR_SUCCESS
)
2699 /* If buffer size is less than the bytes needed or pointer is null */
2700 if ((!lpServices
) || (cbBufSize
< *pcbBytesNeeded
))
2702 dwError
= ERROR_MORE_DATA
;
2706 /* Allocate memory for array of service pointers */
2707 lpServicesArray
= HeapAlloc(GetProcessHeap(),
2709 (dwServicesReturned
+ 1) * sizeof(PSERVICE
));
2710 if (!lpServicesArray
)
2712 DPRINT1("Could not allocate a buffer!!\n");
2713 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
2717 dwServicesReturned
= 0;
2718 *pcbBytesNeeded
= 0;
2720 dwError
= Int_EnumDependentServicesW(hServicesKey
,
2725 &dwServicesReturned
);
2726 if (dwError
!= ERROR_SUCCESS
)
2731 lpServicesPtr
= (LPENUM_SERVICE_STATUSW
)lpServices
;
2732 lpStr
= (LPWSTR
)(lpServices
+ (dwServicesReturned
* sizeof(ENUM_SERVICE_STATUSW
)));
2734 /* Copy EnumDepenedentService to Buffer */
2735 for (dwServiceCount
= 0; dwServiceCount
< dwServicesReturned
; dwServiceCount
++)
2737 lpService
= lpServicesArray
[dwServiceCount
];
2739 /* Copy status info */
2740 memcpy(&lpServicesPtr
->ServiceStatus
,
2742 sizeof(SERVICE_STATUS
));
2744 /* Copy display name */
2745 wcscpy(lpStr
, lpService
->lpDisplayName
);
2746 lpServicesPtr
->lpDisplayName
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServices
);
2747 lpStr
+= (wcslen(lpService
->lpDisplayName
) + 1);
2749 /* Copy service name */
2750 wcscpy(lpStr
, lpService
->lpServiceName
);
2751 lpServicesPtr
->lpServiceName
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServices
);
2752 lpStr
+= (wcslen(lpService
->lpServiceName
) + 1);
2757 *lpServicesReturned
= dwServicesReturned
;
2760 if (lpServicesArray
!= NULL
)
2761 HeapFree(GetProcessHeap(), 0, lpServicesArray
);
2763 RegCloseKey(hServicesKey
);
2765 DPRINT("REnumDependentServicesW() done (Error %lu)\n", dwError
);
2774 REnumServicesStatusW(
2775 SC_RPC_HANDLE hSCManager
,
2776 DWORD dwServiceType
,
2777 DWORD dwServiceState
,
2780 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
2781 LPBOUNDED_DWORD_256K lpServicesReturned
,
2782 LPBOUNDED_DWORD_256K lpResumeHandle
)
2784 /* Enumerate all the services, not regarding of their group */
2785 return REnumServiceGroupW(hSCManager
,
2801 LPWSTR lpMachineName
,
2802 LPWSTR lpDatabaseName
,
2803 DWORD dwDesiredAccess
,
2804 LPSC_RPC_HANDLE lpScHandle
)
2809 DPRINT("ROpenSCManagerW() called\n");
2810 DPRINT("lpMachineName = %p\n", lpMachineName
);
2811 DPRINT("lpMachineName: %S\n", lpMachineName
);
2812 DPRINT("lpDataBaseName = %p\n", lpDatabaseName
);
2813 DPRINT("lpDataBaseName: %S\n", lpDatabaseName
);
2814 DPRINT("dwDesiredAccess = %x\n", dwDesiredAccess
);
2817 return ERROR_SHUTDOWN_IN_PROGRESS
;
2820 return ERROR_INVALID_PARAMETER
;
2822 dwError
= ScmCreateManagerHandle(lpDatabaseName
,
2824 if (dwError
!= ERROR_SUCCESS
)
2826 DPRINT("ScmCreateManagerHandle() failed (Error %lu)\n", dwError
);
2830 /* Check the desired access */
2831 dwError
= ScmCheckAccess(hHandle
,
2832 dwDesiredAccess
| SC_MANAGER_CONNECT
);
2833 if (dwError
!= ERROR_SUCCESS
)
2835 DPRINT("ScmCheckAccess() failed (Error %lu)\n", dwError
);
2836 HeapFree(GetProcessHeap(), 0, hHandle
);
2840 *lpScHandle
= (SC_RPC_HANDLE
)hHandle
;
2841 DPRINT("*hScm = %p\n", *lpScHandle
);
2843 DPRINT("ROpenSCManagerW() done\n");
2845 return ERROR_SUCCESS
;
2853 SC_RPC_HANDLE hSCManager
,
2854 LPWSTR lpServiceName
,
2855 DWORD dwDesiredAccess
,
2856 LPSC_RPC_HANDLE lpServiceHandle
)
2859 PMANAGER_HANDLE hManager
;
2861 DWORD dwError
= ERROR_SUCCESS
;
2863 DPRINT("ROpenServiceW() called\n");
2864 DPRINT("hSCManager = %p\n", hSCManager
);
2865 DPRINT("lpServiceName = %p\n", lpServiceName
);
2866 DPRINT("lpServiceName: %S\n", lpServiceName
);
2867 DPRINT("dwDesiredAccess = %x\n", dwDesiredAccess
);
2870 return ERROR_SHUTDOWN_IN_PROGRESS
;
2872 hManager
= ScmGetServiceManagerFromHandle(hSCManager
);
2873 if (hManager
== NULL
)
2875 DPRINT1("Invalid service manager handle!\n");
2876 return ERROR_INVALID_HANDLE
;
2879 if (!lpServiceHandle
)
2880 return ERROR_INVALID_PARAMETER
;
2883 return ERROR_INVALID_ADDRESS
;
2885 /* Lock the service database exclusive */
2886 ScmLockDatabaseExclusive();
2888 /* Get service database entry */
2889 lpService
= ScmGetServiceEntryByName(lpServiceName
);
2890 if (lpService
== NULL
)
2892 DPRINT("Could not find a service!\n");
2893 dwError
= ERROR_SERVICE_DOES_NOT_EXIST
;
2897 /* Create a service handle */
2898 dwError
= ScmCreateServiceHandle(lpService
,
2900 if (dwError
!= ERROR_SUCCESS
)
2902 DPRINT("ScmCreateServiceHandle() failed (Error %lu)\n", dwError
);
2906 /* Check the desired access */
2907 dwError
= ScmCheckAccess(hHandle
,
2909 if (dwError
!= ERROR_SUCCESS
)
2911 DPRINT("ScmCheckAccess() failed (Error %lu)\n", dwError
);
2912 HeapFree(GetProcessHeap(), 0, hHandle
);
2916 lpService
->dwRefCount
++;
2917 DPRINT("OpenService - lpService->dwRefCount %u\n",lpService
->dwRefCount
);
2919 *lpServiceHandle
= (SC_RPC_HANDLE
)hHandle
;
2920 DPRINT("*hService = %p\n", *lpServiceHandle
);
2923 /* Unlock the service database */
2924 ScmUnlockDatabase();
2926 DPRINT("ROpenServiceW() done\n");
2935 RQueryServiceConfigW(
2936 SC_RPC_HANDLE hService
,
2937 LPBYTE lpBuf
, //LPQUERY_SERVICE_CONFIGW lpServiceConfig,
2939 LPBOUNDED_DWORD_8K pcbBytesNeeded
)
2941 LPQUERY_SERVICE_CONFIGW lpServiceConfig
= (LPQUERY_SERVICE_CONFIGW
)lpBuf
;
2942 DWORD dwError
= ERROR_SUCCESS
;
2943 PSERVICE_HANDLE hSvc
;
2944 PSERVICE lpService
= NULL
;
2945 HKEY hServiceKey
= NULL
;
2946 LPWSTR lpImagePath
= NULL
;
2947 LPWSTR lpServiceStartName
= NULL
;
2948 LPWSTR lpDependencies
= NULL
;
2949 DWORD dwDependenciesLength
= 0;
2950 DWORD dwRequiredSize
;
2953 DPRINT("RQueryServiceConfigW() called\n");
2956 return ERROR_SHUTDOWN_IN_PROGRESS
;
2958 hSvc
= ScmGetServiceFromHandle(hService
);
2961 DPRINT1("Invalid service handle!\n");
2962 return ERROR_INVALID_HANDLE
;
2965 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
2966 SERVICE_QUERY_CONFIG
))
2968 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
2969 return ERROR_ACCESS_DENIED
;
2972 lpService
= hSvc
->ServiceEntry
;
2973 if (lpService
== NULL
)
2975 DPRINT("lpService == NULL!\n");
2976 return ERROR_INVALID_HANDLE
;
2979 /* Lock the service database shared */
2980 ScmLockDatabaseShared();
2982 dwError
= ScmOpenServiceKey(lpService
->lpServiceName
,
2985 if (dwError
!= ERROR_SUCCESS
)
2988 /* Read the image path */
2989 dwError
= ScmReadString(hServiceKey
,
2992 if (dwError
!= ERROR_SUCCESS
)
2995 /* Read the service start name */
2996 ScmReadString(hServiceKey
,
2998 &lpServiceStartName
);
3000 /* Read the dependencies */
3001 ScmReadDependencies(hServiceKey
,
3003 &dwDependenciesLength
);
3005 dwRequiredSize
= sizeof(QUERY_SERVICE_CONFIGW
);
3007 if (lpImagePath
!= NULL
)
3008 dwRequiredSize
+= (DWORD
)((wcslen(lpImagePath
) + 1) * sizeof(WCHAR
));
3010 dwRequiredSize
+= 2 * sizeof(WCHAR
);
3012 if ((lpService
->lpGroup
!= NULL
) && (lpService
->lpGroup
->lpGroupName
!= NULL
))
3013 dwRequiredSize
+= (DWORD
)((wcslen(lpService
->lpGroup
->lpGroupName
) + 1) * sizeof(WCHAR
));
3015 dwRequiredSize
+= 2 * sizeof(WCHAR
);
3017 if (lpDependencies
!= NULL
)
3018 dwRequiredSize
+= dwDependenciesLength
* sizeof(WCHAR
);
3020 dwRequiredSize
+= 2 * sizeof(WCHAR
);
3022 if (lpServiceStartName
!= NULL
)
3023 dwRequiredSize
+= (DWORD
)((wcslen(lpServiceStartName
) + 1) * sizeof(WCHAR
));
3025 dwRequiredSize
+= 2 * sizeof(WCHAR
);
3027 if (lpService
->lpDisplayName
!= NULL
)
3028 dwRequiredSize
+= (DWORD
)((wcslen(lpService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
3030 dwRequiredSize
+= 2 * sizeof(WCHAR
);
3032 if (lpServiceConfig
== NULL
|| cbBufSize
< dwRequiredSize
)
3034 dwError
= ERROR_INSUFFICIENT_BUFFER
;
3038 lpServiceConfig
->dwServiceType
= lpService
->Status
.dwServiceType
;
3039 lpServiceConfig
->dwStartType
= lpService
->dwStartType
;
3040 lpServiceConfig
->dwErrorControl
= lpService
->dwErrorControl
;
3041 lpServiceConfig
->dwTagId
= lpService
->dwTag
;
3043 lpStr
= (LPWSTR
)(lpServiceConfig
+ 1);
3045 /* Append the image path */
3046 if (lpImagePath
!= NULL
)
3048 wcscpy(lpStr
, lpImagePath
);
3055 lpServiceConfig
->lpBinaryPathName
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
3056 lpStr
+= (wcslen(lpStr
) + 1);
3058 /* Append the group name */
3059 if ((lpService
->lpGroup
!= NULL
) && (lpService
->lpGroup
->lpGroupName
!= NULL
))
3061 wcscpy(lpStr
, lpService
->lpGroup
->lpGroupName
);
3068 lpServiceConfig
->lpLoadOrderGroup
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
3069 lpStr
+= (wcslen(lpStr
) + 1);
3071 /* Append Dependencies */
3072 if (lpDependencies
!= NULL
)
3076 dwDependenciesLength
* sizeof(WCHAR
));
3083 lpServiceConfig
->lpDependencies
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
3084 if (lpDependencies
!= NULL
)
3085 lpStr
+= dwDependenciesLength
;
3087 lpStr
+= (wcslen(lpStr
) + 1);
3089 /* Append the service start name */
3090 if (lpServiceStartName
!= NULL
)
3092 wcscpy(lpStr
, lpServiceStartName
);
3099 lpServiceConfig
->lpServiceStartName
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
3100 lpStr
+= (wcslen(lpStr
) + 1);
3102 /* Append the display name */
3103 if (lpService
->lpDisplayName
!= NULL
)
3105 wcscpy(lpStr
, lpService
->lpDisplayName
);
3112 lpServiceConfig
->lpDisplayName
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
3115 if (pcbBytesNeeded
!= NULL
)
3116 *pcbBytesNeeded
= dwRequiredSize
;
3119 /* Unlock the service database */
3120 ScmUnlockDatabase();
3122 if (lpImagePath
!= NULL
)
3123 HeapFree(GetProcessHeap(), 0, lpImagePath
);
3125 if (lpServiceStartName
!= NULL
)
3126 HeapFree(GetProcessHeap(), 0, lpServiceStartName
);
3128 if (lpDependencies
!= NULL
)
3129 HeapFree(GetProcessHeap(), 0, lpDependencies
);
3131 if (hServiceKey
!= NULL
)
3132 RegCloseKey(hServiceKey
);
3134 DPRINT("RQueryServiceConfigW() done\n");
3143 RQueryServiceLockStatusW(
3144 SC_RPC_HANDLE hSCManager
,
3145 LPBYTE lpBuf
, // LPQUERY_SERVICE_LOCK_STATUSW lpLockStatus,
3147 LPBOUNDED_DWORD_4K pcbBytesNeeded
)
3149 LPQUERY_SERVICE_LOCK_STATUSW lpLockStatus
= (LPQUERY_SERVICE_LOCK_STATUSW
)lpBuf
;
3150 PMANAGER_HANDLE hMgr
;
3151 DWORD dwRequiredSize
;
3153 if (!lpLockStatus
|| !pcbBytesNeeded
)
3154 return ERROR_INVALID_PARAMETER
;
3156 hMgr
= ScmGetServiceManagerFromHandle(hSCManager
);
3159 DPRINT1("Invalid service manager handle!\n");
3160 return ERROR_INVALID_HANDLE
;
3163 if (!RtlAreAllAccessesGranted(hMgr
->Handle
.DesiredAccess
,
3164 SC_MANAGER_QUERY_LOCK_STATUS
))
3166 DPRINT("Insufficient access rights! 0x%lx\n", hMgr
->Handle
.DesiredAccess
);
3167 return ERROR_ACCESS_DENIED
;
3170 /* FIXME: we need to compute instead the real length of the owner name */
3171 dwRequiredSize
= sizeof(QUERY_SERVICE_LOCK_STATUSW
) + sizeof(WCHAR
);
3172 *pcbBytesNeeded
= dwRequiredSize
;
3174 if (cbBufSize
< dwRequiredSize
)
3175 return ERROR_INSUFFICIENT_BUFFER
;
3177 ScmQueryServiceLockStatusW(lpLockStatus
);
3179 return ERROR_SUCCESS
;
3187 SC_RPC_HANDLE hService
,
3189 LPSTRING_PTRSW argv
)
3191 DWORD dwError
= ERROR_SUCCESS
;
3192 PSERVICE_HANDLE hSvc
;
3193 PSERVICE lpService
= NULL
;
3198 DPRINT("RStartServiceW(%p %lu %p) called\n", hService
, argc
, argv
);
3199 DPRINT(" argc: %lu\n", argc
);
3202 for (i
= 0; i
< argc
; i
++)
3204 DPRINT(" argv[%lu]: %S\n", i
, argv
[i
].StringPtr
);
3210 return ERROR_SHUTDOWN_IN_PROGRESS
;
3212 hSvc
= ScmGetServiceFromHandle(hService
);
3215 DPRINT1("Invalid service handle!\n");
3216 return ERROR_INVALID_HANDLE
;
3219 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
3222 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
3223 return ERROR_ACCESS_DENIED
;
3226 lpService
= hSvc
->ServiceEntry
;
3227 if (lpService
== NULL
)
3229 DPRINT("lpService == NULL!\n");
3230 return ERROR_INVALID_HANDLE
;
3233 if (lpService
->dwStartType
== SERVICE_DISABLED
)
3234 return ERROR_SERVICE_DISABLED
;
3236 if (lpService
->bDeleted
)
3237 return ERROR_SERVICE_MARKED_FOR_DELETE
;
3239 /* Start the service */
3240 dwError
= ScmStartService(lpService
, argc
, (LPWSTR
*)argv
);
3249 RGetServiceDisplayNameW(
3250 SC_RPC_HANDLE hSCManager
,
3251 LPCWSTR lpServiceName
,
3252 LPWSTR lpDisplayName
,
3255 // PMANAGER_HANDLE hManager;
3260 DPRINT("RGetServiceDisplayNameW() called\n");
3261 DPRINT("hSCManager = %p\n", hSCManager
);
3262 DPRINT("lpServiceName: %S\n", lpServiceName
);
3263 DPRINT("lpDisplayName: %p\n", lpDisplayName
);
3264 DPRINT("*lpcchBuffer: %lu\n", *lpcchBuffer
);
3266 // hManager = (PMANAGER_HANDLE)hSCManager;
3267 // if (hManager->Handle.Tag != MANAGER_TAG)
3269 // DPRINT("Invalid manager handle!\n");
3270 // return ERROR_INVALID_HANDLE;
3273 /* Get service database entry */
3274 lpService
= ScmGetServiceEntryByName(lpServiceName
);
3275 if (lpService
== NULL
)
3277 DPRINT("Could not find a service!\n");
3279 /* If the service could not be found and lpcchBuffer is less than 2, windows
3280 puts null in lpDisplayName and puts 2 in lpcchBuffer */
3281 if (*lpcchBuffer
< sizeof(WCHAR
))
3283 *lpcchBuffer
= sizeof(WCHAR
);
3284 if (lpDisplayName
!= NULL
)
3290 return ERROR_SERVICE_DOES_NOT_EXIST
;
3293 if (!lpService
->lpDisplayName
)
3295 dwLength
= (DWORD
)wcslen(lpService
->lpServiceName
);
3297 if (lpDisplayName
!= NULL
&&
3298 *lpcchBuffer
> dwLength
)
3300 wcscpy(lpDisplayName
, lpService
->lpServiceName
);
3305 dwLength
= (DWORD
)wcslen(lpService
->lpDisplayName
);
3307 if (lpDisplayName
!= NULL
&&
3308 *lpcchBuffer
> dwLength
)
3310 wcscpy(lpDisplayName
, lpService
->lpDisplayName
);
3314 dwError
= (*lpcchBuffer
> dwLength
) ? ERROR_SUCCESS
: ERROR_INSUFFICIENT_BUFFER
;
3316 *lpcchBuffer
= dwLength
;
3325 RGetServiceKeyNameW(
3326 SC_RPC_HANDLE hSCManager
,
3327 LPCWSTR lpDisplayName
,
3328 LPWSTR lpServiceName
,
3331 // PMANAGER_HANDLE hManager;
3336 DPRINT("RGetServiceKeyNameW() called\n");
3337 DPRINT("hSCManager = %p\n", hSCManager
);
3338 DPRINT("lpDisplayName: %S\n", lpDisplayName
);
3339 DPRINT("lpServiceName: %p\n", lpServiceName
);
3340 DPRINT("*lpcchBuffer: %lu\n", *lpcchBuffer
);
3342 // hManager = (PMANAGER_HANDLE)hSCManager;
3343 // if (hManager->Handle.Tag != MANAGER_TAG)
3345 // DPRINT("Invalid manager handle!\n");
3346 // return ERROR_INVALID_HANDLE;
3349 /* Get service database entry */
3350 lpService
= ScmGetServiceEntryByDisplayName(lpDisplayName
);
3351 if (lpService
== NULL
)
3353 DPRINT("Could not find a service!\n");
3355 /* If the service could not be found and lpcchBuffer is less than 2, windows
3356 puts null in lpDisplayName and puts 2 in lpcchBuffer */
3357 if (*lpcchBuffer
< sizeof(WCHAR
))
3359 *lpcchBuffer
= sizeof(WCHAR
);
3360 if (lpServiceName
!= NULL
)
3366 return ERROR_SERVICE_DOES_NOT_EXIST
;
3369 dwLength
= (DWORD
)wcslen(lpService
->lpServiceName
);
3371 if (lpServiceName
!= NULL
&&
3372 *lpcchBuffer
> dwLength
)
3374 wcscpy(lpServiceName
, lpService
->lpServiceName
);
3375 *lpcchBuffer
= dwLength
;
3376 return ERROR_SUCCESS
;
3379 dwError
= (*lpcchBuffer
> dwLength
) ? ERROR_SUCCESS
: ERROR_INSUFFICIENT_BUFFER
;
3381 *lpcchBuffer
= dwLength
;
3390 RI_ScSetServiceBitsA(
3391 RPC_SERVICE_STATUS_HANDLE hServiceStatus
,
3392 DWORD dwServiceBits
,
3394 int bUpdateImmediately
,
3398 return ERROR_SHUTDOWN_IN_PROGRESS
;
3400 if (lpString
!= NULL
)
3401 return ERROR_INVALID_PARAMETER
;
3403 return RI_ScSetServiceBitsW(hServiceStatus
,
3414 RChangeServiceConfigA(
3415 SC_RPC_HANDLE hService
,
3416 DWORD dwServiceType
,
3418 DWORD dwErrorControl
,
3419 LPSTR lpBinaryPathName
,
3420 LPSTR lpLoadOrderGroup
,
3422 LPBYTE lpDependencies
,
3424 LPSTR lpServiceStartName
,
3427 LPSTR lpDisplayName
)
3429 DWORD dwError
= ERROR_SUCCESS
;
3430 LPWSTR lpBinaryPathNameW
= NULL
;
3431 LPWSTR lpLoadOrderGroupW
= NULL
;
3432 LPWSTR lpDependenciesW
= NULL
;
3433 LPWSTR lpServiceStartNameW
= NULL
;
3434 LPWSTR lpDisplayNameW
= NULL
;
3435 DWORD dwDependenciesLength
= 0;
3440 if (lpBinaryPathName
)
3442 len
= MultiByteToWideChar(CP_ACP
, 0, lpBinaryPathName
, -1, NULL
, 0);
3443 lpBinaryPathNameW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3444 if (!lpBinaryPathNameW
)
3446 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3449 MultiByteToWideChar(CP_ACP
, 0, lpBinaryPathName
, -1, lpBinaryPathNameW
, len
);
3452 if (lpLoadOrderGroup
)
3454 len
= MultiByteToWideChar(CP_ACP
, 0, lpLoadOrderGroup
, -1, NULL
, 0);
3455 lpLoadOrderGroupW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3456 if (!lpLoadOrderGroupW
)
3458 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3461 MultiByteToWideChar(CP_ACP
, 0, lpLoadOrderGroup
, -1, lpLoadOrderGroupW
, len
);
3466 lpStr
= (LPCSTR
)lpDependencies
;
3469 cchLength
= strlen(lpStr
) + 1;
3470 dwDependenciesLength
+= (DWORD
)cchLength
;
3471 lpStr
= lpStr
+ cchLength
;
3473 dwDependenciesLength
++;
3475 lpDependenciesW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, dwDependenciesLength
* sizeof(WCHAR
));
3476 if (!lpDependenciesW
)
3478 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3481 MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)lpDependencies
, dwDependenciesLength
, lpDependenciesW
, dwDependenciesLength
);
3484 if (lpServiceStartName
)
3486 len
= MultiByteToWideChar(CP_ACP
, 0, lpServiceStartName
, -1, NULL
, 0);
3487 lpServiceStartNameW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3488 if (!lpServiceStartNameW
)
3490 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3493 MultiByteToWideChar(CP_ACP
, 0, lpServiceStartName
, -1, lpServiceStartNameW
, len
);
3498 len
= MultiByteToWideChar(CP_ACP
, 0, lpDisplayName
, -1, NULL
, 0);
3499 lpDisplayNameW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3500 if (!lpDisplayNameW
)
3502 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3505 MultiByteToWideChar(CP_ACP
, 0, lpDisplayName
, -1, lpDisplayNameW
, len
);
3508 dwError
= RChangeServiceConfigW(hService
,
3515 (LPBYTE
)lpDependenciesW
,
3516 dwDependenciesLength
,
3517 lpServiceStartNameW
,
3523 if (lpBinaryPathNameW
!= NULL
)
3524 HeapFree(GetProcessHeap(), 0, lpBinaryPathNameW
);
3526 if (lpLoadOrderGroupW
!= NULL
)
3527 HeapFree(GetProcessHeap(), 0, lpLoadOrderGroupW
);
3529 if (lpDependenciesW
!= NULL
)
3530 HeapFree(GetProcessHeap(), 0, lpDependenciesW
);
3532 if (lpServiceStartNameW
!= NULL
)
3533 HeapFree(GetProcessHeap(), 0, lpServiceStartNameW
);
3535 if (lpDisplayNameW
!= NULL
)
3536 HeapFree(GetProcessHeap(), 0, lpDisplayNameW
);
3546 SC_RPC_HANDLE hSCManager
,
3547 LPSTR lpServiceName
,
3548 LPSTR lpDisplayName
,
3549 DWORD dwDesiredAccess
,
3550 DWORD dwServiceType
,
3552 DWORD dwErrorControl
,
3553 LPSTR lpBinaryPathName
,
3554 LPSTR lpLoadOrderGroup
,
3556 LPBYTE lpDependencies
,
3558 LPSTR lpServiceStartName
,
3561 LPSC_RPC_HANDLE lpServiceHandle
)
3563 DWORD dwError
= ERROR_SUCCESS
;
3564 LPWSTR lpServiceNameW
= NULL
;
3565 LPWSTR lpDisplayNameW
= NULL
;
3566 LPWSTR lpBinaryPathNameW
= NULL
;
3567 LPWSTR lpLoadOrderGroupW
= NULL
;
3568 LPWSTR lpDependenciesW
= NULL
;
3569 LPWSTR lpServiceStartNameW
= NULL
;
3570 DWORD dwDependenciesLength
= 0;
3577 len
= MultiByteToWideChar(CP_ACP
, 0, lpServiceName
, -1, NULL
, 0);
3578 lpServiceNameW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3579 if (!lpServiceNameW
)
3581 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3584 MultiByteToWideChar(CP_ACP
, 0, lpServiceName
, -1, lpServiceNameW
, len
);
3589 len
= MultiByteToWideChar(CP_ACP
, 0, lpDisplayName
, -1, NULL
, 0);
3590 lpDisplayNameW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3591 if (!lpDisplayNameW
)
3593 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3596 MultiByteToWideChar(CP_ACP
, 0, lpDisplayName
, -1, lpDisplayNameW
, len
);
3599 if (lpBinaryPathName
)
3601 len
= MultiByteToWideChar(CP_ACP
, 0, lpBinaryPathName
, -1, NULL
, 0);
3602 lpBinaryPathNameW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3603 if (!lpBinaryPathNameW
)
3605 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3608 MultiByteToWideChar(CP_ACP
, 0, lpBinaryPathName
, -1, lpBinaryPathNameW
, len
);
3611 if (lpLoadOrderGroup
)
3613 len
= MultiByteToWideChar(CP_ACP
, 0, lpLoadOrderGroup
, -1, NULL
, 0);
3614 lpLoadOrderGroupW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3615 if (!lpLoadOrderGroupW
)
3617 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3620 MultiByteToWideChar(CP_ACP
, 0, lpLoadOrderGroup
, -1, lpLoadOrderGroupW
, len
);
3625 lpStr
= (LPCSTR
)lpDependencies
;
3628 cchLength
= strlen(lpStr
) + 1;
3629 dwDependenciesLength
+= (DWORD
)cchLength
;
3630 lpStr
= lpStr
+ cchLength
;
3632 dwDependenciesLength
++;
3634 lpDependenciesW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, dwDependenciesLength
* sizeof(WCHAR
));
3635 if (!lpDependenciesW
)
3637 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3640 MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)lpDependencies
, dwDependenciesLength
, lpDependenciesW
, dwDependenciesLength
);
3643 if (lpServiceStartName
)
3645 len
= MultiByteToWideChar(CP_ACP
, 0, lpServiceStartName
, -1, NULL
, 0);
3646 lpServiceStartNameW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3647 if (!lpServiceStartNameW
)
3649 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3652 MultiByteToWideChar(CP_ACP
, 0, lpServiceStartName
, -1, lpServiceStartNameW
, len
);
3655 dwError
= RCreateServiceW(hSCManager
,
3665 (LPBYTE
)lpDependenciesW
,
3666 dwDependenciesLength
,
3667 lpServiceStartNameW
,
3673 if (lpServiceNameW
!=NULL
)
3674 HeapFree(GetProcessHeap(), 0, lpServiceNameW
);
3676 if (lpDisplayNameW
!= NULL
)
3677 HeapFree(GetProcessHeap(), 0, lpDisplayNameW
);
3679 if (lpBinaryPathNameW
!= NULL
)
3680 HeapFree(GetProcessHeap(), 0, lpBinaryPathNameW
);
3682 if (lpLoadOrderGroupW
!= NULL
)
3683 HeapFree(GetProcessHeap(), 0, lpLoadOrderGroupW
);
3685 if (lpDependenciesW
!= NULL
)
3686 HeapFree(GetProcessHeap(), 0, lpDependenciesW
);
3688 if (lpServiceStartNameW
!= NULL
)
3689 HeapFree(GetProcessHeap(), 0, lpServiceStartNameW
);
3698 REnumDependentServicesA(
3699 SC_RPC_HANDLE hService
,
3700 DWORD dwServiceState
,
3703 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
3704 LPBOUNDED_DWORD_256K lpServicesReturned
)
3706 DWORD dwError
= ERROR_SUCCESS
;
3707 DWORD dwServicesReturned
= 0;
3708 DWORD dwServiceCount
;
3709 HKEY hServicesKey
= NULL
;
3710 PSERVICE_HANDLE hSvc
;
3711 PSERVICE lpService
= NULL
;
3712 PSERVICE
*lpServicesArray
= NULL
;
3713 LPENUM_SERVICE_STATUSA lpServicesPtr
= NULL
;
3716 *pcbBytesNeeded
= 0;
3717 *lpServicesReturned
= 0;
3719 DPRINT("REnumDependentServicesA() called\n");
3721 hSvc
= ScmGetServiceFromHandle(hService
);
3724 DPRINT1("Invalid service handle!\n");
3725 return ERROR_INVALID_HANDLE
;
3728 lpService
= hSvc
->ServiceEntry
;
3730 /* Check access rights */
3731 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
3732 SC_MANAGER_ENUMERATE_SERVICE
))
3734 DPRINT("Insufficient access rights! 0x%lx\n",
3735 hSvc
->Handle
.DesiredAccess
);
3736 return ERROR_ACCESS_DENIED
;
3739 /* Open the Services Reg key */
3740 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
3741 L
"System\\CurrentControlSet\\Services",
3746 if (dwError
!= ERROR_SUCCESS
)
3749 /* NOTE: Windows calculates the pcbBytesNeeded based on WCHAR strings for
3750 both EnumDependentServicesA and EnumDependentServicesW. So returned pcbBytesNeeded
3751 are the same for both. Verified in WINXP. */
3753 /* First determine the bytes needed and get the number of dependent services*/
3754 dwError
= Int_EnumDependentServicesW(hServicesKey
,
3759 &dwServicesReturned
);
3760 if (dwError
!= ERROR_SUCCESS
)
3763 /* If buffer size is less than the bytes needed or pointer is null*/
3764 if ((!lpServices
) || (cbBufSize
< *pcbBytesNeeded
))
3766 dwError
= ERROR_MORE_DATA
;
3770 /* Allocate memory for array of service pointers */
3771 lpServicesArray
= HeapAlloc(GetProcessHeap(),
3773 (dwServicesReturned
+ 1) * sizeof(PSERVICE
));
3774 if (!lpServicesArray
)
3776 DPRINT("Could not allocate a buffer!!\n");
3777 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
3781 dwServicesReturned
= 0;
3782 *pcbBytesNeeded
= 0;
3784 dwError
= Int_EnumDependentServicesW(hServicesKey
,
3789 &dwServicesReturned
);
3790 if (dwError
!= ERROR_SUCCESS
)
3795 lpServicesPtr
= (LPENUM_SERVICE_STATUSA
)lpServices
;
3796 lpStr
= (LPSTR
)(lpServices
+ (dwServicesReturned
* sizeof(ENUM_SERVICE_STATUSA
)));
3798 /* Copy EnumDepenedentService to Buffer */
3799 for (dwServiceCount
= 0; dwServiceCount
< dwServicesReturned
; dwServiceCount
++)
3801 lpService
= lpServicesArray
[dwServiceCount
];
3803 /* Copy the status info */
3804 memcpy(&lpServicesPtr
->ServiceStatus
,
3806 sizeof(SERVICE_STATUS
));
3808 /* Copy display name */
3809 WideCharToMultiByte(CP_ACP
,
3811 lpService
->lpDisplayName
,
3814 (int)wcslen(lpService
->lpDisplayName
),
3817 lpServicesPtr
->lpDisplayName
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServices
);
3818 lpStr
+= strlen(lpStr
) + 1;
3820 /* Copy service name */
3821 WideCharToMultiByte(CP_ACP
,
3823 lpService
->lpServiceName
,
3826 (int)wcslen(lpService
->lpServiceName
),
3829 lpServicesPtr
->lpServiceName
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServices
);
3830 lpStr
+= strlen(lpStr
) + 1;
3835 *lpServicesReturned
= dwServicesReturned
;
3838 if (lpServicesArray
)
3839 HeapFree(GetProcessHeap(), 0, lpServicesArray
);
3841 RegCloseKey(hServicesKey
);
3843 DPRINT("REnumDependentServicesA() done (Error %lu)\n", dwError
);
3852 REnumServicesStatusA(
3853 SC_RPC_HANDLE hSCManager
,
3854 DWORD dwServiceType
,
3855 DWORD dwServiceState
,
3858 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
3859 LPBOUNDED_DWORD_256K lpServicesReturned
,
3860 LPBOUNDED_DWORD_256K lpResumeHandle
)
3862 LPENUM_SERVICE_STATUSW lpStatusPtrW
= NULL
;
3863 LPENUM_SERVICE_STATUSW lpStatusPtrIncrW
;
3864 LPENUM_SERVICE_STATUSA lpStatusPtrA
= NULL
;
3865 LPWSTR lpStringPtrW
;
3868 DWORD dwServiceCount
;
3870 DPRINT("REnumServicesStatusA() called\n");
3872 if (pcbBytesNeeded
== NULL
|| lpServicesReturned
== NULL
)
3874 return ERROR_INVALID_ADDRESS
;
3877 if ((dwBufSize
> 0) && (lpBuffer
))
3879 lpStatusPtrW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, dwBufSize
);
3882 DPRINT("Failed to allocate buffer!\n");
3883 return ERROR_NOT_ENOUGH_MEMORY
;
3887 dwError
= REnumServicesStatusW(hSCManager
,
3890 (LPBYTE
)lpStatusPtrW
,
3896 /* if no services were returned then we are Done */
3897 if (*lpServicesReturned
== 0)
3900 lpStatusPtrIncrW
= lpStatusPtrW
;
3901 lpStatusPtrA
= (LPENUM_SERVICE_STATUSA
)lpBuffer
;
3902 lpStringPtrA
= (LPSTR
)((ULONG_PTR
)lpBuffer
+
3903 *lpServicesReturned
* sizeof(ENUM_SERVICE_STATUSA
));
3904 lpStringPtrW
= (LPWSTR
)((ULONG_PTR
)lpStatusPtrW
+
3905 *lpServicesReturned
* sizeof(ENUM_SERVICE_STATUSW
));
3907 for (dwServiceCount
= 0; dwServiceCount
< *lpServicesReturned
; dwServiceCount
++)
3909 /* Copy the service name */
3910 WideCharToMultiByte(CP_ACP
,
3915 (int)wcslen(lpStringPtrW
),
3919 lpStatusPtrA
->lpServiceName
= (LPSTR
)((ULONG_PTR
)lpStringPtrA
- (ULONG_PTR
)lpBuffer
);
3920 lpStringPtrA
+= wcslen(lpStringPtrW
) + 1;
3921 lpStringPtrW
+= wcslen(lpStringPtrW
) + 1;
3923 /* Copy the display name */
3924 WideCharToMultiByte(CP_ACP
,
3929 (int)wcslen(lpStringPtrW
),
3933 lpStatusPtrA
->lpDisplayName
= (LPSTR
)((ULONG_PTR
)lpStringPtrA
- (ULONG_PTR
)lpBuffer
);
3934 lpStringPtrA
+= wcslen(lpStringPtrW
) + 1;
3935 lpStringPtrW
+= wcslen(lpStringPtrW
) + 1;
3937 /* Copy the status information */
3938 memcpy(&lpStatusPtrA
->ServiceStatus
,
3939 &lpStatusPtrIncrW
->ServiceStatus
,
3940 sizeof(SERVICE_STATUS
));
3948 HeapFree(GetProcessHeap(), 0, lpStatusPtrW
);
3950 DPRINT("REnumServicesStatusA() done (Error %lu)\n", dwError
);
3960 LPSTR lpMachineName
,
3961 LPSTR lpDatabaseName
,
3962 DWORD dwDesiredAccess
,
3963 LPSC_RPC_HANDLE lpScHandle
)
3965 UNICODE_STRING MachineName
;
3966 UNICODE_STRING DatabaseName
;
3969 DPRINT("ROpenSCManagerA() called\n");
3972 RtlCreateUnicodeStringFromAsciiz(&MachineName
,
3976 RtlCreateUnicodeStringFromAsciiz(&DatabaseName
,
3979 dwError
= ROpenSCManagerW(lpMachineName
? MachineName
.Buffer
: NULL
,
3980 lpDatabaseName
? DatabaseName
.Buffer
: NULL
,
3985 RtlFreeUnicodeString(&MachineName
);
3988 RtlFreeUnicodeString(&DatabaseName
);
3998 SC_RPC_HANDLE hSCManager
,
3999 LPSTR lpServiceName
,
4000 DWORD dwDesiredAccess
,
4001 LPSC_RPC_HANDLE lpServiceHandle
)
4003 UNICODE_STRING ServiceName
;
4006 DPRINT("ROpenServiceA() called\n");
4009 RtlCreateUnicodeStringFromAsciiz(&ServiceName
,
4012 dwError
= ROpenServiceW(hSCManager
,
4013 lpServiceName
? ServiceName
.Buffer
: NULL
,
4018 RtlFreeUnicodeString(&ServiceName
);
4027 RQueryServiceConfigA(
4028 SC_RPC_HANDLE hService
,
4029 LPBYTE lpBuf
, //LPQUERY_SERVICE_CONFIGA lpServiceConfig,
4031 LPBOUNDED_DWORD_8K pcbBytesNeeded
)
4033 LPQUERY_SERVICE_CONFIGA lpServiceConfig
= (LPQUERY_SERVICE_CONFIGA
)lpBuf
;
4034 DWORD dwError
= ERROR_SUCCESS
;
4035 PSERVICE_HANDLE hSvc
;
4036 PSERVICE lpService
= NULL
;
4037 HKEY hServiceKey
= NULL
;
4038 LPWSTR lpImagePath
= NULL
;
4039 LPWSTR lpServiceStartName
= NULL
;
4040 LPWSTR lpDependencies
= NULL
;
4041 DWORD dwDependenciesLength
= 0;
4042 DWORD dwRequiredSize
;
4045 DPRINT("RQueryServiceConfigA() called\n");
4048 return ERROR_SHUTDOWN_IN_PROGRESS
;
4050 hSvc
= ScmGetServiceFromHandle(hService
);
4053 DPRINT1("Invalid service handle!\n");
4054 return ERROR_INVALID_HANDLE
;
4057 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
4058 SERVICE_QUERY_CONFIG
))
4060 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
4061 return ERROR_ACCESS_DENIED
;
4064 lpService
= hSvc
->ServiceEntry
;
4065 if (lpService
== NULL
)
4067 DPRINT("lpService == NULL!\n");
4068 return ERROR_INVALID_HANDLE
;
4071 /* Lock the service database shared */
4072 ScmLockDatabaseShared();
4074 dwError
= ScmOpenServiceKey(lpService
->lpServiceName
,
4077 if (dwError
!= ERROR_SUCCESS
)
4080 /* Read the image path */
4081 dwError
= ScmReadString(hServiceKey
,
4084 if (dwError
!= ERROR_SUCCESS
)
4087 /* Read the service start name */
4088 ScmReadString(hServiceKey
,
4090 &lpServiceStartName
);
4092 /* Read the dependencies */
4093 ScmReadDependencies(hServiceKey
,
4095 &dwDependenciesLength
);
4097 dwRequiredSize
= sizeof(QUERY_SERVICE_CONFIGA
);
4099 if (lpImagePath
!= NULL
)
4100 dwRequiredSize
+= (DWORD
)(wcslen(lpImagePath
) + 1);
4102 dwRequiredSize
+= 2 * sizeof(CHAR
);
4104 if ((lpService
->lpGroup
!= NULL
) && (lpService
->lpGroup
->lpGroupName
!= NULL
))
4105 dwRequiredSize
+= (DWORD
)(wcslen(lpService
->lpGroup
->lpGroupName
) + 1);
4107 dwRequiredSize
+= 2 * sizeof(CHAR
);
4109 /* Add Dependencies length */
4110 if (lpDependencies
!= NULL
)
4111 dwRequiredSize
+= dwDependenciesLength
;
4113 dwRequiredSize
+= 2 * sizeof(CHAR
);
4115 if (lpServiceStartName
!= NULL
)
4116 dwRequiredSize
+= (DWORD
)(wcslen(lpServiceStartName
) + 1);
4118 dwRequiredSize
+= 2 * sizeof(CHAR
);
4120 if (lpService
->lpDisplayName
!= NULL
)
4121 dwRequiredSize
+= (DWORD
)(wcslen(lpService
->lpDisplayName
) + 1);
4123 dwRequiredSize
+= 2 * sizeof(CHAR
);
4125 if (lpServiceConfig
== NULL
|| cbBufSize
< dwRequiredSize
)
4127 dwError
= ERROR_INSUFFICIENT_BUFFER
;
4131 lpServiceConfig
->dwServiceType
= lpService
->Status
.dwServiceType
;
4132 lpServiceConfig
->dwStartType
= lpService
->dwStartType
;
4133 lpServiceConfig
->dwErrorControl
= lpService
->dwErrorControl
;
4134 lpServiceConfig
->dwTagId
= lpService
->dwTag
;
4136 lpStr
= (LPSTR
)(lpServiceConfig
+ 1);
4138 /* NOTE: Strings that are NULL for QUERY_SERVICE_CONFIG are pointers to empty strings.
4139 Verified in WINXP */
4143 WideCharToMultiByte(CP_ACP
,
4148 (int)(wcslen(lpImagePath
) + 1),
4157 lpServiceConfig
->lpBinaryPathName
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
4158 lpStr
+= (strlen((LPSTR
)lpStr
) + 1);
4160 if (lpService
->lpGroup
&& lpService
->lpGroup
->lpGroupName
)
4162 WideCharToMultiByte(CP_ACP
,
4164 lpService
->lpGroup
->lpGroupName
,
4167 (int)(wcslen(lpService
->lpGroup
->lpGroupName
) + 1),
4176 lpServiceConfig
->lpLoadOrderGroup
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
4177 lpStr
+= (strlen(lpStr
) + 1);
4179 /* Append Dependencies */
4182 WideCharToMultiByte(CP_ACP
,
4185 dwDependenciesLength
,
4187 dwDependenciesLength
,
4196 lpServiceConfig
->lpDependencies
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
4198 lpStr
+= dwDependenciesLength
;
4200 lpStr
+= (strlen(lpStr
) + 1);
4202 if (lpServiceStartName
)
4204 WideCharToMultiByte(CP_ACP
,
4209 (int)(wcslen(lpServiceStartName
) + 1),
4218 lpServiceConfig
->lpServiceStartName
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
4219 lpStr
+= (strlen(lpStr
) + 1);
4221 if (lpService
->lpDisplayName
)
4223 WideCharToMultiByte(CP_ACP
,
4225 lpService
->lpDisplayName
,
4228 (int)(wcslen(lpService
->lpDisplayName
) + 1),
4237 lpServiceConfig
->lpDisplayName
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
4240 if (pcbBytesNeeded
!= NULL
)
4241 *pcbBytesNeeded
= dwRequiredSize
;
4244 /* Unlock the service database */
4245 ScmUnlockDatabase();
4247 if (lpImagePath
!= NULL
)
4248 HeapFree(GetProcessHeap(), 0, lpImagePath
);
4250 if (lpServiceStartName
!= NULL
)
4251 HeapFree(GetProcessHeap(), 0, lpServiceStartName
);
4253 if (lpDependencies
!= NULL
)
4254 HeapFree(GetProcessHeap(), 0, lpDependencies
);
4256 if (hServiceKey
!= NULL
)
4257 RegCloseKey(hServiceKey
);
4259 DPRINT("RQueryServiceConfigA() done\n");
4268 RQueryServiceLockStatusA(
4269 SC_RPC_HANDLE hSCManager
,
4270 LPBYTE lpBuf
, // LPQUERY_SERVICE_LOCK_STATUSA lpLockStatus,
4272 LPBOUNDED_DWORD_4K pcbBytesNeeded
)
4274 LPQUERY_SERVICE_LOCK_STATUSA lpLockStatus
= (LPQUERY_SERVICE_LOCK_STATUSA
)lpBuf
;
4275 PMANAGER_HANDLE hMgr
;
4276 DWORD dwRequiredSize
;
4278 if (!lpLockStatus
|| !pcbBytesNeeded
)
4279 return ERROR_INVALID_PARAMETER
;
4281 hMgr
= ScmGetServiceManagerFromHandle(hSCManager
);
4284 DPRINT1("Invalid service manager handle!\n");
4285 return ERROR_INVALID_HANDLE
;
4288 if (!RtlAreAllAccessesGranted(hMgr
->Handle
.DesiredAccess
,
4289 SC_MANAGER_QUERY_LOCK_STATUS
))
4291 DPRINT("Insufficient access rights! 0x%lx\n", hMgr
->Handle
.DesiredAccess
);
4292 return ERROR_ACCESS_DENIED
;
4295 /* FIXME: we need to compute instead the real length of the owner name */
4296 dwRequiredSize
= sizeof(QUERY_SERVICE_LOCK_STATUSA
) + sizeof(CHAR
);
4297 *pcbBytesNeeded
= dwRequiredSize
;
4299 if (cbBufSize
< dwRequiredSize
)
4300 return ERROR_INSUFFICIENT_BUFFER
;
4302 ScmQueryServiceLockStatusA(lpLockStatus
);
4304 return ERROR_SUCCESS
;
4312 SC_RPC_HANDLE hService
,
4314 LPSTRING_PTRSA argv
)
4316 DWORD dwError
= ERROR_SUCCESS
;
4317 PSERVICE_HANDLE hSvc
;
4318 PSERVICE lpService
= NULL
;
4319 LPWSTR
*lpVector
= NULL
;
4323 DPRINT("RStartServiceA() called\n");
4326 return ERROR_SHUTDOWN_IN_PROGRESS
;
4328 hSvc
= ScmGetServiceFromHandle(hService
);
4331 DPRINT1("Invalid service handle!\n");
4332 return ERROR_INVALID_HANDLE
;
4335 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
4338 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
4339 return ERROR_ACCESS_DENIED
;
4342 lpService
= hSvc
->ServiceEntry
;
4343 if (lpService
== NULL
)
4345 DPRINT("lpService == NULL!\n");
4346 return ERROR_INVALID_HANDLE
;
4349 if (lpService
->dwStartType
== SERVICE_DISABLED
)
4350 return ERROR_SERVICE_DISABLED
;
4352 if (lpService
->bDeleted
)
4353 return ERROR_SERVICE_MARKED_FOR_DELETE
;
4355 /* Build a Unicode argument vector */
4358 lpVector
= HeapAlloc(GetProcessHeap(),
4360 argc
* sizeof(LPWSTR
));
4361 if (lpVector
== NULL
)
4362 return ERROR_NOT_ENOUGH_MEMORY
;
4364 for (i
= 0; i
< argc
; i
++)
4366 dwLength
= MultiByteToWideChar(CP_ACP
,
4373 lpVector
[i
] = HeapAlloc(GetProcessHeap(),
4375 dwLength
* sizeof(WCHAR
));
4376 if (lpVector
[i
] == NULL
)
4378 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
4382 MultiByteToWideChar(CP_ACP
,
4391 /* Start the service */
4392 dwError
= ScmStartService(lpService
, argc
, lpVector
);
4395 /* Free the Unicode argument vector */
4396 if (lpVector
!= NULL
)
4398 for (i
= 0; i
< argc
; i
++)
4400 if (lpVector
[i
] != NULL
)
4401 HeapFree(GetProcessHeap(), 0, lpVector
[i
]);
4403 HeapFree(GetProcessHeap(), 0, lpVector
);
4413 RGetServiceDisplayNameA(
4414 SC_RPC_HANDLE hSCManager
,
4415 LPCSTR lpServiceName
,
4416 LPSTR lpDisplayName
,
4417 LPBOUNDED_DWORD_4K lpcchBuffer
)
4419 // PMANAGER_HANDLE hManager;
4420 PSERVICE lpService
= NULL
;
4423 LPWSTR lpServiceNameW
;
4425 DPRINT("RGetServiceDisplayNameA() called\n");
4426 DPRINT("hSCManager = %p\n", hSCManager
);
4427 DPRINT("lpServiceName: %s\n", lpServiceName
);
4428 DPRINT("lpDisplayName: %p\n", lpDisplayName
);
4429 DPRINT("*lpcchBuffer: %lu\n", *lpcchBuffer
);
4431 // hManager = (PMANAGER_HANDLE)hSCManager;
4432 // if (hManager->Handle.Tag != MANAGER_TAG)
4434 // DPRINT("Invalid manager handle!\n");
4435 // return ERROR_INVALID_HANDLE;
4438 if (lpServiceName
!= NULL
)
4440 dwLength
= (DWORD
)(strlen(lpServiceName
) + 1);
4441 lpServiceNameW
= HeapAlloc(GetProcessHeap(),
4443 dwLength
* sizeof(WCHAR
));
4444 if (!lpServiceNameW
)
4445 return ERROR_NOT_ENOUGH_MEMORY
;
4447 MultiByteToWideChar(CP_ACP
,
4454 lpService
= ScmGetServiceEntryByName(lpServiceNameW
);
4456 HeapFree(GetProcessHeap(), 0, lpServiceNameW
);
4459 if (lpService
== NULL
)
4461 DPRINT("Could not find a service!\n");
4463 /* If the service could not be found and lpcchBuffer is 0, windows
4464 puts null in lpDisplayName and puts 1 in lpcchBuffer */
4465 if (*lpcchBuffer
== 0)
4467 *lpcchBuffer
= sizeof(CHAR
);
4468 if (lpDisplayName
!= NULL
)
4473 return ERROR_SERVICE_DOES_NOT_EXIST
;
4476 if (!lpService
->lpDisplayName
)
4478 dwLength
= (DWORD
)wcslen(lpService
->lpServiceName
);
4479 if (lpDisplayName
!= NULL
&&
4480 *lpcchBuffer
> dwLength
)
4482 WideCharToMultiByte(CP_ACP
,
4484 lpService
->lpServiceName
,
4485 (int)wcslen(lpService
->lpServiceName
),
4490 return ERROR_SUCCESS
;
4495 dwLength
= (DWORD
)wcslen(lpService
->lpDisplayName
);
4496 if (lpDisplayName
!= NULL
&&
4497 *lpcchBuffer
> dwLength
)
4499 WideCharToMultiByte(CP_ACP
,
4501 lpService
->lpDisplayName
,
4502 (int)wcslen(lpService
->lpDisplayName
),
4507 return ERROR_SUCCESS
;
4511 dwError
= (*lpcchBuffer
> dwLength
) ? ERROR_SUCCESS
: ERROR_INSUFFICIENT_BUFFER
;
4513 *lpcchBuffer
= dwLength
* 2;
4522 RGetServiceKeyNameA(
4523 SC_RPC_HANDLE hSCManager
,
4524 LPCSTR lpDisplayName
,
4525 LPSTR lpServiceName
,
4526 LPBOUNDED_DWORD_4K lpcchBuffer
)
4531 LPWSTR lpDisplayNameW
;
4533 DPRINT("RGetServiceKeyNameA() called\n");
4534 DPRINT("hSCManager = %p\n", hSCManager
);
4535 DPRINT("lpDisplayName: %s\n", lpDisplayName
);
4536 DPRINT("lpServiceName: %p\n", lpServiceName
);
4537 DPRINT("*lpcchBuffer: %lu\n", *lpcchBuffer
);
4539 dwLength
= (DWORD
)(strlen(lpDisplayName
) + 1);
4540 lpDisplayNameW
= HeapAlloc(GetProcessHeap(),
4542 dwLength
* sizeof(WCHAR
));
4543 if (!lpDisplayNameW
)
4544 return ERROR_NOT_ENOUGH_MEMORY
;
4546 MultiByteToWideChar(CP_ACP
,
4553 lpService
= ScmGetServiceEntryByDisplayName(lpDisplayNameW
);
4555 HeapFree(GetProcessHeap(), 0, lpDisplayNameW
);
4557 if (lpService
== NULL
)
4559 DPRINT("Could not find the service!\n");
4561 /* If the service could not be found and lpcchBuffer is 0,
4562 put null in lpDisplayName and puts 1 in lpcchBuffer, verified WINXP. */
4563 if (*lpcchBuffer
== 0)
4565 *lpcchBuffer
= sizeof(CHAR
);
4566 if (lpServiceName
!= NULL
)
4572 return ERROR_SERVICE_DOES_NOT_EXIST
;
4575 dwLength
= (DWORD
)wcslen(lpService
->lpServiceName
);
4576 if (lpServiceName
!= NULL
&&
4577 *lpcchBuffer
> dwLength
)
4579 WideCharToMultiByte(CP_ACP
,
4581 lpService
->lpServiceName
,
4582 (int)wcslen(lpService
->lpServiceName
),
4587 return ERROR_SUCCESS
;
4590 dwError
= (*lpcchBuffer
> dwLength
) ? ERROR_SUCCESS
: ERROR_INSUFFICIENT_BUFFER
;
4592 *lpcchBuffer
= dwLength
* 2;
4601 RI_ScGetCurrentGroupStateW(
4602 SC_RPC_HANDLE hSCManager
,
4603 LPWSTR lpLoadOrderGroup
,
4606 PMANAGER_HANDLE hManager
;
4607 PSERVICE_GROUP pServiceGroup
;
4608 DWORD dwError
= ERROR_SUCCESS
;
4610 DPRINT("RI_ScGetCurrentGroupStateW() called\n");
4613 return ERROR_SHUTDOWN_IN_PROGRESS
;
4615 hManager
= ScmGetServiceManagerFromHandle(hSCManager
);
4616 if (hManager
== NULL
)
4618 DPRINT1("Invalid service manager handle!\n");
4619 return ERROR_INVALID_HANDLE
;
4622 /* Check for SC_MANAGER_ENUMERATE_SERVICE access right */
4623 if (!RtlAreAllAccessesGranted(hManager
->Handle
.DesiredAccess
,
4624 SC_MANAGER_ENUMERATE_SERVICE
))
4626 DPRINT("Insufficient access rights! 0x%lx\n",
4627 hManager
->Handle
.DesiredAccess
);
4628 return ERROR_ACCESS_DENIED
;
4631 /* Lock the service database shared */
4632 ScmLockDatabaseShared();
4634 /* Get the group list entry */
4635 pServiceGroup
= ScmGetServiceGroupByName(lpLoadOrderGroup
);
4636 if (pServiceGroup
== NULL
)
4638 dwError
= ERROR_SERVICE_DOES_NOT_EXIST
;
4642 /* FIXME: Return the group state */
4646 /* Unlock the service database */
4647 ScmUnlockDatabase();
4649 DPRINT("RI_ScGetCurrentGroupStateW() done (Error %lu)\n", dwError
);
4659 SC_RPC_HANDLE hSCManager
,
4660 DWORD dwServiceType
,
4661 DWORD dwServiceState
,
4664 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
4665 LPBOUNDED_DWORD_256K lpServicesReturned
,
4666 LPBOUNDED_DWORD_256K lpResumeIndex
,
4667 LPCWSTR pszGroupName
)
4669 PMANAGER_HANDLE hManager
;
4671 DWORD dwError
= ERROR_SUCCESS
;
4672 PLIST_ENTRY ServiceEntry
;
4673 PSERVICE CurrentService
;
4675 DWORD dwRequiredSize
;
4676 DWORD dwServiceCount
;
4678 DWORD dwLastResumeCount
= 0;
4679 LPENUM_SERVICE_STATUSW lpStatusPtr
;
4682 DPRINT("REnumServiceGroupW() called\n");
4685 return ERROR_SHUTDOWN_IN_PROGRESS
;
4687 hManager
= ScmGetServiceManagerFromHandle(hSCManager
);
4688 if (hManager
== NULL
)
4690 DPRINT1("Invalid service manager handle!\n");
4691 return ERROR_INVALID_HANDLE
;
4694 if (pcbBytesNeeded
== NULL
|| lpServicesReturned
== NULL
)
4696 return ERROR_INVALID_ADDRESS
;
4699 *pcbBytesNeeded
= 0;
4700 *lpServicesReturned
= 0;
4702 if ((dwServiceType
== 0) ||
4703 ((dwServiceType
& ~SERVICE_TYPE_ALL
) != 0))
4705 DPRINT("Not a valid Service Type!\n");
4706 return ERROR_INVALID_PARAMETER
;
4709 if ((dwServiceState
== 0) ||
4710 ((dwServiceState
& ~SERVICE_STATE_ALL
) != 0))
4712 DPRINT("Not a valid Service State!\n");
4713 return ERROR_INVALID_PARAMETER
;
4716 /* Check access rights */
4717 if (!RtlAreAllAccessesGranted(hManager
->Handle
.DesiredAccess
,
4718 SC_MANAGER_ENUMERATE_SERVICE
))
4720 DPRINT("Insufficient access rights! 0x%lx\n",
4721 hManager
->Handle
.DesiredAccess
);
4722 return ERROR_ACCESS_DENIED
;
4726 dwLastResumeCount
= *lpResumeIndex
;
4728 /* Lock the service database shared */
4729 ScmLockDatabaseShared();
4731 lpService
= ScmGetServiceEntryByResumeCount(dwLastResumeCount
);
4732 if (lpService
== NULL
)
4734 dwError
= ERROR_SUCCESS
;
4741 for (ServiceEntry
= &lpService
->ServiceListEntry
;
4742 ServiceEntry
!= &ServiceListHead
;
4743 ServiceEntry
= ServiceEntry
->Flink
)
4745 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
4749 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
4752 dwState
= SERVICE_ACTIVE
;
4753 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
4754 dwState
= SERVICE_INACTIVE
;
4756 if ((dwState
& dwServiceState
) == 0)
4761 if (*pszGroupName
== 0)
4763 if (CurrentService
->lpGroup
!= NULL
)
4768 if ((CurrentService
->lpGroup
== NULL
) ||
4769 _wcsicmp(pszGroupName
, CurrentService
->lpGroup
->lpGroupName
) != 0)
4774 dwSize
= sizeof(ENUM_SERVICE_STATUSW
) +
4775 (DWORD
)((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
4776 (DWORD
)((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
4778 if (dwRequiredSize
+ dwSize
> cbBufSize
)
4780 DPRINT("Service name: %S no fit\n", CurrentService
->lpServiceName
);
4784 DPRINT("Service name: %S fit\n", CurrentService
->lpServiceName
);
4785 dwRequiredSize
+= dwSize
;
4787 dwLastResumeCount
= CurrentService
->dwResumeCount
;
4790 DPRINT("dwRequiredSize: %lu\n", dwRequiredSize
);
4791 DPRINT("dwServiceCount: %lu\n", dwServiceCount
);
4794 ServiceEntry
!= &ServiceListHead
;
4795 ServiceEntry
= ServiceEntry
->Flink
)
4797 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
4801 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
4804 dwState
= SERVICE_ACTIVE
;
4805 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
4806 dwState
= SERVICE_INACTIVE
;
4808 if ((dwState
& dwServiceState
) == 0)
4813 if (*pszGroupName
== 0)
4815 if (CurrentService
->lpGroup
!= NULL
)
4820 if ((CurrentService
->lpGroup
== NULL
) ||
4821 _wcsicmp(pszGroupName
, CurrentService
->lpGroup
->lpGroupName
) != 0)
4826 dwRequiredSize
+= (sizeof(ENUM_SERVICE_STATUSW
) +
4827 (DWORD
)((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
4828 (DWORD
)((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
)));
4830 dwError
= ERROR_MORE_DATA
;
4833 DPRINT("*pcbBytesNeeded: %lu\n", dwRequiredSize
);
4836 *lpResumeIndex
= dwLastResumeCount
;
4838 *lpServicesReturned
= dwServiceCount
;
4839 *pcbBytesNeeded
= dwRequiredSize
;
4841 lpStatusPtr
= (LPENUM_SERVICE_STATUSW
)lpBuffer
;
4842 lpStringPtr
= (LPWSTR
)((ULONG_PTR
)lpBuffer
+
4843 dwServiceCount
* sizeof(ENUM_SERVICE_STATUSW
));
4846 for (ServiceEntry
= &lpService
->ServiceListEntry
;
4847 ServiceEntry
!= &ServiceListHead
;
4848 ServiceEntry
= ServiceEntry
->Flink
)
4850 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
4854 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
4857 dwState
= SERVICE_ACTIVE
;
4858 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
4859 dwState
= SERVICE_INACTIVE
;
4861 if ((dwState
& dwServiceState
) == 0)
4866 if (*pszGroupName
== 0)
4868 if (CurrentService
->lpGroup
!= NULL
)
4873 if ((CurrentService
->lpGroup
== NULL
) ||
4874 _wcsicmp(pszGroupName
, CurrentService
->lpGroup
->lpGroupName
) != 0)
4879 dwSize
= sizeof(ENUM_SERVICE_STATUSW
) +
4880 (DWORD
)((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
4881 (DWORD
)((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
4883 if (dwRequiredSize
+ dwSize
> cbBufSize
)
4886 /* Copy the service name */
4887 wcscpy(lpStringPtr
, CurrentService
->lpServiceName
);
4888 lpStatusPtr
->lpServiceName
= (LPWSTR
)((ULONG_PTR
)lpStringPtr
- (ULONG_PTR
)lpBuffer
);
4889 lpStringPtr
+= (wcslen(CurrentService
->lpServiceName
) + 1);
4891 /* Copy the display name */
4892 wcscpy(lpStringPtr
, CurrentService
->lpDisplayName
);
4893 lpStatusPtr
->lpDisplayName
= (LPWSTR
)((ULONG_PTR
)lpStringPtr
- (ULONG_PTR
)lpBuffer
);
4894 lpStringPtr
+= (wcslen(CurrentService
->lpDisplayName
) + 1);
4896 /* Copy the status information */
4897 memcpy(&lpStatusPtr
->ServiceStatus
,
4898 &CurrentService
->Status
,
4899 sizeof(SERVICE_STATUS
));
4902 dwRequiredSize
+= dwSize
;
4905 if (dwError
== ERROR_SUCCESS
)
4907 *pcbBytesNeeded
= 0;
4908 if (lpResumeIndex
) *lpResumeIndex
= 0;
4912 /* Unlock the service database */
4913 ScmUnlockDatabase();
4915 DPRINT("REnumServiceGroupW() done (Error %lu)\n", dwError
);
4924 RChangeServiceConfig2A(
4925 SC_RPC_HANDLE hService
,
4926 SC_RPC_CONFIG_INFOA Info
)
4928 SC_RPC_CONFIG_INFOW InfoW
= { 0 };
4929 DWORD dwRet
, dwLength
;
4932 DPRINT("RChangeServiceConfig2A() called\n");
4933 DPRINT("dwInfoLevel = %lu\n", Info
.dwInfoLevel
);
4935 if ((Info
.dwInfoLevel
< SERVICE_CONFIG_DESCRIPTION
) ||
4936 (Info
.dwInfoLevel
> SERVICE_CONFIG_FAILURE_ACTIONS
))
4938 return ERROR_INVALID_LEVEL
;
4941 InfoW
.dwInfoLevel
= Info
.dwInfoLevel
;
4943 if (InfoW
.dwInfoLevel
== SERVICE_CONFIG_DESCRIPTION
)
4945 LPSERVICE_DESCRIPTIONW lpServiceDescriptionW
;
4946 LPSERVICE_DESCRIPTIONA lpServiceDescriptionA
;
4948 lpServiceDescriptionA
= Info
.psd
;
4950 if (lpServiceDescriptionA
&&
4951 lpServiceDescriptionA
->lpDescription
)
4953 dwLength
= (DWORD
)((strlen(lpServiceDescriptionA
->lpDescription
) + 1) * sizeof(WCHAR
));
4955 lpServiceDescriptionW
= HeapAlloc(GetProcessHeap(),
4957 dwLength
+ sizeof(SERVICE_DESCRIPTIONW
));
4958 if (!lpServiceDescriptionW
)
4960 return ERROR_NOT_ENOUGH_MEMORY
;
4963 lpServiceDescriptionW
->lpDescription
= (LPWSTR
)(lpServiceDescriptionW
+ 1);
4965 MultiByteToWideChar(CP_ACP
,
4967 lpServiceDescriptionA
->lpDescription
,
4969 lpServiceDescriptionW
->lpDescription
,
4972 ptr
= lpServiceDescriptionW
;
4973 InfoW
.psd
= lpServiceDescriptionW
;
4976 else if (Info
.dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
4978 LPSERVICE_FAILURE_ACTIONSW lpServiceFailureActionsW
;
4979 LPSERVICE_FAILURE_ACTIONSA lpServiceFailureActionsA
;
4980 DWORD dwRebootLen
= 0;
4981 DWORD dwCommandLen
= 0;
4982 DWORD dwActionArrayLen
= 0;
4983 LPWSTR lpStr
= NULL
;
4985 lpServiceFailureActionsA
= Info
.psfa
;
4987 if (lpServiceFailureActionsA
)
4990 * The following code is inspired by the
4991 * SERVICE_CONFIG_FAILURE_ACTIONS case of
4992 * the RQueryServiceConfig2W function.
4995 /* Retrieve the needed length for the two data strings */
4996 if (lpServiceFailureActionsA
->lpRebootMsg
)
4998 dwRebootLen
= (DWORD
)((strlen(lpServiceFailureActionsA
->lpRebootMsg
) + 1) * sizeof(WCHAR
));
5000 if (lpServiceFailureActionsA
->lpCommand
)
5002 dwCommandLen
= (DWORD
)((strlen(lpServiceFailureActionsA
->lpCommand
) + 1) * sizeof(WCHAR
));
5006 * Retrieve the size of the lpsaActions array if needed.
5007 * We will copy the lpsaActions array only if there is at
5008 * least one action AND that the original array is valid.
5010 if (lpServiceFailureActionsA
->cActions
> 0 && lpServiceFailureActionsA
->lpsaActions
)
5012 dwActionArrayLen
= lpServiceFailureActionsA
->cActions
* sizeof(SC_ACTION
);
5015 /* Compute the total length for the UNICODE structure, including data */
5016 dwLength
= sizeof(SERVICE_FAILURE_ACTIONSW
) +
5017 dwActionArrayLen
+ dwRebootLen
+ dwCommandLen
;
5019 /* Allocate the structure */
5020 lpServiceFailureActionsW
= HeapAlloc(GetProcessHeap(),
5023 if (!lpServiceFailureActionsW
)
5025 return ERROR_NOT_ENOUGH_MEMORY
;
5028 /* Copy the members */
5029 lpServiceFailureActionsW
->dwResetPeriod
= lpServiceFailureActionsA
->dwResetPeriod
;
5030 lpServiceFailureActionsW
->cActions
= lpServiceFailureActionsA
->cActions
;
5032 /* Copy the lpsaActions array if needed */
5033 if (dwActionArrayLen
> 0)
5035 /* The storage zone is just after the end of the SERVICE_FAILURE_ACTIONSW structure */
5036 lpServiceFailureActionsW
->lpsaActions
= (LPSC_ACTION
)((ULONG_PTR
)(lpServiceFailureActionsW
+ 1));
5038 /* dwActionArrayLen == lpServiceFailureActionsW->cActions * sizeof(SC_ACTION) */
5039 RtlCopyMemory(lpServiceFailureActionsW
->lpsaActions
,
5040 lpServiceFailureActionsA
->lpsaActions
,
5045 /* No lpsaActions array */
5046 lpServiceFailureActionsW
->lpsaActions
= NULL
;
5048 /* The data strings are stored just after the lpsaActions array */
5049 lpStr
= (LPWSTR
)((ULONG_PTR
)(lpServiceFailureActionsW
+ 1) + dwActionArrayLen
);
5052 * Convert the data strings to UNICODE
5055 lpServiceFailureActionsW
->lpRebootMsg
= NULL
;
5056 lpServiceFailureActionsW
->lpCommand
= NULL
;
5060 /* lpRebootMsg points just after the lpsaActions array */
5061 lpServiceFailureActionsW
->lpRebootMsg
= lpStr
;
5063 MultiByteToWideChar(CP_ACP
,
5065 lpServiceFailureActionsA
->lpRebootMsg
,
5067 lpServiceFailureActionsW
->lpRebootMsg
,
5070 lpStr
+= dwRebootLen
/ sizeof(WCHAR
);
5075 /* lpRebootMsg points just after the lpRebootMsg data string */
5076 lpServiceFailureActionsW
->lpCommand
= lpStr
;
5078 MultiByteToWideChar(CP_ACP
,
5080 lpServiceFailureActionsA
->lpCommand
,
5082 lpServiceFailureActionsW
->lpCommand
,
5086 /* Set the pointers */
5087 ptr
= lpServiceFailureActionsW
;
5088 InfoW
.psfa
= lpServiceFailureActionsW
;
5092 dwRet
= RChangeServiceConfig2W(hService
, InfoW
);
5094 HeapFree(GetProcessHeap(), 0, ptr
);
5101 ScmSetFailureActions(HKEY hServiceKey
,
5102 LPSERVICE_FAILURE_ACTIONSW lpFailureActions
)
5104 LPSERVICE_FAILURE_ACTIONSW lpReadBuffer
= NULL
;
5105 LPSERVICE_FAILURE_ACTIONSW lpWriteBuffer
= NULL
;
5106 DWORD dwRequiredSize
= 0;
5110 /* There is nothing to be done if we have no failure actions */
5111 if (lpFailureActions
== NULL
)
5112 return ERROR_SUCCESS
;
5115 * 1- Retrieve the original value of FailureActions.
5118 /* Query value length */
5119 dwError
= RegQueryValueExW(hServiceKey
,
5125 if (dwError
!= ERROR_SUCCESS
&&
5126 dwError
!= ERROR_MORE_DATA
&&
5127 dwError
!= ERROR_FILE_NOT_FOUND
)
5132 dwRequiredSize
= (dwType
== REG_BINARY
) ? max(sizeof(SERVICE_FAILURE_ACTIONSW
), dwRequiredSize
)
5133 : sizeof(SERVICE_FAILURE_ACTIONSW
);
5135 /* Initialize the read buffer */
5136 lpReadBuffer
= HeapAlloc(GetProcessHeap(),
5139 if (lpReadBuffer
== NULL
)
5140 return ERROR_NOT_ENOUGH_MEMORY
;
5142 /* Now we can fill the read buffer */
5143 if (dwError
!= ERROR_FILE_NOT_FOUND
&&
5144 dwType
== REG_BINARY
)
5146 dwError
= RegQueryValueExW(hServiceKey
,
5150 (LPBYTE
)lpReadBuffer
,
5152 if (dwError
!= ERROR_SUCCESS
&&
5153 dwError
!= ERROR_FILE_NOT_FOUND
)
5156 if (dwRequiredSize
< sizeof(SERVICE_FAILURE_ACTIONSW
))
5157 dwRequiredSize
= sizeof(SERVICE_FAILURE_ACTIONSW
);
5162 * The value of the error doesn't really matter, the only
5163 * important thing is that it must be != ERROR_SUCCESS.
5165 dwError
= ERROR_INVALID_DATA
;
5168 if (dwError
== ERROR_SUCCESS
)
5170 lpReadBuffer
->cActions
= min(lpReadBuffer
->cActions
, (dwRequiredSize
- sizeof(SERVICE_FAILURE_ACTIONSW
)) / sizeof(SC_ACTION
));
5171 lpReadBuffer
->lpsaActions
= (lpReadBuffer
->cActions
> 0 ? (LPSC_ACTION
)(lpReadBuffer
+ 1) : NULL
);
5175 lpReadBuffer
->dwResetPeriod
= 0;
5176 lpReadBuffer
->cActions
= 0;
5177 lpReadBuffer
->lpsaActions
= NULL
;
5180 lpReadBuffer
->lpRebootMsg
= NULL
;
5181 lpReadBuffer
->lpCommand
= NULL
;
5184 * 2- Initialize the new value to set.
5187 dwRequiredSize
= sizeof(SERVICE_FAILURE_ACTIONSW
);
5189 if (lpFailureActions
->lpsaActions
== NULL
)
5192 * lpFailureActions->cActions is ignored.
5193 * Therefore we use the original values
5194 * of cActions and lpsaActions.
5196 dwRequiredSize
+= lpReadBuffer
->cActions
* sizeof(SC_ACTION
);
5201 * The reset period and array of failure actions
5202 * are deleted if lpFailureActions->cActions == 0 .
5204 dwRequiredSize
+= lpFailureActions
->cActions
* sizeof(SC_ACTION
);
5207 lpWriteBuffer
= HeapAlloc(GetProcessHeap(),
5210 if (lpWriteBuffer
== NULL
)
5212 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
5216 /* Clean the pointers as they have no meaning when the structure is stored in the registry */
5217 lpWriteBuffer
->lpRebootMsg
= NULL
;
5218 lpWriteBuffer
->lpCommand
= NULL
;
5219 lpWriteBuffer
->lpsaActions
= NULL
;
5221 /* Set the members */
5222 if (lpFailureActions
->lpsaActions
== NULL
)
5225 * lpFailureActions->dwResetPeriod and lpFailureActions->cActions are ignored.
5226 * Therefore we use the original values of dwResetPeriod, cActions and lpsaActions.
5228 lpWriteBuffer
->dwResetPeriod
= lpReadBuffer
->dwResetPeriod
;
5229 lpWriteBuffer
->cActions
= lpReadBuffer
->cActions
;
5231 if (lpReadBuffer
->lpsaActions
!= NULL
)
5233 memmove(lpWriteBuffer
+ 1,
5234 lpReadBuffer
->lpsaActions
,
5235 lpReadBuffer
->cActions
* sizeof(SC_ACTION
));
5240 if (lpFailureActions
->cActions
> 0)
5242 lpWriteBuffer
->dwResetPeriod
= lpFailureActions
->dwResetPeriod
;
5243 lpWriteBuffer
->cActions
= lpFailureActions
->cActions
;
5245 memmove(lpWriteBuffer
+ 1,
5246 lpFailureActions
->lpsaActions
,
5247 lpFailureActions
->cActions
* sizeof(SC_ACTION
));
5251 /* The reset period and array of failure actions are deleted */
5252 lpWriteBuffer
->dwResetPeriod
= 0;
5253 lpWriteBuffer
->cActions
= 0;
5257 /* Save the new failure actions into the registry */
5258 dwError
= RegSetValueExW(hServiceKey
,
5262 (LPBYTE
)lpWriteBuffer
,
5265 /* We modify the strings only in case of success.*/
5266 if (dwError
== ERROR_SUCCESS
)
5268 /* Modify the Reboot Message value, if specified */
5269 if (lpFailureActions
->lpRebootMsg
!= NULL
)
5271 /* If the Reboot Message is "" then we delete it */
5272 if (*lpFailureActions
->lpRebootMsg
== 0)
5274 DPRINT("Delete Reboot Message value\n");
5275 RegDeleteValueW(hServiceKey
, L
"RebootMessage");
5279 DPRINT("Setting Reboot Message value %S\n", lpFailureActions
->lpRebootMsg
);
5280 RegSetValueExW(hServiceKey
,
5284 (LPBYTE
)lpFailureActions
->lpRebootMsg
,
5285 (DWORD
)((wcslen(lpFailureActions
->lpRebootMsg
) + 1) * sizeof(WCHAR
)));
5289 /* Modify the Failure Command value, if specified */
5290 if (lpFailureActions
->lpCommand
!= NULL
)
5292 /* If the FailureCommand string is an empty string, delete the value */
5293 if (*lpFailureActions
->lpCommand
== 0)
5295 DPRINT("Delete Failure Command value\n");
5296 RegDeleteValueW(hServiceKey
, L
"FailureCommand");
5300 DPRINT("Setting Failure Command value %S\n", lpFailureActions
->lpCommand
);
5301 RegSetValueExW(hServiceKey
,
5305 (LPBYTE
)lpFailureActions
->lpCommand
,
5306 (DWORD
)((wcslen(lpFailureActions
->lpCommand
) + 1) * sizeof(WCHAR
)));
5312 if (lpWriteBuffer
!= NULL
)
5313 HeapFree(GetProcessHeap(), 0, lpWriteBuffer
);
5315 if (lpReadBuffer
!= NULL
)
5316 HeapFree(GetProcessHeap(), 0, lpReadBuffer
);
5325 RChangeServiceConfig2W(
5326 SC_RPC_HANDLE hService
,
5327 SC_RPC_CONFIG_INFOW Info
)
5329 DWORD dwError
= ERROR_SUCCESS
;
5330 PSERVICE_HANDLE hSvc
;
5331 PSERVICE lpService
= NULL
;
5332 HKEY hServiceKey
= NULL
;
5333 ACCESS_MASK RequiredAccess
= SERVICE_CHANGE_CONFIG
;
5335 DPRINT("RChangeServiceConfig2W() called\n");
5336 DPRINT("dwInfoLevel = %lu\n", Info
.dwInfoLevel
);
5339 return ERROR_SHUTDOWN_IN_PROGRESS
;
5341 if ((Info
.dwInfoLevel
< SERVICE_CONFIG_DESCRIPTION
) ||
5342 (Info
.dwInfoLevel
> SERVICE_CONFIG_FAILURE_ACTIONS
))
5344 return ERROR_INVALID_LEVEL
;
5347 hSvc
= ScmGetServiceFromHandle(hService
);
5350 DPRINT("Invalid service handle!\n");
5351 return ERROR_INVALID_HANDLE
;
5354 if (Info
.dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
5355 RequiredAccess
|= SERVICE_START
;
5357 /* Check the access rights */
5358 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
5361 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
5362 return ERROR_ACCESS_DENIED
;
5365 if (Info
.dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
5367 /* FIXME: Check if the caller has the SE_SHUTDOWN_NAME privilege */
5371 lpService
= hSvc
->ServiceEntry
;
5372 if (lpService
== NULL
)
5374 DPRINT("lpService == NULL!\n");
5375 return ERROR_INVALID_HANDLE
;
5378 /* Failure actions can only be set for Win32 services, not for drivers */
5379 if (Info
.dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
5381 if (lpService
->Status
.dwServiceType
& SERVICE_DRIVER
)
5382 return ERROR_CANNOT_DETECT_DRIVER_FAILURE
;
5385 /* Lock the service database exclusively */
5386 ScmLockDatabaseExclusive();
5388 if (lpService
->bDeleted
)
5390 DPRINT("The service has already been marked for delete!\n");
5391 dwError
= ERROR_SERVICE_MARKED_FOR_DELETE
;
5395 /* Open the service key */
5396 dwError
= ScmOpenServiceKey(lpService
->szServiceName
,
5397 KEY_READ
| KEY_SET_VALUE
,
5399 if (dwError
!= ERROR_SUCCESS
)
5402 if (Info
.dwInfoLevel
== SERVICE_CONFIG_DESCRIPTION
)
5404 LPSERVICE_DESCRIPTIONW lpServiceDescription
= (LPSERVICE_DESCRIPTIONW
)Info
.psd
;
5406 /* Modify the service description, if specified */
5407 if (lpServiceDescription
!= NULL
&&
5408 lpServiceDescription
->lpDescription
!= NULL
)
5410 /* If the description is "" then we delete it */
5411 if (*lpServiceDescription
->lpDescription
== 0)
5413 DPRINT("Delete service description\n");
5414 dwError
= RegDeleteValueW(hServiceKey
, L
"Description");
5416 if (dwError
== ERROR_FILE_NOT_FOUND
)
5417 dwError
= ERROR_SUCCESS
;
5421 DPRINT("Setting service description value %S\n", lpServiceDescription
->lpDescription
);
5422 dwError
= RegSetValueExW(hServiceKey
,
5426 (LPBYTE
)lpServiceDescription
->lpDescription
,
5427 (DWORD
)((wcslen(lpServiceDescription
->lpDescription
) + 1) * sizeof(WCHAR
)));
5432 dwError
= ERROR_SUCCESS
;
5435 else if (Info
.dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
5437 dwError
= ScmSetFailureActions(hServiceKey
,
5438 (LPSERVICE_FAILURE_ACTIONSW
)Info
.psfa
);
5442 if (hServiceKey
!= NULL
)
5443 RegCloseKey(hServiceKey
);
5445 /* Unlock the service database */
5446 ScmUnlockDatabase();
5448 DPRINT("RChangeServiceConfig2W() done (Error %lu)\n", dwError
);
5457 RQueryServiceConfig2A(
5458 SC_RPC_HANDLE hService
,
5462 LPBOUNDED_DWORD_8K pcbBytesNeeded
)
5464 DWORD dwError
= ERROR_SUCCESS
;
5465 PSERVICE_HANDLE hSvc
;
5466 PSERVICE lpService
= NULL
;
5467 HKEY hServiceKey
= NULL
;
5468 DWORD dwRequiredSize
= 0;
5470 LPWSTR lpDescriptionW
= NULL
;
5471 LPWSTR lpRebootMessageW
= NULL
;
5472 LPWSTR lpFailureCommandW
= NULL
;
5474 DPRINT("RQueryServiceConfig2A() called hService %p dwInfoLevel %u, lpBuffer %p cbBufSize %u pcbBytesNeeded %p\n",
5475 hService
, dwInfoLevel
, lpBuffer
, cbBufSize
, pcbBytesNeeded
);
5478 return ERROR_INVALID_ADDRESS
;
5481 return ERROR_SHUTDOWN_IN_PROGRESS
;
5483 if ((dwInfoLevel
< SERVICE_CONFIG_DESCRIPTION
) ||
5484 (dwInfoLevel
> SERVICE_CONFIG_FAILURE_ACTIONS
))
5486 return ERROR_INVALID_LEVEL
;
5489 hSvc
= ScmGetServiceFromHandle(hService
);
5492 DPRINT1("Invalid service handle!\n");
5493 return ERROR_INVALID_HANDLE
;
5496 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
5497 SERVICE_QUERY_CONFIG
))
5499 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
5500 return ERROR_ACCESS_DENIED
;
5503 lpService
= hSvc
->ServiceEntry
;
5504 if (lpService
== NULL
)
5506 DPRINT("lpService == NULL!\n");
5507 return ERROR_INVALID_HANDLE
;
5510 /* Lock the service database shared */
5511 ScmLockDatabaseShared();
5513 dwError
= ScmOpenServiceKey(lpService
->lpServiceName
,
5516 if (dwError
!= ERROR_SUCCESS
)
5519 if (dwInfoLevel
== SERVICE_CONFIG_DESCRIPTION
)
5521 LPSERVICE_DESCRIPTIONA lpServiceDescription
= (LPSERVICE_DESCRIPTIONA
)lpBuffer
;
5524 dwError
= ScmReadString(hServiceKey
,
5527 if (dwError
!= ERROR_SUCCESS
&& dwError
!= ERROR_FILE_NOT_FOUND
)
5530 *pcbBytesNeeded
= sizeof(SERVICE_DESCRIPTIONA
);
5531 if (dwError
== ERROR_SUCCESS
)
5532 *pcbBytesNeeded
+= (DWORD
)((wcslen(lpDescriptionW
) + 1) * sizeof(WCHAR
));
5534 if (cbBufSize
< *pcbBytesNeeded
)
5536 dwError
= ERROR_INSUFFICIENT_BUFFER
;
5540 if (dwError
== ERROR_SUCCESS
)
5542 lpStr
= (LPSTR
)(lpServiceDescription
+ 1);
5544 WideCharToMultiByte(CP_ACP
,
5549 (int)wcslen(lpDescriptionW
),
5552 lpServiceDescription
->lpDescription
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceDescription
);
5556 lpServiceDescription
->lpDescription
= NULL
;
5557 dwError
= ERROR_SUCCESS
;
5560 else if (dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
5562 LPSERVICE_FAILURE_ACTIONSA lpFailureActions
= (LPSERVICE_FAILURE_ACTIONSA
)lpBuffer
;
5565 /* Query value length */
5566 dwError
= RegQueryValueExW(hServiceKey
,
5572 if (dwError
!= ERROR_SUCCESS
&&
5573 dwError
!= ERROR_MORE_DATA
&&
5574 dwError
!= ERROR_FILE_NOT_FOUND
)
5579 dwRequiredSize
= (dwType
== REG_BINARY
) ? max(sizeof(SERVICE_FAILURE_ACTIONSA
), dwRequiredSize
)
5580 : sizeof(SERVICE_FAILURE_ACTIONSA
);
5582 /* Get the strings */
5583 ScmReadString(hServiceKey
,
5585 &lpFailureCommandW
);
5587 ScmReadString(hServiceKey
,
5591 if (lpRebootMessageW
)
5592 dwRequiredSize
+= (DWORD
)((wcslen(lpRebootMessageW
) + 1) * sizeof(WCHAR
));
5594 if (lpFailureCommandW
)
5595 dwRequiredSize
+= (DWORD
)((wcslen(lpFailureCommandW
) + 1) * sizeof(WCHAR
));
5597 if (cbBufSize
< dwRequiredSize
)
5599 *pcbBytesNeeded
= dwRequiredSize
;
5600 dwError
= ERROR_INSUFFICIENT_BUFFER
;
5604 /* Now we can fill the buffer */
5605 if (dwError
!= ERROR_FILE_NOT_FOUND
&& dwType
== REG_BINARY
)
5607 dwError
= RegQueryValueExW(hServiceKey
,
5611 (LPBYTE
)lpFailureActions
,
5613 if (dwError
!= ERROR_SUCCESS
&& dwError
!= ERROR_FILE_NOT_FOUND
)
5616 if (dwRequiredSize
< sizeof(SERVICE_FAILURE_ACTIONSA
))
5617 dwRequiredSize
= sizeof(SERVICE_FAILURE_ACTIONSA
);
5622 * The value of the error doesn't really matter, the only
5623 * important thing is that it must be != ERROR_SUCCESS .
5625 dwError
= ERROR_INVALID_DATA
;
5628 if (dwError
== ERROR_SUCCESS
)
5630 lpFailureActions
->cActions
= min(lpFailureActions
->cActions
, (dwRequiredSize
- sizeof(SERVICE_FAILURE_ACTIONSA
)) / sizeof(SC_ACTION
));
5632 /* Here lpFailureActions->lpsaActions contains an offset. The conversion is done by the caller. */
5633 lpFailureActions
->lpsaActions
= (lpFailureActions
->cActions
> 0 ? (LPSC_ACTION
)(ULONG_PTR
)sizeof(SERVICE_FAILURE_ACTIONSA
) : NULL
);
5635 lpStr
= (LPSTR
)((ULONG_PTR
)(lpFailureActions
+ 1) + lpFailureActions
->cActions
* sizeof(SC_ACTION
));
5639 lpFailureActions
->dwResetPeriod
= 0;
5640 lpFailureActions
->cActions
= 0;
5641 lpFailureActions
->lpsaActions
= NULL
;
5642 lpStr
= (LPSTR
)(lpFailureActions
+ 1);
5645 lpFailureActions
->lpRebootMsg
= NULL
;
5646 lpFailureActions
->lpCommand
= NULL
;
5648 if (lpRebootMessageW
)
5650 WideCharToMultiByte(CP_ACP
,
5655 (int)wcslen(lpRebootMessageW
),
5658 lpFailureActions
->lpRebootMsg
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpFailureActions
);
5659 lpStr
+= strlen(lpStr
) + 1;
5662 if (lpFailureCommandW
)
5664 WideCharToMultiByte(CP_ACP
,
5669 (int)wcslen(lpFailureCommandW
),
5672 lpFailureActions
->lpCommand
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpFailureActions
);
5673 /* lpStr += strlen(lpStr) + 1; */
5676 dwError
= ERROR_SUCCESS
;
5680 /* Unlock the service database */
5681 ScmUnlockDatabase();
5683 if (lpDescriptionW
!= NULL
)
5684 HeapFree(GetProcessHeap(), 0, lpDescriptionW
);
5686 if (lpRebootMessageW
!= NULL
)
5687 HeapFree(GetProcessHeap(), 0, lpRebootMessageW
);
5689 if (lpFailureCommandW
!= NULL
)
5690 HeapFree(GetProcessHeap(), 0, lpFailureCommandW
);
5692 if (hServiceKey
!= NULL
)
5693 RegCloseKey(hServiceKey
);
5695 DPRINT("RQueryServiceConfig2A() done (Error %lu)\n", dwError
);
5704 RQueryServiceConfig2W(
5705 SC_RPC_HANDLE hService
,
5709 LPBOUNDED_DWORD_8K pcbBytesNeeded
)
5711 DWORD dwError
= ERROR_SUCCESS
;
5712 PSERVICE_HANDLE hSvc
;
5713 PSERVICE lpService
= NULL
;
5714 HKEY hServiceKey
= NULL
;
5715 DWORD dwRequiredSize
= 0;
5717 LPWSTR lpDescription
= NULL
;
5718 LPWSTR lpRebootMessage
= NULL
;
5719 LPWSTR lpFailureCommand
= NULL
;
5721 DPRINT("RQueryServiceConfig2W() called\n");
5724 return ERROR_INVALID_ADDRESS
;
5727 return ERROR_SHUTDOWN_IN_PROGRESS
;
5729 if ((dwInfoLevel
< SERVICE_CONFIG_DESCRIPTION
) ||
5730 (dwInfoLevel
> SERVICE_CONFIG_FAILURE_ACTIONS
))
5732 return ERROR_INVALID_LEVEL
;
5735 hSvc
= ScmGetServiceFromHandle(hService
);
5738 DPRINT1("Invalid service handle!\n");
5739 return ERROR_INVALID_HANDLE
;
5742 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
5743 SERVICE_QUERY_CONFIG
))
5745 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
5746 return ERROR_ACCESS_DENIED
;
5749 lpService
= hSvc
->ServiceEntry
;
5750 if (lpService
== NULL
)
5752 DPRINT("lpService == NULL!\n");
5753 return ERROR_INVALID_HANDLE
;
5756 /* Lock the service database shared */
5757 ScmLockDatabaseShared();
5759 dwError
= ScmOpenServiceKey(lpService
->lpServiceName
,
5762 if (dwError
!= ERROR_SUCCESS
)
5765 if (dwInfoLevel
== SERVICE_CONFIG_DESCRIPTION
)
5767 LPSERVICE_DESCRIPTIONW lpServiceDescription
= (LPSERVICE_DESCRIPTIONW
)lpBuffer
;
5770 dwError
= ScmReadString(hServiceKey
,
5773 if (dwError
!= ERROR_SUCCESS
&& dwError
!= ERROR_FILE_NOT_FOUND
)
5776 *pcbBytesNeeded
= sizeof(SERVICE_DESCRIPTIONW
);
5777 if (dwError
== ERROR_SUCCESS
)
5778 *pcbBytesNeeded
+= (DWORD
)((wcslen(lpDescription
) + 1) * sizeof(WCHAR
));
5780 if (cbBufSize
< *pcbBytesNeeded
)
5782 dwError
= ERROR_INSUFFICIENT_BUFFER
;
5786 if (dwError
== ERROR_SUCCESS
)
5788 lpStr
= (LPWSTR
)(lpServiceDescription
+ 1);
5789 wcscpy(lpStr
, lpDescription
);
5790 lpServiceDescription
->lpDescription
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceDescription
);
5794 lpServiceDescription
->lpDescription
= NULL
;
5795 dwError
= ERROR_SUCCESS
;
5798 else if (dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
5800 LPSERVICE_FAILURE_ACTIONSW lpFailureActions
= (LPSERVICE_FAILURE_ACTIONSW
)lpBuffer
;
5801 LPWSTR lpStr
= NULL
;
5803 /* Query value length */
5804 dwError
= RegQueryValueExW(hServiceKey
,
5810 if (dwError
!= ERROR_SUCCESS
&&
5811 dwError
!= ERROR_MORE_DATA
&&
5812 dwError
!= ERROR_FILE_NOT_FOUND
)
5817 dwRequiredSize
= (dwType
== REG_BINARY
) ? max(sizeof(SERVICE_FAILURE_ACTIONSW
), dwRequiredSize
)
5818 : sizeof(SERVICE_FAILURE_ACTIONSW
);
5820 /* Get the strings */
5821 ScmReadString(hServiceKey
,
5825 ScmReadString(hServiceKey
,
5829 if (lpRebootMessage
)
5830 dwRequiredSize
+= (DWORD
)((wcslen(lpRebootMessage
) + 1) * sizeof(WCHAR
));
5832 if (lpFailureCommand
)
5833 dwRequiredSize
+= (DWORD
)((wcslen(lpFailureCommand
) + 1) * sizeof(WCHAR
));
5835 if (cbBufSize
< dwRequiredSize
)
5837 *pcbBytesNeeded
= dwRequiredSize
;
5838 dwError
= ERROR_INSUFFICIENT_BUFFER
;
5842 /* Now we can fill the buffer */
5843 if (dwError
!= ERROR_FILE_NOT_FOUND
&& dwType
== REG_BINARY
)
5845 dwError
= RegQueryValueExW(hServiceKey
,
5849 (LPBYTE
)lpFailureActions
,
5851 if (dwError
!= ERROR_SUCCESS
&& dwError
!= ERROR_FILE_NOT_FOUND
)
5854 if (dwRequiredSize
< sizeof(SERVICE_FAILURE_ACTIONSW
))
5855 dwRequiredSize
= sizeof(SERVICE_FAILURE_ACTIONSW
);
5860 * The value of the error doesn't really matter, the only
5861 * important thing is that it must be != ERROR_SUCCESS .
5863 dwError
= ERROR_INVALID_DATA
;
5866 if (dwError
== ERROR_SUCCESS
)
5868 lpFailureActions
->cActions
= min(lpFailureActions
->cActions
, (dwRequiredSize
- sizeof(SERVICE_FAILURE_ACTIONSW
)) / sizeof(SC_ACTION
));
5870 /* Here lpFailureActions->lpsaActions contains an offset. The conversion is done by the caller. */
5871 lpFailureActions
->lpsaActions
= (lpFailureActions
->cActions
> 0 ? (LPSC_ACTION
)(ULONG_PTR
)sizeof(SERVICE_FAILURE_ACTIONSW
) : NULL
);
5873 lpStr
= (LPWSTR
)((ULONG_PTR
)(lpFailureActions
+ 1) + lpFailureActions
->cActions
* sizeof(SC_ACTION
));
5877 lpFailureActions
->dwResetPeriod
= 0;
5878 lpFailureActions
->cActions
= 0;
5879 lpFailureActions
->lpsaActions
= NULL
;
5880 lpStr
= (LPWSTR
)(lpFailureActions
+ 1);
5883 lpFailureActions
->lpRebootMsg
= NULL
;
5884 lpFailureActions
->lpCommand
= NULL
;
5886 if (lpRebootMessage
)
5888 wcscpy(lpStr
, lpRebootMessage
);
5889 lpFailureActions
->lpRebootMsg
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpFailureActions
);
5890 lpStr
+= wcslen(lpStr
) + 1;
5893 if (lpFailureCommand
)
5895 wcscpy(lpStr
, lpFailureCommand
);
5896 lpFailureActions
->lpCommand
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpFailureActions
);
5897 /* lpStr += wcslen(lpStr) + 1; */
5900 dwError
= ERROR_SUCCESS
;
5904 /* Unlock the service database */
5905 ScmUnlockDatabase();
5907 if (lpDescription
!= NULL
)
5908 HeapFree(GetProcessHeap(), 0, lpDescription
);
5910 if (lpRebootMessage
!= NULL
)
5911 HeapFree(GetProcessHeap(), 0, lpRebootMessage
);
5913 if (lpFailureCommand
!= NULL
)
5914 HeapFree(GetProcessHeap(), 0, lpFailureCommand
);
5916 if (hServiceKey
!= NULL
)
5917 RegCloseKey(hServiceKey
);
5919 DPRINT("RQueryServiceConfig2W() done (Error %lu)\n", dwError
);
5928 RQueryServiceStatusEx(
5929 SC_RPC_HANDLE hService
,
5930 SC_STATUS_TYPE InfoLevel
,
5933 LPBOUNDED_DWORD_8K pcbBytesNeeded
)
5935 LPSERVICE_STATUS_PROCESS lpStatus
;
5936 PSERVICE_HANDLE hSvc
;
5939 DPRINT("RQueryServiceStatusEx() called\n");
5942 return ERROR_SHUTDOWN_IN_PROGRESS
;
5944 if (InfoLevel
!= SC_STATUS_PROCESS_INFO
)
5945 return ERROR_INVALID_LEVEL
;
5947 *pcbBytesNeeded
= sizeof(SERVICE_STATUS_PROCESS
);
5949 if (cbBufSize
< sizeof(SERVICE_STATUS_PROCESS
))
5950 return ERROR_INSUFFICIENT_BUFFER
;
5952 hSvc
= ScmGetServiceFromHandle(hService
);
5955 DPRINT1("Invalid service handle!\n");
5956 return ERROR_INVALID_HANDLE
;
5959 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
5960 SERVICE_QUERY_STATUS
))
5962 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
5963 return ERROR_ACCESS_DENIED
;
5966 lpService
= hSvc
->ServiceEntry
;
5967 if (lpService
== NULL
)
5969 DPRINT("lpService == NULL!\n");
5970 return ERROR_INVALID_HANDLE
;
5973 /* Lock the service database shared */
5974 ScmLockDatabaseShared();
5976 lpStatus
= (LPSERVICE_STATUS_PROCESS
)lpBuffer
;
5978 /* Return service status information */
5979 RtlCopyMemory(lpStatus
,
5981 sizeof(SERVICE_STATUS
));
5983 /* Copy the service process ID */
5984 if ((lpService
->Status
.dwCurrentState
== SERVICE_STOPPED
) || (lpService
->lpImage
== NULL
))
5985 lpStatus
->dwProcessId
= 0;
5987 lpStatus
->dwProcessId
= lpService
->lpImage
->dwProcessId
;
5989 lpStatus
->dwServiceFlags
= 0; /* FIXME */
5991 /* Unlock the service database */
5992 ScmUnlockDatabase();
5994 return ERROR_SUCCESS
;
6001 REnumServicesStatusExA(
6002 SC_RPC_HANDLE hSCManager
,
6003 SC_ENUM_TYPE InfoLevel
,
6004 DWORD dwServiceType
,
6005 DWORD dwServiceState
,
6008 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
6009 LPBOUNDED_DWORD_256K lpServicesReturned
,
6010 LPBOUNDED_DWORD_256K lpResumeIndex
,
6011 LPCSTR pszGroupName
)
6013 LPENUM_SERVICE_STATUS_PROCESSW lpStatusPtrW
= NULL
;
6014 LPENUM_SERVICE_STATUS_PROCESSW lpStatusPtrIncrW
;
6015 LPENUM_SERVICE_STATUS_PROCESSA lpStatusPtrA
= NULL
;
6016 LPWSTR lpStringPtrW
;
6018 LPWSTR pszGroupNameW
= NULL
;
6020 DWORD dwServiceCount
;
6022 DPRINT("REnumServicesStatusExA() called\n");
6024 if (pcbBytesNeeded
== NULL
|| lpServicesReturned
== NULL
)
6026 return ERROR_INVALID_ADDRESS
;
6031 pszGroupNameW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (strlen(pszGroupName
) + 1) * sizeof(WCHAR
));
6034 DPRINT("Failed to allocate buffer!\n");
6035 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
6039 MultiByteToWideChar(CP_ACP
,
6044 (int)(strlen(pszGroupName
) + 1));
6047 if ((cbBufSize
> 0) && (lpBuffer
))
6049 lpStatusPtrW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, cbBufSize
);
6052 DPRINT("Failed to allocate buffer!\n");
6053 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
6058 dwError
= REnumServicesStatusExW(hSCManager
,
6062 (LPBYTE
)lpStatusPtrW
,
6069 /* if no services were returned then we are Done */
6070 if (*lpServicesReturned
== 0)
6073 lpStatusPtrIncrW
= lpStatusPtrW
;
6074 lpStatusPtrA
= (LPENUM_SERVICE_STATUS_PROCESSA
)lpBuffer
;
6075 lpStringPtrA
= (LPSTR
)((ULONG_PTR
)lpBuffer
+
6076 *lpServicesReturned
* sizeof(ENUM_SERVICE_STATUS_PROCESSA
));
6077 lpStringPtrW
= (LPWSTR
)((ULONG_PTR
)lpStatusPtrW
+
6078 *lpServicesReturned
* sizeof(ENUM_SERVICE_STATUS_PROCESSW
));
6080 for (dwServiceCount
= 0; dwServiceCount
< *lpServicesReturned
; dwServiceCount
++)
6082 /* Copy the service name */
6083 WideCharToMultiByte(CP_ACP
,
6088 (int)wcslen(lpStringPtrW
),
6092 lpStatusPtrA
->lpServiceName
= (LPSTR
)((ULONG_PTR
)lpStringPtrA
- (ULONG_PTR
)lpBuffer
);
6093 lpStringPtrA
+= wcslen(lpStringPtrW
) + 1;
6094 lpStringPtrW
+= wcslen(lpStringPtrW
) + 1;
6096 /* Copy the display name */
6097 WideCharToMultiByte(CP_ACP
,
6102 (int)wcslen(lpStringPtrW
),
6106 lpStatusPtrA
->lpDisplayName
= (LPSTR
)((ULONG_PTR
)lpStringPtrA
- (ULONG_PTR
)lpBuffer
);
6107 lpStringPtrA
+= wcslen(lpStringPtrW
) + 1;
6108 lpStringPtrW
+= wcslen(lpStringPtrW
) + 1;
6110 /* Copy the status information */
6111 memcpy(&lpStatusPtrA
->ServiceStatusProcess
,
6112 &lpStatusPtrIncrW
->ServiceStatusProcess
,
6113 sizeof(SERVICE_STATUS
));
6115 /* Copy the service process ID */
6116 lpStatusPtrA
->ServiceStatusProcess
.dwProcessId
= lpStatusPtrIncrW
->ServiceStatusProcess
.dwProcessId
;
6118 lpStatusPtrA
->ServiceStatusProcess
.dwServiceFlags
= 0; /* FIXME */
6126 HeapFree(GetProcessHeap(), 0, pszGroupNameW
);
6129 HeapFree(GetProcessHeap(), 0, lpStatusPtrW
);
6131 DPRINT("REnumServicesStatusExA() done (Error %lu)\n", dwError
);
6140 REnumServicesStatusExW(
6141 SC_RPC_HANDLE hSCManager
,
6142 SC_ENUM_TYPE InfoLevel
,
6143 DWORD dwServiceType
,
6144 DWORD dwServiceState
,
6147 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
6148 LPBOUNDED_DWORD_256K lpServicesReturned
,
6149 LPBOUNDED_DWORD_256K lpResumeIndex
,
6150 LPCWSTR pszGroupName
)
6152 PMANAGER_HANDLE hManager
;
6154 DWORD dwError
= ERROR_SUCCESS
;
6155 PLIST_ENTRY ServiceEntry
;
6156 PSERVICE CurrentService
;
6158 DWORD dwRequiredSize
;
6159 DWORD dwServiceCount
;
6161 DWORD dwLastResumeCount
= 0;
6162 LPENUM_SERVICE_STATUS_PROCESSW lpStatusPtr
;
6165 DPRINT("REnumServicesStatusExW() called\n");
6168 return ERROR_SHUTDOWN_IN_PROGRESS
;
6170 if (InfoLevel
!= SC_ENUM_PROCESS_INFO
)
6171 return ERROR_INVALID_LEVEL
;
6173 hManager
= ScmGetServiceManagerFromHandle(hSCManager
);
6174 if (hManager
== NULL
)
6176 DPRINT1("Invalid service manager handle!\n");
6177 return ERROR_INVALID_HANDLE
;
6180 if (pcbBytesNeeded
== NULL
|| lpServicesReturned
== NULL
)
6182 return ERROR_INVALID_ADDRESS
;
6185 *pcbBytesNeeded
= 0;
6186 *lpServicesReturned
= 0;
6188 if ((dwServiceType
== 0) ||
6189 ((dwServiceType
& ~SERVICE_TYPE_ALL
) != 0))
6191 DPRINT("Not a valid Service Type!\n");
6192 return ERROR_INVALID_PARAMETER
;
6195 if ((dwServiceState
== 0) ||
6196 ((dwServiceState
& ~SERVICE_STATE_ALL
) != 0))
6198 DPRINT("Not a valid Service State!\n");
6199 return ERROR_INVALID_PARAMETER
;
6202 /* Check access rights */
6203 if (!RtlAreAllAccessesGranted(hManager
->Handle
.DesiredAccess
,
6204 SC_MANAGER_ENUMERATE_SERVICE
))
6206 DPRINT("Insufficient access rights! 0x%lx\n",
6207 hManager
->Handle
.DesiredAccess
);
6208 return ERROR_ACCESS_DENIED
;
6212 dwLastResumeCount
= *lpResumeIndex
;
6214 /* Lock the service database shared */
6215 ScmLockDatabaseShared();
6217 lpService
= ScmGetServiceEntryByResumeCount(dwLastResumeCount
);
6218 if (lpService
== NULL
)
6220 dwError
= ERROR_SUCCESS
;
6227 for (ServiceEntry
= &lpService
->ServiceListEntry
;
6228 ServiceEntry
!= &ServiceListHead
;
6229 ServiceEntry
= ServiceEntry
->Flink
)
6231 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
6235 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
6238 dwState
= SERVICE_ACTIVE
;
6239 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
6240 dwState
= SERVICE_INACTIVE
;
6242 if ((dwState
& dwServiceState
) == 0)
6247 if (*pszGroupName
== 0)
6249 if (CurrentService
->lpGroup
!= NULL
)
6254 if ((CurrentService
->lpGroup
== NULL
) ||
6255 _wcsicmp(pszGroupName
, CurrentService
->lpGroup
->lpGroupName
) != 0)
6260 dwSize
= sizeof(ENUM_SERVICE_STATUS_PROCESSW
) +
6261 (DWORD
)((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
6262 (DWORD
)((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
6264 if (dwRequiredSize
+ dwSize
<= cbBufSize
)
6266 DPRINT("Service name: %S fit\n", CurrentService
->lpServiceName
);
6267 dwRequiredSize
+= dwSize
;
6269 dwLastResumeCount
= CurrentService
->dwResumeCount
;
6273 DPRINT("Service name: %S no fit\n", CurrentService
->lpServiceName
);
6279 DPRINT("dwRequiredSize: %lu\n", dwRequiredSize
);
6280 DPRINT("dwServiceCount: %lu\n", dwServiceCount
);
6283 ServiceEntry
!= &ServiceListHead
;
6284 ServiceEntry
= ServiceEntry
->Flink
)
6286 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
6290 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
6293 dwState
= SERVICE_ACTIVE
;
6294 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
6295 dwState
= SERVICE_INACTIVE
;
6297 if ((dwState
& dwServiceState
) == 0)
6302 if (*pszGroupName
== 0)
6304 if (CurrentService
->lpGroup
!= NULL
)
6309 if ((CurrentService
->lpGroup
== NULL
) ||
6310 _wcsicmp(pszGroupName
, CurrentService
->lpGroup
->lpGroupName
) != 0)
6315 dwRequiredSize
+= (sizeof(ENUM_SERVICE_STATUS_PROCESSW
) +
6316 (DWORD
)((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
6317 (DWORD
)((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
)));
6319 dwError
= ERROR_MORE_DATA
;
6322 DPRINT("*pcbBytesNeeded: %lu\n", dwRequiredSize
);
6325 *lpResumeIndex
= dwLastResumeCount
;
6327 *lpServicesReturned
= dwServiceCount
;
6328 *pcbBytesNeeded
= dwRequiredSize
;
6330 /* If there was no services that matched */
6331 if ((!dwServiceCount
) && (dwError
!= ERROR_MORE_DATA
))
6333 dwError
= ERROR_SERVICE_DOES_NOT_EXIST
;
6337 lpStatusPtr
= (LPENUM_SERVICE_STATUS_PROCESSW
)lpBuffer
;
6338 lpStringPtr
= (LPWSTR
)((ULONG_PTR
)lpBuffer
+
6339 dwServiceCount
* sizeof(ENUM_SERVICE_STATUS_PROCESSW
));
6342 for (ServiceEntry
= &lpService
->ServiceListEntry
;
6343 ServiceEntry
!= &ServiceListHead
;
6344 ServiceEntry
= ServiceEntry
->Flink
)
6346 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
6350 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
6353 dwState
= SERVICE_ACTIVE
;
6354 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
6355 dwState
= SERVICE_INACTIVE
;
6357 if ((dwState
& dwServiceState
) == 0)
6362 if (*pszGroupName
== 0)
6364 if (CurrentService
->lpGroup
!= NULL
)
6369 if ((CurrentService
->lpGroup
== NULL
) ||
6370 _wcsicmp(pszGroupName
, CurrentService
->lpGroup
->lpGroupName
) != 0)
6375 dwSize
= sizeof(ENUM_SERVICE_STATUS_PROCESSW
) +
6376 (DWORD
)((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
6377 (DWORD
)((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
6379 if (dwRequiredSize
+ dwSize
<= cbBufSize
)
6381 /* Copy the service name */
6383 CurrentService
->lpServiceName
);
6384 lpStatusPtr
->lpServiceName
= (LPWSTR
)((ULONG_PTR
)lpStringPtr
- (ULONG_PTR
)lpBuffer
);
6385 lpStringPtr
+= (wcslen(CurrentService
->lpServiceName
) + 1);
6387 /* Copy the display name */
6389 CurrentService
->lpDisplayName
);
6390 lpStatusPtr
->lpDisplayName
= (LPWSTR
)((ULONG_PTR
)lpStringPtr
- (ULONG_PTR
)lpBuffer
);
6391 lpStringPtr
+= (wcslen(CurrentService
->lpDisplayName
) + 1);
6393 /* Copy the status information */
6394 memcpy(&lpStatusPtr
->ServiceStatusProcess
,
6395 &CurrentService
->Status
,
6396 sizeof(SERVICE_STATUS
));
6398 /* Copy the service process ID */
6399 if ((CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
) || (CurrentService
->lpImage
== NULL
))
6400 lpStatusPtr
->ServiceStatusProcess
.dwProcessId
= 0;
6402 lpStatusPtr
->ServiceStatusProcess
.dwProcessId
= CurrentService
->lpImage
->dwProcessId
;
6404 lpStatusPtr
->ServiceStatusProcess
.dwServiceFlags
= 0; /* FIXME */
6407 dwRequiredSize
+= dwSize
;
6417 *pcbBytesNeeded
= 0;
6423 /* Unlock the service database */
6424 ScmUnlockDatabase();
6426 DPRINT("REnumServicesStatusExW() done (Error %lu)\n", dwError
);
6436 handle_t BindingHandle
) /* FIXME */
6439 return ERROR_CALL_NOT_IMPLEMENTED
;
6446 RCreateServiceWOW64A(
6447 handle_t BindingHandle
,
6448 LPSTR lpServiceName
,
6449 LPSTR lpDisplayName
,
6450 DWORD dwDesiredAccess
,
6451 DWORD dwServiceType
,
6453 DWORD dwErrorControl
,
6454 LPSTR lpBinaryPathName
,
6455 LPSTR lpLoadOrderGroup
,
6457 LPBYTE lpDependencies
,
6459 LPSTR lpServiceStartName
,
6462 LPSC_RPC_HANDLE lpServiceHandle
)
6465 return ERROR_CALL_NOT_IMPLEMENTED
;
6472 RCreateServiceWOW64W(
6473 handle_t BindingHandle
,
6474 LPWSTR lpServiceName
,
6475 LPWSTR lpDisplayName
,
6476 DWORD dwDesiredAccess
,
6477 DWORD dwServiceType
,
6479 DWORD dwErrorControl
,
6480 LPWSTR lpBinaryPathName
,
6481 LPWSTR lpLoadOrderGroup
,
6483 LPBYTE lpDependencies
,
6485 LPWSTR lpServiceStartName
,
6488 LPSC_RPC_HANDLE lpServiceHandle
)
6491 return ERROR_CALL_NOT_IMPLEMENTED
;
6498 RQueryServiceTagInfo(
6499 handle_t BindingHandle
) /* FIXME */
6502 return ERROR_CALL_NOT_IMPLEMENTED
;
6509 RNotifyServiceStatusChange(
6510 SC_RPC_HANDLE hService
,
6511 SC_RPC_NOTIFY_PARAMS NotifyParams
,
6512 GUID
*pClientProcessGuid
,
6513 GUID
*pSCMProcessGuid
,
6514 PBOOL pfCreateRemoteQueue
,
6515 LPSC_NOTIFY_RPC_HANDLE phNotify
)
6518 return ERROR_CALL_NOT_IMPLEMENTED
;
6526 SC_NOTIFY_RPC_HANDLE hNotify
,
6527 PSC_RPC_NOTIFY_PARAMS_LIST
*ppNotifyParams
)
6530 return ERROR_CALL_NOT_IMPLEMENTED
;
6538 LPSC_NOTIFY_RPC_HANDLE phNotify
,
6542 return ERROR_CALL_NOT_IMPLEMENTED
;
6550 SC_RPC_HANDLE hService
,
6555 return ERROR_CALL_NOT_IMPLEMENTED
;
6563 SC_RPC_HANDLE hService
,
6568 return ERROR_CALL_NOT_IMPLEMENTED
;
6576 handle_t BindingHandle
) /* FIXME */
6579 return ERROR_CALL_NOT_IMPLEMENTED
;
6586 RValidatePnPService(
6587 handle_t BindingHandle
) /* FIXME */
6590 return ERROR_CALL_NOT_IMPLEMENTED
;
6597 ROpenServiceStatusHandle(
6598 handle_t BindingHandle
) /* FIXME */
6601 return ERROR_CALL_NOT_IMPLEMENTED
;
6609 handle_t BindingHandle
) /* FIXME */
6612 return ERROR_CALL_NOT_IMPLEMENTED
;
6616 void __RPC_FAR
* __RPC_USER
midl_user_allocate(SIZE_T len
)
6618 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
);
6622 void __RPC_USER
midl_user_free(void __RPC_FAR
* ptr
)
6624 HeapFree(GetProcessHeap(), 0, ptr
);
6628 void __RPC_USER
SC_RPC_HANDLE_rundown(SC_RPC_HANDLE hSCObject
)
6630 /* Close the handle */
6631 RCloseServiceHandle(&hSCObject
);
6635 void __RPC_USER
SC_RPC_LOCK_rundown(SC_RPC_LOCK Lock
)
6637 /* Unlock the database */
6638 RUnlockServiceDatabase(&Lock
);
6642 void __RPC_USER
SC_NOTIFY_RPC_HANDLE_rundown(SC_NOTIFY_RPC_HANDLE hNotify
)