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);
469 DPRINT("Error during a call to ExpandEnvironmentStringsW()\n");
470 return ERROR_INVALID_ENVIRONMENT
;
473 /* Allocate memory, since the size is known now */
474 Expanded
= HeapAlloc(GetProcessHeap(),
476 (BufferSize
+ 1) * sizeof(WCHAR
));
479 DPRINT("Error allocating memory for boot driver name!\n");
480 return ERROR_NOT_ENOUGH_MEMORY
;
484 if (ExpandEnvironmentStringsW(L
"%SystemRoot%\\", Expanded
, BufferSize
) >
487 DPRINT("Error during a call to ExpandEnvironmentStringsW()\n");
488 HeapFree(GetProcessHeap(), 0, Expanded
);
489 return ERROR_NOT_ENOUGH_MEMORY
;
492 /* Convert to NT-style path */
493 if (!RtlDosPathNameToNtPathName_U(Expanded
, &NtPathName
, NULL
, NULL
))
495 DPRINT("Error during a call to RtlDosPathNameToNtPathName_U()\n");
496 return ERROR_INVALID_ENVIRONMENT
;
499 DPRINT("Converted to NT-style %wZ\n", &NtPathName
);
501 /* No need to keep the dos-path anymore */
502 HeapFree(GetProcessHeap(), 0, Expanded
);
504 /* Copy it to the allocated place */
505 Expanded
= HeapAlloc(GetProcessHeap(),
507 NtPathName
.Length
+ sizeof(UNICODE_NULL
));
510 DPRINT("Error allocating memory for boot driver name!\n");
511 RtlFreeUnicodeString(&NtPathName
);
512 return ERROR_NOT_ENOUGH_MEMORY
;
515 ExpandedLen
= NtPathName
.Length
/ sizeof(WCHAR
);
516 wcsncpy(Expanded
, NtPathName
.Buffer
, ExpandedLen
);
517 Expanded
[ExpandedLen
] = UNICODE_NULL
;
518 RtlFreeUnicodeString(&NtPathName
);
520 if (ServiceNameLen
> ExpandedLen
&&
521 !_wcsnicmp(Expanded
, CanonName
, ExpandedLen
))
523 HeapFree(GetProcessHeap(), 0, Expanded
);
525 /* Only \SystemRoot\ is missing */
526 *RelativeName
= HeapAlloc(GetProcessHeap(),
528 (ServiceNameLen
- ExpandedLen
) * sizeof(WCHAR
) + 13*sizeof(WCHAR
));
529 if (*RelativeName
== NULL
)
531 DPRINT("Error allocating memory for boot driver name!\n");
532 return ERROR_NOT_ENOUGH_MEMORY
;
535 wcscpy(*RelativeName
, L
"\\SystemRoot\\");
536 wcscat(*RelativeName
, CanonName
+ ExpandedLen
);
538 return ERROR_SUCCESS
;
541 /* No longer need this */
542 HeapFree(GetProcessHeap(), 0, Expanded
);
544 /* The most complex case starts here */
545 RtlInitUnicodeString(&SystemRoot
, L
"\\SystemRoot");
546 InitializeObjectAttributes(&ObjectAttributes
,
548 OBJ_CASE_INSENSITIVE
,
552 /* Open this symlink */
553 Status
= NtOpenSymbolicLinkObject(&SymbolicLinkHandle
, SYMBOLIC_LINK_QUERY
, &ObjectAttributes
);
554 if (NT_SUCCESS(Status
))
556 DPRINT("Opened symbolic link object\n");
558 RtlInitEmptyUnicodeString(&LinkTarget
, NULL
, 0);
559 Status
= NtQuerySymbolicLinkObject(SymbolicLinkHandle
, &LinkTarget
, &BufferSize
);
560 if (NT_SUCCESS(Status
) || Status
== STATUS_BUFFER_TOO_SMALL
)
562 /* Check if required buffer size is sane */
563 if (BufferSize
> UNICODE_STRING_MAX_BYTES
- sizeof(UNICODE_NULL
))
565 DPRINT("Too large buffer required\n");
567 NtClose(SymbolicLinkHandle
);
568 return ERROR_NOT_ENOUGH_MEMORY
;
571 /* Alloc the string */
572 LinkTarget
.Length
= (USHORT
)BufferSize
;
573 LinkTarget
.MaximumLength
= LinkTarget
.Length
+ sizeof(UNICODE_NULL
);
574 LinkTarget
.Buffer
= HeapAlloc(GetProcessHeap(),
576 LinkTarget
.MaximumLength
);
577 if (!LinkTarget
.Buffer
)
579 DPRINT("Unable to alloc buffer\n");
580 NtClose(SymbolicLinkHandle
);
581 return ERROR_NOT_ENOUGH_MEMORY
;
584 /* Do a real query now */
585 Status
= NtQuerySymbolicLinkObject(SymbolicLinkHandle
, &LinkTarget
, &BufferSize
);
586 NtClose(SymbolicLinkHandle
);
587 if (NT_SUCCESS(Status
))
589 DPRINT("LinkTarget: %wZ\n", &LinkTarget
);
591 ExpandedLen
= LinkTarget
.Length
/ sizeof(WCHAR
);
592 if ((ServiceNameLen
> ExpandedLen
) &&
593 !_wcsnicmp(LinkTarget
.Buffer
, CanonName
, ExpandedLen
))
595 *RelativeName
= HeapAlloc(GetProcessHeap(),
597 (ServiceNameLen
- ExpandedLen
) * sizeof(WCHAR
) + 13*sizeof(WCHAR
));
599 if (*RelativeName
== NULL
)
601 DPRINT("Unable to alloc buffer\n");
602 return ERROR_NOT_ENOUGH_MEMORY
;
605 /* Copy it over, substituting the first part
607 wcscpy(*RelativeName
, L
"\\SystemRoot\\");
608 wcscat(*RelativeName
, CanonName
+ExpandedLen
+1);
611 return ERROR_SUCCESS
;
615 return ERROR_INVALID_PARAMETER
;
620 DPRINT("Error, Status = %08X\n", Status
);
621 return ERROR_INVALID_PARAMETER
;
626 DPRINT("Error, Status = %08X\n", Status
);
627 NtClose(SymbolicLinkHandle
);
628 return ERROR_INVALID_PARAMETER
;
634 DPRINT("Error, Status = %08X\n", Status
);
635 return ERROR_INVALID_PARAMETER
;
641 ScmCanonDriverImagePath(DWORD dwStartType
,
642 const wchar_t *lpServiceName
,
643 wchar_t **lpCanonName
)
646 SIZE_T ServiceNameLen
;
647 UNICODE_STRING NtServiceName
;
649 const WCHAR
*SourceName
= lpServiceName
;
651 /* Calculate the length of the service's name */
652 ServiceNameLen
= wcslen(lpServiceName
);
654 /* 12 is wcslen(L"\\SystemRoot\\") */
655 if (ServiceNameLen
> 12 &&
656 !_wcsnicmp(L
"\\SystemRoot\\", lpServiceName
, 12))
658 /* SystemRoot prefix is already included */
659 *lpCanonName
= HeapAlloc(GetProcessHeap(),
661 (ServiceNameLen
+ 1) * sizeof(WCHAR
));
663 if (*lpCanonName
== NULL
)
665 DPRINT("Error allocating memory for canonized service name!\n");
666 return ERROR_NOT_ENOUGH_MEMORY
;
669 /* If it's a boot-time driver, it must be systemroot relative */
670 if (dwStartType
== SERVICE_BOOT_START
)
674 wcscpy(*lpCanonName
, SourceName
);
676 DPRINT("Canonicalized name %S\n", *lpCanonName
);
680 /* Check if it has %SystemRoot% (len=13) */
681 if (ServiceNameLen
> 13 &&
682 !_wcsnicmp(L
"%SystemRoot%\\", lpServiceName
, 13))
684 /* Substitute %SystemRoot% with \\SystemRoot\\ */
685 *lpCanonName
= HeapAlloc(GetProcessHeap(),
687 (ServiceNameLen
+ 1) * sizeof(WCHAR
));
689 if (*lpCanonName
== NULL
)
691 DPRINT("Error allocating memory for canonized service name!\n");
692 return ERROR_NOT_ENOUGH_MEMORY
;
695 /* If it's a boot-time driver, it must be systemroot relative */
696 if (dwStartType
== SERVICE_BOOT_START
)
697 wcscpy(*lpCanonName
, L
"\\SystemRoot\\");
699 wcscat(*lpCanonName
, lpServiceName
+ 13);
701 DPRINT("Canonicalized name %S\n", *lpCanonName
);
705 /* Check if it's a relative path name */
706 if (lpServiceName
[0] != L
'\\' && lpServiceName
[1] != L
':')
708 *lpCanonName
= HeapAlloc(GetProcessHeap(),
710 (ServiceNameLen
+ 1) * sizeof(WCHAR
));
712 if (*lpCanonName
== NULL
)
714 DPRINT("Error allocating memory for canonized service name!\n");
715 return ERROR_NOT_ENOUGH_MEMORY
;
718 /* Just copy it over without changing */
719 wcscpy(*lpCanonName
, lpServiceName
);
724 /* It seems to be a DOS path, convert it */
725 if (!RtlDosPathNameToNtPathName_U(lpServiceName
, &NtServiceName
, NULL
, NULL
))
727 DPRINT("RtlDosPathNameToNtPathName_U() failed!\n");
728 return ERROR_INVALID_PARAMETER
;
731 *lpCanonName
= HeapAlloc(GetProcessHeap(),
733 NtServiceName
.Length
+ sizeof(WCHAR
));
735 if (*lpCanonName
== NULL
)
737 DPRINT("Error allocating memory for canonized service name!\n");
738 RtlFreeUnicodeString(&NtServiceName
);
739 return ERROR_NOT_ENOUGH_MEMORY
;
742 /* Copy the string */
743 wcsncpy(*lpCanonName
, NtServiceName
.Buffer
, NtServiceName
.Length
/ sizeof(WCHAR
));
745 /* The unicode string is not needed anymore */
746 RtlFreeUnicodeString(&NtServiceName
);
748 if (dwStartType
!= SERVICE_BOOT_START
)
750 DPRINT("Canonicalized name %S\n", *lpCanonName
);
754 /* The service is boot-started, so must be relative */
755 Result
= ScmConvertToBootPathName(*lpCanonName
, &RelativeName
);
758 /* There is a problem, free name and return */
759 HeapFree(GetProcessHeap(), 0, *lpCanonName
);
760 DPRINT("Error converting named!\n");
764 ASSERT(RelativeName
);
766 /* Copy that string */
767 wcscpy(*lpCanonName
, RelativeName
+ 12);
769 /* Free the allocated buffer */
770 HeapFree(GetProcessHeap(), 0, RelativeName
);
772 DPRINT("Canonicalized name %S\n", *lpCanonName
);
779 /* Internal recursive function */
780 /* Need to search for every dependency on every service */
782 Int_EnumDependentServicesW(HKEY hServicesKey
,
784 DWORD dwServiceState
,
785 PSERVICE
*lpServices
,
786 LPDWORD pcbBytesNeeded
,
787 LPDWORD lpServicesReturned
)
789 DWORD dwError
= ERROR_SUCCESS
;
790 WCHAR szNameBuf
[MAX_PATH
];
791 WCHAR szValueBuf
[MAX_PATH
];
792 WCHAR
*lpszNameBuf
= szNameBuf
;
793 WCHAR
*lpszValueBuf
= szValueBuf
;
797 PSERVICE lpCurrentService
;
798 HKEY hServiceEnumKey
;
799 DWORD dwCurrentServiceState
= SERVICE_ACTIVE
;
800 DWORD dwDependServiceStrPtr
= 0;
801 DWORD dwRequiredSize
= 0;
803 /* Get the number of service keys */
804 dwError
= RegQueryInfoKeyW(hServicesKey
,
816 if (dwError
!= ERROR_SUCCESS
)
818 DPRINT("ERROR! Unable to get number of services keys.\n");
822 /* Iterate the service keys to see if another service depends on the this service */
823 for (dwIteration
= 0; dwIteration
< dwNumSubKeys
; dwIteration
++)
826 dwError
= RegEnumKeyExW(hServicesKey
,
834 if (dwError
!= ERROR_SUCCESS
)
837 /* Open the Service key */
838 dwError
= RegOpenKeyExW(hServicesKey
,
843 if (dwError
!= ERROR_SUCCESS
)
846 dwSize
= MAX_PATH
* sizeof(WCHAR
);
848 /* Check for the DependOnService Value */
849 dwError
= RegQueryValueExW(hServiceEnumKey
,
853 (LPBYTE
)lpszValueBuf
,
856 /* FIXME: Handle load order. */
858 /* If the service found has a DependOnService value */
859 if (dwError
== ERROR_SUCCESS
)
861 dwDependServiceStrPtr
= 0;
863 /* Can be more than one Dependencies in the DependOnService string */
864 while (wcslen(lpszValueBuf
+ dwDependServiceStrPtr
) > 0)
866 if (_wcsicmp(lpszValueBuf
+ dwDependServiceStrPtr
, lpService
->lpServiceName
) == 0)
868 /* Get the current enumed service pointer */
869 lpCurrentService
= ScmGetServiceEntryByName(lpszNameBuf
);
871 /* Check for valid Service */
872 if (!lpCurrentService
)
874 /* This should never happen! */
875 DPRINT("This should not happen at this point, report to Developer\n");
876 return ERROR_NOT_FOUND
;
879 /* Determine state the service is in */
880 if (lpCurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
881 dwCurrentServiceState
= SERVICE_INACTIVE
;
883 /* If the ServiceState matches that requested or searching for SERVICE_STATE_ALL */
884 if ((dwCurrentServiceState
== dwServiceState
) ||
885 (dwServiceState
== SERVICE_STATE_ALL
))
887 /* Calculate the required size */
888 dwRequiredSize
+= sizeof(SERVICE_STATUS
);
889 dwRequiredSize
+= (DWORD
)((wcslen(lpCurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
));
890 dwRequiredSize
+= (DWORD
)((wcslen(lpCurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
892 /* Add the size for service name and display name pointers */
893 dwRequiredSize
+= (2 * sizeof(PVOID
));
895 /* increase the BytesNeeded size */
896 *pcbBytesNeeded
= *pcbBytesNeeded
+ dwRequiredSize
;
898 /* Don't fill callers buffer yet, as MSDN read that the last service with dependency
901 /* Recursive call to check for its dependencies */
902 Int_EnumDependentServicesW(hServicesKey
,
909 /* If the lpServices is valid set the service pointer */
911 lpServices
[*lpServicesReturned
] = lpCurrentService
;
913 *lpServicesReturned
= *lpServicesReturned
+ 1;
917 dwDependServiceStrPtr
+= (DWORD
)(wcslen(lpszValueBuf
+ dwDependServiceStrPtr
) + 1);
920 else if (*pcbBytesNeeded
)
922 dwError
= ERROR_SUCCESS
;
925 RegCloseKey(hServiceEnumKey
);
936 LPSC_RPC_HANDLE hSCObject
)
938 PMANAGER_HANDLE hManager
;
939 PSERVICE_HANDLE hService
;
943 DWORD pcbBytesNeeded
= 0;
944 DWORD dwServicesReturned
= 0;
946 DPRINT("RCloseServiceHandle() called\n");
948 DPRINT("hSCObject = %p\n", *hSCObject
);
951 return ERROR_INVALID_HANDLE
;
953 hManager
= ScmGetServiceManagerFromHandle(*hSCObject
);
954 hService
= ScmGetServiceFromHandle(*hSCObject
);
956 if (hManager
!= NULL
)
958 DPRINT("Found manager handle\n");
960 /* Make sure we don't access stale memory if someone tries to use this handle again. */
961 hManager
->Handle
.Tag
= INVALID_TAG
;
963 HeapFree(GetProcessHeap(), 0, hManager
);
968 DPRINT("RCloseServiceHandle() done\n");
969 return ERROR_SUCCESS
;
971 else if (hService
!= NULL
)
973 DPRINT("Found service handle\n");
975 /* Lock the service database exclusively */
976 ScmLockDatabaseExclusive();
978 /* Get the pointer to the service record */
979 lpService
= hService
->ServiceEntry
;
981 /* Make sure we don't access stale memory if someone tries to use this handle again. */
982 hService
->Handle
.Tag
= INVALID_TAG
;
984 /* Free the handle */
985 HeapFree(GetProcessHeap(), 0, hService
);
988 ASSERT(lpService
->dwRefCount
> 0);
990 lpService
->dwRefCount
--;
991 DPRINT("CloseServiceHandle - lpService->dwRefCount %u\n",
992 lpService
->dwRefCount
);
994 if (lpService
->dwRefCount
== 0)
996 /* If this service has been marked for deletion */
997 if (lpService
->bDeleted
&&
998 lpService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
1000 /* Open the Services Reg key */
1001 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
1002 L
"System\\CurrentControlSet\\Services",
1004 KEY_SET_VALUE
| KEY_READ
,
1006 if (dwError
!= ERROR_SUCCESS
)
1008 DPRINT("Failed to open services key\n");
1009 ScmUnlockDatabase();
1013 /* Call the internal function with NULL, just to get bytes we need */
1014 Int_EnumDependentServicesW(hServicesKey
,
1019 &dwServicesReturned
);
1021 /* if pcbBytesNeeded returned a value then there are services running that are dependent on this service */
1024 DPRINT("Deletion failed due to running dependencies.\n");
1025 RegCloseKey(hServicesKey
);
1026 ScmUnlockDatabase();
1027 return ERROR_SUCCESS
;
1030 /* There are no references and no running dependencies,
1031 it is now safe to delete the service */
1033 /* Delete the Service Key */
1034 dwError
= ScmDeleteRegKey(hServicesKey
,
1035 lpService
->lpServiceName
);
1037 RegCloseKey(hServicesKey
);
1039 if (dwError
!= ERROR_SUCCESS
)
1041 DPRINT("Failed to Delete the Service Registry key\n");
1042 ScmUnlockDatabase();
1046 /* Delete the Service */
1047 ScmDeleteServiceRecord(lpService
);
1051 ScmUnlockDatabase();
1055 DPRINT("RCloseServiceHandle() done\n");
1056 return ERROR_SUCCESS
;
1059 DPRINT("Invalid handle tag (Tag %lx)\n", hManager
->Handle
.Tag
);
1061 return ERROR_INVALID_HANDLE
;
1069 SC_RPC_HANDLE hService
,
1071 LPSERVICE_STATUS lpServiceStatus
)
1073 PSERVICE_HANDLE hSvc
;
1075 ACCESS_MASK DesiredAccess
;
1076 DWORD dwError
= ERROR_SUCCESS
;
1077 DWORD pcbBytesNeeded
= 0;
1078 DWORD dwServicesReturned
= 0;
1079 DWORD dwControlsAccepted
;
1080 DWORD dwCurrentState
;
1081 HKEY hServicesKey
= NULL
;
1082 LPCWSTR lpLogStrings
[2];
1083 WCHAR szLogBuffer
[80];
1086 DPRINT("RControlService() called\n");
1089 return ERROR_SHUTDOWN_IN_PROGRESS
;
1091 /* Check the service handle */
1092 hSvc
= ScmGetServiceFromHandle(hService
);
1095 DPRINT1("Invalid service handle!\n");
1096 return ERROR_INVALID_HANDLE
;
1099 /* Check the service entry point */
1100 lpService
= hSvc
->ServiceEntry
;
1101 if (lpService
== NULL
)
1103 DPRINT1("lpService == NULL!\n");
1104 return ERROR_INVALID_HANDLE
;
1107 /* Check access rights */
1110 case SERVICE_CONTROL_STOP
:
1111 DesiredAccess
= SERVICE_STOP
;
1114 case SERVICE_CONTROL_PAUSE
:
1115 case SERVICE_CONTROL_CONTINUE
:
1116 case SERVICE_CONTROL_PARAMCHANGE
:
1117 case SERVICE_CONTROL_NETBINDADD
:
1118 case SERVICE_CONTROL_NETBINDREMOVE
:
1119 case SERVICE_CONTROL_NETBINDENABLE
:
1120 case SERVICE_CONTROL_NETBINDDISABLE
:
1121 DesiredAccess
= SERVICE_PAUSE_CONTINUE
;
1124 case SERVICE_CONTROL_INTERROGATE
:
1125 DesiredAccess
= SERVICE_INTERROGATE
;
1129 if (dwControl
>= 128 && dwControl
<= 255)
1130 DesiredAccess
= SERVICE_USER_DEFINED_CONTROL
;
1132 return ERROR_INVALID_PARAMETER
;
1136 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
1138 return ERROR_ACCESS_DENIED
;
1140 /* Return the current service status information */
1141 RtlCopyMemory(lpServiceStatus
,
1143 sizeof(SERVICE_STATUS
));
1145 if (dwControl
== SERVICE_CONTROL_STOP
)
1147 /* Check if the service has dependencies running as windows
1148 doesn't stop a service that does */
1150 /* Open the Services Reg key */
1151 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
1152 L
"System\\CurrentControlSet\\Services",
1156 if (dwError
!= ERROR_SUCCESS
)
1158 DPRINT("Failed to open services key\n");
1162 /* Call the internal function with NULL, just to get bytes we need */
1163 Int_EnumDependentServicesW(hServicesKey
,
1168 &dwServicesReturned
);
1170 RegCloseKey(hServicesKey
);
1172 /* If pcbBytesNeeded is not zero then there are services running that
1173 are dependent on this service */
1174 if (pcbBytesNeeded
!= 0)
1176 DPRINT("Service has running dependencies. Failed to stop service.\n");
1177 return ERROR_DEPENDENT_SERVICES_RUNNING
;
1181 if (lpService
->Status
.dwServiceType
& SERVICE_DRIVER
)
1183 /* Send control code to the driver */
1184 dwError
= ScmControlDriver(lpService
,
1190 dwControlsAccepted
= lpService
->Status
.dwControlsAccepted
;
1191 dwCurrentState
= lpService
->Status
.dwCurrentState
;
1193 /* Return ERROR_SERVICE_NOT_ACTIVE if the service has not been started */
1194 if (lpService
->lpImage
== NULL
|| dwCurrentState
== SERVICE_STOPPED
)
1195 return ERROR_SERVICE_NOT_ACTIVE
;
1197 /* Check the current state before sending a control request */
1198 switch (dwCurrentState
)
1200 case SERVICE_STOP_PENDING
:
1201 case SERVICE_STOPPED
:
1202 return ERROR_SERVICE_CANNOT_ACCEPT_CTRL
;
1204 case SERVICE_START_PENDING
:
1207 case SERVICE_CONTROL_STOP
:
1210 case SERVICE_CONTROL_INTERROGATE
:
1211 RtlCopyMemory(lpServiceStatus
,
1213 sizeof(SERVICE_STATUS
));
1214 return ERROR_SUCCESS
;
1217 return ERROR_SERVICE_CANNOT_ACCEPT_CTRL
;
1222 /* Check if the control code is acceptable to the service */
1225 case SERVICE_CONTROL_STOP
:
1226 if ((dwControlsAccepted
& SERVICE_ACCEPT_STOP
) == 0)
1227 return ERROR_INVALID_SERVICE_CONTROL
;
1230 case SERVICE_CONTROL_PAUSE
:
1231 case SERVICE_CONTROL_CONTINUE
:
1232 if ((dwControlsAccepted
& SERVICE_ACCEPT_PAUSE_CONTINUE
) == 0)
1233 return ERROR_INVALID_SERVICE_CONTROL
;
1236 case SERVICE_CONTROL_PARAMCHANGE
:
1237 if ((dwControlsAccepted
& SERVICE_ACCEPT_PARAMCHANGE
) == 0)
1238 return ERROR_INVALID_SERVICE_CONTROL
;
1241 case SERVICE_CONTROL_NETBINDADD
:
1242 case SERVICE_CONTROL_NETBINDREMOVE
:
1243 case SERVICE_CONTROL_NETBINDENABLE
:
1244 case SERVICE_CONTROL_NETBINDDISABLE
:
1245 if ((dwControlsAccepted
& SERVICE_ACCEPT_NETBINDCHANGE
) == 0)
1246 return ERROR_INVALID_SERVICE_CONTROL
;
1250 /* Send control code to the service */
1251 dwError
= ScmControlService(lpService
->lpImage
->hControlPipe
,
1252 lpService
->lpServiceName
,
1253 (SERVICE_STATUS_HANDLE
)lpService
,
1256 /* Return service status information */
1257 RtlCopyMemory(lpServiceStatus
,
1259 sizeof(SERVICE_STATUS
));
1262 if (dwError
== ERROR_SUCCESS
)
1264 if (dwControl
== SERVICE_CONTROL_STOP
||
1265 dwControl
== SERVICE_CONTROL_PAUSE
||
1266 dwControl
== SERVICE_CONTROL_CONTINUE
)
1268 /* Log a successful send control */
1272 case SERVICE_CONTROL_STOP
:
1273 uID
= IDS_SERVICE_STOP
;
1276 case SERVICE_CONTROL_PAUSE
:
1277 uID
= IDS_SERVICE_PAUSE
;
1280 case SERVICE_CONTROL_CONTINUE
:
1281 uID
= IDS_SERVICE_RESUME
;
1284 LoadStringW(GetModuleHandle(NULL
), uID
, szLogBuffer
, ARRAYSIZE(szLogBuffer
));
1286 lpLogStrings
[0] = lpService
->lpDisplayName
;
1287 lpLogStrings
[1] = szLogBuffer
;
1289 ScmLogEvent(EVENT_SERVICE_CONTROL_SUCCESS
,
1290 EVENTLOG_INFORMATION_TYPE
,
1304 SC_RPC_HANDLE hService
)
1306 PSERVICE_HANDLE hSvc
;
1310 DPRINT("RDeleteService() called\n");
1313 return ERROR_SHUTDOWN_IN_PROGRESS
;
1315 hSvc
= ScmGetServiceFromHandle(hService
);
1318 DPRINT1("Invalid service handle!\n");
1319 return ERROR_INVALID_HANDLE
;
1322 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
1324 return ERROR_ACCESS_DENIED
;
1326 lpService
= hSvc
->ServiceEntry
;
1327 if (lpService
== NULL
)
1329 DPRINT("lpService == NULL!\n");
1330 return ERROR_INVALID_HANDLE
;
1333 /* Lock the service database exclusively */
1334 ScmLockDatabaseExclusive();
1336 if (lpService
->bDeleted
)
1338 DPRINT("The service has already been marked for delete!\n");
1339 dwError
= ERROR_SERVICE_MARKED_FOR_DELETE
;
1343 /* Mark service for delete */
1344 lpService
->bDeleted
= TRUE
;
1346 dwError
= ScmMarkServiceForDelete(lpService
);
1349 /* Unlock the service database */
1350 ScmUnlockDatabase();
1352 DPRINT("RDeleteService() done\n");
1361 RLockServiceDatabase(
1362 SC_RPC_HANDLE hSCManager
,
1363 LPSC_RPC_LOCK lpLock
)
1365 PMANAGER_HANDLE hMgr
;
1367 DPRINT("RLockServiceDatabase() called\n");
1371 hMgr
= ScmGetServiceManagerFromHandle(hSCManager
);
1374 DPRINT1("Invalid service manager handle!\n");
1375 return ERROR_INVALID_HANDLE
;
1378 if (!RtlAreAllAccessesGranted(hMgr
->Handle
.DesiredAccess
,
1380 return ERROR_ACCESS_DENIED
;
1382 return ScmAcquireServiceStartLock(FALSE
, lpLock
);
1389 RQueryServiceObjectSecurity(
1390 SC_RPC_HANDLE hService
,
1391 SECURITY_INFORMATION dwSecurityInformation
,
1392 LPBYTE lpSecurityDescriptor
,
1394 LPBOUNDED_DWORD_256K pcbBytesNeeded
)
1396 PSERVICE_HANDLE hSvc
;
1398 ULONG DesiredAccess
= 0;
1400 DWORD dwBytesNeeded
;
1403 DPRINT("RQueryServiceObjectSecurity() called\n");
1405 hSvc
= ScmGetServiceFromHandle(hService
);
1408 DPRINT1("Invalid service handle!\n");
1409 return ERROR_INVALID_HANDLE
;
1412 if (dwSecurityInformation
& (DACL_SECURITY_INFORMATION
|
1413 GROUP_SECURITY_INFORMATION
|
1414 OWNER_SECURITY_INFORMATION
))
1415 DesiredAccess
|= READ_CONTROL
;
1417 if (dwSecurityInformation
& SACL_SECURITY_INFORMATION
)
1418 DesiredAccess
|= ACCESS_SYSTEM_SECURITY
;
1420 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
1423 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
1424 return ERROR_ACCESS_DENIED
;
1427 lpService
= hSvc
->ServiceEntry
;
1428 if (lpService
== NULL
)
1430 DPRINT("lpService == NULL!\n");
1431 return ERROR_INVALID_HANDLE
;
1434 /* Lock the service database */
1435 ScmLockDatabaseShared();
1437 /* Retrieve the security descriptor */
1438 Status
= RtlQuerySecurityObject(lpService
->pSecurityDescriptor
,
1439 dwSecurityInformation
,
1440 (PSECURITY_DESCRIPTOR
)lpSecurityDescriptor
,
1444 /* Unlock the service database */
1445 ScmUnlockDatabase();
1447 if (NT_SUCCESS(Status
))
1449 *pcbBytesNeeded
= dwBytesNeeded
;
1450 dwError
= STATUS_SUCCESS
;
1452 else if (Status
== STATUS_BUFFER_TOO_SMALL
)
1454 *pcbBytesNeeded
= dwBytesNeeded
;
1455 dwError
= ERROR_INSUFFICIENT_BUFFER
;
1457 else if (Status
== STATUS_BAD_DESCRIPTOR_FORMAT
)
1459 dwError
= ERROR_GEN_FAILURE
;
1463 dwError
= RtlNtStatusToDosError(Status
);
1473 RSetServiceObjectSecurity(
1474 SC_RPC_HANDLE hService
,
1475 DWORD dwSecurityInformation
,
1476 LPBYTE lpSecurityDescriptor
,
1477 DWORD dwSecurityDescriptorSize
)
1479 PSERVICE_HANDLE hSvc
;
1481 ACCESS_MASK DesiredAccess
= 0;
1482 HANDLE hToken
= NULL
;
1483 HKEY hServiceKey
= NULL
;
1484 BOOL bDatabaseLocked
= FALSE
;
1488 DPRINT("RSetServiceObjectSecurity() called\n");
1490 hSvc
= ScmGetServiceFromHandle(hService
);
1493 DPRINT1("Invalid service handle!\n");
1494 return ERROR_INVALID_HANDLE
;
1497 if (dwSecurityInformation
== 0 ||
1498 dwSecurityInformation
& ~(OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
1499 | DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
))
1500 return ERROR_INVALID_PARAMETER
;
1502 if (!RtlValidSecurityDescriptor((PSECURITY_DESCRIPTOR
)lpSecurityDescriptor
))
1503 return ERROR_INVALID_PARAMETER
;
1505 if (dwSecurityInformation
& SACL_SECURITY_INFORMATION
)
1506 DesiredAccess
|= ACCESS_SYSTEM_SECURITY
;
1508 if (dwSecurityInformation
& DACL_SECURITY_INFORMATION
)
1509 DesiredAccess
|= WRITE_DAC
;
1511 if (dwSecurityInformation
& (OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
))
1512 DesiredAccess
|= WRITE_OWNER
;
1514 if ((dwSecurityInformation
& OWNER_SECURITY_INFORMATION
) &&
1515 (((PISECURITY_DESCRIPTOR
)lpSecurityDescriptor
)->Owner
== NULL
))
1516 return ERROR_INVALID_PARAMETER
;
1518 if ((dwSecurityInformation
& GROUP_SECURITY_INFORMATION
) &&
1519 (((PISECURITY_DESCRIPTOR
)lpSecurityDescriptor
)->Group
== NULL
))
1520 return ERROR_INVALID_PARAMETER
;
1522 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
1525 DPRINT1("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
1526 return ERROR_ACCESS_DENIED
;
1529 lpService
= hSvc
->ServiceEntry
;
1530 if (lpService
== NULL
)
1532 DPRINT1("lpService == NULL!\n");
1533 return ERROR_INVALID_HANDLE
;
1536 if (lpService
->bDeleted
)
1537 return ERROR_SERVICE_MARKED_FOR_DELETE
;
1540 RpcImpersonateClient(NULL
);
1542 Status
= NtOpenThreadToken(NtCurrentThread(),
1546 if (!NT_SUCCESS(Status
))
1547 return RtlNtStatusToDosError(Status
);
1552 /* Build the new security descriptor */
1553 Status
= RtlSetSecurityObject(dwSecurityInformation
,
1554 (PSECURITY_DESCRIPTOR
)lpSecurityDescriptor
,
1555 &lpService
->pSecurityDescriptor
,
1558 if (!NT_SUCCESS(Status
))
1560 dwError
= RtlNtStatusToDosError(Status
);
1564 /* Lock the service database exclusive */
1565 ScmLockDatabaseExclusive();
1566 bDatabaseLocked
= TRUE
;
1568 /* Open the service key */
1569 dwError
= ScmOpenServiceKey(lpService
->lpServiceName
,
1570 READ_CONTROL
| KEY_CREATE_SUB_KEY
| KEY_SET_VALUE
,
1572 if (dwError
!= ERROR_SUCCESS
)
1575 /* Store the new security descriptor */
1576 dwError
= ScmWriteSecurityDescriptor(hServiceKey
,
1577 lpService
->pSecurityDescriptor
);
1579 RegFlushKey(hServiceKey
);
1582 if (hServiceKey
!= NULL
)
1583 RegCloseKey(hServiceKey
);
1585 /* Unlock service database */
1586 if (bDatabaseLocked
== TRUE
)
1587 ScmUnlockDatabase();
1592 DPRINT("RSetServiceObjectSecurity() done (Error %lu)\n", dwError
);
1601 RQueryServiceStatus(
1602 SC_RPC_HANDLE hService
,
1603 LPSERVICE_STATUS lpServiceStatus
)
1605 PSERVICE_HANDLE hSvc
;
1608 DPRINT("RQueryServiceStatus() called\n");
1611 return ERROR_SHUTDOWN_IN_PROGRESS
;
1613 hSvc
= ScmGetServiceFromHandle(hService
);
1616 DPRINT1("Invalid service handle!\n");
1617 return ERROR_INVALID_HANDLE
;
1620 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
1621 SERVICE_QUERY_STATUS
))
1623 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
1624 return ERROR_ACCESS_DENIED
;
1627 lpService
= hSvc
->ServiceEntry
;
1628 if (lpService
== NULL
)
1630 DPRINT("lpService == NULL!\n");
1631 return ERROR_INVALID_HANDLE
;
1634 /* Lock the service database shared */
1635 ScmLockDatabaseShared();
1637 /* Return service status information */
1638 RtlCopyMemory(lpServiceStatus
,
1640 sizeof(SERVICE_STATUS
));
1642 /* Unlock the service database */
1643 ScmUnlockDatabase();
1645 return ERROR_SUCCESS
;
1650 ScmIsValidServiceState(DWORD dwCurrentState
)
1652 switch (dwCurrentState
)
1654 case SERVICE_STOPPED
:
1655 case SERVICE_START_PENDING
:
1656 case SERVICE_STOP_PENDING
:
1657 case SERVICE_RUNNING
:
1658 case SERVICE_CONTINUE_PENDING
:
1659 case SERVICE_PAUSE_PENDING
:
1660 case SERVICE_PAUSED
:
1673 RPC_SERVICE_STATUS_HANDLE hServiceStatus
,
1674 LPSERVICE_STATUS lpServiceStatus
)
1677 DWORD dwPreviousState
;
1678 DWORD dwPreviousType
;
1679 LPCWSTR lpLogStrings
[2];
1680 WCHAR szLogBuffer
[80];
1683 DPRINT("RSetServiceStatus() called\n");
1684 DPRINT("hServiceStatus = %lu\n", hServiceStatus
);
1685 DPRINT("dwServiceType = 0x%lx\n", lpServiceStatus
->dwServiceType
);
1686 DPRINT("dwCurrentState = %lu\n", lpServiceStatus
->dwCurrentState
);
1687 DPRINT("dwControlsAccepted = %lu\n", lpServiceStatus
->dwControlsAccepted
);
1688 DPRINT("dwWin32ExitCode = %lu\n", lpServiceStatus
->dwWin32ExitCode
);
1689 DPRINT("dwServiceSpecificExitCode = %lu\n", lpServiceStatus
->dwServiceSpecificExitCode
);
1690 DPRINT("dwCheckPoint = %lu\n", lpServiceStatus
->dwCheckPoint
);
1691 DPRINT("dwWaitHint = %lu\n", lpServiceStatus
->dwWaitHint
);
1693 if (hServiceStatus
== 0)
1695 DPRINT("hServiceStatus == NULL!\n");
1696 return ERROR_INVALID_HANDLE
;
1699 lpService
= (PSERVICE
)hServiceStatus
;
1701 /* Check current state */
1702 if (!ScmIsValidServiceState(lpServiceStatus
->dwCurrentState
))
1704 DPRINT("Invalid service state!\n");
1705 return ERROR_INVALID_DATA
;
1708 /* Check service type */
1709 if (!(lpServiceStatus
->dwServiceType
& SERVICE_WIN32
) &&
1710 (lpServiceStatus
->dwServiceType
& SERVICE_DRIVER
))
1712 DPRINT("Invalid service type!\n");
1713 return ERROR_INVALID_DATA
;
1716 /* Check accepted controls */
1717 if (lpServiceStatus
->dwControlsAccepted
& ~0xFF)
1719 DPRINT("Invalid controls accepted!\n");
1720 return ERROR_INVALID_DATA
;
1723 /* Set the wait hint and check point only if the service is in a pending state,
1724 otherwise they should be 0 */
1725 if (lpServiceStatus
->dwCurrentState
== SERVICE_STOPPED
||
1726 lpServiceStatus
->dwCurrentState
== SERVICE_PAUSED
||
1727 lpServiceStatus
->dwCurrentState
== SERVICE_RUNNING
)
1729 lpServiceStatus
->dwWaitHint
= 0;
1730 lpServiceStatus
->dwCheckPoint
= 0;
1733 /* Lock the service database exclusively */
1734 ScmLockDatabaseExclusive();
1736 /* Save the current service state */
1737 dwPreviousState
= lpService
->Status
.dwCurrentState
;
1739 /* Save the current service type */
1740 dwPreviousType
= lpService
->Status
.dwServiceType
;
1742 /* Update the service status */
1743 RtlCopyMemory(&lpService
->Status
,
1745 sizeof(SERVICE_STATUS
));
1747 /* Restore the previous service type */
1748 lpService
->Status
.dwServiceType
= dwPreviousType
;
1750 /* Dereference a stopped service */
1751 if ((lpServiceStatus
->dwServiceType
& SERVICE_WIN32
) &&
1752 (lpServiceStatus
->dwCurrentState
== SERVICE_STOPPED
))
1754 /* Decrement the image run counter */
1755 lpService
->lpImage
->dwImageRunCount
--;
1757 /* If we just stopped the last running service... */
1758 if (lpService
->lpImage
->dwImageRunCount
== 0)
1760 /* Stop the dispatcher thread */
1761 ScmControlService(lpService
->lpImage
->hControlPipe
,
1763 (SERVICE_STATUS_HANDLE
)lpService
,
1764 SERVICE_CONTROL_STOP
);
1766 /* Remove the service image */
1767 ScmRemoveServiceImage(lpService
->lpImage
);
1768 lpService
->lpImage
= NULL
;
1772 /* Unlock the service database */
1773 ScmUnlockDatabase();
1775 if ((lpServiceStatus
->dwCurrentState
== SERVICE_STOPPED
) &&
1776 (dwPreviousState
!= SERVICE_STOPPED
) &&
1777 (lpServiceStatus
->dwWin32ExitCode
!= ERROR_SUCCESS
))
1779 /* Log a failed service stop */
1780 StringCchPrintfW(szLogBuffer
, ARRAYSIZE(szLogBuffer
),
1781 L
"%lu", lpServiceStatus
->dwWin32ExitCode
);
1782 lpLogStrings
[0] = lpService
->lpDisplayName
;
1783 lpLogStrings
[1] = szLogBuffer
;
1785 ScmLogEvent(EVENT_SERVICE_EXIT_FAILED
,
1786 EVENTLOG_ERROR_TYPE
,
1790 else if (lpServiceStatus
->dwCurrentState
!= dwPreviousState
&&
1791 (lpServiceStatus
->dwCurrentState
== SERVICE_STOPPED
||
1792 lpServiceStatus
->dwCurrentState
== SERVICE_RUNNING
||
1793 lpServiceStatus
->dwCurrentState
== SERVICE_PAUSED
))
1795 /* Log a successful service status change */
1796 switch(lpServiceStatus
->dwCurrentState
)
1798 case SERVICE_STOPPED
:
1799 uID
= IDS_SERVICE_STOPPED
;
1802 case SERVICE_RUNNING
:
1803 uID
= IDS_SERVICE_RUNNING
;
1806 case SERVICE_PAUSED
:
1807 uID
= IDS_SERVICE_PAUSED
;
1811 LoadStringW(GetModuleHandle(NULL
), uID
, szLogBuffer
, ARRAYSIZE(szLogBuffer
));
1812 lpLogStrings
[0] = lpService
->lpDisplayName
;
1813 lpLogStrings
[1] = szLogBuffer
;
1815 ScmLogEvent(EVENT_SERVICE_STATUS_SUCCESS
,
1816 EVENTLOG_INFORMATION_TYPE
,
1821 DPRINT("Set %S to %lu\n", lpService
->lpDisplayName
, lpService
->Status
.dwCurrentState
);
1822 DPRINT("RSetServiceStatus() done\n");
1824 return ERROR_SUCCESS
;
1831 RUnlockServiceDatabase(
1834 DPRINT("RUnlockServiceDatabase(%p)\n", Lock
);
1835 return ScmReleaseServiceStartLock(Lock
);
1842 RNotifyBootConfigStatus(
1843 SVCCTL_HANDLEW lpMachineName
,
1844 DWORD BootAcceptable
)
1846 DPRINT1("RNotifyBootConfigStatus(%p %lu) called\n", lpMachineName
, BootAcceptable
);
1847 return ERROR_SUCCESS
;
1850 // return ERROR_CALL_NOT_IMPLEMENTED;
1857 RI_ScSetServiceBitsW(
1858 RPC_SERVICE_STATUS_HANDLE hServiceStatus
,
1859 DWORD dwServiceBits
,
1861 int bUpdateImmediately
,
1866 DPRINT("RI_ScSetServiceBitsW(%p %lx %d %d %S)\n",
1867 hServiceStatus
, dwServiceBits
, bSetBitsOn
,
1868 bUpdateImmediately
, lpString
);
1871 return ERROR_SHUTDOWN_IN_PROGRESS
;
1873 if (lpString
!= NULL
)
1874 return ERROR_INVALID_PARAMETER
;
1876 if (hServiceStatus
== 0)
1878 DPRINT("hServiceStatus == NULL!\n");
1879 return ERROR_INVALID_HANDLE
;
1882 // FIXME: Validate the status handle
1883 pService
= (PSERVICE
)hServiceStatus
;
1887 DPRINT("Old service bits: %08lx\n", pService
->dwServiceBits
);
1888 DPRINT("Old global service bits: %08lx\n", g_dwServiceBits
);
1889 pService
->dwServiceBits
|= dwServiceBits
;
1890 g_dwServiceBits
|= dwServiceBits
;
1891 DPRINT("New service bits: %08lx\n", pService
->dwServiceBits
);
1892 DPRINT("New global service bits: %08lx\n", g_dwServiceBits
);
1896 DPRINT("Old service bits: %08lx\n", pService
->dwServiceBits
);
1897 DPRINT("Old global service bits: %08lx\n", g_dwServiceBits
);
1898 pService
->dwServiceBits
&= ~dwServiceBits
;
1899 g_dwServiceBits
&= ~dwServiceBits
;
1900 DPRINT("New service bits: %08lx\n", pService
->dwServiceBits
);
1901 DPRINT("New global service bits: %08lx\n", g_dwServiceBits
);
1904 return ERROR_SUCCESS
;
1911 RChangeServiceConfigW(
1912 SC_RPC_HANDLE hService
,
1913 DWORD dwServiceType
,
1915 DWORD dwErrorControl
,
1916 LPWSTR lpBinaryPathName
,
1917 LPWSTR lpLoadOrderGroup
,
1919 LPBYTE lpDependencies
,
1921 LPWSTR lpServiceStartName
,
1924 LPWSTR lpDisplayName
)
1926 DWORD dwError
= ERROR_SUCCESS
;
1927 PSERVICE_HANDLE hSvc
;
1928 PSERVICE lpService
= NULL
;
1929 HKEY hServiceKey
= NULL
;
1930 LPWSTR lpDisplayNameW
= NULL
;
1931 LPWSTR lpImagePathW
= NULL
;
1933 DPRINT("RChangeServiceConfigW() called\n");
1934 DPRINT("dwServiceType = 0x%lx\n", dwServiceType
);
1935 DPRINT("dwStartType = %lu\n", dwStartType
);
1936 DPRINT("dwErrorControl = %lu\n", dwErrorControl
);
1937 DPRINT("lpBinaryPathName = %S\n", lpBinaryPathName
);
1938 DPRINT("lpLoadOrderGroup = %S\n", lpLoadOrderGroup
);
1939 DPRINT("lpDisplayName = %S\n", lpDisplayName
);
1942 return ERROR_SHUTDOWN_IN_PROGRESS
;
1944 hSvc
= ScmGetServiceFromHandle(hService
);
1947 DPRINT1("Invalid service handle!\n");
1948 return ERROR_INVALID_HANDLE
;
1951 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
1952 SERVICE_CHANGE_CONFIG
))
1954 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
1955 return ERROR_ACCESS_DENIED
;
1958 /* Check for invalid service type value */
1959 if ((dwServiceType
!= SERVICE_NO_CHANGE
) &&
1960 (dwServiceType
!= SERVICE_KERNEL_DRIVER
) &&
1961 (dwServiceType
!= SERVICE_FILE_SYSTEM_DRIVER
) &&
1962 ((dwServiceType
& ~SERVICE_INTERACTIVE_PROCESS
) != SERVICE_WIN32_OWN_PROCESS
) &&
1963 ((dwServiceType
& ~SERVICE_INTERACTIVE_PROCESS
) != SERVICE_WIN32_SHARE_PROCESS
))
1964 return ERROR_INVALID_PARAMETER
;
1966 /* Check for invalid start type value */
1967 if ((dwStartType
!= SERVICE_NO_CHANGE
) &&
1968 (dwStartType
!= SERVICE_BOOT_START
) &&
1969 (dwStartType
!= SERVICE_SYSTEM_START
) &&
1970 (dwStartType
!= SERVICE_AUTO_START
) &&
1971 (dwStartType
!= SERVICE_DEMAND_START
) &&
1972 (dwStartType
!= SERVICE_DISABLED
))
1973 return ERROR_INVALID_PARAMETER
;
1975 /* Only drivers can be boot start or system start services */
1976 if ((dwStartType
== SERVICE_BOOT_START
) ||
1977 (dwStartType
== SERVICE_SYSTEM_START
))
1979 if ((dwServiceType
!= SERVICE_KERNEL_DRIVER
) &&
1980 (dwServiceType
!= SERVICE_FILE_SYSTEM_DRIVER
))
1981 return ERROR_INVALID_PARAMETER
;
1984 /* Check for invalid error control value */
1985 if ((dwErrorControl
!= SERVICE_NO_CHANGE
) &&
1986 (dwErrorControl
!= SERVICE_ERROR_IGNORE
) &&
1987 (dwErrorControl
!= SERVICE_ERROR_NORMAL
) &&
1988 (dwErrorControl
!= SERVICE_ERROR_SEVERE
) &&
1989 (dwErrorControl
!= SERVICE_ERROR_CRITICAL
))
1990 return ERROR_INVALID_PARAMETER
;
1992 if (lpdwTagId
&& (!lpLoadOrderGroup
|| !*lpLoadOrderGroup
))
1994 return ERROR_INVALID_PARAMETER
;
1997 lpService
= hSvc
->ServiceEntry
;
1998 if (lpService
== NULL
)
2000 DPRINT("lpService == NULL!\n");
2001 return ERROR_INVALID_HANDLE
;
2004 /* Lock the service database exclusively */
2005 ScmLockDatabaseExclusive();
2007 if (lpService
->bDeleted
)
2009 DPRINT("The service has already been marked for delete!\n");
2010 dwError
= ERROR_SERVICE_MARKED_FOR_DELETE
;
2014 /* Open the service key */
2015 dwError
= ScmOpenServiceKey(lpService
->szServiceName
,
2018 if (dwError
!= ERROR_SUCCESS
)
2021 /* Write service data to the registry */
2023 /* Set the display name */
2024 if (lpDisplayName
!= NULL
&& *lpDisplayName
!= 0)
2026 RegSetValueExW(hServiceKey
,
2030 (LPBYTE
)lpDisplayName
,
2031 (DWORD
)((wcslen(lpDisplayName
) + 1) * sizeof(WCHAR
)));
2033 /* Update the display name */
2034 lpDisplayNameW
= HeapAlloc(GetProcessHeap(),
2036 (wcslen(lpDisplayName
) + 1) * sizeof(WCHAR
));
2037 if (lpDisplayNameW
== NULL
)
2039 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
2043 wcscpy(lpDisplayNameW
, lpDisplayName
);
2044 if (lpService
->lpDisplayName
!= lpService
->lpServiceName
)
2045 HeapFree(GetProcessHeap(), 0, lpService
->lpDisplayName
);
2047 lpService
->lpDisplayName
= lpDisplayNameW
;
2050 if (dwServiceType
!= SERVICE_NO_CHANGE
)
2052 /* Set the service type */
2053 dwError
= RegSetValueExW(hServiceKey
,
2057 (LPBYTE
)&dwServiceType
,
2059 if (dwError
!= ERROR_SUCCESS
)
2062 lpService
->Status
.dwServiceType
= dwServiceType
;
2065 if (dwStartType
!= SERVICE_NO_CHANGE
)
2067 /* Set the start value */
2068 dwError
= RegSetValueExW(hServiceKey
,
2072 (LPBYTE
)&dwStartType
,
2074 if (dwError
!= ERROR_SUCCESS
)
2077 lpService
->dwStartType
= dwStartType
;
2080 if (dwErrorControl
!= SERVICE_NO_CHANGE
)
2082 /* Set the error control value */
2083 dwError
= RegSetValueExW(hServiceKey
,
2087 (LPBYTE
)&dwErrorControl
,
2089 if (dwError
!= ERROR_SUCCESS
)
2092 lpService
->dwErrorControl
= dwErrorControl
;
2095 if (lpBinaryPathName
!= NULL
&& *lpBinaryPathName
!= 0)
2097 /* Set the image path */
2098 lpImagePathW
= lpBinaryPathName
;
2100 if (lpService
->Status
.dwServiceType
& SERVICE_DRIVER
)
2102 dwError
= ScmCanonDriverImagePath(lpService
->dwStartType
,
2106 if (dwError
!= ERROR_SUCCESS
)
2110 dwError
= RegSetValueExW(hServiceKey
,
2114 (LPBYTE
)lpImagePathW
,
2115 (DWORD
)((wcslen(lpImagePathW
) + 1) * sizeof(WCHAR
)));
2117 if (lpImagePathW
!= lpBinaryPathName
)
2118 HeapFree(GetProcessHeap(), 0, lpImagePathW
);
2120 if (dwError
!= ERROR_SUCCESS
)
2124 /* Set the group name */
2125 if (lpLoadOrderGroup
!= NULL
&& *lpLoadOrderGroup
!= 0)
2127 dwError
= RegSetValueExW(hServiceKey
,
2131 (LPBYTE
)lpLoadOrderGroup
,
2132 (DWORD
)((wcslen(lpLoadOrderGroup
) + 1) * sizeof(WCHAR
)));
2133 if (dwError
!= ERROR_SUCCESS
)
2136 dwError
= ScmSetServiceGroup(lpService
,
2138 if (dwError
!= ERROR_SUCCESS
)
2143 if (lpdwTagId
!= NULL
)
2145 dwError
= ScmAssignNewTag(lpService
);
2146 if (dwError
!= ERROR_SUCCESS
)
2149 dwError
= RegSetValueExW(hServiceKey
,
2153 (LPBYTE
)&lpService
->dwTag
,
2155 if (dwError
!= ERROR_SUCCESS
)
2158 *lpdwTagId
= lpService
->dwTag
;
2161 /* Write dependencies */
2162 if (lpDependencies
!= NULL
&& *lpDependencies
!= 0)
2164 dwError
= ScmWriteDependencies(hServiceKey
,
2165 (LPWSTR
)lpDependencies
,
2167 if (dwError
!= ERROR_SUCCESS
)
2171 if (lpPassword
!= NULL
)
2173 if (wcslen((LPWSTR
)lpPassword
) != 0)
2175 /* FIXME: Decrypt the password */
2177 /* Write the password */
2178 dwError
= ScmSetServicePassword(lpService
->szServiceName
,
2179 (LPCWSTR
)lpPassword
);
2180 if (dwError
!= ERROR_SUCCESS
)
2185 /* Delete the password */
2186 dwError
= ScmSetServicePassword(lpService
->szServiceName
,
2188 if (dwError
== ERROR_FILE_NOT_FOUND
)
2189 dwError
= ERROR_SUCCESS
;
2191 if (dwError
!= ERROR_SUCCESS
)
2197 if (hServiceKey
!= NULL
)
2198 RegCloseKey(hServiceKey
);
2200 /* Unlock the service database */
2201 ScmUnlockDatabase();
2203 DPRINT("RChangeServiceConfigW() done (Error %lu)\n", dwError
);
2213 SC_RPC_HANDLE hSCManager
,
2214 LPCWSTR lpServiceName
,
2215 LPCWSTR lpDisplayName
,
2216 DWORD dwDesiredAccess
,
2217 DWORD dwServiceType
,
2219 DWORD dwErrorControl
,
2220 LPCWSTR lpBinaryPathName
,
2221 LPCWSTR lpLoadOrderGroup
,
2223 LPBYTE lpDependencies
,
2225 LPCWSTR lpServiceStartName
,
2228 LPSC_RPC_HANDLE lpServiceHandle
)
2230 PMANAGER_HANDLE hManager
;
2231 DWORD dwError
= ERROR_SUCCESS
;
2232 PSERVICE lpService
= NULL
;
2233 SC_HANDLE hServiceHandle
= NULL
;
2234 LPWSTR lpImagePath
= NULL
;
2235 HKEY hServiceKey
= NULL
;
2236 LPWSTR lpObjectName
;
2238 DPRINT("RCreateServiceW() called\n");
2239 DPRINT("lpServiceName = %S\n", lpServiceName
);
2240 DPRINT("lpDisplayName = %S\n", lpDisplayName
);
2241 DPRINT("dwDesiredAccess = %lx\n", dwDesiredAccess
);
2242 DPRINT("dwServiceType = 0x%lx\n", dwServiceType
);
2243 DPRINT("dwStartType = %lu\n", dwStartType
);
2244 DPRINT("dwErrorControl = %lu\n", dwErrorControl
);
2245 DPRINT("lpBinaryPathName = %S\n", lpBinaryPathName
);
2246 DPRINT("lpLoadOrderGroup = %S\n", lpLoadOrderGroup
);
2247 DPRINT("lpdwTagId = %p\n", lpdwTagId
);
2250 return ERROR_SHUTDOWN_IN_PROGRESS
;
2252 hManager
= ScmGetServiceManagerFromHandle(hSCManager
);
2253 if (hManager
== NULL
)
2255 DPRINT1("Invalid service manager handle!\n");
2256 return ERROR_INVALID_HANDLE
;
2259 /* Check access rights */
2260 if (!RtlAreAllAccessesGranted(hManager
->Handle
.DesiredAccess
,
2261 SC_MANAGER_CREATE_SERVICE
))
2263 DPRINT("Insufficient access rights! 0x%lx\n",
2264 hManager
->Handle
.DesiredAccess
);
2265 return ERROR_ACCESS_DENIED
;
2268 if (wcslen(lpServiceName
) == 0)
2270 return ERROR_INVALID_NAME
;
2273 if (wcslen(lpBinaryPathName
) == 0)
2275 return ERROR_INVALID_PARAMETER
;
2278 /* Check for invalid service type value */
2279 if ((dwServiceType
!= SERVICE_KERNEL_DRIVER
) &&
2280 (dwServiceType
!= SERVICE_FILE_SYSTEM_DRIVER
) &&
2281 ((dwServiceType
& ~SERVICE_INTERACTIVE_PROCESS
) != SERVICE_WIN32_OWN_PROCESS
) &&
2282 ((dwServiceType
& ~SERVICE_INTERACTIVE_PROCESS
) != SERVICE_WIN32_SHARE_PROCESS
))
2283 return ERROR_INVALID_PARAMETER
;
2285 /* Check for invalid start type value */
2286 if ((dwStartType
!= SERVICE_BOOT_START
) &&
2287 (dwStartType
!= SERVICE_SYSTEM_START
) &&
2288 (dwStartType
!= SERVICE_AUTO_START
) &&
2289 (dwStartType
!= SERVICE_DEMAND_START
) &&
2290 (dwStartType
!= SERVICE_DISABLED
))
2291 return ERROR_INVALID_PARAMETER
;
2293 /* Only drivers can be boot start or system start services */
2294 if ((dwStartType
== SERVICE_BOOT_START
) ||
2295 (dwStartType
== SERVICE_SYSTEM_START
))
2297 if ((dwServiceType
!= SERVICE_KERNEL_DRIVER
) &&
2298 (dwServiceType
!= SERVICE_FILE_SYSTEM_DRIVER
))
2299 return ERROR_INVALID_PARAMETER
;
2302 /* Check for invalid error control value */
2303 if ((dwErrorControl
!= SERVICE_ERROR_IGNORE
) &&
2304 (dwErrorControl
!= SERVICE_ERROR_NORMAL
) &&
2305 (dwErrorControl
!= SERVICE_ERROR_SEVERE
) &&
2306 (dwErrorControl
!= SERVICE_ERROR_CRITICAL
))
2307 return ERROR_INVALID_PARAMETER
;
2309 if ((dwServiceType
== (SERVICE_WIN32_OWN_PROCESS
| SERVICE_INTERACTIVE_PROCESS
)) &&
2310 (lpServiceStartName
))
2312 /* We allow LocalSystem to run interactive. */
2313 if (wcsicmp(lpServiceStartName
, L
"LocalSystem"))
2315 return ERROR_INVALID_PARAMETER
;
2319 if (lpdwTagId
&& (!lpLoadOrderGroup
|| !*lpLoadOrderGroup
))
2321 return ERROR_INVALID_PARAMETER
;
2324 /* Lock the service database exclusively */
2325 ScmLockDatabaseExclusive();
2327 lpService
= ScmGetServiceEntryByName(lpServiceName
);
2330 /* Unlock the service database */
2331 ScmUnlockDatabase();
2333 /* Check if it is marked for deletion */
2334 if (lpService
->bDeleted
)
2335 return ERROR_SERVICE_MARKED_FOR_DELETE
;
2337 /* Return Error exist */
2338 return ERROR_SERVICE_EXISTS
;
2341 if (lpDisplayName
!= NULL
&&
2342 ScmGetServiceEntryByDisplayName(lpDisplayName
) != NULL
)
2344 /* Unlock the service database */
2345 ScmUnlockDatabase();
2347 return ERROR_DUPLICATE_SERVICE_NAME
;
2350 if (dwServiceType
& SERVICE_DRIVER
)
2352 dwError
= ScmCanonDriverImagePath(dwStartType
,
2355 if (dwError
!= ERROR_SUCCESS
)
2360 if (dwStartType
== SERVICE_BOOT_START
||
2361 dwStartType
== SERVICE_SYSTEM_START
)
2363 /* Unlock the service database */
2364 ScmUnlockDatabase();
2366 return ERROR_INVALID_PARAMETER
;
2370 /* Allocate a new service entry */
2371 dwError
= ScmCreateNewServiceRecord(lpServiceName
,
2375 if (dwError
!= ERROR_SUCCESS
)
2378 /* Fill the new service entry */
2379 lpService
->dwErrorControl
= dwErrorControl
;
2381 /* Fill the display name */
2382 if (lpDisplayName
!= NULL
&&
2383 *lpDisplayName
!= 0 &&
2384 _wcsicmp(lpService
->lpDisplayName
, lpDisplayName
) != 0)
2386 lpService
->lpDisplayName
= HeapAlloc(GetProcessHeap(),
2388 (wcslen(lpDisplayName
) + 1) * sizeof(WCHAR
));
2389 if (lpService
->lpDisplayName
== NULL
)
2391 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
2394 wcscpy(lpService
->lpDisplayName
, lpDisplayName
);
2397 /* Assign the service to a group */
2398 if (lpLoadOrderGroup
!= NULL
&& *lpLoadOrderGroup
!= 0)
2400 dwError
= ScmSetServiceGroup(lpService
,
2402 if (dwError
!= ERROR_SUCCESS
)
2406 /* Assign a new tag */
2407 if (lpdwTagId
!= NULL
)
2409 dwError
= ScmAssignNewTag(lpService
);
2410 if (dwError
!= ERROR_SUCCESS
)
2414 /* Assign the default security descriptor */
2415 if (dwServiceType
& SERVICE_WIN32
)
2417 dwError
= ScmCreateDefaultServiceSD(&lpService
->pSecurityDescriptor
);
2418 if (dwError
!= ERROR_SUCCESS
)
2422 /* Write service data to the registry */
2423 /* Create the service key */
2424 dwError
= ScmCreateServiceKey(lpServiceName
,
2427 if (dwError
!= ERROR_SUCCESS
)
2430 /* Set the display name */
2431 if (lpDisplayName
!= NULL
&& *lpDisplayName
!= 0)
2433 RegSetValueExW(hServiceKey
,
2437 (LPBYTE
)lpDisplayName
,
2438 (DWORD
)((wcslen(lpDisplayName
) + 1) * sizeof(WCHAR
)));
2441 /* Set the service type */
2442 dwError
= RegSetValueExW(hServiceKey
,
2446 (LPBYTE
)&dwServiceType
,
2448 if (dwError
!= ERROR_SUCCESS
)
2451 /* Set the start value */
2452 dwError
= RegSetValueExW(hServiceKey
,
2456 (LPBYTE
)&dwStartType
,
2458 if (dwError
!= ERROR_SUCCESS
)
2461 /* Set the error control value */
2462 dwError
= RegSetValueExW(hServiceKey
,
2466 (LPBYTE
)&dwErrorControl
,
2468 if (dwError
!= ERROR_SUCCESS
)
2471 /* Set the image path */
2472 if (dwServiceType
& SERVICE_WIN32
)
2474 dwError
= RegSetValueExW(hServiceKey
,
2478 (LPBYTE
)lpBinaryPathName
,
2479 (DWORD
)((wcslen(lpBinaryPathName
) + 1) * sizeof(WCHAR
)));
2480 if (dwError
!= ERROR_SUCCESS
)
2483 else if (dwServiceType
& SERVICE_DRIVER
)
2485 dwError
= RegSetValueExW(hServiceKey
,
2489 (LPBYTE
)lpImagePath
,
2490 (DWORD
)((wcslen(lpImagePath
) + 1) * sizeof(WCHAR
)));
2491 if (dwError
!= ERROR_SUCCESS
)
2495 /* Set the group name */
2496 if (lpLoadOrderGroup
!= NULL
&& *lpLoadOrderGroup
!= 0)
2498 dwError
= RegSetValueExW(hServiceKey
,
2502 (LPBYTE
)lpLoadOrderGroup
,
2503 (DWORD
)((wcslen(lpLoadOrderGroup
) + 1) * sizeof(WCHAR
)));
2504 if (dwError
!= ERROR_SUCCESS
)
2508 /* Set the service tag */
2509 if (lpdwTagId
!= NULL
)
2511 dwError
= RegSetValueExW(hServiceKey
,
2515 (LPBYTE
)&lpService
->dwTag
,
2517 if (dwError
!= ERROR_SUCCESS
)
2521 /* Write dependencies */
2522 if (lpDependencies
!= NULL
&& *lpDependencies
!= 0)
2524 dwError
= ScmWriteDependencies(hServiceKey
,
2525 (LPCWSTR
)lpDependencies
,
2527 if (dwError
!= ERROR_SUCCESS
)
2531 /* Start name and password are only used by Win32 services */
2532 if (dwServiceType
& SERVICE_WIN32
)
2534 /* Write service start name */
2535 lpObjectName
= (lpServiceStartName
!= NULL
) ? (LPWSTR
)lpServiceStartName
: L
"LocalSystem";
2536 dwError
= RegSetValueExW(hServiceKey
,
2540 (LPBYTE
)lpObjectName
,
2541 (DWORD
)((wcslen(lpObjectName
) + 1) * sizeof(WCHAR
)));
2542 if (dwError
!= ERROR_SUCCESS
)
2545 if (lpPassword
!= NULL
&& wcslen((LPWSTR
)lpPassword
) != 0)
2547 /* FIXME: Decrypt the password */
2549 /* Write the password */
2550 dwError
= ScmSetServicePassword(lpServiceName
,
2551 (LPCWSTR
)lpPassword
);
2552 if (dwError
!= ERROR_SUCCESS
)
2556 /* Write the security descriptor */
2557 dwError
= ScmWriteSecurityDescriptor(hServiceKey
,
2558 lpService
->pSecurityDescriptor
);
2559 if (dwError
!= ERROR_SUCCESS
)
2563 dwError
= ScmCreateServiceHandle(lpService
,
2565 if (dwError
!= ERROR_SUCCESS
)
2568 dwError
= ScmCheckAccess(hServiceHandle
,
2570 if (dwError
!= ERROR_SUCCESS
)
2573 lpService
->dwRefCount
= 1;
2574 DPRINT("CreateService - lpService->dwRefCount %u\n", lpService
->dwRefCount
);
2577 /* Unlock the service database */
2578 ScmUnlockDatabase();
2580 if (hServiceKey
!= NULL
)
2581 RegCloseKey(hServiceKey
);
2583 if (dwError
== ERROR_SUCCESS
)
2585 DPRINT("hService %p\n", hServiceHandle
);
2586 *lpServiceHandle
= (SC_RPC_HANDLE
)hServiceHandle
;
2588 if (lpdwTagId
!= NULL
)
2589 *lpdwTagId
= lpService
->dwTag
;
2593 if (lpService
!= NULL
&&
2594 lpService
->lpServiceName
!= NULL
)
2596 /* Release the display name buffer */
2597 HeapFree(GetProcessHeap(), 0, lpService
->lpDisplayName
);
2602 /* Remove the service handle */
2603 HeapFree(GetProcessHeap(), 0, hServiceHandle
);
2606 if (lpService
!= NULL
)
2608 /* FIXME: remove the service entry */
2612 if (lpImagePath
!= NULL
)
2613 HeapFree(GetProcessHeap(), 0, lpImagePath
);
2615 DPRINT("RCreateServiceW() done (Error %lu)\n", dwError
);
2624 REnumDependentServicesW(
2625 SC_RPC_HANDLE hService
,
2626 DWORD dwServiceState
,
2629 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
2630 LPBOUNDED_DWORD_256K lpServicesReturned
)
2632 DWORD dwError
= ERROR_SUCCESS
;
2633 DWORD dwServicesReturned
= 0;
2634 DWORD dwServiceCount
;
2635 HKEY hServicesKey
= NULL
;
2636 PSERVICE_HANDLE hSvc
;
2637 PSERVICE lpService
= NULL
;
2638 PSERVICE
*lpServicesArray
= NULL
;
2639 LPENUM_SERVICE_STATUSW lpServicesPtr
= NULL
;
2642 *pcbBytesNeeded
= 0;
2643 *lpServicesReturned
= 0;
2645 DPRINT("REnumDependentServicesW() called\n");
2647 hSvc
= ScmGetServiceFromHandle(hService
);
2650 DPRINT1("Invalid service handle!\n");
2651 return ERROR_INVALID_HANDLE
;
2654 lpService
= hSvc
->ServiceEntry
;
2656 /* Check access rights */
2657 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
2658 SC_MANAGER_ENUMERATE_SERVICE
))
2660 DPRINT("Insufficient access rights! 0x%lx\n",
2661 hSvc
->Handle
.DesiredAccess
);
2662 return ERROR_ACCESS_DENIED
;
2665 /* Open the Services Reg key */
2666 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
2667 L
"System\\CurrentControlSet\\Services",
2671 if (dwError
!= ERROR_SUCCESS
)
2674 /* First determine the bytes needed and get the number of dependent services */
2675 dwError
= Int_EnumDependentServicesW(hServicesKey
,
2680 &dwServicesReturned
);
2681 if (dwError
!= ERROR_SUCCESS
)
2684 /* If buffer size is less than the bytes needed or pointer is null */
2685 if ((!lpServices
) || (cbBufSize
< *pcbBytesNeeded
))
2687 dwError
= ERROR_MORE_DATA
;
2691 /* Allocate memory for array of service pointers */
2692 lpServicesArray
= HeapAlloc(GetProcessHeap(),
2694 (dwServicesReturned
+ 1) * sizeof(PSERVICE
));
2695 if (!lpServicesArray
)
2697 DPRINT1("Could not allocate a buffer!!\n");
2698 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
2702 dwServicesReturned
= 0;
2703 *pcbBytesNeeded
= 0;
2705 dwError
= Int_EnumDependentServicesW(hServicesKey
,
2710 &dwServicesReturned
);
2711 if (dwError
!= ERROR_SUCCESS
)
2716 lpServicesPtr
= (LPENUM_SERVICE_STATUSW
)lpServices
;
2717 lpStr
= (LPWSTR
)(lpServices
+ (dwServicesReturned
* sizeof(ENUM_SERVICE_STATUSW
)));
2719 /* Copy EnumDepenedentService to Buffer */
2720 for (dwServiceCount
= 0; dwServiceCount
< dwServicesReturned
; dwServiceCount
++)
2722 lpService
= lpServicesArray
[dwServiceCount
];
2724 /* Copy status info */
2725 memcpy(&lpServicesPtr
->ServiceStatus
,
2727 sizeof(SERVICE_STATUS
));
2729 /* Copy display name */
2730 wcscpy(lpStr
, lpService
->lpDisplayName
);
2731 lpServicesPtr
->lpDisplayName
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServices
);
2732 lpStr
+= (wcslen(lpService
->lpDisplayName
) + 1);
2734 /* Copy service name */
2735 wcscpy(lpStr
, lpService
->lpServiceName
);
2736 lpServicesPtr
->lpServiceName
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServices
);
2737 lpStr
+= (wcslen(lpService
->lpServiceName
) + 1);
2742 *lpServicesReturned
= dwServicesReturned
;
2745 if (lpServicesArray
!= NULL
)
2746 HeapFree(GetProcessHeap(), 0, lpServicesArray
);
2748 RegCloseKey(hServicesKey
);
2750 DPRINT("REnumDependentServicesW() done (Error %lu)\n", dwError
);
2759 REnumServicesStatusW(
2760 SC_RPC_HANDLE hSCManager
,
2761 DWORD dwServiceType
,
2762 DWORD dwServiceState
,
2765 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
2766 LPBOUNDED_DWORD_256K lpServicesReturned
,
2767 LPBOUNDED_DWORD_256K lpResumeHandle
)
2769 /* Enumerate all the services, not regarding of their group */
2770 return REnumServiceGroupW(hSCManager
,
2786 LPWSTR lpMachineName
,
2787 LPWSTR lpDatabaseName
,
2788 DWORD dwDesiredAccess
,
2789 LPSC_RPC_HANDLE lpScHandle
)
2794 DPRINT("ROpenSCManagerW() called\n");
2795 DPRINT("lpMachineName = %p\n", lpMachineName
);
2796 DPRINT("lpMachineName: %S\n", lpMachineName
);
2797 DPRINT("lpDataBaseName = %p\n", lpDatabaseName
);
2798 DPRINT("lpDataBaseName: %S\n", lpDatabaseName
);
2799 DPRINT("dwDesiredAccess = %x\n", dwDesiredAccess
);
2802 return ERROR_SHUTDOWN_IN_PROGRESS
;
2805 return ERROR_INVALID_PARAMETER
;
2807 dwError
= ScmCreateManagerHandle(lpDatabaseName
,
2809 if (dwError
!= ERROR_SUCCESS
)
2811 DPRINT("ScmCreateManagerHandle() failed (Error %lu)\n", dwError
);
2815 /* Check the desired access */
2816 dwError
= ScmCheckAccess(hHandle
,
2817 dwDesiredAccess
| SC_MANAGER_CONNECT
);
2818 if (dwError
!= ERROR_SUCCESS
)
2820 DPRINT("ScmCheckAccess() failed (Error %lu)\n", dwError
);
2821 HeapFree(GetProcessHeap(), 0, hHandle
);
2825 *lpScHandle
= (SC_RPC_HANDLE
)hHandle
;
2826 DPRINT("*hScm = %p\n", *lpScHandle
);
2828 DPRINT("ROpenSCManagerW() done\n");
2830 return ERROR_SUCCESS
;
2838 SC_RPC_HANDLE hSCManager
,
2839 LPWSTR lpServiceName
,
2840 DWORD dwDesiredAccess
,
2841 LPSC_RPC_HANDLE lpServiceHandle
)
2844 PMANAGER_HANDLE hManager
;
2846 DWORD dwError
= ERROR_SUCCESS
;
2848 DPRINT("ROpenServiceW() called\n");
2849 DPRINT("hSCManager = %p\n", hSCManager
);
2850 DPRINT("lpServiceName = %p\n", lpServiceName
);
2851 DPRINT("lpServiceName: %S\n", lpServiceName
);
2852 DPRINT("dwDesiredAccess = %x\n", dwDesiredAccess
);
2855 return ERROR_SHUTDOWN_IN_PROGRESS
;
2857 hManager
= ScmGetServiceManagerFromHandle(hSCManager
);
2858 if (hManager
== NULL
)
2860 DPRINT1("Invalid service manager handle!\n");
2861 return ERROR_INVALID_HANDLE
;
2864 if (!lpServiceHandle
)
2865 return ERROR_INVALID_PARAMETER
;
2868 return ERROR_INVALID_ADDRESS
;
2870 /* Lock the service database exclusive */
2871 ScmLockDatabaseExclusive();
2873 /* Get service database entry */
2874 lpService
= ScmGetServiceEntryByName(lpServiceName
);
2875 if (lpService
== NULL
)
2877 DPRINT("Could not find a service!\n");
2878 dwError
= ERROR_SERVICE_DOES_NOT_EXIST
;
2882 /* Create a service handle */
2883 dwError
= ScmCreateServiceHandle(lpService
,
2885 if (dwError
!= ERROR_SUCCESS
)
2887 DPRINT("ScmCreateServiceHandle() failed (Error %lu)\n", dwError
);
2891 /* Check the desired access */
2892 dwError
= ScmCheckAccess(hHandle
,
2894 if (dwError
!= ERROR_SUCCESS
)
2896 DPRINT("ScmCheckAccess() failed (Error %lu)\n", dwError
);
2897 HeapFree(GetProcessHeap(), 0, hHandle
);
2901 lpService
->dwRefCount
++;
2902 DPRINT("OpenService - lpService->dwRefCount %u\n",lpService
->dwRefCount
);
2904 *lpServiceHandle
= (SC_RPC_HANDLE
)hHandle
;
2905 DPRINT("*hService = %p\n", *lpServiceHandle
);
2908 /* Unlock the service database */
2909 ScmUnlockDatabase();
2911 DPRINT("ROpenServiceW() done\n");
2920 RQueryServiceConfigW(
2921 SC_RPC_HANDLE hService
,
2922 LPBYTE lpBuf
, //LPQUERY_SERVICE_CONFIGW lpServiceConfig,
2924 LPBOUNDED_DWORD_8K pcbBytesNeeded
)
2926 LPQUERY_SERVICE_CONFIGW lpServiceConfig
= (LPQUERY_SERVICE_CONFIGW
)lpBuf
;
2927 DWORD dwError
= ERROR_SUCCESS
;
2928 PSERVICE_HANDLE hSvc
;
2929 PSERVICE lpService
= NULL
;
2930 HKEY hServiceKey
= NULL
;
2931 LPWSTR lpImagePath
= NULL
;
2932 LPWSTR lpServiceStartName
= NULL
;
2933 LPWSTR lpDependencies
= NULL
;
2934 DWORD dwDependenciesLength
= 0;
2935 DWORD dwRequiredSize
;
2936 WCHAR lpEmptyString
[] = {0,0};
2939 DPRINT("RQueryServiceConfigW() called\n");
2942 return ERROR_SHUTDOWN_IN_PROGRESS
;
2944 hSvc
= ScmGetServiceFromHandle(hService
);
2947 DPRINT1("Invalid service handle!\n");
2948 return ERROR_INVALID_HANDLE
;
2951 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
2952 SERVICE_QUERY_CONFIG
))
2954 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
2955 return ERROR_ACCESS_DENIED
;
2958 lpService
= hSvc
->ServiceEntry
;
2959 if (lpService
== NULL
)
2961 DPRINT("lpService == NULL!\n");
2962 return ERROR_INVALID_HANDLE
;
2965 /* Lock the service database shared */
2966 ScmLockDatabaseShared();
2968 dwError
= ScmOpenServiceKey(lpService
->lpServiceName
,
2971 if (dwError
!= ERROR_SUCCESS
)
2974 /* Read the image path */
2975 dwError
= ScmReadString(hServiceKey
,
2978 if (dwError
!= ERROR_SUCCESS
)
2981 /* Read the service start name */
2982 ScmReadString(hServiceKey
,
2984 &lpServiceStartName
);
2986 /* Read the dependencies */
2987 ScmReadDependencies(hServiceKey
,
2989 &dwDependenciesLength
);
2991 dwRequiredSize
= sizeof(QUERY_SERVICE_CONFIGW
);
2993 if (lpImagePath
!= NULL
)
2994 dwRequiredSize
+= (DWORD
)((wcslen(lpImagePath
) + 1) * sizeof(WCHAR
));
2996 dwRequiredSize
+= 2 * sizeof(WCHAR
);
2998 if ((lpService
->lpGroup
!= NULL
) && (lpService
->lpGroup
->lpGroupName
!= NULL
))
2999 dwRequiredSize
+= (DWORD
)((wcslen(lpService
->lpGroup
->lpGroupName
) + 1) * sizeof(WCHAR
));
3001 dwRequiredSize
+= 2 * sizeof(WCHAR
);
3003 if (lpDependencies
!= NULL
)
3004 dwRequiredSize
+= dwDependenciesLength
* sizeof(WCHAR
);
3006 dwRequiredSize
+= 2 * sizeof(WCHAR
);
3008 if (lpServiceStartName
!= NULL
)
3009 dwRequiredSize
+= (DWORD
)((wcslen(lpServiceStartName
) + 1) * sizeof(WCHAR
));
3011 dwRequiredSize
+= 2 * sizeof(WCHAR
);
3013 if (lpService
->lpDisplayName
!= NULL
)
3014 dwRequiredSize
+= (DWORD
)((wcslen(lpService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
3016 dwRequiredSize
+= 2 * sizeof(WCHAR
);
3018 if (lpServiceConfig
== NULL
|| cbBufSize
< dwRequiredSize
)
3020 dwError
= ERROR_INSUFFICIENT_BUFFER
;
3024 lpServiceConfig
->dwServiceType
= lpService
->Status
.dwServiceType
;
3025 lpServiceConfig
->dwStartType
= lpService
->dwStartType
;
3026 lpServiceConfig
->dwErrorControl
= lpService
->dwErrorControl
;
3027 lpServiceConfig
->dwTagId
= lpService
->dwTag
;
3029 lpStr
= (LPWSTR
)(lpServiceConfig
+ 1);
3031 /* Append the image path */
3032 if (lpImagePath
!= NULL
)
3034 wcscpy(lpStr
, lpImagePath
);
3038 wcscpy(lpStr
, lpEmptyString
);
3041 lpServiceConfig
->lpBinaryPathName
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
3042 lpStr
+= (wcslen(lpStr
) + 1);
3044 /* Append the group name */
3045 if ((lpService
->lpGroup
!= NULL
) && (lpService
->lpGroup
->lpGroupName
!= NULL
))
3047 wcscpy(lpStr
, lpService
->lpGroup
->lpGroupName
);
3051 wcscpy(lpStr
, lpEmptyString
);
3054 lpServiceConfig
->lpLoadOrderGroup
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
3055 lpStr
+= (wcslen(lpStr
) + 1);
3057 /* Append Dependencies */
3058 if (lpDependencies
!= NULL
)
3062 dwDependenciesLength
* sizeof(WCHAR
));
3066 wcscpy(lpStr
, lpEmptyString
);
3069 lpServiceConfig
->lpDependencies
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
3070 if (lpDependencies
!= NULL
)
3071 lpStr
+= dwDependenciesLength
;
3073 lpStr
+= (wcslen(lpStr
) + 1);
3075 /* Append the service start name */
3076 if (lpServiceStartName
!= NULL
)
3078 wcscpy(lpStr
, lpServiceStartName
);
3082 wcscpy(lpStr
, lpEmptyString
);
3085 lpServiceConfig
->lpServiceStartName
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
3086 lpStr
+= (wcslen(lpStr
) + 1);
3088 /* Append the display name */
3089 if (lpService
->lpDisplayName
!= NULL
)
3091 wcscpy(lpStr
, lpService
->lpDisplayName
);
3095 wcscpy(lpStr
, lpEmptyString
);
3098 lpServiceConfig
->lpDisplayName
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
3101 if (pcbBytesNeeded
!= NULL
)
3102 *pcbBytesNeeded
= dwRequiredSize
;
3105 /* Unlock the service database */
3106 ScmUnlockDatabase();
3108 if (lpImagePath
!= NULL
)
3109 HeapFree(GetProcessHeap(), 0, lpImagePath
);
3111 if (lpServiceStartName
!= NULL
)
3112 HeapFree(GetProcessHeap(), 0, lpServiceStartName
);
3114 if (lpDependencies
!= NULL
)
3115 HeapFree(GetProcessHeap(), 0, lpDependencies
);
3117 if (hServiceKey
!= NULL
)
3118 RegCloseKey(hServiceKey
);
3120 DPRINT("RQueryServiceConfigW() done\n");
3129 RQueryServiceLockStatusW(
3130 SC_RPC_HANDLE hSCManager
,
3131 LPBYTE lpBuf
, // LPQUERY_SERVICE_LOCK_STATUSW lpLockStatus,
3133 LPBOUNDED_DWORD_4K pcbBytesNeeded
)
3135 LPQUERY_SERVICE_LOCK_STATUSW lpLockStatus
= (LPQUERY_SERVICE_LOCK_STATUSW
)lpBuf
;
3136 PMANAGER_HANDLE hMgr
;
3137 DWORD dwRequiredSize
;
3139 if (!lpLockStatus
|| !pcbBytesNeeded
)
3140 return ERROR_INVALID_PARAMETER
;
3142 hMgr
= ScmGetServiceManagerFromHandle(hSCManager
);
3145 DPRINT1("Invalid service manager handle!\n");
3146 return ERROR_INVALID_HANDLE
;
3149 if (!RtlAreAllAccessesGranted(hMgr
->Handle
.DesiredAccess
,
3150 SC_MANAGER_QUERY_LOCK_STATUS
))
3152 DPRINT("Insufficient access rights! 0x%lx\n", hMgr
->Handle
.DesiredAccess
);
3153 return ERROR_ACCESS_DENIED
;
3156 /* FIXME: we need to compute instead the real length of the owner name */
3157 dwRequiredSize
= sizeof(QUERY_SERVICE_LOCK_STATUSW
) + sizeof(WCHAR
);
3158 *pcbBytesNeeded
= dwRequiredSize
;
3160 if (cbBufSize
< dwRequiredSize
)
3161 return ERROR_INSUFFICIENT_BUFFER
;
3163 ScmQueryServiceLockStatusW(lpLockStatus
);
3165 return ERROR_SUCCESS
;
3173 SC_RPC_HANDLE hService
,
3175 LPSTRING_PTRSW argv
)
3177 DWORD dwError
= ERROR_SUCCESS
;
3178 PSERVICE_HANDLE hSvc
;
3179 PSERVICE lpService
= NULL
;
3184 DPRINT("RStartServiceW(%p %lu %p) called\n", hService
, argc
, argv
);
3185 DPRINT(" argc: %lu\n", argc
);
3188 for (i
= 0; i
< argc
; i
++)
3190 DPRINT(" argv[%lu]: %S\n", i
, argv
[i
].StringPtr
);
3196 return ERROR_SHUTDOWN_IN_PROGRESS
;
3198 hSvc
= ScmGetServiceFromHandle(hService
);
3201 DPRINT1("Invalid service handle!\n");
3202 return ERROR_INVALID_HANDLE
;
3205 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
3208 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
3209 return ERROR_ACCESS_DENIED
;
3212 lpService
= hSvc
->ServiceEntry
;
3213 if (lpService
== NULL
)
3215 DPRINT("lpService == NULL!\n");
3216 return ERROR_INVALID_HANDLE
;
3219 if (lpService
->dwStartType
== SERVICE_DISABLED
)
3220 return ERROR_SERVICE_DISABLED
;
3222 if (lpService
->bDeleted
)
3223 return ERROR_SERVICE_MARKED_FOR_DELETE
;
3225 /* Start the service */
3226 dwError
= ScmStartService(lpService
, argc
, (LPWSTR
*)argv
);
3235 RGetServiceDisplayNameW(
3236 SC_RPC_HANDLE hSCManager
,
3237 LPCWSTR lpServiceName
,
3238 LPWSTR lpDisplayName
,
3241 // PMANAGER_HANDLE hManager;
3246 DPRINT("RGetServiceDisplayNameW() called\n");
3247 DPRINT("hSCManager = %p\n", hSCManager
);
3248 DPRINT("lpServiceName: %S\n", lpServiceName
);
3249 DPRINT("lpDisplayName: %p\n", lpDisplayName
);
3250 DPRINT("*lpcchBuffer: %lu\n", *lpcchBuffer
);
3252 // hManager = (PMANAGER_HANDLE)hSCManager;
3253 // if (hManager->Handle.Tag != MANAGER_TAG)
3255 // DPRINT("Invalid manager handle!\n");
3256 // return ERROR_INVALID_HANDLE;
3259 /* Get service database entry */
3260 lpService
= ScmGetServiceEntryByName(lpServiceName
);
3261 if (lpService
== NULL
)
3263 DPRINT("Could not find a service!\n");
3265 /* If the service could not be found and lpcchBuffer is less than 2, windows
3266 puts null in lpDisplayName and puts 2 in lpcchBuffer */
3267 if (*lpcchBuffer
< 2)
3270 if (lpDisplayName
!= NULL
)
3276 return ERROR_SERVICE_DOES_NOT_EXIST
;
3279 if (!lpService
->lpDisplayName
)
3281 dwLength
= (DWORD
)wcslen(lpService
->lpServiceName
);
3283 if (lpDisplayName
!= NULL
&&
3284 *lpcchBuffer
> dwLength
)
3286 wcscpy(lpDisplayName
, lpService
->lpServiceName
);
3291 dwLength
= (DWORD
)wcslen(lpService
->lpDisplayName
);
3293 if (lpDisplayName
!= NULL
&&
3294 *lpcchBuffer
> dwLength
)
3296 wcscpy(lpDisplayName
, lpService
->lpDisplayName
);
3300 dwError
= (*lpcchBuffer
> dwLength
) ? ERROR_SUCCESS
: ERROR_INSUFFICIENT_BUFFER
;
3302 *lpcchBuffer
= dwLength
;
3311 RGetServiceKeyNameW(
3312 SC_RPC_HANDLE hSCManager
,
3313 LPCWSTR lpDisplayName
,
3314 LPWSTR lpServiceName
,
3317 // PMANAGER_HANDLE hManager;
3322 DPRINT("RGetServiceKeyNameW() called\n");
3323 DPRINT("hSCManager = %p\n", hSCManager
);
3324 DPRINT("lpDisplayName: %S\n", lpDisplayName
);
3325 DPRINT("lpServiceName: %p\n", lpServiceName
);
3326 DPRINT("*lpcchBuffer: %lu\n", *lpcchBuffer
);
3328 // hManager = (PMANAGER_HANDLE)hSCManager;
3329 // if (hManager->Handle.Tag != MANAGER_TAG)
3331 // DPRINT("Invalid manager handle!\n");
3332 // return ERROR_INVALID_HANDLE;
3335 /* Get service database entry */
3336 lpService
= ScmGetServiceEntryByDisplayName(lpDisplayName
);
3337 if (lpService
== NULL
)
3339 DPRINT("Could not find a service!\n");
3341 /* If the service could not be found and lpcchBuffer is less than 2, windows
3342 puts null in lpDisplayName and puts 2 in lpcchBuffer */
3343 if (*lpcchBuffer
< 2)
3346 if (lpServiceName
!= NULL
)
3352 return ERROR_SERVICE_DOES_NOT_EXIST
;
3355 dwLength
= (DWORD
)wcslen(lpService
->lpServiceName
);
3357 if (lpServiceName
!= NULL
&&
3358 *lpcchBuffer
> dwLength
)
3360 wcscpy(lpServiceName
, lpService
->lpServiceName
);
3361 *lpcchBuffer
= dwLength
;
3362 return ERROR_SUCCESS
;
3365 dwError
= (*lpcchBuffer
> dwLength
) ? ERROR_SUCCESS
: ERROR_INSUFFICIENT_BUFFER
;
3367 *lpcchBuffer
= dwLength
;
3376 RI_ScSetServiceBitsA(
3377 RPC_SERVICE_STATUS_HANDLE hServiceStatus
,
3378 DWORD dwServiceBits
,
3380 int bUpdateImmediately
,
3384 return ERROR_SHUTDOWN_IN_PROGRESS
;
3386 if (lpString
!= NULL
)
3387 return ERROR_INVALID_PARAMETER
;
3389 return RI_ScSetServiceBitsW(hServiceStatus
,
3400 RChangeServiceConfigA(
3401 SC_RPC_HANDLE hService
,
3402 DWORD dwServiceType
,
3404 DWORD dwErrorControl
,
3405 LPSTR lpBinaryPathName
,
3406 LPSTR lpLoadOrderGroup
,
3408 LPBYTE lpDependencies
,
3410 LPSTR lpServiceStartName
,
3413 LPSTR lpDisplayName
)
3415 DWORD dwError
= ERROR_SUCCESS
;
3416 LPWSTR lpBinaryPathNameW
= NULL
;
3417 LPWSTR lpLoadOrderGroupW
= NULL
;
3418 LPWSTR lpDependenciesW
= NULL
;
3419 LPWSTR lpServiceStartNameW
= NULL
;
3420 LPWSTR lpDisplayNameW
= NULL
;
3421 DWORD dwDependenciesLength
= 0;
3426 if (lpBinaryPathName
)
3428 len
= MultiByteToWideChar(CP_ACP
, 0, lpBinaryPathName
, -1, NULL
, 0);
3429 lpBinaryPathNameW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3430 if (!lpBinaryPathNameW
)
3432 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3435 MultiByteToWideChar(CP_ACP
, 0, lpBinaryPathName
, -1, lpBinaryPathNameW
, len
);
3438 if (lpLoadOrderGroup
)
3440 len
= MultiByteToWideChar(CP_ACP
, 0, lpLoadOrderGroup
, -1, NULL
, 0);
3441 lpLoadOrderGroupW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3442 if (!lpLoadOrderGroupW
)
3444 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3447 MultiByteToWideChar(CP_ACP
, 0, lpLoadOrderGroup
, -1, lpLoadOrderGroupW
, len
);
3452 lpStr
= (LPCSTR
)lpDependencies
;
3455 cchLength
= strlen(lpStr
) + 1;
3456 dwDependenciesLength
+= (DWORD
)cchLength
;
3457 lpStr
= lpStr
+ cchLength
;
3459 dwDependenciesLength
++;
3461 lpDependenciesW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, dwDependenciesLength
* sizeof(WCHAR
));
3462 if (!lpDependenciesW
)
3464 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3467 MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)lpDependencies
, dwDependenciesLength
, lpDependenciesW
, dwDependenciesLength
);
3470 if (lpServiceStartName
)
3472 len
= MultiByteToWideChar(CP_ACP
, 0, lpServiceStartName
, -1, NULL
, 0);
3473 lpServiceStartNameW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3474 if (!lpServiceStartNameW
)
3476 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3479 MultiByteToWideChar(CP_ACP
, 0, lpServiceStartName
, -1, lpServiceStartNameW
, len
);
3484 len
= MultiByteToWideChar(CP_ACP
, 0, lpDisplayName
, -1, NULL
, 0);
3485 lpDisplayNameW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3486 if (!lpDisplayNameW
)
3488 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3491 MultiByteToWideChar(CP_ACP
, 0, lpDisplayName
, -1, lpDisplayNameW
, len
);
3494 dwError
= RChangeServiceConfigW(hService
,
3501 (LPBYTE
)lpDependenciesW
,
3502 dwDependenciesLength
,
3503 lpServiceStartNameW
,
3509 if (lpBinaryPathNameW
!= NULL
)
3510 HeapFree(GetProcessHeap(), 0, lpBinaryPathNameW
);
3512 if (lpLoadOrderGroupW
!= NULL
)
3513 HeapFree(GetProcessHeap(), 0, lpLoadOrderGroupW
);
3515 if (lpDependenciesW
!= NULL
)
3516 HeapFree(GetProcessHeap(), 0, lpDependenciesW
);
3518 if (lpServiceStartNameW
!= NULL
)
3519 HeapFree(GetProcessHeap(), 0, lpServiceStartNameW
);
3521 if (lpDisplayNameW
!= NULL
)
3522 HeapFree(GetProcessHeap(), 0, lpDisplayNameW
);
3532 SC_RPC_HANDLE hSCManager
,
3533 LPSTR lpServiceName
,
3534 LPSTR lpDisplayName
,
3535 DWORD dwDesiredAccess
,
3536 DWORD dwServiceType
,
3538 DWORD dwErrorControl
,
3539 LPSTR lpBinaryPathName
,
3540 LPSTR lpLoadOrderGroup
,
3542 LPBYTE lpDependencies
,
3544 LPSTR lpServiceStartName
,
3547 LPSC_RPC_HANDLE lpServiceHandle
)
3549 DWORD dwError
= ERROR_SUCCESS
;
3550 LPWSTR lpServiceNameW
= NULL
;
3551 LPWSTR lpDisplayNameW
= NULL
;
3552 LPWSTR lpBinaryPathNameW
= NULL
;
3553 LPWSTR lpLoadOrderGroupW
= NULL
;
3554 LPWSTR lpDependenciesW
= NULL
;
3555 LPWSTR lpServiceStartNameW
= NULL
;
3556 DWORD dwDependenciesLength
= 0;
3563 len
= MultiByteToWideChar(CP_ACP
, 0, lpServiceName
, -1, NULL
, 0);
3564 lpServiceNameW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3565 if (!lpServiceNameW
)
3567 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3570 MultiByteToWideChar(CP_ACP
, 0, lpServiceName
, -1, lpServiceNameW
, len
);
3575 len
= MultiByteToWideChar(CP_ACP
, 0, lpDisplayName
, -1, NULL
, 0);
3576 lpDisplayNameW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3577 if (!lpDisplayNameW
)
3579 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3582 MultiByteToWideChar(CP_ACP
, 0, lpDisplayName
, -1, lpDisplayNameW
, len
);
3585 if (lpBinaryPathName
)
3587 len
= MultiByteToWideChar(CP_ACP
, 0, lpBinaryPathName
, -1, NULL
, 0);
3588 lpBinaryPathNameW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3589 if (!lpBinaryPathNameW
)
3591 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3594 MultiByteToWideChar(CP_ACP
, 0, lpBinaryPathName
, -1, lpBinaryPathNameW
, len
);
3597 if (lpLoadOrderGroup
)
3599 len
= MultiByteToWideChar(CP_ACP
, 0, lpLoadOrderGroup
, -1, NULL
, 0);
3600 lpLoadOrderGroupW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3601 if (!lpLoadOrderGroupW
)
3603 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3606 MultiByteToWideChar(CP_ACP
, 0, lpLoadOrderGroup
, -1, lpLoadOrderGroupW
, len
);
3611 lpStr
= (LPCSTR
)lpDependencies
;
3614 cchLength
= strlen(lpStr
) + 1;
3615 dwDependenciesLength
+= (DWORD
)cchLength
;
3616 lpStr
= lpStr
+ cchLength
;
3618 dwDependenciesLength
++;
3620 lpDependenciesW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, dwDependenciesLength
* sizeof(WCHAR
));
3621 if (!lpDependenciesW
)
3623 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3626 MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)lpDependencies
, dwDependenciesLength
, lpDependenciesW
, dwDependenciesLength
);
3629 if (lpServiceStartName
)
3631 len
= MultiByteToWideChar(CP_ACP
, 0, lpServiceStartName
, -1, NULL
, 0);
3632 lpServiceStartNameW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
3633 if (!lpServiceStartNameW
)
3635 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3638 MultiByteToWideChar(CP_ACP
, 0, lpServiceStartName
, -1, lpServiceStartNameW
, len
);
3641 dwError
= RCreateServiceW(hSCManager
,
3651 (LPBYTE
)lpDependenciesW
,
3652 dwDependenciesLength
,
3653 lpServiceStartNameW
,
3659 if (lpServiceNameW
!=NULL
)
3660 HeapFree(GetProcessHeap(), 0, lpServiceNameW
);
3662 if (lpDisplayNameW
!= NULL
)
3663 HeapFree(GetProcessHeap(), 0, lpDisplayNameW
);
3665 if (lpBinaryPathNameW
!= NULL
)
3666 HeapFree(GetProcessHeap(), 0, lpBinaryPathNameW
);
3668 if (lpLoadOrderGroupW
!= NULL
)
3669 HeapFree(GetProcessHeap(), 0, lpLoadOrderGroupW
);
3671 if (lpDependenciesW
!= NULL
)
3672 HeapFree(GetProcessHeap(), 0, lpDependenciesW
);
3674 if (lpServiceStartNameW
!= NULL
)
3675 HeapFree(GetProcessHeap(), 0, lpServiceStartNameW
);
3684 REnumDependentServicesA(
3685 SC_RPC_HANDLE hService
,
3686 DWORD dwServiceState
,
3689 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
3690 LPBOUNDED_DWORD_256K lpServicesReturned
)
3692 DWORD dwError
= ERROR_SUCCESS
;
3693 DWORD dwServicesReturned
= 0;
3694 DWORD dwServiceCount
;
3695 HKEY hServicesKey
= NULL
;
3696 PSERVICE_HANDLE hSvc
;
3697 PSERVICE lpService
= NULL
;
3698 PSERVICE
*lpServicesArray
= NULL
;
3699 LPENUM_SERVICE_STATUSA lpServicesPtr
= NULL
;
3702 *pcbBytesNeeded
= 0;
3703 *lpServicesReturned
= 0;
3705 DPRINT("REnumDependentServicesA() called\n");
3707 hSvc
= ScmGetServiceFromHandle(hService
);
3710 DPRINT1("Invalid service handle!\n");
3711 return ERROR_INVALID_HANDLE
;
3714 lpService
= hSvc
->ServiceEntry
;
3716 /* Check access rights */
3717 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
3718 SC_MANAGER_ENUMERATE_SERVICE
))
3720 DPRINT("Insufficient access rights! 0x%lx\n",
3721 hSvc
->Handle
.DesiredAccess
);
3722 return ERROR_ACCESS_DENIED
;
3725 /* Open the Services Reg key */
3726 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
3727 L
"System\\CurrentControlSet\\Services",
3732 if (dwError
!= ERROR_SUCCESS
)
3735 /* NOTE: Windows calculates the pcbBytesNeeded based on WCHAR strings for
3736 both EnumDependentServicesA and EnumDependentServicesW. So returned pcbBytesNeeded
3737 are the same for both. Verified in WINXP. */
3739 /* First determine the bytes needed and get the number of dependent services*/
3740 dwError
= Int_EnumDependentServicesW(hServicesKey
,
3745 &dwServicesReturned
);
3746 if (dwError
!= ERROR_SUCCESS
)
3749 /* If buffer size is less than the bytes needed or pointer is null*/
3750 if ((!lpServices
) || (cbBufSize
< *pcbBytesNeeded
))
3752 dwError
= ERROR_MORE_DATA
;
3756 /* Allocate memory for array of service pointers */
3757 lpServicesArray
= HeapAlloc(GetProcessHeap(),
3759 (dwServicesReturned
+ 1) * sizeof(PSERVICE
));
3760 if (!lpServicesArray
)
3762 DPRINT("Could not allocate a buffer!!\n");
3763 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
3767 dwServicesReturned
= 0;
3768 *pcbBytesNeeded
= 0;
3770 dwError
= Int_EnumDependentServicesW(hServicesKey
,
3775 &dwServicesReturned
);
3776 if (dwError
!= ERROR_SUCCESS
)
3781 lpServicesPtr
= (LPENUM_SERVICE_STATUSA
)lpServices
;
3782 lpStr
= (LPSTR
)(lpServices
+ (dwServicesReturned
* sizeof(ENUM_SERVICE_STATUSA
)));
3784 /* Copy EnumDepenedentService to Buffer */
3785 for (dwServiceCount
= 0; dwServiceCount
< dwServicesReturned
; dwServiceCount
++)
3787 lpService
= lpServicesArray
[dwServiceCount
];
3789 /* Copy the status info */
3790 memcpy(&lpServicesPtr
->ServiceStatus
,
3792 sizeof(SERVICE_STATUS
));
3794 /* Copy display name */
3795 WideCharToMultiByte(CP_ACP
,
3797 lpService
->lpDisplayName
,
3800 (int)wcslen(lpService
->lpDisplayName
),
3803 lpServicesPtr
->lpDisplayName
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServices
);
3804 lpStr
+= strlen(lpStr
) + 1;
3806 /* Copy service name */
3807 WideCharToMultiByte(CP_ACP
,
3809 lpService
->lpServiceName
,
3812 (int)wcslen(lpService
->lpServiceName
),
3815 lpServicesPtr
->lpServiceName
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServices
);
3816 lpStr
+= strlen(lpStr
) + 1;
3821 *lpServicesReturned
= dwServicesReturned
;
3824 if (lpServicesArray
)
3825 HeapFree(GetProcessHeap(), 0, lpServicesArray
);
3827 RegCloseKey(hServicesKey
);
3829 DPRINT("REnumDependentServicesA() done (Error %lu)\n", dwError
);
3838 REnumServicesStatusA(
3839 SC_RPC_HANDLE hSCManager
,
3840 DWORD dwServiceType
,
3841 DWORD dwServiceState
,
3844 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
3845 LPBOUNDED_DWORD_256K lpServicesReturned
,
3846 LPBOUNDED_DWORD_256K lpResumeHandle
)
3848 LPENUM_SERVICE_STATUSW lpStatusPtrW
= NULL
;
3849 LPENUM_SERVICE_STATUSW lpStatusPtrIncrW
;
3850 LPENUM_SERVICE_STATUSA lpStatusPtrA
= NULL
;
3851 LPWSTR lpStringPtrW
;
3854 DWORD dwServiceCount
;
3856 DPRINT("REnumServicesStatusA() called\n");
3858 if (pcbBytesNeeded
== NULL
|| lpServicesReturned
== NULL
)
3860 return ERROR_INVALID_ADDRESS
;
3863 if ((dwBufSize
> 0) && (lpBuffer
))
3865 lpStatusPtrW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, dwBufSize
);
3868 DPRINT("Failed to allocate buffer!\n");
3869 return ERROR_NOT_ENOUGH_MEMORY
;
3873 dwError
= REnumServicesStatusW(hSCManager
,
3876 (LPBYTE
)lpStatusPtrW
,
3882 /* if no services were returned then we are Done */
3883 if (*lpServicesReturned
== 0)
3886 lpStatusPtrIncrW
= lpStatusPtrW
;
3887 lpStatusPtrA
= (LPENUM_SERVICE_STATUSA
)lpBuffer
;
3888 lpStringPtrA
= (LPSTR
)((ULONG_PTR
)lpBuffer
+
3889 *lpServicesReturned
* sizeof(ENUM_SERVICE_STATUSA
));
3890 lpStringPtrW
= (LPWSTR
)((ULONG_PTR
)lpStatusPtrW
+
3891 *lpServicesReturned
* sizeof(ENUM_SERVICE_STATUSW
));
3893 for (dwServiceCount
= 0; dwServiceCount
< *lpServicesReturned
; dwServiceCount
++)
3895 /* Copy the service name */
3896 WideCharToMultiByte(CP_ACP
,
3901 (int)wcslen(lpStringPtrW
),
3905 lpStatusPtrA
->lpServiceName
= (LPSTR
)((ULONG_PTR
)lpStringPtrA
- (ULONG_PTR
)lpBuffer
);
3906 lpStringPtrA
+= wcslen(lpStringPtrW
) + 1;
3907 lpStringPtrW
+= wcslen(lpStringPtrW
) + 1;
3909 /* Copy the display name */
3910 WideCharToMultiByte(CP_ACP
,
3915 (int)wcslen(lpStringPtrW
),
3919 lpStatusPtrA
->lpDisplayName
= (LPSTR
)((ULONG_PTR
)lpStringPtrA
- (ULONG_PTR
)lpBuffer
);
3920 lpStringPtrA
+= wcslen(lpStringPtrW
) + 1;
3921 lpStringPtrW
+= wcslen(lpStringPtrW
) + 1;
3923 /* Copy the status information */
3924 memcpy(&lpStatusPtrA
->ServiceStatus
,
3925 &lpStatusPtrIncrW
->ServiceStatus
,
3926 sizeof(SERVICE_STATUS
));
3934 HeapFree(GetProcessHeap(), 0, lpStatusPtrW
);
3936 DPRINT("REnumServicesStatusA() done (Error %lu)\n", dwError
);
3946 LPSTR lpMachineName
,
3947 LPSTR lpDatabaseName
,
3948 DWORD dwDesiredAccess
,
3949 LPSC_RPC_HANDLE lpScHandle
)
3951 UNICODE_STRING MachineName
;
3952 UNICODE_STRING DatabaseName
;
3955 DPRINT("ROpenSCManagerA() called\n");
3958 RtlCreateUnicodeStringFromAsciiz(&MachineName
,
3962 RtlCreateUnicodeStringFromAsciiz(&DatabaseName
,
3965 dwError
= ROpenSCManagerW(lpMachineName
? MachineName
.Buffer
: NULL
,
3966 lpDatabaseName
? DatabaseName
.Buffer
: NULL
,
3971 RtlFreeUnicodeString(&MachineName
);
3974 RtlFreeUnicodeString(&DatabaseName
);
3984 SC_RPC_HANDLE hSCManager
,
3985 LPSTR lpServiceName
,
3986 DWORD dwDesiredAccess
,
3987 LPSC_RPC_HANDLE lpServiceHandle
)
3989 UNICODE_STRING ServiceName
;
3992 DPRINT("ROpenServiceA() called\n");
3995 RtlCreateUnicodeStringFromAsciiz(&ServiceName
,
3998 dwError
= ROpenServiceW(hSCManager
,
3999 lpServiceName
? ServiceName
.Buffer
: NULL
,
4004 RtlFreeUnicodeString(&ServiceName
);
4013 RQueryServiceConfigA(
4014 SC_RPC_HANDLE hService
,
4015 LPBYTE lpBuf
, //LPQUERY_SERVICE_CONFIGA lpServiceConfig,
4017 LPBOUNDED_DWORD_8K pcbBytesNeeded
)
4019 LPQUERY_SERVICE_CONFIGA lpServiceConfig
= (LPQUERY_SERVICE_CONFIGA
)lpBuf
;
4020 DWORD dwError
= ERROR_SUCCESS
;
4021 PSERVICE_HANDLE hSvc
;
4022 PSERVICE lpService
= NULL
;
4023 HKEY hServiceKey
= NULL
;
4024 LPWSTR lpImagePath
= NULL
;
4025 LPWSTR lpServiceStartName
= NULL
;
4026 LPWSTR lpDependencies
= NULL
;
4027 DWORD dwDependenciesLength
= 0;
4028 DWORD dwRequiredSize
;
4029 CHAR lpEmptyString
[]={0,0};
4032 DPRINT("RQueryServiceConfigA() called\n");
4035 return ERROR_SHUTDOWN_IN_PROGRESS
;
4037 hSvc
= ScmGetServiceFromHandle(hService
);
4040 DPRINT1("Invalid service handle!\n");
4041 return ERROR_INVALID_HANDLE
;
4044 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
4045 SERVICE_QUERY_CONFIG
))
4047 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
4048 return ERROR_ACCESS_DENIED
;
4051 lpService
= hSvc
->ServiceEntry
;
4052 if (lpService
== NULL
)
4054 DPRINT("lpService == NULL!\n");
4055 return ERROR_INVALID_HANDLE
;
4058 /* Lock the service database shared */
4059 ScmLockDatabaseShared();
4061 dwError
= ScmOpenServiceKey(lpService
->lpServiceName
,
4064 if (dwError
!= ERROR_SUCCESS
)
4067 /* Read the image path */
4068 dwError
= ScmReadString(hServiceKey
,
4071 if (dwError
!= ERROR_SUCCESS
)
4074 /* Read the service start name */
4075 ScmReadString(hServiceKey
,
4077 &lpServiceStartName
);
4079 /* Read the dependencies */
4080 ScmReadDependencies(hServiceKey
,
4082 &dwDependenciesLength
);
4084 dwRequiredSize
= sizeof(QUERY_SERVICE_CONFIGA
);
4086 if (lpImagePath
!= NULL
)
4087 dwRequiredSize
+= (DWORD
)(wcslen(lpImagePath
) + 1);
4089 dwRequiredSize
+= 2;
4091 if ((lpService
->lpGroup
!= NULL
) && (lpService
->lpGroup
->lpGroupName
!= NULL
))
4092 dwRequiredSize
+= (DWORD
)(wcslen(lpService
->lpGroup
->lpGroupName
) + 1);
4094 dwRequiredSize
+= 2;
4096 /* Add Dependencies length */
4097 if (lpDependencies
!= NULL
)
4098 dwRequiredSize
+= dwDependenciesLength
;
4100 dwRequiredSize
+= 2;
4102 if (lpServiceStartName
!= NULL
)
4103 dwRequiredSize
+= (DWORD
)(wcslen(lpServiceStartName
) + 1);
4105 dwRequiredSize
+= 2;
4107 if (lpService
->lpDisplayName
!= NULL
)
4108 dwRequiredSize
+= (DWORD
)(wcslen(lpService
->lpDisplayName
) + 1);
4110 dwRequiredSize
+= 2;
4112 if (lpServiceConfig
== NULL
|| cbBufSize
< dwRequiredSize
)
4114 dwError
= ERROR_INSUFFICIENT_BUFFER
;
4118 lpServiceConfig
->dwServiceType
= lpService
->Status
.dwServiceType
;
4119 lpServiceConfig
->dwStartType
= lpService
->dwStartType
;
4120 lpServiceConfig
->dwErrorControl
= lpService
->dwErrorControl
;
4121 lpServiceConfig
->dwTagId
= lpService
->dwTag
;
4123 lpStr
= (LPSTR
)(lpServiceConfig
+ 1);
4125 /* NOTE: Strings that are NULL for QUERY_SERVICE_CONFIG are pointers to empty strings.
4126 Verified in WINXP */
4130 WideCharToMultiByte(CP_ACP
,
4135 (int)(wcslen(lpImagePath
) + 1),
4141 strcpy(lpStr
, lpEmptyString
);
4144 lpServiceConfig
->lpBinaryPathName
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
4145 lpStr
+= (strlen((LPSTR
)lpStr
) + 1);
4147 if (lpService
->lpGroup
&& lpService
->lpGroup
->lpGroupName
)
4149 WideCharToMultiByte(CP_ACP
,
4151 lpService
->lpGroup
->lpGroupName
,
4154 (int)(wcslen(lpService
->lpGroup
->lpGroupName
) + 1),
4160 strcpy(lpStr
, lpEmptyString
);
4163 lpServiceConfig
->lpLoadOrderGroup
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
4164 lpStr
+= (strlen(lpStr
) + 1);
4166 /* Append Dependencies */
4169 WideCharToMultiByte(CP_ACP
,
4172 dwDependenciesLength
,
4174 dwDependenciesLength
,
4180 strcpy(lpStr
, lpEmptyString
);
4183 lpServiceConfig
->lpDependencies
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
4185 lpStr
+= dwDependenciesLength
;
4187 lpStr
+= (strlen(lpStr
) + 1);
4189 if (lpServiceStartName
)
4191 WideCharToMultiByte(CP_ACP
,
4196 (int)(wcslen(lpServiceStartName
) + 1),
4202 strcpy(lpStr
, lpEmptyString
);
4205 lpServiceConfig
->lpServiceStartName
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
4206 lpStr
+= (strlen(lpStr
) + 1);
4208 if (lpService
->lpDisplayName
)
4210 WideCharToMultiByte(CP_ACP
,
4212 lpService
->lpDisplayName
,
4215 (int)(wcslen(lpService
->lpDisplayName
) + 1),
4221 strcpy(lpStr
, lpEmptyString
);
4224 lpServiceConfig
->lpDisplayName
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceConfig
);
4227 if (pcbBytesNeeded
!= NULL
)
4228 *pcbBytesNeeded
= dwRequiredSize
;
4231 /* Unlock the service database */
4232 ScmUnlockDatabase();
4234 if (lpImagePath
!= NULL
)
4235 HeapFree(GetProcessHeap(), 0, lpImagePath
);
4237 if (lpServiceStartName
!= NULL
)
4238 HeapFree(GetProcessHeap(), 0, lpServiceStartName
);
4240 if (lpDependencies
!= NULL
)
4241 HeapFree(GetProcessHeap(), 0, lpDependencies
);
4243 if (hServiceKey
!= NULL
)
4244 RegCloseKey(hServiceKey
);
4246 DPRINT("RQueryServiceConfigA() done\n");
4255 RQueryServiceLockStatusA(
4256 SC_RPC_HANDLE hSCManager
,
4257 LPBYTE lpBuf
, // LPQUERY_SERVICE_LOCK_STATUSA lpLockStatus,
4259 LPBOUNDED_DWORD_4K pcbBytesNeeded
)
4261 LPQUERY_SERVICE_LOCK_STATUSA lpLockStatus
= (LPQUERY_SERVICE_LOCK_STATUSA
)lpBuf
;
4262 PMANAGER_HANDLE hMgr
;
4263 DWORD dwRequiredSize
;
4265 if (!lpLockStatus
|| !pcbBytesNeeded
)
4266 return ERROR_INVALID_PARAMETER
;
4268 hMgr
= ScmGetServiceManagerFromHandle(hSCManager
);
4271 DPRINT1("Invalid service manager handle!\n");
4272 return ERROR_INVALID_HANDLE
;
4275 if (!RtlAreAllAccessesGranted(hMgr
->Handle
.DesiredAccess
,
4276 SC_MANAGER_QUERY_LOCK_STATUS
))
4278 DPRINT("Insufficient access rights! 0x%lx\n", hMgr
->Handle
.DesiredAccess
);
4279 return ERROR_ACCESS_DENIED
;
4282 /* FIXME: we need to compute instead the real length of the owner name */
4283 dwRequiredSize
= sizeof(QUERY_SERVICE_LOCK_STATUSA
) + sizeof(CHAR
);
4284 *pcbBytesNeeded
= dwRequiredSize
;
4286 if (cbBufSize
< dwRequiredSize
)
4287 return ERROR_INSUFFICIENT_BUFFER
;
4289 ScmQueryServiceLockStatusA(lpLockStatus
);
4291 return ERROR_SUCCESS
;
4299 SC_RPC_HANDLE hService
,
4301 LPSTRING_PTRSA argv
)
4303 DWORD dwError
= ERROR_SUCCESS
;
4304 PSERVICE_HANDLE hSvc
;
4305 PSERVICE lpService
= NULL
;
4306 LPWSTR
*lpVector
= NULL
;
4310 DPRINT("RStartServiceA() called\n");
4313 return ERROR_SHUTDOWN_IN_PROGRESS
;
4315 hSvc
= ScmGetServiceFromHandle(hService
);
4318 DPRINT1("Invalid service handle!\n");
4319 return ERROR_INVALID_HANDLE
;
4322 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
4325 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
4326 return ERROR_ACCESS_DENIED
;
4329 lpService
= hSvc
->ServiceEntry
;
4330 if (lpService
== NULL
)
4332 DPRINT("lpService == NULL!\n");
4333 return ERROR_INVALID_HANDLE
;
4336 if (lpService
->dwStartType
== SERVICE_DISABLED
)
4337 return ERROR_SERVICE_DISABLED
;
4339 if (lpService
->bDeleted
)
4340 return ERROR_SERVICE_MARKED_FOR_DELETE
;
4342 /* Build a Unicode argument vector */
4345 lpVector
= HeapAlloc(GetProcessHeap(),
4347 argc
* sizeof(LPWSTR
));
4348 if (lpVector
== NULL
)
4349 return ERROR_NOT_ENOUGH_MEMORY
;
4351 for (i
= 0; i
< argc
; i
++)
4353 dwLength
= MultiByteToWideChar(CP_ACP
,
4360 lpVector
[i
] = HeapAlloc(GetProcessHeap(),
4362 dwLength
* sizeof(WCHAR
));
4363 if (lpVector
[i
] == NULL
)
4365 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
4369 MultiByteToWideChar(CP_ACP
,
4378 /* Start the service */
4379 dwError
= ScmStartService(lpService
, argc
, lpVector
);
4382 /* Free the Unicode argument vector */
4383 if (lpVector
!= NULL
)
4385 for (i
= 0; i
< argc
; i
++)
4387 if (lpVector
[i
] != NULL
)
4388 HeapFree(GetProcessHeap(), 0, lpVector
[i
]);
4390 HeapFree(GetProcessHeap(), 0, lpVector
);
4400 RGetServiceDisplayNameA(
4401 SC_RPC_HANDLE hSCManager
,
4402 LPCSTR lpServiceName
,
4403 LPSTR lpDisplayName
,
4404 LPBOUNDED_DWORD_4K lpcchBuffer
)
4406 // PMANAGER_HANDLE hManager;
4407 PSERVICE lpService
= NULL
;
4410 LPWSTR lpServiceNameW
;
4412 DPRINT("RGetServiceDisplayNameA() called\n");
4413 DPRINT("hSCManager = %p\n", hSCManager
);
4414 DPRINT("lpServiceName: %s\n", lpServiceName
);
4415 DPRINT("lpDisplayName: %p\n", lpDisplayName
);
4416 DPRINT("*lpcchBuffer: %lu\n", *lpcchBuffer
);
4418 // hManager = (PMANAGER_HANDLE)hSCManager;
4419 // if (hManager->Handle.Tag != MANAGER_TAG)
4421 // DPRINT("Invalid manager handle!\n");
4422 // return ERROR_INVALID_HANDLE;
4425 if (lpServiceName
!= NULL
)
4427 dwLength
= (DWORD
)(strlen(lpServiceName
) + 1);
4428 lpServiceNameW
= HeapAlloc(GetProcessHeap(),
4430 dwLength
* sizeof(WCHAR
));
4431 if (!lpServiceNameW
)
4432 return ERROR_NOT_ENOUGH_MEMORY
;
4434 MultiByteToWideChar(CP_ACP
,
4441 lpService
= ScmGetServiceEntryByName(lpServiceNameW
);
4443 HeapFree(GetProcessHeap(), 0, lpServiceNameW
);
4446 if (lpService
== NULL
)
4448 DPRINT("Could not find a service!\n");
4450 /* If the service could not be found and lpcchBuffer is 0, windows
4451 puts null in lpDisplayName and puts 1 in lpcchBuffer */
4452 if (*lpcchBuffer
== 0)
4455 if (lpDisplayName
!= NULL
)
4460 return ERROR_SERVICE_DOES_NOT_EXIST
;
4463 if (!lpService
->lpDisplayName
)
4465 dwLength
= (DWORD
)wcslen(lpService
->lpServiceName
);
4466 if (lpDisplayName
!= NULL
&&
4467 *lpcchBuffer
> dwLength
)
4469 WideCharToMultiByte(CP_ACP
,
4471 lpService
->lpServiceName
,
4472 (int)wcslen(lpService
->lpServiceName
),
4477 return ERROR_SUCCESS
;
4482 dwLength
= (DWORD
)wcslen(lpService
->lpDisplayName
);
4483 if (lpDisplayName
!= NULL
&&
4484 *lpcchBuffer
> dwLength
)
4486 WideCharToMultiByte(CP_ACP
,
4488 lpService
->lpDisplayName
,
4489 (int)wcslen(lpService
->lpDisplayName
),
4494 return ERROR_SUCCESS
;
4498 dwError
= (*lpcchBuffer
> dwLength
) ? ERROR_SUCCESS
: ERROR_INSUFFICIENT_BUFFER
;
4500 *lpcchBuffer
= dwLength
* 2;
4509 RGetServiceKeyNameA(
4510 SC_RPC_HANDLE hSCManager
,
4511 LPCSTR lpDisplayName
,
4512 LPSTR lpServiceName
,
4513 LPBOUNDED_DWORD_4K lpcchBuffer
)
4518 LPWSTR lpDisplayNameW
;
4520 DPRINT("RGetServiceKeyNameA() called\n");
4521 DPRINT("hSCManager = %p\n", hSCManager
);
4522 DPRINT("lpDisplayName: %s\n", lpDisplayName
);
4523 DPRINT("lpServiceName: %p\n", lpServiceName
);
4524 DPRINT("*lpcchBuffer: %lu\n", *lpcchBuffer
);
4526 dwLength
= (DWORD
)(strlen(lpDisplayName
) + 1);
4527 lpDisplayNameW
= HeapAlloc(GetProcessHeap(),
4529 dwLength
* sizeof(WCHAR
));
4530 if (!lpDisplayNameW
)
4531 return ERROR_NOT_ENOUGH_MEMORY
;
4533 MultiByteToWideChar(CP_ACP
,
4540 lpService
= ScmGetServiceEntryByDisplayName(lpDisplayNameW
);
4542 HeapFree(GetProcessHeap(), 0, lpDisplayNameW
);
4544 if (lpService
== NULL
)
4546 DPRINT("Could not find the service!\n");
4548 /* If the service could not be found and lpcchBuffer is 0,
4549 put null in lpDisplayName and puts 1 in lpcchBuffer, verified WINXP. */
4550 if (*lpcchBuffer
== 0)
4553 if (lpServiceName
!= NULL
)
4559 return ERROR_SERVICE_DOES_NOT_EXIST
;
4562 dwLength
= (DWORD
)wcslen(lpService
->lpServiceName
);
4563 if (lpServiceName
!= NULL
&&
4564 *lpcchBuffer
> dwLength
)
4566 WideCharToMultiByte(CP_ACP
,
4568 lpService
->lpServiceName
,
4569 (int)wcslen(lpService
->lpServiceName
),
4574 return ERROR_SUCCESS
;
4577 dwError
= (*lpcchBuffer
> dwLength
) ? ERROR_SUCCESS
: ERROR_INSUFFICIENT_BUFFER
;
4579 *lpcchBuffer
= dwLength
* 2;
4588 RI_ScGetCurrentGroupStateW(
4589 SC_RPC_HANDLE hSCManager
,
4590 LPWSTR lpLoadOrderGroup
,
4593 PMANAGER_HANDLE hManager
;
4594 PSERVICE_GROUP pServiceGroup
;
4595 DWORD dwError
= ERROR_SUCCESS
;
4597 DPRINT("RI_ScGetCurrentGroupStateW() called\n");
4600 return ERROR_SHUTDOWN_IN_PROGRESS
;
4602 hManager
= ScmGetServiceManagerFromHandle(hSCManager
);
4603 if (hManager
== NULL
)
4605 DPRINT1("Invalid service manager handle!\n");
4606 return ERROR_INVALID_HANDLE
;
4609 /* Check for SC_MANAGER_ENUMERATE_SERVICE access right */
4610 if (!RtlAreAllAccessesGranted(hManager
->Handle
.DesiredAccess
,
4611 SC_MANAGER_ENUMERATE_SERVICE
))
4613 DPRINT("Insufficient access rights! 0x%lx\n",
4614 hManager
->Handle
.DesiredAccess
);
4615 return ERROR_ACCESS_DENIED
;
4618 /* Lock the service database shared */
4619 ScmLockDatabaseShared();
4621 /* Get the group list entry */
4622 pServiceGroup
= ScmGetServiceGroupByName(lpLoadOrderGroup
);
4623 if (pServiceGroup
== NULL
)
4625 dwError
= ERROR_SERVICE_DOES_NOT_EXIST
;
4629 /* FIXME: Return the group state */
4633 /* Unlock the service database */
4634 ScmUnlockDatabase();
4636 DPRINT("RI_ScGetCurrentGroupStateW() done (Error %lu)\n", dwError
);
4646 SC_RPC_HANDLE hSCManager
,
4647 DWORD dwServiceType
,
4648 DWORD dwServiceState
,
4651 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
4652 LPBOUNDED_DWORD_256K lpServicesReturned
,
4653 LPBOUNDED_DWORD_256K lpResumeIndex
,
4654 LPCWSTR pszGroupName
)
4656 PMANAGER_HANDLE hManager
;
4658 DWORD dwError
= ERROR_SUCCESS
;
4659 PLIST_ENTRY ServiceEntry
;
4660 PSERVICE CurrentService
;
4662 DWORD dwRequiredSize
;
4663 DWORD dwServiceCount
;
4665 DWORD dwLastResumeCount
= 0;
4666 LPENUM_SERVICE_STATUSW lpStatusPtr
;
4669 DPRINT("REnumServiceGroupW() called\n");
4672 return ERROR_SHUTDOWN_IN_PROGRESS
;
4674 hManager
= ScmGetServiceManagerFromHandle(hSCManager
);
4675 if (hManager
== NULL
)
4677 DPRINT1("Invalid service manager handle!\n");
4678 return ERROR_INVALID_HANDLE
;
4681 if (pcbBytesNeeded
== NULL
|| lpServicesReturned
== NULL
)
4683 return ERROR_INVALID_ADDRESS
;
4686 *pcbBytesNeeded
= 0;
4687 *lpServicesReturned
= 0;
4689 if ((dwServiceType
== 0) ||
4690 ((dwServiceType
& ~SERVICE_TYPE_ALL
) != 0))
4692 DPRINT("Not a valid Service Type!\n");
4693 return ERROR_INVALID_PARAMETER
;
4696 if ((dwServiceState
== 0) ||
4697 ((dwServiceState
& ~SERVICE_STATE_ALL
) != 0))
4699 DPRINT("Not a valid Service State!\n");
4700 return ERROR_INVALID_PARAMETER
;
4703 /* Check access rights */
4704 if (!RtlAreAllAccessesGranted(hManager
->Handle
.DesiredAccess
,
4705 SC_MANAGER_ENUMERATE_SERVICE
))
4707 DPRINT("Insufficient access rights! 0x%lx\n",
4708 hManager
->Handle
.DesiredAccess
);
4709 return ERROR_ACCESS_DENIED
;
4713 dwLastResumeCount
= *lpResumeIndex
;
4715 /* Lock the service database shared */
4716 ScmLockDatabaseShared();
4718 lpService
= ScmGetServiceEntryByResumeCount(dwLastResumeCount
);
4719 if (lpService
== NULL
)
4721 dwError
= ERROR_SUCCESS
;
4728 for (ServiceEntry
= &lpService
->ServiceListEntry
;
4729 ServiceEntry
!= &ServiceListHead
;
4730 ServiceEntry
= ServiceEntry
->Flink
)
4732 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
4736 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
4739 dwState
= SERVICE_ACTIVE
;
4740 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
4741 dwState
= SERVICE_INACTIVE
;
4743 if ((dwState
& dwServiceState
) == 0)
4748 if (*pszGroupName
== 0)
4750 if (CurrentService
->lpGroup
!= NULL
)
4755 if ((CurrentService
->lpGroup
== NULL
) ||
4756 _wcsicmp(pszGroupName
, CurrentService
->lpGroup
->lpGroupName
) != 0)
4761 dwSize
= sizeof(ENUM_SERVICE_STATUSW
) +
4762 (DWORD
)((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
4763 (DWORD
)((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
4765 if (dwRequiredSize
+ dwSize
> cbBufSize
)
4767 DPRINT("Service name: %S no fit\n", CurrentService
->lpServiceName
);
4771 DPRINT("Service name: %S fit\n", CurrentService
->lpServiceName
);
4772 dwRequiredSize
+= dwSize
;
4774 dwLastResumeCount
= CurrentService
->dwResumeCount
;
4777 DPRINT("dwRequiredSize: %lu\n", dwRequiredSize
);
4778 DPRINT("dwServiceCount: %lu\n", dwServiceCount
);
4781 ServiceEntry
!= &ServiceListHead
;
4782 ServiceEntry
= ServiceEntry
->Flink
)
4784 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
4788 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
4791 dwState
= SERVICE_ACTIVE
;
4792 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
4793 dwState
= SERVICE_INACTIVE
;
4795 if ((dwState
& dwServiceState
) == 0)
4800 if (*pszGroupName
== 0)
4802 if (CurrentService
->lpGroup
!= NULL
)
4807 if ((CurrentService
->lpGroup
== NULL
) ||
4808 _wcsicmp(pszGroupName
, CurrentService
->lpGroup
->lpGroupName
) != 0)
4813 dwRequiredSize
+= (sizeof(ENUM_SERVICE_STATUSW
) +
4814 (DWORD
)((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
4815 (DWORD
)((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
)));
4817 dwError
= ERROR_MORE_DATA
;
4820 DPRINT("*pcbBytesNeeded: %lu\n", dwRequiredSize
);
4823 *lpResumeIndex
= dwLastResumeCount
;
4825 *lpServicesReturned
= dwServiceCount
;
4826 *pcbBytesNeeded
= dwRequiredSize
;
4828 lpStatusPtr
= (LPENUM_SERVICE_STATUSW
)lpBuffer
;
4829 lpStringPtr
= (LPWSTR
)((ULONG_PTR
)lpBuffer
+
4830 dwServiceCount
* sizeof(ENUM_SERVICE_STATUSW
));
4833 for (ServiceEntry
= &lpService
->ServiceListEntry
;
4834 ServiceEntry
!= &ServiceListHead
;
4835 ServiceEntry
= ServiceEntry
->Flink
)
4837 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
4841 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
4844 dwState
= SERVICE_ACTIVE
;
4845 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
4846 dwState
= SERVICE_INACTIVE
;
4848 if ((dwState
& dwServiceState
) == 0)
4853 if (*pszGroupName
== 0)
4855 if (CurrentService
->lpGroup
!= NULL
)
4860 if ((CurrentService
->lpGroup
== NULL
) ||
4861 _wcsicmp(pszGroupName
, CurrentService
->lpGroup
->lpGroupName
) != 0)
4866 dwSize
= sizeof(ENUM_SERVICE_STATUSW
) +
4867 (DWORD
)((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
4868 (DWORD
)((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
4870 if (dwRequiredSize
+ dwSize
> cbBufSize
)
4873 /* Copy the service name */
4874 wcscpy(lpStringPtr
, CurrentService
->lpServiceName
);
4875 lpStatusPtr
->lpServiceName
= (LPWSTR
)((ULONG_PTR
)lpStringPtr
- (ULONG_PTR
)lpBuffer
);
4876 lpStringPtr
+= (wcslen(CurrentService
->lpServiceName
) + 1);
4878 /* Copy the display name */
4879 wcscpy(lpStringPtr
, CurrentService
->lpDisplayName
);
4880 lpStatusPtr
->lpDisplayName
= (LPWSTR
)((ULONG_PTR
)lpStringPtr
- (ULONG_PTR
)lpBuffer
);
4881 lpStringPtr
+= (wcslen(CurrentService
->lpDisplayName
) + 1);
4883 /* Copy the status information */
4884 memcpy(&lpStatusPtr
->ServiceStatus
,
4885 &CurrentService
->Status
,
4886 sizeof(SERVICE_STATUS
));
4889 dwRequiredSize
+= dwSize
;
4892 if (dwError
== ERROR_SUCCESS
)
4894 *pcbBytesNeeded
= 0;
4895 if (lpResumeIndex
) *lpResumeIndex
= 0;
4899 /* Unlock the service database */
4900 ScmUnlockDatabase();
4902 DPRINT("REnumServiceGroupW() done (Error %lu)\n", dwError
);
4911 RChangeServiceConfig2A(
4912 SC_RPC_HANDLE hService
,
4913 SC_RPC_CONFIG_INFOA Info
)
4915 SC_RPC_CONFIG_INFOW InfoW
= { 0 };
4916 DWORD dwRet
, dwLength
;
4919 DPRINT("RChangeServiceConfig2A() called\n");
4920 DPRINT("dwInfoLevel = %lu\n", Info
.dwInfoLevel
);
4922 if ((Info
.dwInfoLevel
< SERVICE_CONFIG_DESCRIPTION
) ||
4923 (Info
.dwInfoLevel
> SERVICE_CONFIG_FAILURE_ACTIONS
))
4924 return ERROR_INVALID_LEVEL
;
4926 InfoW
.dwInfoLevel
= Info
.dwInfoLevel
;
4928 if (InfoW
.dwInfoLevel
== SERVICE_CONFIG_DESCRIPTION
)
4930 LPSERVICE_DESCRIPTIONW lpServiceDescriptionW
;
4931 LPSERVICE_DESCRIPTIONA lpServiceDescriptionA
;
4933 lpServiceDescriptionA
= Info
.psd
;
4935 if (lpServiceDescriptionA
&&
4936 lpServiceDescriptionA
->lpDescription
)
4938 dwLength
= (DWORD
)((strlen(lpServiceDescriptionA
->lpDescription
) + 1) * sizeof(WCHAR
));
4940 lpServiceDescriptionW
= HeapAlloc(GetProcessHeap(),
4942 dwLength
+ sizeof(SERVICE_DESCRIPTIONW
));
4943 if (!lpServiceDescriptionW
)
4945 return ERROR_NOT_ENOUGH_MEMORY
;
4948 lpServiceDescriptionW
->lpDescription
= (LPWSTR
)(lpServiceDescriptionW
+ 1);
4950 MultiByteToWideChar(CP_ACP
,
4952 lpServiceDescriptionA
->lpDescription
,
4954 lpServiceDescriptionW
->lpDescription
,
4957 ptr
= lpServiceDescriptionW
;
4958 InfoW
.psd
= lpServiceDescriptionW
;
4961 else if (Info
.dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
4963 LPSERVICE_FAILURE_ACTIONSW lpServiceFailureActionsW
;
4964 LPSERVICE_FAILURE_ACTIONSA lpServiceFailureActionsA
;
4965 DWORD dwRebootLen
= 0;
4966 DWORD dwCommandLen
= 0;
4967 DWORD dwActionArrayLen
= 0;
4968 LPWSTR lpStr
= NULL
;
4970 lpServiceFailureActionsA
= Info
.psfa
;
4972 if (lpServiceFailureActionsA
)
4975 * The following code is inspired by the
4976 * SERVICE_CONFIG_FAILURE_ACTIONS case of
4977 * the RQueryServiceConfig2W function.
4980 /* Retrieve the needed length for the two data strings */
4981 if (lpServiceFailureActionsA
->lpRebootMsg
)
4983 dwRebootLen
= (DWORD
)((strlen(lpServiceFailureActionsA
->lpRebootMsg
) + 1) * sizeof(WCHAR
));
4985 if (lpServiceFailureActionsA
->lpCommand
)
4987 dwCommandLen
= (DWORD
)((strlen(lpServiceFailureActionsA
->lpCommand
) + 1) * sizeof(WCHAR
));
4991 * Retrieve the size of the lpsaActions array if needed.
4992 * We will copy the lpsaActions array only if there is at
4993 * least one action AND that the original array is valid.
4995 if (lpServiceFailureActionsA
->cActions
> 0 && lpServiceFailureActionsA
->lpsaActions
)
4997 dwActionArrayLen
= lpServiceFailureActionsA
->cActions
* sizeof(SC_ACTION
);
5000 /* Compute the total length for the UNICODE structure, including data */
5001 dwLength
= sizeof(SERVICE_FAILURE_ACTIONSW
) +
5002 dwActionArrayLen
+ dwRebootLen
+ dwCommandLen
;
5004 /* Allocate the structure */
5005 lpServiceFailureActionsW
= HeapAlloc(GetProcessHeap(),
5008 if (!lpServiceFailureActionsW
)
5010 return ERROR_NOT_ENOUGH_MEMORY
;
5013 /* Copy the members */
5014 lpServiceFailureActionsW
->dwResetPeriod
= lpServiceFailureActionsA
->dwResetPeriod
;
5015 lpServiceFailureActionsW
->cActions
= lpServiceFailureActionsA
->cActions
;
5017 /* Copy the lpsaActions array if needed */
5018 if (dwActionArrayLen
> 0)
5020 /* The storage zone is just after the end of the SERVICE_FAILURE_ACTIONSW structure */
5021 lpServiceFailureActionsW
->lpsaActions
= (LPSC_ACTION
)((ULONG_PTR
)(lpServiceFailureActionsW
+ 1));
5023 /* dwActionArrayLen == lpServiceFailureActionsW->cActions * sizeof(SC_ACTION) */
5024 RtlCopyMemory(lpServiceFailureActionsW
->lpsaActions
,
5025 lpServiceFailureActionsA
->lpsaActions
,
5030 /* No lpsaActions array */
5031 lpServiceFailureActionsW
->lpsaActions
= NULL
;
5033 /* The data strings are stored just after the lpsaActions array */
5034 lpStr
= (LPWSTR
)((ULONG_PTR
)(lpServiceFailureActionsW
+ 1) + dwActionArrayLen
);
5037 * Convert the data strings to UNICODE
5040 lpServiceFailureActionsW
->lpRebootMsg
= NULL
;
5041 lpServiceFailureActionsW
->lpCommand
= NULL
;
5045 /* lpRebootMsg points just after the lpsaActions array */
5046 lpServiceFailureActionsW
->lpRebootMsg
= lpStr
;
5048 MultiByteToWideChar(CP_ACP
,
5050 lpServiceFailureActionsA
->lpRebootMsg
,
5052 lpServiceFailureActionsW
->lpRebootMsg
,
5055 lpStr
+= dwRebootLen
/ sizeof(WCHAR
);
5060 /* lpRebootMsg points just after the lpRebootMsg data string */
5061 lpServiceFailureActionsW
->lpCommand
= lpStr
;
5063 MultiByteToWideChar(CP_ACP
,
5065 lpServiceFailureActionsA
->lpCommand
,
5067 lpServiceFailureActionsW
->lpCommand
,
5071 /* Set the pointers */
5072 ptr
= lpServiceFailureActionsW
;
5073 InfoW
.psfa
= lpServiceFailureActionsW
;
5077 dwRet
= RChangeServiceConfig2W(hService
, InfoW
);
5079 HeapFree(GetProcessHeap(), 0, ptr
);
5086 ScmSetFailureActions(HKEY hServiceKey
,
5087 LPSERVICE_FAILURE_ACTIONSW lpFailureActions
)
5089 LPSERVICE_FAILURE_ACTIONSW lpReadBuffer
= NULL
;
5090 LPSERVICE_FAILURE_ACTIONSW lpWriteBuffer
= NULL
;
5091 DWORD dwRequiredSize
= 0;
5095 /* There is nothing to be done if we have no failure actions */
5096 if (lpFailureActions
== NULL
)
5097 return ERROR_SUCCESS
;
5100 * 1- Retrieve the original value of FailureActions.
5103 /* Query value length */
5104 dwError
= RegQueryValueExW(hServiceKey
,
5110 if (dwError
!= ERROR_SUCCESS
&&
5111 dwError
!= ERROR_MORE_DATA
&&
5112 dwError
!= ERROR_FILE_NOT_FOUND
)
5115 dwRequiredSize
= (dwType
== REG_BINARY
) ? max(sizeof(SERVICE_FAILURE_ACTIONSW
), dwRequiredSize
)
5116 : sizeof(SERVICE_FAILURE_ACTIONSW
);
5118 /* Initialize the read buffer */
5119 lpReadBuffer
= HeapAlloc(GetProcessHeap(),
5122 if (lpReadBuffer
== NULL
)
5123 return ERROR_NOT_ENOUGH_MEMORY
;
5125 /* Now we can fill the read buffer */
5126 if (dwError
!= ERROR_FILE_NOT_FOUND
&&
5127 dwType
== REG_BINARY
)
5129 dwError
= RegQueryValueExW(hServiceKey
,
5133 (LPBYTE
)lpReadBuffer
,
5135 if (dwError
!= ERROR_SUCCESS
&&
5136 dwError
!= ERROR_FILE_NOT_FOUND
)
5139 if (dwRequiredSize
< sizeof(SERVICE_FAILURE_ACTIONSW
))
5140 dwRequiredSize
= sizeof(SERVICE_FAILURE_ACTIONSW
);
5145 * The value of the error doesn't really matter, the only
5146 * important thing is that it must be != ERROR_SUCCESS.
5148 dwError
= ERROR_INVALID_DATA
;
5151 if (dwError
== ERROR_SUCCESS
)
5153 lpReadBuffer
->cActions
= min(lpReadBuffer
->cActions
, (dwRequiredSize
- sizeof(SERVICE_FAILURE_ACTIONSW
)) / sizeof(SC_ACTION
));
5154 lpReadBuffer
->lpsaActions
= (lpReadBuffer
->cActions
> 0 ? (LPSC_ACTION
)(lpReadBuffer
+ 1) : NULL
);
5158 lpReadBuffer
->dwResetPeriod
= 0;
5159 lpReadBuffer
->cActions
= 0;
5160 lpReadBuffer
->lpsaActions
= NULL
;
5163 lpReadBuffer
->lpRebootMsg
= NULL
;
5164 lpReadBuffer
->lpCommand
= NULL
;
5167 * 2- Initialize the new value to set.
5170 dwRequiredSize
= sizeof(SERVICE_FAILURE_ACTIONSW
);
5172 if (lpFailureActions
->lpsaActions
== NULL
)
5175 * lpFailureActions->cActions is ignored.
5176 * Therefore we use the original values
5177 * of cActions and lpsaActions.
5179 dwRequiredSize
+= lpReadBuffer
->cActions
* sizeof(SC_ACTION
);
5184 * The reset period and array of failure actions
5185 * are deleted if lpFailureActions->cActions == 0 .
5187 dwRequiredSize
+= lpFailureActions
->cActions
* sizeof(SC_ACTION
);
5190 lpWriteBuffer
= HeapAlloc(GetProcessHeap(),
5193 if (lpWriteBuffer
== NULL
)
5195 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
5199 /* Clean the pointers as they have no meaning when the structure is stored in the registry */
5200 lpWriteBuffer
->lpRebootMsg
= NULL
;
5201 lpWriteBuffer
->lpCommand
= NULL
;
5202 lpWriteBuffer
->lpsaActions
= NULL
;
5204 /* Set the members */
5205 if (lpFailureActions
->lpsaActions
== NULL
)
5208 * lpFailureActions->dwResetPeriod and lpFailureActions->cActions are ignored.
5209 * Therefore we use the original values of dwResetPeriod, cActions and lpsaActions.
5211 lpWriteBuffer
->dwResetPeriod
= lpReadBuffer
->dwResetPeriod
;
5212 lpWriteBuffer
->cActions
= lpReadBuffer
->cActions
;
5214 if (lpReadBuffer
->lpsaActions
!= NULL
)
5216 memmove(lpWriteBuffer
+ 1,
5217 lpReadBuffer
->lpsaActions
,
5218 lpReadBuffer
->cActions
* sizeof(SC_ACTION
));
5223 if (lpFailureActions
->cActions
> 0)
5225 lpWriteBuffer
->dwResetPeriod
= lpFailureActions
->dwResetPeriod
;
5226 lpWriteBuffer
->cActions
= lpFailureActions
->cActions
;
5228 memmove(lpWriteBuffer
+ 1,
5229 lpFailureActions
->lpsaActions
,
5230 lpFailureActions
->cActions
* sizeof(SC_ACTION
));
5234 /* The reset period and array of failure actions are deleted */
5235 lpWriteBuffer
->dwResetPeriod
= 0;
5236 lpWriteBuffer
->cActions
= 0;
5240 /* Save the new failure actions into the registry */
5241 dwError
= RegSetValueExW(hServiceKey
,
5245 (LPBYTE
)lpWriteBuffer
,
5248 /* We modify the strings only in case of success.*/
5249 if (dwError
== ERROR_SUCCESS
)
5251 /* Modify the Reboot Message value, if specified */
5252 if (lpFailureActions
->lpRebootMsg
!= NULL
)
5254 /* If the Reboot Message is "" then we delete it */
5255 if (*lpFailureActions
->lpRebootMsg
== 0)
5257 DPRINT("Delete Reboot Message value\n");
5258 RegDeleteValueW(hServiceKey
, L
"RebootMessage");
5262 DPRINT("Setting Reboot Message value %S\n", lpFailureActions
->lpRebootMsg
);
5263 RegSetValueExW(hServiceKey
,
5267 (LPBYTE
)lpFailureActions
->lpRebootMsg
,
5268 (DWORD
)((wcslen(lpFailureActions
->lpRebootMsg
) + 1) * sizeof(WCHAR
)));
5272 /* Modify the Failure Command value, if specified */
5273 if (lpFailureActions
->lpCommand
!= NULL
)
5275 /* If the FailureCommand string is an empty string, delete the value */
5276 if (*lpFailureActions
->lpCommand
== 0)
5278 DPRINT("Delete Failure Command value\n");
5279 RegDeleteValueW(hServiceKey
, L
"FailureCommand");
5283 DPRINT("Setting Failure Command value %S\n", lpFailureActions
->lpCommand
);
5284 RegSetValueExW(hServiceKey
,
5288 (LPBYTE
)lpFailureActions
->lpCommand
,
5289 (DWORD
)((wcslen(lpFailureActions
->lpCommand
) + 1) * sizeof(WCHAR
)));
5295 if (lpWriteBuffer
!= NULL
)
5296 HeapFree(GetProcessHeap(), 0, lpWriteBuffer
);
5298 if (lpReadBuffer
!= NULL
)
5299 HeapFree(GetProcessHeap(), 0, lpReadBuffer
);
5308 RChangeServiceConfig2W(
5309 SC_RPC_HANDLE hService
,
5310 SC_RPC_CONFIG_INFOW Info
)
5312 DWORD dwError
= ERROR_SUCCESS
;
5313 PSERVICE_HANDLE hSvc
;
5314 PSERVICE lpService
= NULL
;
5315 HKEY hServiceKey
= NULL
;
5316 ACCESS_MASK RequiredAccess
= SERVICE_CHANGE_CONFIG
;
5318 DPRINT("RChangeServiceConfig2W() called\n");
5319 DPRINT("dwInfoLevel = %lu\n", Info
.dwInfoLevel
);
5322 return ERROR_SHUTDOWN_IN_PROGRESS
;
5324 if ((Info
.dwInfoLevel
< SERVICE_CONFIG_DESCRIPTION
) ||
5325 (Info
.dwInfoLevel
> SERVICE_CONFIG_FAILURE_ACTIONS
))
5326 return ERROR_INVALID_LEVEL
;
5328 hSvc
= ScmGetServiceFromHandle(hService
);
5331 DPRINT("Invalid service handle!\n");
5332 return ERROR_INVALID_HANDLE
;
5335 if (Info
.dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
5336 RequiredAccess
|= SERVICE_START
;
5338 /* Check the access rights */
5339 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
5342 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
5343 return ERROR_ACCESS_DENIED
;
5346 if (Info
.dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
5348 /* FIXME: Check if the caller has the SE_SHUTDOWN_NAME privilege */
5352 lpService
= hSvc
->ServiceEntry
;
5353 if (lpService
== NULL
)
5355 DPRINT("lpService == NULL!\n");
5356 return ERROR_INVALID_HANDLE
;
5359 /* Failure actions can only be set for Win32 services, not for drivers */
5360 if (Info
.dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
5362 if (lpService
->Status
.dwServiceType
& SERVICE_DRIVER
)
5363 return ERROR_CANNOT_DETECT_DRIVER_FAILURE
;
5366 /* Lock the service database exclusively */
5367 ScmLockDatabaseExclusive();
5369 if (lpService
->bDeleted
)
5371 DPRINT("The service has already been marked for delete!\n");
5372 dwError
= ERROR_SERVICE_MARKED_FOR_DELETE
;
5376 /* Open the service key */
5377 dwError
= ScmOpenServiceKey(lpService
->szServiceName
,
5378 KEY_READ
| KEY_SET_VALUE
,
5380 if (dwError
!= ERROR_SUCCESS
)
5383 if (Info
.dwInfoLevel
== SERVICE_CONFIG_DESCRIPTION
)
5385 LPSERVICE_DESCRIPTIONW lpServiceDescription
= (LPSERVICE_DESCRIPTIONW
)Info
.psd
;
5387 /* Modify the service description, if specified */
5388 if (lpServiceDescription
!= NULL
&&
5389 lpServiceDescription
->lpDescription
!= NULL
)
5391 /* If the description is "" then we delete it */
5392 if (*lpServiceDescription
->lpDescription
== 0)
5394 DPRINT("Delete service description\n");
5395 dwError
= RegDeleteValueW(hServiceKey
, L
"Description");
5397 if (dwError
== ERROR_FILE_NOT_FOUND
)
5398 dwError
= ERROR_SUCCESS
;
5402 DPRINT("Setting service description value %S\n", lpServiceDescription
->lpDescription
);
5403 dwError
= RegSetValueExW(hServiceKey
,
5407 (LPBYTE
)lpServiceDescription
->lpDescription
,
5408 (DWORD
)((wcslen(lpServiceDescription
->lpDescription
) + 1) * sizeof(WCHAR
)));
5413 dwError
= ERROR_SUCCESS
;
5416 else if (Info
.dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
5418 dwError
= ScmSetFailureActions(hServiceKey
,
5419 (LPSERVICE_FAILURE_ACTIONSW
)Info
.psfa
);
5423 if (hServiceKey
!= NULL
)
5424 RegCloseKey(hServiceKey
);
5426 /* Unlock the service database */
5427 ScmUnlockDatabase();
5429 DPRINT("RChangeServiceConfig2W() done (Error %lu)\n", dwError
);
5438 RQueryServiceConfig2A(
5439 SC_RPC_HANDLE hService
,
5443 LPBOUNDED_DWORD_8K pcbBytesNeeded
)
5445 DWORD dwError
= ERROR_SUCCESS
;
5446 PSERVICE_HANDLE hSvc
;
5447 PSERVICE lpService
= NULL
;
5448 HKEY hServiceKey
= NULL
;
5449 DWORD dwRequiredSize
= 0;
5451 LPWSTR lpDescriptionW
= NULL
;
5452 LPWSTR lpRebootMessageW
= NULL
;
5453 LPWSTR lpFailureCommandW
= NULL
;
5455 DPRINT("RQueryServiceConfig2A() called hService %p dwInfoLevel %u, lpBuffer %p cbBufSize %u pcbBytesNeeded %p\n",
5456 hService
, dwInfoLevel
, lpBuffer
, cbBufSize
, pcbBytesNeeded
);
5459 return ERROR_INVALID_ADDRESS
;
5462 return ERROR_SHUTDOWN_IN_PROGRESS
;
5464 if ((dwInfoLevel
< SERVICE_CONFIG_DESCRIPTION
) ||
5465 (dwInfoLevel
> SERVICE_CONFIG_FAILURE_ACTIONS
))
5466 return ERROR_INVALID_LEVEL
;
5468 hSvc
= ScmGetServiceFromHandle(hService
);
5471 DPRINT1("Invalid service handle!\n");
5472 return ERROR_INVALID_HANDLE
;
5475 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
5476 SERVICE_QUERY_CONFIG
))
5478 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
5479 return ERROR_ACCESS_DENIED
;
5482 lpService
= hSvc
->ServiceEntry
;
5483 if (lpService
== NULL
)
5485 DPRINT("lpService == NULL!\n");
5486 return ERROR_INVALID_HANDLE
;
5489 /* Lock the service database shared */
5490 ScmLockDatabaseShared();
5492 dwError
= ScmOpenServiceKey(lpService
->lpServiceName
,
5495 if (dwError
!= ERROR_SUCCESS
)
5498 if (dwInfoLevel
== SERVICE_CONFIG_DESCRIPTION
)
5500 LPSERVICE_DESCRIPTIONA lpServiceDescription
= (LPSERVICE_DESCRIPTIONA
)lpBuffer
;
5503 dwError
= ScmReadString(hServiceKey
,
5506 if (dwError
!= ERROR_SUCCESS
&& dwError
!= ERROR_FILE_NOT_FOUND
)
5509 *pcbBytesNeeded
= sizeof(SERVICE_DESCRIPTIONA
);
5510 if (dwError
== ERROR_SUCCESS
)
5511 *pcbBytesNeeded
+= (DWORD
)((wcslen(lpDescriptionW
) + 1) * sizeof(WCHAR
));
5513 if (cbBufSize
< *pcbBytesNeeded
)
5515 dwError
= ERROR_INSUFFICIENT_BUFFER
;
5519 if (dwError
== ERROR_SUCCESS
)
5521 lpStr
= (LPSTR
)(lpServiceDescription
+ 1);
5523 WideCharToMultiByte(CP_ACP
,
5528 (int)wcslen(lpDescriptionW
),
5531 lpServiceDescription
->lpDescription
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceDescription
);
5535 lpServiceDescription
->lpDescription
= NULL
;
5536 dwError
= ERROR_SUCCESS
;
5539 else if (dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
5541 LPSERVICE_FAILURE_ACTIONSA lpFailureActions
= (LPSERVICE_FAILURE_ACTIONSA
)lpBuffer
;
5544 /* Query value length */
5545 dwError
= RegQueryValueExW(hServiceKey
,
5551 if (dwError
!= ERROR_SUCCESS
&&
5552 dwError
!= ERROR_MORE_DATA
&&
5553 dwError
!= ERROR_FILE_NOT_FOUND
)
5556 dwRequiredSize
= (dwType
== REG_BINARY
) ? max(sizeof(SERVICE_FAILURE_ACTIONSA
), dwRequiredSize
)
5557 : sizeof(SERVICE_FAILURE_ACTIONSA
);
5559 /* Get the strings */
5560 ScmReadString(hServiceKey
,
5562 &lpFailureCommandW
);
5564 ScmReadString(hServiceKey
,
5568 if (lpRebootMessageW
)
5569 dwRequiredSize
+= (DWORD
)((wcslen(lpRebootMessageW
) + 1) * sizeof(WCHAR
));
5571 if (lpFailureCommandW
)
5572 dwRequiredSize
+= (DWORD
)((wcslen(lpFailureCommandW
) + 1) * sizeof(WCHAR
));
5574 if (cbBufSize
< dwRequiredSize
)
5576 *pcbBytesNeeded
= dwRequiredSize
;
5577 dwError
= ERROR_INSUFFICIENT_BUFFER
;
5581 /* Now we can fill the buffer */
5582 if (dwError
!= ERROR_FILE_NOT_FOUND
&& dwType
== REG_BINARY
)
5584 dwError
= RegQueryValueExW(hServiceKey
,
5588 (LPBYTE
)lpFailureActions
,
5590 if (dwError
!= ERROR_SUCCESS
&& dwError
!= ERROR_FILE_NOT_FOUND
)
5593 if (dwRequiredSize
< sizeof(SERVICE_FAILURE_ACTIONSA
))
5594 dwRequiredSize
= sizeof(SERVICE_FAILURE_ACTIONSA
);
5599 * The value of the error doesn't really matter, the only
5600 * important thing is that it must be != ERROR_SUCCESS .
5602 dwError
= ERROR_INVALID_DATA
;
5605 if (dwError
== ERROR_SUCCESS
)
5607 lpFailureActions
->cActions
= min(lpFailureActions
->cActions
, (dwRequiredSize
- sizeof(SERVICE_FAILURE_ACTIONSA
)) / sizeof(SC_ACTION
));
5609 /* Here lpFailureActions->lpsaActions contains an offset. The conversion is done by the caller. */
5610 lpFailureActions
->lpsaActions
= (lpFailureActions
->cActions
> 0 ? (LPSC_ACTION
)(ULONG_PTR
)sizeof(SERVICE_FAILURE_ACTIONSA
) : NULL
);
5612 lpStr
= (LPSTR
)((ULONG_PTR
)(lpFailureActions
+ 1) + lpFailureActions
->cActions
* sizeof(SC_ACTION
));
5616 lpFailureActions
->dwResetPeriod
= 0;
5617 lpFailureActions
->cActions
= 0;
5618 lpFailureActions
->lpsaActions
= NULL
;
5619 lpStr
= (LPSTR
)(lpFailureActions
+ 1);
5622 lpFailureActions
->lpRebootMsg
= NULL
;
5623 lpFailureActions
->lpCommand
= NULL
;
5625 if (lpRebootMessageW
)
5627 WideCharToMultiByte(CP_ACP
,
5632 (int)wcslen(lpRebootMessageW
),
5635 lpFailureActions
->lpRebootMsg
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpFailureActions
);
5636 lpStr
+= strlen(lpStr
) + 1;
5639 if (lpFailureCommandW
)
5641 WideCharToMultiByte(CP_ACP
,
5646 (int)wcslen(lpFailureCommandW
),
5649 lpFailureActions
->lpCommand
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpFailureActions
);
5650 /* lpStr += strlen(lpStr) + 1; */
5653 dwError
= ERROR_SUCCESS
;
5657 /* Unlock the service database */
5658 ScmUnlockDatabase();
5660 if (lpDescriptionW
!= NULL
)
5661 HeapFree(GetProcessHeap(), 0, lpDescriptionW
);
5663 if (lpRebootMessageW
!= NULL
)
5664 HeapFree(GetProcessHeap(), 0, lpRebootMessageW
);
5666 if (lpFailureCommandW
!= NULL
)
5667 HeapFree(GetProcessHeap(), 0, lpFailureCommandW
);
5669 if (hServiceKey
!= NULL
)
5670 RegCloseKey(hServiceKey
);
5672 DPRINT("RQueryServiceConfig2A() done (Error %lu)\n", dwError
);
5681 RQueryServiceConfig2W(
5682 SC_RPC_HANDLE hService
,
5686 LPBOUNDED_DWORD_8K pcbBytesNeeded
)
5688 DWORD dwError
= ERROR_SUCCESS
;
5689 PSERVICE_HANDLE hSvc
;
5690 PSERVICE lpService
= NULL
;
5691 HKEY hServiceKey
= NULL
;
5692 DWORD dwRequiredSize
= 0;
5694 LPWSTR lpDescription
= NULL
;
5695 LPWSTR lpRebootMessage
= NULL
;
5696 LPWSTR lpFailureCommand
= NULL
;
5698 DPRINT("RQueryServiceConfig2W() called\n");
5701 return ERROR_INVALID_ADDRESS
;
5704 return ERROR_SHUTDOWN_IN_PROGRESS
;
5706 if ((dwInfoLevel
< SERVICE_CONFIG_DESCRIPTION
) ||
5707 (dwInfoLevel
> SERVICE_CONFIG_FAILURE_ACTIONS
))
5708 return ERROR_INVALID_LEVEL
;
5710 hSvc
= ScmGetServiceFromHandle(hService
);
5713 DPRINT1("Invalid service handle!\n");
5714 return ERROR_INVALID_HANDLE
;
5717 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
5718 SERVICE_QUERY_CONFIG
))
5720 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
5721 return ERROR_ACCESS_DENIED
;
5724 lpService
= hSvc
->ServiceEntry
;
5725 if (lpService
== NULL
)
5727 DPRINT("lpService == NULL!\n");
5728 return ERROR_INVALID_HANDLE
;
5731 /* Lock the service database shared */
5732 ScmLockDatabaseShared();
5734 dwError
= ScmOpenServiceKey(lpService
->lpServiceName
,
5737 if (dwError
!= ERROR_SUCCESS
)
5740 if (dwInfoLevel
== SERVICE_CONFIG_DESCRIPTION
)
5742 LPSERVICE_DESCRIPTIONW lpServiceDescription
= (LPSERVICE_DESCRIPTIONW
)lpBuffer
;
5745 dwError
= ScmReadString(hServiceKey
,
5748 if (dwError
!= ERROR_SUCCESS
&& dwError
!= ERROR_FILE_NOT_FOUND
)
5751 *pcbBytesNeeded
= sizeof(SERVICE_DESCRIPTIONW
);
5752 if (dwError
== ERROR_SUCCESS
)
5753 *pcbBytesNeeded
+= (DWORD
)((wcslen(lpDescription
) + 1) * sizeof(WCHAR
));
5755 if (cbBufSize
< *pcbBytesNeeded
)
5757 dwError
= ERROR_INSUFFICIENT_BUFFER
;
5761 if (dwError
== ERROR_SUCCESS
)
5763 lpStr
= (LPWSTR
)(lpServiceDescription
+ 1);
5764 wcscpy(lpStr
, lpDescription
);
5765 lpServiceDescription
->lpDescription
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceDescription
);
5769 lpServiceDescription
->lpDescription
= NULL
;
5770 dwError
= ERROR_SUCCESS
;
5773 else if (dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
5775 LPSERVICE_FAILURE_ACTIONSW lpFailureActions
= (LPSERVICE_FAILURE_ACTIONSW
)lpBuffer
;
5776 LPWSTR lpStr
= NULL
;
5778 /* Query value length */
5779 dwError
= RegQueryValueExW(hServiceKey
,
5785 if (dwError
!= ERROR_SUCCESS
&&
5786 dwError
!= ERROR_MORE_DATA
&&
5787 dwError
!= ERROR_FILE_NOT_FOUND
)
5790 dwRequiredSize
= (dwType
== REG_BINARY
) ? max(sizeof(SERVICE_FAILURE_ACTIONSW
), dwRequiredSize
)
5791 : sizeof(SERVICE_FAILURE_ACTIONSW
);
5793 /* Get the strings */
5794 ScmReadString(hServiceKey
,
5798 ScmReadString(hServiceKey
,
5802 if (lpRebootMessage
)
5803 dwRequiredSize
+= (DWORD
)((wcslen(lpRebootMessage
) + 1) * sizeof(WCHAR
));
5805 if (lpFailureCommand
)
5806 dwRequiredSize
+= (DWORD
)((wcslen(lpFailureCommand
) + 1) * sizeof(WCHAR
));
5808 if (cbBufSize
< dwRequiredSize
)
5810 *pcbBytesNeeded
= dwRequiredSize
;
5811 dwError
= ERROR_INSUFFICIENT_BUFFER
;
5815 /* Now we can fill the buffer */
5816 if (dwError
!= ERROR_FILE_NOT_FOUND
&& dwType
== REG_BINARY
)
5818 dwError
= RegQueryValueExW(hServiceKey
,
5822 (LPBYTE
)lpFailureActions
,
5824 if (dwError
!= ERROR_SUCCESS
&& dwError
!= ERROR_FILE_NOT_FOUND
)
5827 if (dwRequiredSize
< sizeof(SERVICE_FAILURE_ACTIONSW
))
5828 dwRequiredSize
= sizeof(SERVICE_FAILURE_ACTIONSW
);
5833 * The value of the error doesn't really matter, the only
5834 * important thing is that it must be != ERROR_SUCCESS .
5836 dwError
= ERROR_INVALID_DATA
;
5839 if (dwError
== ERROR_SUCCESS
)
5841 lpFailureActions
->cActions
= min(lpFailureActions
->cActions
, (dwRequiredSize
- sizeof(SERVICE_FAILURE_ACTIONSW
)) / sizeof(SC_ACTION
));
5843 /* Here lpFailureActions->lpsaActions contains an offset. The conversion is done by the caller. */
5844 lpFailureActions
->lpsaActions
= (lpFailureActions
->cActions
> 0 ? (LPSC_ACTION
)(ULONG_PTR
)sizeof(SERVICE_FAILURE_ACTIONSW
) : NULL
);
5846 lpStr
= (LPWSTR
)((ULONG_PTR
)(lpFailureActions
+ 1) + lpFailureActions
->cActions
* sizeof(SC_ACTION
));
5850 lpFailureActions
->dwResetPeriod
= 0;
5851 lpFailureActions
->cActions
= 0;
5852 lpFailureActions
->lpsaActions
= NULL
;
5853 lpStr
= (LPWSTR
)(lpFailureActions
+ 1);
5856 lpFailureActions
->lpRebootMsg
= NULL
;
5857 lpFailureActions
->lpCommand
= NULL
;
5859 if (lpRebootMessage
)
5861 wcscpy(lpStr
, lpRebootMessage
);
5862 lpFailureActions
->lpRebootMsg
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpFailureActions
);
5863 lpStr
+= wcslen(lpStr
) + 1;
5866 if (lpFailureCommand
)
5868 wcscpy(lpStr
, lpFailureCommand
);
5869 lpFailureActions
->lpCommand
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpFailureActions
);
5870 /* lpStr += wcslen(lpStr) + 1; */
5873 dwError
= ERROR_SUCCESS
;
5877 /* Unlock the service database */
5878 ScmUnlockDatabase();
5880 if (lpDescription
!= NULL
)
5881 HeapFree(GetProcessHeap(), 0, lpDescription
);
5883 if (lpRebootMessage
!= NULL
)
5884 HeapFree(GetProcessHeap(), 0, lpRebootMessage
);
5886 if (lpFailureCommand
!= NULL
)
5887 HeapFree(GetProcessHeap(), 0, lpFailureCommand
);
5889 if (hServiceKey
!= NULL
)
5890 RegCloseKey(hServiceKey
);
5892 DPRINT("RQueryServiceConfig2W() done (Error %lu)\n", dwError
);
5901 RQueryServiceStatusEx(
5902 SC_RPC_HANDLE hService
,
5903 SC_STATUS_TYPE InfoLevel
,
5906 LPBOUNDED_DWORD_8K pcbBytesNeeded
)
5908 LPSERVICE_STATUS_PROCESS lpStatus
;
5909 PSERVICE_HANDLE hSvc
;
5912 DPRINT("RQueryServiceStatusEx() called\n");
5915 return ERROR_SHUTDOWN_IN_PROGRESS
;
5917 if (InfoLevel
!= SC_STATUS_PROCESS_INFO
)
5918 return ERROR_INVALID_LEVEL
;
5920 *pcbBytesNeeded
= sizeof(SERVICE_STATUS_PROCESS
);
5922 if (cbBufSize
< sizeof(SERVICE_STATUS_PROCESS
))
5923 return ERROR_INSUFFICIENT_BUFFER
;
5925 hSvc
= ScmGetServiceFromHandle(hService
);
5928 DPRINT1("Invalid service handle!\n");
5929 return ERROR_INVALID_HANDLE
;
5932 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
5933 SERVICE_QUERY_STATUS
))
5935 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
5936 return ERROR_ACCESS_DENIED
;
5939 lpService
= hSvc
->ServiceEntry
;
5940 if (lpService
== NULL
)
5942 DPRINT("lpService == NULL!\n");
5943 return ERROR_INVALID_HANDLE
;
5946 /* Lock the service database shared */
5947 ScmLockDatabaseShared();
5949 lpStatus
= (LPSERVICE_STATUS_PROCESS
)lpBuffer
;
5951 /* Return service status information */
5952 RtlCopyMemory(lpStatus
,
5954 sizeof(SERVICE_STATUS
));
5956 /* Copy the service process ID */
5957 if ((lpService
->Status
.dwCurrentState
== SERVICE_STOPPED
) || (lpService
->lpImage
== NULL
))
5958 lpStatus
->dwProcessId
= 0;
5960 lpStatus
->dwProcessId
= lpService
->lpImage
->dwProcessId
;
5962 lpStatus
->dwServiceFlags
= 0; /* FIXME */
5964 /* Unlock the service database */
5965 ScmUnlockDatabase();
5967 return ERROR_SUCCESS
;
5974 REnumServicesStatusExA(
5975 SC_RPC_HANDLE hSCManager
,
5976 SC_ENUM_TYPE InfoLevel
,
5977 DWORD dwServiceType
,
5978 DWORD dwServiceState
,
5981 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
5982 LPBOUNDED_DWORD_256K lpServicesReturned
,
5983 LPBOUNDED_DWORD_256K lpResumeIndex
,
5984 LPCSTR pszGroupName
)
5986 LPENUM_SERVICE_STATUS_PROCESSW lpStatusPtrW
= NULL
;
5987 LPENUM_SERVICE_STATUS_PROCESSW lpStatusPtrIncrW
;
5988 LPENUM_SERVICE_STATUS_PROCESSA lpStatusPtrA
= NULL
;
5989 LPWSTR lpStringPtrW
;
5991 LPWSTR pszGroupNameW
= NULL
;
5993 DWORD dwServiceCount
;
5995 DPRINT("REnumServicesStatusExA() called\n");
5997 if (pcbBytesNeeded
== NULL
|| lpServicesReturned
== NULL
)
5999 return ERROR_INVALID_ADDRESS
;
6004 pszGroupNameW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (strlen(pszGroupName
) + 1) * sizeof(WCHAR
));
6007 DPRINT("Failed to allocate buffer!\n");
6008 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
6012 MultiByteToWideChar(CP_ACP
,
6017 (int)(strlen(pszGroupName
) + 1));
6020 if ((cbBufSize
> 0) && (lpBuffer
))
6022 lpStatusPtrW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, cbBufSize
);
6025 DPRINT("Failed to allocate buffer!\n");
6026 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
6031 dwError
= REnumServicesStatusExW(hSCManager
,
6035 (LPBYTE
)lpStatusPtrW
,
6042 /* if no services were returned then we are Done */
6043 if (*lpServicesReturned
== 0)
6046 lpStatusPtrIncrW
= lpStatusPtrW
;
6047 lpStatusPtrA
= (LPENUM_SERVICE_STATUS_PROCESSA
)lpBuffer
;
6048 lpStringPtrA
= (LPSTR
)((ULONG_PTR
)lpBuffer
+
6049 *lpServicesReturned
* sizeof(ENUM_SERVICE_STATUS_PROCESSA
));
6050 lpStringPtrW
= (LPWSTR
)((ULONG_PTR
)lpStatusPtrW
+
6051 *lpServicesReturned
* sizeof(ENUM_SERVICE_STATUS_PROCESSW
));
6053 for (dwServiceCount
= 0; dwServiceCount
< *lpServicesReturned
; dwServiceCount
++)
6055 /* Copy the service name */
6056 WideCharToMultiByte(CP_ACP
,
6061 (int)wcslen(lpStringPtrW
),
6065 lpStatusPtrA
->lpServiceName
= (LPSTR
)((ULONG_PTR
)lpStringPtrA
- (ULONG_PTR
)lpBuffer
);
6066 lpStringPtrA
+= wcslen(lpStringPtrW
) + 1;
6067 lpStringPtrW
+= wcslen(lpStringPtrW
) + 1;
6069 /* Copy the display name */
6070 WideCharToMultiByte(CP_ACP
,
6075 (int)wcslen(lpStringPtrW
),
6079 lpStatusPtrA
->lpDisplayName
= (LPSTR
)((ULONG_PTR
)lpStringPtrA
- (ULONG_PTR
)lpBuffer
);
6080 lpStringPtrA
+= wcslen(lpStringPtrW
) + 1;
6081 lpStringPtrW
+= wcslen(lpStringPtrW
) + 1;
6083 /* Copy the status information */
6084 memcpy(&lpStatusPtrA
->ServiceStatusProcess
,
6085 &lpStatusPtrIncrW
->ServiceStatusProcess
,
6086 sizeof(SERVICE_STATUS
));
6088 /* Copy the service process ID */
6089 lpStatusPtrA
->ServiceStatusProcess
.dwProcessId
= lpStatusPtrIncrW
->ServiceStatusProcess
.dwProcessId
;
6091 lpStatusPtrA
->ServiceStatusProcess
.dwServiceFlags
= 0; /* FIXME */
6099 HeapFree(GetProcessHeap(), 0, pszGroupNameW
);
6102 HeapFree(GetProcessHeap(), 0, lpStatusPtrW
);
6104 DPRINT("REnumServicesStatusExA() done (Error %lu)\n", dwError
);
6113 REnumServicesStatusExW(
6114 SC_RPC_HANDLE hSCManager
,
6115 SC_ENUM_TYPE InfoLevel
,
6116 DWORD dwServiceType
,
6117 DWORD dwServiceState
,
6120 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
6121 LPBOUNDED_DWORD_256K lpServicesReturned
,
6122 LPBOUNDED_DWORD_256K lpResumeIndex
,
6123 LPCWSTR pszGroupName
)
6125 PMANAGER_HANDLE hManager
;
6127 DWORD dwError
= ERROR_SUCCESS
;
6128 PLIST_ENTRY ServiceEntry
;
6129 PSERVICE CurrentService
;
6131 DWORD dwRequiredSize
;
6132 DWORD dwServiceCount
;
6134 DWORD dwLastResumeCount
= 0;
6135 LPENUM_SERVICE_STATUS_PROCESSW lpStatusPtr
;
6138 DPRINT("REnumServicesStatusExW() called\n");
6141 return ERROR_SHUTDOWN_IN_PROGRESS
;
6143 if (InfoLevel
!= SC_ENUM_PROCESS_INFO
)
6144 return ERROR_INVALID_LEVEL
;
6146 hManager
= ScmGetServiceManagerFromHandle(hSCManager
);
6147 if (hManager
== NULL
)
6149 DPRINT1("Invalid service manager handle!\n");
6150 return ERROR_INVALID_HANDLE
;
6153 if (pcbBytesNeeded
== NULL
|| lpServicesReturned
== NULL
)
6155 return ERROR_INVALID_ADDRESS
;
6158 *pcbBytesNeeded
= 0;
6159 *lpServicesReturned
= 0;
6161 if ((dwServiceType
== 0) ||
6162 ((dwServiceType
& ~SERVICE_TYPE_ALL
) != 0))
6164 DPRINT("Not a valid Service Type!\n");
6165 return ERROR_INVALID_PARAMETER
;
6168 if ((dwServiceState
== 0) ||
6169 ((dwServiceState
& ~SERVICE_STATE_ALL
) != 0))
6171 DPRINT("Not a valid Service State!\n");
6172 return ERROR_INVALID_PARAMETER
;
6175 /* Check access rights */
6176 if (!RtlAreAllAccessesGranted(hManager
->Handle
.DesiredAccess
,
6177 SC_MANAGER_ENUMERATE_SERVICE
))
6179 DPRINT("Insufficient access rights! 0x%lx\n",
6180 hManager
->Handle
.DesiredAccess
);
6181 return ERROR_ACCESS_DENIED
;
6185 dwLastResumeCount
= *lpResumeIndex
;
6187 /* Lock the service database shared */
6188 ScmLockDatabaseShared();
6190 lpService
= ScmGetServiceEntryByResumeCount(dwLastResumeCount
);
6191 if (lpService
== NULL
)
6193 dwError
= ERROR_SUCCESS
;
6200 for (ServiceEntry
= &lpService
->ServiceListEntry
;
6201 ServiceEntry
!= &ServiceListHead
;
6202 ServiceEntry
= ServiceEntry
->Flink
)
6204 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
6208 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
6211 dwState
= SERVICE_ACTIVE
;
6212 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
6213 dwState
= SERVICE_INACTIVE
;
6215 if ((dwState
& dwServiceState
) == 0)
6220 if (*pszGroupName
== 0)
6222 if (CurrentService
->lpGroup
!= NULL
)
6227 if ((CurrentService
->lpGroup
== NULL
) ||
6228 _wcsicmp(pszGroupName
, CurrentService
->lpGroup
->lpGroupName
) != 0)
6233 dwSize
= sizeof(ENUM_SERVICE_STATUS_PROCESSW
) +
6234 (DWORD
)((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
6235 (DWORD
)((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
6237 if (dwRequiredSize
+ dwSize
<= cbBufSize
)
6239 DPRINT("Service name: %S fit\n", CurrentService
->lpServiceName
);
6240 dwRequiredSize
+= dwSize
;
6242 dwLastResumeCount
= CurrentService
->dwResumeCount
;
6246 DPRINT("Service name: %S no fit\n", CurrentService
->lpServiceName
);
6252 DPRINT("dwRequiredSize: %lu\n", dwRequiredSize
);
6253 DPRINT("dwServiceCount: %lu\n", dwServiceCount
);
6256 ServiceEntry
!= &ServiceListHead
;
6257 ServiceEntry
= ServiceEntry
->Flink
)
6259 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
6263 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
6266 dwState
= SERVICE_ACTIVE
;
6267 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
6268 dwState
= SERVICE_INACTIVE
;
6270 if ((dwState
& dwServiceState
) == 0)
6275 if (*pszGroupName
== 0)
6277 if (CurrentService
->lpGroup
!= NULL
)
6282 if ((CurrentService
->lpGroup
== NULL
) ||
6283 _wcsicmp(pszGroupName
, CurrentService
->lpGroup
->lpGroupName
) != 0)
6288 dwRequiredSize
+= (sizeof(ENUM_SERVICE_STATUS_PROCESSW
) +
6289 (DWORD
)((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
6290 (DWORD
)((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
)));
6292 dwError
= ERROR_MORE_DATA
;
6295 DPRINT("*pcbBytesNeeded: %lu\n", dwRequiredSize
);
6298 *lpResumeIndex
= dwLastResumeCount
;
6300 *lpServicesReturned
= dwServiceCount
;
6301 *pcbBytesNeeded
= dwRequiredSize
;
6303 /* If there was no services that matched */
6304 if ((!dwServiceCount
) && (dwError
!= ERROR_MORE_DATA
))
6306 dwError
= ERROR_SERVICE_DOES_NOT_EXIST
;
6310 lpStatusPtr
= (LPENUM_SERVICE_STATUS_PROCESSW
)lpBuffer
;
6311 lpStringPtr
= (LPWSTR
)((ULONG_PTR
)lpBuffer
+
6312 dwServiceCount
* sizeof(ENUM_SERVICE_STATUS_PROCESSW
));
6315 for (ServiceEntry
= &lpService
->ServiceListEntry
;
6316 ServiceEntry
!= &ServiceListHead
;
6317 ServiceEntry
= ServiceEntry
->Flink
)
6319 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
6323 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
6326 dwState
= SERVICE_ACTIVE
;
6327 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
6328 dwState
= SERVICE_INACTIVE
;
6330 if ((dwState
& dwServiceState
) == 0)
6335 if (*pszGroupName
== 0)
6337 if (CurrentService
->lpGroup
!= NULL
)
6342 if ((CurrentService
->lpGroup
== NULL
) ||
6343 _wcsicmp(pszGroupName
, CurrentService
->lpGroup
->lpGroupName
) != 0)
6348 dwSize
= sizeof(ENUM_SERVICE_STATUS_PROCESSW
) +
6349 (DWORD
)((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
6350 (DWORD
)((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
6352 if (dwRequiredSize
+ dwSize
<= cbBufSize
)
6354 /* Copy the service name */
6356 CurrentService
->lpServiceName
);
6357 lpStatusPtr
->lpServiceName
= (LPWSTR
)((ULONG_PTR
)lpStringPtr
- (ULONG_PTR
)lpBuffer
);
6358 lpStringPtr
+= (wcslen(CurrentService
->lpServiceName
) + 1);
6360 /* Copy the display name */
6362 CurrentService
->lpDisplayName
);
6363 lpStatusPtr
->lpDisplayName
= (LPWSTR
)((ULONG_PTR
)lpStringPtr
- (ULONG_PTR
)lpBuffer
);
6364 lpStringPtr
+= (wcslen(CurrentService
->lpDisplayName
) + 1);
6366 /* Copy the status information */
6367 memcpy(&lpStatusPtr
->ServiceStatusProcess
,
6368 &CurrentService
->Status
,
6369 sizeof(SERVICE_STATUS
));
6371 /* Copy the service process ID */
6372 if ((CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
) || (CurrentService
->lpImage
== NULL
))
6373 lpStatusPtr
->ServiceStatusProcess
.dwProcessId
= 0;
6375 lpStatusPtr
->ServiceStatusProcess
.dwProcessId
= CurrentService
->lpImage
->dwProcessId
;
6377 lpStatusPtr
->ServiceStatusProcess
.dwServiceFlags
= 0; /* FIXME */
6380 dwRequiredSize
+= dwSize
;
6390 *pcbBytesNeeded
= 0;
6396 /* Unlock the service database */
6397 ScmUnlockDatabase();
6399 DPRINT("REnumServicesStatusExW() done (Error %lu)\n", dwError
);
6409 handle_t BindingHandle
) /* FIXME */
6412 return ERROR_CALL_NOT_IMPLEMENTED
;
6419 RCreateServiceWOW64A(
6420 handle_t BindingHandle
,
6421 LPSTR lpServiceName
,
6422 LPSTR lpDisplayName
,
6423 DWORD dwDesiredAccess
,
6424 DWORD dwServiceType
,
6426 DWORD dwErrorControl
,
6427 LPSTR lpBinaryPathName
,
6428 LPSTR lpLoadOrderGroup
,
6430 LPBYTE lpDependencies
,
6432 LPSTR lpServiceStartName
,
6435 LPSC_RPC_HANDLE lpServiceHandle
)
6438 return ERROR_CALL_NOT_IMPLEMENTED
;
6445 RCreateServiceWOW64W(
6446 handle_t BindingHandle
,
6447 LPWSTR lpServiceName
,
6448 LPWSTR lpDisplayName
,
6449 DWORD dwDesiredAccess
,
6450 DWORD dwServiceType
,
6452 DWORD dwErrorControl
,
6453 LPWSTR lpBinaryPathName
,
6454 LPWSTR lpLoadOrderGroup
,
6456 LPBYTE lpDependencies
,
6458 LPWSTR lpServiceStartName
,
6461 LPSC_RPC_HANDLE lpServiceHandle
)
6464 return ERROR_CALL_NOT_IMPLEMENTED
;
6471 RQueryServiceTagInfo(
6472 handle_t BindingHandle
) /* FIXME */
6475 return ERROR_CALL_NOT_IMPLEMENTED
;
6482 RNotifyServiceStatusChange(
6483 SC_RPC_HANDLE hService
,
6484 SC_RPC_NOTIFY_PARAMS NotifyParams
,
6485 GUID
*pClientProcessGuid
,
6486 GUID
*pSCMProcessGuid
,
6487 PBOOL pfCreateRemoteQueue
,
6488 LPSC_NOTIFY_RPC_HANDLE phNotify
)
6491 return ERROR_CALL_NOT_IMPLEMENTED
;
6499 SC_NOTIFY_RPC_HANDLE hNotify
,
6500 PSC_RPC_NOTIFY_PARAMS_LIST
*ppNotifyParams
)
6503 return ERROR_CALL_NOT_IMPLEMENTED
;
6511 LPSC_NOTIFY_RPC_HANDLE phNotify
,
6515 return ERROR_CALL_NOT_IMPLEMENTED
;
6523 SC_RPC_HANDLE hService
,
6528 return ERROR_CALL_NOT_IMPLEMENTED
;
6536 SC_RPC_HANDLE hService
,
6541 return ERROR_CALL_NOT_IMPLEMENTED
;
6549 handle_t BindingHandle
) /* FIXME */
6552 return ERROR_CALL_NOT_IMPLEMENTED
;
6559 RValidatePnPService(
6560 handle_t BindingHandle
) /* FIXME */
6563 return ERROR_CALL_NOT_IMPLEMENTED
;
6570 ROpenServiceStatusHandle(
6571 handle_t BindingHandle
) /* FIXME */
6574 return ERROR_CALL_NOT_IMPLEMENTED
;
6582 handle_t BindingHandle
) /* FIXME */
6585 return ERROR_CALL_NOT_IMPLEMENTED
;
6589 void __RPC_FAR
* __RPC_USER
midl_user_allocate(SIZE_T len
)
6591 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
);
6595 void __RPC_USER
midl_user_free(void __RPC_FAR
* ptr
)
6597 HeapFree(GetProcessHeap(), 0, ptr
);
6601 void __RPC_USER
SC_RPC_HANDLE_rundown(SC_RPC_HANDLE hSCObject
)
6603 /* Close the handle */
6604 RCloseServiceHandle(&hSCObject
);
6608 void __RPC_USER
SC_RPC_LOCK_rundown(SC_RPC_LOCK Lock
)
6610 /* Unlock the database */
6611 RUnlockServiceDatabase(&Lock
);
6615 void __RPC_USER
SC_NOTIFY_RPC_HANDLE_rundown(SC_NOTIFY_RPC_HANDLE hNotify
)