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 ****************************************************************/
18 /* GLOBALS *****************************************************************/
20 #define MANAGER_TAG 0x72674D68 /* 'hMgr' */
21 #define SERVICE_TAG 0x63765368 /* 'hSvc' */
23 typedef struct _SCMGR_HANDLE
30 typedef struct _MANAGER_HANDLE
33 WCHAR DatabaseName
[1];
34 } MANAGER_HANDLE
, *PMANAGER_HANDLE
;
37 typedef struct _SERVICE_HANDLE
40 PSERVICE ServiceEntry
;
41 } SERVICE_HANDLE
, *PSERVICE_HANDLE
;
44 #define SC_MANAGER_READ \
45 (STANDARD_RIGHTS_READ | \
46 SC_MANAGER_QUERY_LOCK_STATUS | \
47 SC_MANAGER_ENUMERATE_SERVICE)
49 #define SC_MANAGER_WRITE \
50 (STANDARD_RIGHTS_WRITE | \
51 SC_MANAGER_MODIFY_BOOT_CONFIG | \
52 SC_MANAGER_CREATE_SERVICE)
54 #define SC_MANAGER_EXECUTE \
55 (STANDARD_RIGHTS_EXECUTE | \
57 SC_MANAGER_ENUMERATE_SERVICE | \
58 SC_MANAGER_CONNECT | \
59 SC_MANAGER_CREATE_SERVICE)
62 #define SERVICE_READ \
63 (STANDARD_RIGHTS_READ | \
64 SERVICE_INTERROGATE | \
65 SERVICE_ENUMERATE_DEPENDENTS | \
66 SERVICE_QUERY_STATUS | \
69 #define SERVICE_WRITE \
70 (STANDARD_RIGHTS_WRITE | \
71 SERVICE_CHANGE_CONFIG)
73 #define SERVICE_EXECUTE \
74 (STANDARD_RIGHTS_EXECUTE | \
75 SERVICE_USER_DEFINED_CONTROL | \
76 SERVICE_PAUSE_CONTINUE | \
80 #define TAG_ARRAY_SIZE 32
82 /* VARIABLES ***************************************************************/
84 static GENERIC_MAPPING
85 ScmManagerMapping
= {SC_MANAGER_READ
,
88 SC_MANAGER_ALL_ACCESS
};
90 static GENERIC_MAPPING
91 ScmServiceMapping
= {SERVICE_READ
,
97 /* FUNCTIONS ***************************************************************/
100 ScmStartRpcServer(VOID
)
104 DPRINT("ScmStartRpcServer() called\n");
106 Status
= RpcServerUseProtseqEpW(L
"ncacn_np",
110 if (Status
!= RPC_S_OK
)
112 DPRINT1("RpcServerUseProtseqEpW() failed (Status %lx)\n", Status
);
116 Status
= RpcServerRegisterIf(svcctl_v2_0_s_ifspec
,
119 if (Status
!= RPC_S_OK
)
121 DPRINT1("RpcServerRegisterIf() failed (Status %lx)\n", Status
);
125 Status
= RpcServerListen(1, 20, TRUE
);
126 if (Status
!= RPC_S_OK
)
128 DPRINT1("RpcServerListen() failed (Status %lx)\n", Status
);
132 DPRINT("ScmStartRpcServer() done\n");
137 ScmCreateManagerHandle(LPWSTR lpDatabaseName
,
142 if (lpDatabaseName
== NULL
)
143 lpDatabaseName
= SERVICES_ACTIVE_DATABASEW
;
145 if (_wcsicmp(lpDatabaseName
, SERVICES_FAILED_DATABASEW
) == 0)
147 DPRINT("Database %S, does not exist\n", lpDatabaseName
);
148 return ERROR_DATABASE_DOES_NOT_EXIST
;
150 else if (_wcsicmp(lpDatabaseName
, SERVICES_ACTIVE_DATABASEW
) != 0)
152 DPRINT("Invalid Database name %S.\n", lpDatabaseName
);
153 return ERROR_INVALID_NAME
;
156 Ptr
= HeapAlloc(GetProcessHeap(),
158 FIELD_OFFSET(MANAGER_HANDLE
, DatabaseName
[wcslen(lpDatabaseName
) + 1]));
160 return ERROR_NOT_ENOUGH_MEMORY
;
162 Ptr
->Handle
.Tag
= MANAGER_TAG
;
164 wcscpy(Ptr
->DatabaseName
, lpDatabaseName
);
166 *Handle
= (SC_HANDLE
)Ptr
;
168 return ERROR_SUCCESS
;
173 ScmCreateServiceHandle(PSERVICE lpServiceEntry
,
178 Ptr
= HeapAlloc(GetProcessHeap(),
180 sizeof(SERVICE_HANDLE
));
182 return ERROR_NOT_ENOUGH_MEMORY
;
184 Ptr
->Handle
.Tag
= SERVICE_TAG
;
186 Ptr
->ServiceEntry
= lpServiceEntry
;
188 *Handle
= (SC_HANDLE
)Ptr
;
190 return ERROR_SUCCESS
;
194 static PMANAGER_HANDLE
195 ScmGetServiceManagerFromHandle(SC_RPC_HANDLE Handle
)
197 PMANAGER_HANDLE pManager
= NULL
;
201 if (((PMANAGER_HANDLE
)Handle
)->Handle
.Tag
== MANAGER_TAG
)
202 pManager
= (PMANAGER_HANDLE
)Handle
;
204 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
206 DPRINT1("Exception: Invalid Service Manager handle!\n");
214 static PSERVICE_HANDLE
215 ScmGetServiceFromHandle(SC_RPC_HANDLE Handle
)
217 PSERVICE_HANDLE pService
= NULL
;
221 if (((PSERVICE_HANDLE
)Handle
)->Handle
.Tag
== SERVICE_TAG
)
222 pService
= (PSERVICE_HANDLE
)Handle
;
224 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
226 DPRINT1("Exception: Invalid Service handle!\n");
235 ScmCheckAccess(SC_HANDLE Handle
,
236 DWORD dwDesiredAccess
)
238 PMANAGER_HANDLE hMgr
;
240 hMgr
= (PMANAGER_HANDLE
)Handle
;
241 if (hMgr
->Handle
.Tag
== MANAGER_TAG
)
243 RtlMapGenericMask(&dwDesiredAccess
,
246 hMgr
->Handle
.DesiredAccess
= dwDesiredAccess
;
248 return ERROR_SUCCESS
;
250 else if (hMgr
->Handle
.Tag
== SERVICE_TAG
)
252 RtlMapGenericMask(&dwDesiredAccess
,
255 hMgr
->Handle
.DesiredAccess
= dwDesiredAccess
;
257 return ERROR_SUCCESS
;
260 return ERROR_INVALID_HANDLE
;
265 ScmAssignNewTag(PSERVICE lpService
)
269 DWORD dwGroupTagCount
= 0;
270 PDWORD pdwGroupTags
= NULL
;
272 DWORD dwTagUsedBase
= 1;
273 BOOLEAN TagUsed
[TAG_ARRAY_SIZE
];
277 PLIST_ENTRY ServiceEntry
;
278 PSERVICE CurrentService
;
280 ASSERT(lpService
!= NULL
);
281 ASSERT(lpService
->lpGroup
!= NULL
);
283 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
284 L
"System\\CurrentControlSet\\Control\\GroupOrderList",
289 if (dwError
!= ERROR_SUCCESS
)
292 /* query value length */
294 dwError
= RegQueryValueExW(hKey
,
295 lpService
->lpGroup
->szGroupName
,
301 if (dwError
!= ERROR_SUCCESS
&& dwError
!= ERROR_MORE_DATA
)
304 pdwGroupTags
= HeapAlloc(GetProcessHeap(), 0, cbDataSize
);
307 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
311 dwError
= RegQueryValueExW(hKey
,
312 lpService
->lpGroup
->szGroupName
,
315 (LPBYTE
)pdwGroupTags
,
318 if (dwError
!= ERROR_SUCCESS
)
321 if (cbDataSize
< sizeof(pdwGroupTags
[0]))
324 dwGroupTagCount
= min(pdwGroupTags
[0], cbDataSize
/ sizeof(pdwGroupTags
[0]) - 1);
329 /* mark all tags as unused */
330 for (i
= 0; i
< TAG_ARRAY_SIZE
; i
++)
333 /* mark tags in GroupOrderList as used */
334 for (i
= 1; i
<= dwGroupTagCount
; i
++)
336 nTagOffset
= pdwGroupTags
[i
] - dwTagUsedBase
;
337 if (nTagOffset
>= 0 && nTagOffset
< TAG_ARRAY_SIZE
)
338 TagUsed
[nTagOffset
] = TRUE
;
341 /* mark tags in service list as used */
342 ServiceEntry
= lpService
->ServiceListEntry
.Flink
;
343 while (ServiceEntry
!= &lpService
->ServiceListEntry
)
345 ASSERT(ServiceEntry
!= NULL
);
346 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
347 if (CurrentService
->lpGroup
== lpService
->lpGroup
)
349 nTagOffset
= CurrentService
->dwTag
- dwTagUsedBase
;
350 if (nTagOffset
>= 0 && nTagOffset
< TAG_ARRAY_SIZE
)
351 TagUsed
[nTagOffset
] = TRUE
;
354 ServiceEntry
= ServiceEntry
->Flink
;
357 /* find unused tag, if any */
358 for (i
= 0; i
< TAG_ARRAY_SIZE
; i
++)
362 dwFreeTag
= dwTagUsedBase
+ i
;
367 dwTagUsedBase
+= TAG_ARRAY_SIZE
;
368 } while (!dwFreeTag
);
372 HeapFree(GetProcessHeap(), 0, pdwGroupTags
);
379 lpService
->dwTag
= dwFreeTag
;
380 DPRINT("Assigning new tag %lu to service %S in group %S\n",
381 lpService
->dwTag
, lpService
->lpServiceName
, lpService
->lpGroup
->szGroupName
);
382 dwError
= ERROR_SUCCESS
;
386 DPRINT1("Failed to assign new tag to service %S, error=%lu\n",
387 lpService
->lpServiceName
, dwError
);
394 /* Create a path suitable for the bootloader out of the full path */
396 ScmConvertToBootPathName(wchar_t *CanonName
, wchar_t **RelativeName
)
398 DWORD ServiceNameLen
, BufferSize
, ExpandedLen
;
401 UNICODE_STRING NtPathName
, SystemRoot
, LinkTarget
;
402 OBJECT_ATTRIBUTES ObjectAttributes
;
404 HANDLE SymbolicLinkHandle
;
406 DPRINT("ScmConvertToBootPathName %S\n", CanonName
);
409 return ERROR_INVALID_PARAMETER
;
411 *RelativeName
= NULL
;
413 ServiceNameLen
= wcslen(CanonName
);
415 /* First check, if it's already good */
416 if (ServiceNameLen
> 12 &&
417 !_wcsnicmp(L
"\\SystemRoot\\", CanonName
, 12))
419 *RelativeName
= HeapAlloc(GetProcessHeap(),
421 (ServiceNameLen
+ 1) * sizeof(WCHAR
));
422 if (*RelativeName
== NULL
)
424 DPRINT("Error allocating memory for boot driver name!\n");
425 return ERROR_NOT_ENOUGH_MEMORY
;
429 wcscpy(*RelativeName
, CanonName
);
431 DPRINT("Bootdriver name %S\n", *RelativeName
);
432 return ERROR_SUCCESS
;
435 /* If it has %SystemRoot% prefix, substitute it to \System*/
436 if (ServiceNameLen
> 13 &&
437 !_wcsnicmp(L
"%SystemRoot%\\", CanonName
, 13))
439 /* There is no +sizeof(wchar_t) because the name is less by 1 wchar */
440 *RelativeName
= HeapAlloc(GetProcessHeap(),
442 ServiceNameLen
* sizeof(WCHAR
));
444 if (*RelativeName
== NULL
)
446 DPRINT("Error allocating memory for boot driver name!\n");
447 return ERROR_NOT_ENOUGH_MEMORY
;
451 wcscpy(*RelativeName
, L
"\\SystemRoot\\");
452 wcscat(*RelativeName
, CanonName
+ 13);
454 DPRINT("Bootdriver name %S\n", *RelativeName
);
455 return ERROR_SUCCESS
;
458 /* Get buffer size needed for expanding env strings */
459 BufferSize
= ExpandEnvironmentStringsW(L
"%SystemRoot%\\", &Dest
, 1);
463 DPRINT("Error during a call to ExpandEnvironmentStringsW()\n");
464 return ERROR_INVALID_ENVIRONMENT
;
467 /* Allocate memory, since the size is known now */
468 Expanded
= HeapAlloc(GetProcessHeap(),
470 (BufferSize
+ 1) * sizeof(WCHAR
));
473 DPRINT("Error allocating memory for boot driver name!\n");
474 return ERROR_NOT_ENOUGH_MEMORY
;
478 if (ExpandEnvironmentStringsW(L
"%SystemRoot%\\", Expanded
, BufferSize
) >
481 DPRINT("Error during a call to ExpandEnvironmentStringsW()\n");
482 HeapFree(GetProcessHeap(), 0, Expanded
);
483 return ERROR_NOT_ENOUGH_MEMORY
;
486 /* Convert to NT-style path */
487 if (!RtlDosPathNameToNtPathName_U(Expanded
, &NtPathName
, NULL
, NULL
))
489 DPRINT("Error during a call to RtlDosPathNameToNtPathName_U()\n");
490 return ERROR_INVALID_ENVIRONMENT
;
493 DPRINT("Converted to NT-style %wZ\n", &NtPathName
);
495 /* No need to keep the dos-path anymore */
496 HeapFree(GetProcessHeap(), 0, Expanded
);
498 /* Copy it to the allocated place */
499 Expanded
= HeapAlloc(GetProcessHeap(),
501 NtPathName
.Length
+ sizeof(UNICODE_NULL
));
504 DPRINT("Error allocating memory for boot driver name!\n");
505 return ERROR_NOT_ENOUGH_MEMORY
;
508 ExpandedLen
= NtPathName
.Length
/ sizeof(WCHAR
);
509 wcsncpy(Expanded
, NtPathName
.Buffer
, ExpandedLen
);
510 Expanded
[ExpandedLen
] = UNICODE_NULL
;
512 if (ServiceNameLen
> ExpandedLen
&&
513 !_wcsnicmp(Expanded
, CanonName
, ExpandedLen
))
515 /* Only \SystemRoot\ is missing */
516 *RelativeName
= HeapAlloc(GetProcessHeap(),
518 (ServiceNameLen
- ExpandedLen
) * sizeof(WCHAR
) + 13*sizeof(WCHAR
));
519 if (*RelativeName
== NULL
)
521 DPRINT("Error allocating memory for boot driver name!\n");
522 HeapFree(GetProcessHeap(), 0, Expanded
);
523 return ERROR_NOT_ENOUGH_MEMORY
;
526 wcscpy(*RelativeName
, L
"\\SystemRoot\\");
527 wcscat(*RelativeName
, CanonName
+ ExpandedLen
);
529 RtlFreeUnicodeString(&NtPathName
);
530 return ERROR_SUCCESS
;
533 /* The most complex case starts here */
534 RtlInitUnicodeString(&SystemRoot
, L
"\\SystemRoot");
535 InitializeObjectAttributes(&ObjectAttributes
,
537 OBJ_CASE_INSENSITIVE
,
541 /* Open this symlink */
542 Status
= NtOpenSymbolicLinkObject(&SymbolicLinkHandle
, SYMBOLIC_LINK_QUERY
, &ObjectAttributes
);
544 if (NT_SUCCESS(Status
))
546 LinkTarget
.Length
= 0;
547 LinkTarget
.MaximumLength
= 0;
549 DPRINT("Opened symbolic link object\n");
551 Status
= NtQuerySymbolicLinkObject(SymbolicLinkHandle
, &LinkTarget
, &BufferSize
);
552 if (NT_SUCCESS(Status
) || Status
== STATUS_BUFFER_TOO_SMALL
)
554 /* Check if required buffer size is sane */
555 if (BufferSize
> 0xFFFD)
557 DPRINT("Too large buffer required\n");
559 if (SymbolicLinkHandle
) NtClose(SymbolicLinkHandle
);
560 HeapFree(GetProcessHeap(), 0, Expanded
);
561 return ERROR_NOT_ENOUGH_MEMORY
;
564 /* Alloc the string */
565 LinkTarget
.Length
= (USHORT
)BufferSize
;
566 LinkTarget
.MaximumLength
= LinkTarget
.Length
+ sizeof(UNICODE_NULL
);
567 LinkTarget
.Buffer
= HeapAlloc(GetProcessHeap(),
569 LinkTarget
.MaximumLength
);
570 if (!LinkTarget
.Buffer
)
572 DPRINT("Unable to alloc buffer\n");
573 if (SymbolicLinkHandle
) NtClose(SymbolicLinkHandle
);
574 HeapFree(GetProcessHeap(), 0, Expanded
);
575 return ERROR_NOT_ENOUGH_MEMORY
;
578 /* Do a real query now */
579 Status
= NtQuerySymbolicLinkObject(SymbolicLinkHandle
, &LinkTarget
, &BufferSize
);
580 if (NT_SUCCESS(Status
))
582 DPRINT("LinkTarget: %wZ\n", &LinkTarget
);
584 ExpandedLen
= LinkTarget
.Length
/ sizeof(WCHAR
);
585 if ((ServiceNameLen
> ExpandedLen
) &&
586 !_wcsnicmp(LinkTarget
.Buffer
, CanonName
, ExpandedLen
))
588 *RelativeName
= HeapAlloc(GetProcessHeap(),
590 (ServiceNameLen
- ExpandedLen
) * sizeof(WCHAR
) + 13*sizeof(WCHAR
));
592 if (*RelativeName
== NULL
)
594 DPRINT("Unable to alloc buffer\n");
595 if (SymbolicLinkHandle
) NtClose(SymbolicLinkHandle
);
596 HeapFree(GetProcessHeap(), 0, Expanded
);
597 RtlFreeUnicodeString(&NtPathName
);
598 return ERROR_NOT_ENOUGH_MEMORY
;
601 /* Copy it over, substituting the first part
603 wcscpy(*RelativeName
, L
"\\SystemRoot\\");
604 wcscat(*RelativeName
, CanonName
+ExpandedLen
+1);
607 if (SymbolicLinkHandle
) NtClose(SymbolicLinkHandle
);
608 HeapFree(GetProcessHeap(), 0, Expanded
);
609 RtlFreeUnicodeString(&NtPathName
);
612 return ERROR_SUCCESS
;
616 if (SymbolicLinkHandle
) NtClose(SymbolicLinkHandle
);
617 HeapFree(GetProcessHeap(), 0, Expanded
);
618 RtlFreeUnicodeString(&NtPathName
);
619 return ERROR_INVALID_PARAMETER
;
624 DPRINT("Error, Status = %08X\n", Status
);
625 if (SymbolicLinkHandle
) NtClose(SymbolicLinkHandle
);
626 HeapFree(GetProcessHeap(), 0, Expanded
);
627 RtlFreeUnicodeString(&NtPathName
);
628 return ERROR_INVALID_PARAMETER
;
633 DPRINT("Error, Status = %08X\n", Status
);
634 if (SymbolicLinkHandle
) NtClose(SymbolicLinkHandle
);
635 HeapFree(GetProcessHeap(), 0, Expanded
);
636 RtlFreeUnicodeString(&NtPathName
);
637 return ERROR_INVALID_PARAMETER
;
643 DPRINT("Error, Status = %08X\n", Status
);
644 HeapFree(GetProcessHeap(), 0, Expanded
);
645 return ERROR_INVALID_PARAMETER
;
651 ScmCanonDriverImagePath(DWORD dwStartType
,
652 const wchar_t *lpServiceName
,
653 wchar_t **lpCanonName
)
655 DWORD ServiceNameLen
, Result
;
656 UNICODE_STRING NtServiceName
;
658 const WCHAR
*SourceName
= lpServiceName
;
660 /* Calculate the length of the service's name */
661 ServiceNameLen
= wcslen(lpServiceName
);
663 /* 12 is wcslen(L"\\SystemRoot\\") */
664 if (ServiceNameLen
> 12 &&
665 !_wcsnicmp(L
"\\SystemRoot\\", lpServiceName
, 12))
667 /* SystemRoot prefix is already included */
668 *lpCanonName
= HeapAlloc(GetProcessHeap(),
670 (ServiceNameLen
+ 1) * sizeof(WCHAR
));
672 if (*lpCanonName
== NULL
)
674 DPRINT("Error allocating memory for canonized service name!\n");
675 return ERROR_NOT_ENOUGH_MEMORY
;
678 /* If it's a boot-time driver, it must be systemroot relative */
679 if (dwStartType
== SERVICE_BOOT_START
)
683 wcscpy(*lpCanonName
, SourceName
);
685 DPRINT("Canonicalized name %S\n", *lpCanonName
);
689 /* Check if it has %SystemRoot% (len=13) */
690 if (ServiceNameLen
> 13 &&
691 !_wcsnicmp(L
"%SystemRoot%\\", lpServiceName
, 13))
693 /* Substitute %SystemRoot% with \\SystemRoot\\ */
694 *lpCanonName
= HeapAlloc(GetProcessHeap(),
696 (ServiceNameLen
+ 1) * sizeof(WCHAR
));
698 if (*lpCanonName
== NULL
)
700 DPRINT("Error allocating memory for canonized service name!\n");
701 return ERROR_NOT_ENOUGH_MEMORY
;
704 /* If it's a boot-time driver, it must be systemroot relative */
705 if (dwStartType
== SERVICE_BOOT_START
)
706 wcscpy(*lpCanonName
, L
"\\SystemRoot\\");
708 wcscat(*lpCanonName
, lpServiceName
+ 13);
710 DPRINT("Canonicalized name %S\n", *lpCanonName
);
714 /* Check if it's a relative path name */
715 if (lpServiceName
[0] != L
'\\' && lpServiceName
[1] != L
':')
717 *lpCanonName
= HeapAlloc(GetProcessHeap(),
719 (ServiceNameLen
+ 1) * sizeof(WCHAR
));
721 if (*lpCanonName
== NULL
)
723 DPRINT("Error allocating memory for canonized service name!\n");
724 return ERROR_NOT_ENOUGH_MEMORY
;
727 /* Just copy it over without changing */
728 wcscpy(*lpCanonName
, lpServiceName
);
733 /* It seems to be a DOS path, convert it */
734 if (!RtlDosPathNameToNtPathName_U(lpServiceName
, &NtServiceName
, NULL
, NULL
))
736 DPRINT("RtlDosPathNameToNtPathName_U() failed!\n");
737 return ERROR_INVALID_PARAMETER
;
740 *lpCanonName
= HeapAlloc(GetProcessHeap(),
742 NtServiceName
.Length
+ sizeof(WCHAR
));
744 if (*lpCanonName
== NULL
)
746 DPRINT("Error allocating memory for canonized service name!\n");
747 RtlFreeUnicodeString(&NtServiceName
);
748 return ERROR_NOT_ENOUGH_MEMORY
;
751 /* Copy the string */
752 wcsncpy(*lpCanonName
, NtServiceName
.Buffer
, NtServiceName
.Length
/ sizeof(WCHAR
));
754 /* The unicode string is not needed anymore */
755 RtlFreeUnicodeString(&NtServiceName
);
757 if (dwStartType
!= SERVICE_BOOT_START
)
759 DPRINT("Canonicalized name %S\n", *lpCanonName
);
763 /* The service is boot-started, so must be relative */
764 Result
= ScmConvertToBootPathName(*lpCanonName
, &RelativeName
);
767 /* There is a problem, free name and return */
768 HeapFree(GetProcessHeap(), 0, *lpCanonName
);
769 DPRINT("Error converting named!\n");
773 ASSERT(RelativeName
);
775 /* Copy that string */
776 wcscpy(*lpCanonName
, RelativeName
+ 12);
778 /* Free the allocated buffer */
779 HeapFree(GetProcessHeap(), 0, RelativeName
);
781 DPRINT("Canonicalized name %S\n", *lpCanonName
);
788 /* Internal recursive function */
789 /* Need to search for every dependency on every service */
791 Int_EnumDependentServicesW(HKEY hServicesKey
,
793 DWORD dwServiceState
,
794 PSERVICE
*lpServices
,
795 LPDWORD pcbBytesNeeded
,
796 LPDWORD lpServicesReturned
)
798 DWORD dwError
= ERROR_SUCCESS
;
799 WCHAR szNameBuf
[MAX_PATH
];
800 WCHAR szValueBuf
[MAX_PATH
];
801 WCHAR
*lpszNameBuf
= szNameBuf
;
802 WCHAR
*lpszValueBuf
= szValueBuf
;
806 PSERVICE lpCurrentService
;
807 HKEY hServiceEnumKey
;
808 DWORD dwCurrentServiceState
= SERVICE_ACTIVE
;
809 DWORD dwDependServiceStrPtr
= 0;
810 DWORD dwRequiredSize
= 0;
812 /* Get the number of service keys */
813 dwError
= RegQueryInfoKeyW(hServicesKey
,
825 if (dwError
!= ERROR_SUCCESS
)
827 DPRINT("ERROR! Unable to get number of services keys.\n");
831 /* Iterate the service keys to see if another service depends on the this service */
832 for (dwIteration
= 0; dwIteration
< dwNumSubKeys
; dwIteration
++)
835 dwError
= RegEnumKeyExW(hServicesKey
,
843 if (dwError
!= ERROR_SUCCESS
)
846 /* Open the Service key */
847 dwError
= RegOpenKeyExW(hServicesKey
,
852 if (dwError
!= ERROR_SUCCESS
)
857 /* Check for the DependOnService Value */
858 dwError
= RegQueryValueExW(hServiceEnumKey
,
862 (LPBYTE
)lpszValueBuf
,
865 /* FIXME: Handle load order. */
867 /* If the service found has a DependOnService value */
868 if (dwError
== ERROR_SUCCESS
)
870 dwDependServiceStrPtr
= 0;
872 /* Can be more than one Dependencies in the DependOnService string */
873 while (wcslen(lpszValueBuf
+ dwDependServiceStrPtr
) > 0)
875 if (_wcsicmp(lpszValueBuf
+ dwDependServiceStrPtr
, lpService
->lpServiceName
) == 0)
877 /* Get the current enumed service pointer */
878 lpCurrentService
= ScmGetServiceEntryByName(lpszNameBuf
);
880 /* Check for valid Service */
881 if (!lpCurrentService
)
883 /* This should never happen! */
884 DPRINT("This should not happen at this point, report to Developer\n");
885 return ERROR_NOT_FOUND
;
888 /* Determine state the service is in */
889 if (lpCurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
890 dwCurrentServiceState
= SERVICE_INACTIVE
;
892 /* If the ServiceState matches that requested or searching for SERVICE_STATE_ALL */
893 if ((dwCurrentServiceState
== dwServiceState
) ||
894 (dwServiceState
== SERVICE_STATE_ALL
))
896 /* Calculate the required size */
897 dwRequiredSize
+= sizeof(SERVICE_STATUS
);
898 dwRequiredSize
+= ((wcslen(lpCurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
));
899 dwRequiredSize
+= ((wcslen(lpCurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
901 /* Add the size for service name and display name pointers */
902 dwRequiredSize
+= (2 * sizeof(PVOID
));
904 /* increase the BytesNeeded size */
905 *pcbBytesNeeded
= *pcbBytesNeeded
+ dwRequiredSize
;
907 /* Don't fill callers buffer yet, as MSDN read that the last service with dependency
910 /* Recursive call to check for its dependencies */
911 Int_EnumDependentServicesW(hServicesKey
,
918 /* If the lpServices is valid set the service pointer */
920 lpServices
[*lpServicesReturned
] = lpCurrentService
;
922 *lpServicesReturned
= *lpServicesReturned
+ 1;
926 dwDependServiceStrPtr
+= (wcslen(lpszValueBuf
+ dwDependServiceStrPtr
) + 1);
929 else if (*pcbBytesNeeded
)
931 dwError
= ERROR_SUCCESS
;
934 RegCloseKey(hServiceEnumKey
);
942 DWORD
RCloseServiceHandle(
943 LPSC_RPC_HANDLE hSCObject
)
945 PMANAGER_HANDLE hManager
;
946 PSERVICE_HANDLE hService
;
950 DWORD pcbBytesNeeded
= 0;
951 DWORD dwServicesReturned
= 0;
953 DPRINT("RCloseServiceHandle() called\n");
955 DPRINT("hSCObject = %p\n", *hSCObject
);
958 return ERROR_INVALID_HANDLE
;
960 hManager
= ScmGetServiceManagerFromHandle(*hSCObject
);
961 hService
= ScmGetServiceFromHandle(*hSCObject
);
963 if (hManager
!= NULL
)
965 DPRINT("Found manager handle\n");
967 /* FIXME: add handle cleanup code */
969 HeapFree(GetProcessHeap(), 0, hManager
);
974 DPRINT("RCloseServiceHandle() done\n");
975 return ERROR_SUCCESS
;
977 else if (hService
!= NULL
)
979 DPRINT("Found service handle\n");
981 /* Lock the service database exlusively */
982 ScmLockDatabaseExclusive();
984 /* Get the pointer to the service record */
985 lpService
= hService
->ServiceEntry
;
987 /* FIXME: add handle cleanup code */
989 /* Free the handle */
990 HeapFree(GetProcessHeap(), 0, hService
);
993 ASSERT(lpService
->dwRefCount
> 0);
995 lpService
->dwRefCount
--;
996 DPRINT("CloseServiceHandle - lpService->dwRefCount %u\n",
997 lpService
->dwRefCount
);
999 if (lpService
->dwRefCount
== 0)
1001 /* If this service has been marked for deletion */
1002 if (lpService
->bDeleted
)
1004 /* Open the Services Reg key */
1005 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
1006 L
"System\\CurrentControlSet\\Services",
1008 KEY_SET_VALUE
| KEY_READ
,
1010 if (dwError
!= ERROR_SUCCESS
)
1012 DPRINT("Failed to open services key\n");
1013 ScmUnlockDatabase();
1017 /* Call the internal function with NULL, just to get bytes we need */
1018 Int_EnumDependentServicesW(hServicesKey
,
1023 &dwServicesReturned
);
1025 /* if pcbBytesNeeded returned a value then there are services running that are dependent on this service */
1028 DPRINT("Deletion failed due to running dependencies.\n");
1029 RegCloseKey(hServicesKey
);
1030 ScmUnlockDatabase();
1031 return ERROR_SUCCESS
;
1034 /* There are no references and no runnning dependencies,
1035 it is now safe to delete the service */
1037 /* Delete the Service Key */
1038 dwError
= RegDeleteKeyW(hServicesKey
,
1039 lpService
->lpServiceName
);
1041 RegCloseKey(hServicesKey
);
1043 if (dwError
!= ERROR_SUCCESS
)
1045 DPRINT("Failed to Delete the Service Registry key\n");
1046 ScmUnlockDatabase();
1050 /* Delete the Service */
1051 ScmDeleteServiceRecord(lpService
);
1055 ScmUnlockDatabase();
1059 DPRINT("RCloseServiceHandle() done\n");
1060 return ERROR_SUCCESS
;
1063 DPRINT("Invalid handle tag (Tag %lx)\n", hManager
->Handle
.Tag
);
1065 return ERROR_INVALID_HANDLE
;
1070 DWORD
RControlService(
1071 SC_RPC_HANDLE hService
,
1073 LPSERVICE_STATUS lpServiceStatus
)
1075 PSERVICE_HANDLE hSvc
;
1077 ACCESS_MASK DesiredAccess
;
1078 DWORD dwError
= ERROR_SUCCESS
;
1079 DWORD pcbBytesNeeded
= 0;
1080 DWORD dwServicesReturned
= 0;
1081 DWORD dwControlsAccepted
;
1082 DWORD dwCurrentState
;
1083 HKEY hServicesKey
= NULL
;
1085 DPRINT("RControlService() called\n");
1088 return ERROR_SHUTDOWN_IN_PROGRESS
;
1090 /* Check the service handle */
1091 hSvc
= ScmGetServiceFromHandle(hService
);
1094 DPRINT1("Invalid service handle!\n");
1095 return ERROR_INVALID_HANDLE
;
1098 /* Check the service entry point */
1099 lpService
= hSvc
->ServiceEntry
;
1100 if (lpService
== NULL
)
1102 DPRINT1("lpService == NULL!\n");
1103 return ERROR_INVALID_HANDLE
;
1106 /* Check access rights */
1109 case SERVICE_CONTROL_STOP
:
1110 DesiredAccess
= SERVICE_STOP
;
1113 case SERVICE_CONTROL_PAUSE
:
1114 case SERVICE_CONTROL_CONTINUE
:
1115 DesiredAccess
= SERVICE_PAUSE_CONTINUE
;
1118 case SERVICE_CONTROL_INTERROGATE
:
1119 DesiredAccess
= SERVICE_INTERROGATE
;
1123 if (dwControl
>= 128 && dwControl
<= 255)
1124 DesiredAccess
= SERVICE_USER_DEFINED_CONTROL
;
1126 return ERROR_INVALID_PARAMETER
;
1130 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
1132 return ERROR_ACCESS_DENIED
;
1134 /* Return the current service status information */
1135 RtlCopyMemory(lpServiceStatus
,
1137 sizeof(SERVICE_STATUS
));
1139 if (dwControl
== SERVICE_CONTROL_STOP
)
1141 /* Check if the service has dependencies running as windows
1142 doesn't stop a service that does */
1144 /* Open the Services Reg key */
1145 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
1146 L
"System\\CurrentControlSet\\Services",
1150 if (dwError
!= ERROR_SUCCESS
)
1152 DPRINT("Failed to open services key\n");
1156 /* Call the internal function with NULL, just to get bytes we need */
1157 Int_EnumDependentServicesW(hServicesKey
,
1162 &dwServicesReturned
);
1164 RegCloseKey(hServicesKey
);
1166 /* If pcbBytesNeeded is not zero then there are services running that
1167 are dependent on this service */
1168 if (pcbBytesNeeded
!= 0)
1170 DPRINT("Service has running dependencies. Failed to stop service.\n");
1171 return ERROR_DEPENDENT_SERVICES_RUNNING
;
1175 if (lpService
->Status
.dwServiceType
& SERVICE_DRIVER
)
1177 /* Send control code to the driver */
1178 dwError
= ScmControlDriver(lpService
,
1184 dwControlsAccepted
= lpService
->Status
.dwControlsAccepted
;
1185 dwCurrentState
= lpService
->Status
.dwCurrentState
;
1187 /* Return ERROR_SERVICE_NOT_ACTIVE if the service has not been started */
1188 if (lpService
->lpImage
== NULL
|| dwCurrentState
== SERVICE_STOPPED
)
1189 return ERROR_SERVICE_NOT_ACTIVE
;
1191 /* Check the current state before sending a control request */
1192 switch (dwCurrentState
)
1194 case SERVICE_STOP_PENDING
:
1195 case SERVICE_STOPPED
:
1196 return ERROR_SERVICE_CANNOT_ACCEPT_CTRL
;
1198 case SERVICE_START_PENDING
:
1201 case SERVICE_CONTROL_STOP
:
1204 case SERVICE_CONTROL_INTERROGATE
:
1205 RtlCopyMemory(lpServiceStatus
,
1207 sizeof(SERVICE_STATUS
));
1208 return ERROR_SUCCESS
;
1211 return ERROR_SERVICE_CANNOT_ACCEPT_CTRL
;
1216 /* Check if the control code is acceptable to the service */
1219 case SERVICE_CONTROL_STOP
:
1220 if ((dwControlsAccepted
& SERVICE_ACCEPT_STOP
) == 0)
1221 return ERROR_INVALID_SERVICE_CONTROL
;
1224 case SERVICE_CONTROL_PAUSE
:
1225 case SERVICE_CONTROL_CONTINUE
:
1226 if ((dwControlsAccepted
& SERVICE_ACCEPT_PAUSE_CONTINUE
) == 0)
1227 return ERROR_INVALID_SERVICE_CONTROL
;
1231 /* Send control code to the service */
1232 dwError
= ScmControlService(lpService
,
1235 /* Return service status information */
1236 RtlCopyMemory(lpServiceStatus
,
1238 sizeof(SERVICE_STATUS
));
1246 DWORD
RDeleteService(
1247 SC_RPC_HANDLE hService
)
1249 PSERVICE_HANDLE hSvc
;
1253 DPRINT("RDeleteService() called\n");
1256 return ERROR_SHUTDOWN_IN_PROGRESS
;
1258 hSvc
= ScmGetServiceFromHandle(hService
);
1261 DPRINT1("Invalid service handle!\n");
1262 return ERROR_INVALID_HANDLE
;
1265 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
1267 return ERROR_ACCESS_DENIED
;
1269 lpService
= hSvc
->ServiceEntry
;
1270 if (lpService
== NULL
)
1272 DPRINT("lpService == NULL!\n");
1273 return ERROR_INVALID_HANDLE
;
1276 /* Lock the service database exclusively */
1277 ScmLockDatabaseExclusive();
1279 if (lpService
->bDeleted
)
1281 DPRINT("The service has already been marked for delete!\n");
1282 dwError
= ERROR_SERVICE_MARKED_FOR_DELETE
;
1286 /* Mark service for delete */
1287 lpService
->bDeleted
= TRUE
;
1289 dwError
= ScmMarkServiceForDelete(lpService
);
1292 /* Unlock the service database */
1293 ScmUnlockDatabase();
1295 DPRINT("RDeleteService() done\n");
1302 DWORD
RLockServiceDatabase(
1303 SC_RPC_HANDLE hSCManager
,
1304 LPSC_RPC_LOCK lpLock
)
1306 PMANAGER_HANDLE hMgr
;
1308 DPRINT("RLockServiceDatabase() called\n");
1312 hMgr
= ScmGetServiceManagerFromHandle(hSCManager
);
1315 DPRINT1("Invalid service manager handle!\n");
1316 return ERROR_INVALID_HANDLE
;
1319 if (!RtlAreAllAccessesGranted(hMgr
->Handle
.DesiredAccess
,
1321 return ERROR_ACCESS_DENIED
;
1323 // return ScmLockDatabase(0, hMgr->0xC, hLock);
1325 /* FIXME: Lock the database */
1326 *lpLock
= (SC_RPC_LOCK
)0x12345678; /* Dummy! */
1328 return ERROR_SUCCESS
;
1333 DWORD
RQueryServiceObjectSecurity(
1334 SC_RPC_HANDLE hService
,
1335 SECURITY_INFORMATION dwSecurityInformation
,
1336 LPBYTE lpSecurityDescriptor
,
1338 LPBOUNDED_DWORD_256K pcbBytesNeeded
)
1340 PSERVICE_HANDLE hSvc
;
1342 ULONG DesiredAccess
= 0;
1344 DWORD dwBytesNeeded
;
1348 SECURITY_DESCRIPTOR ObjectDescriptor
;
1350 DPRINT("RQueryServiceObjectSecurity() called\n");
1352 hSvc
= ScmGetServiceFromHandle(hService
);
1355 DPRINT1("Invalid service handle!\n");
1356 return ERROR_INVALID_HANDLE
;
1359 if (dwSecurityInformation
& (DACL_SECURITY_INFORMATION
|
1360 GROUP_SECURITY_INFORMATION
|
1361 OWNER_SECURITY_INFORMATION
))
1362 DesiredAccess
|= READ_CONTROL
;
1364 if (dwSecurityInformation
& SACL_SECURITY_INFORMATION
)
1365 DesiredAccess
|= ACCESS_SYSTEM_SECURITY
;
1367 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
1370 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
1371 return ERROR_ACCESS_DENIED
;
1374 lpService
= hSvc
->ServiceEntry
;
1375 if (lpService
== NULL
)
1377 DPRINT("lpService == NULL!\n");
1378 return ERROR_INVALID_HANDLE
;
1381 /* Lock the service database */
1382 ScmLockDatabaseShared();
1386 Status
= RtlCreateSecurityDescriptor(&ObjectDescriptor
, SECURITY_DESCRIPTOR_REVISION
);
1388 Status
= RtlQuerySecurityObject(&ObjectDescriptor
/* lpService->lpSecurityDescriptor */,
1389 dwSecurityInformation
,
1390 (PSECURITY_DESCRIPTOR
)lpSecurityDescriptor
,
1394 /* Unlock the service database */
1395 ScmUnlockDatabase();
1397 if (NT_SUCCESS(Status
))
1399 *pcbBytesNeeded
= dwBytesNeeded
;
1400 dwError
= STATUS_SUCCESS
;
1402 else if (Status
== STATUS_BUFFER_TOO_SMALL
)
1404 *pcbBytesNeeded
= dwBytesNeeded
;
1405 dwError
= ERROR_INSUFFICIENT_BUFFER
;
1407 else if (Status
== STATUS_BAD_DESCRIPTOR_FORMAT
)
1409 dwError
= ERROR_GEN_FAILURE
;
1413 dwError
= RtlNtStatusToDosError(Status
);
1421 DWORD
RSetServiceObjectSecurity(
1422 SC_RPC_HANDLE hService
,
1423 DWORD dwSecurityInformation
,
1424 LPBYTE lpSecurityDescriptor
,
1425 DWORD dwSecuityDescriptorSize
)
1427 PSERVICE_HANDLE hSvc
;
1429 ULONG DesiredAccess
= 0;
1430 /* HANDLE hToken = NULL; */
1432 /* NTSTATUS Status; */
1435 DPRINT("RSetServiceObjectSecurity() called\n");
1437 hSvc
= ScmGetServiceFromHandle(hService
);
1440 DPRINT1("Invalid service handle!\n");
1441 return ERROR_INVALID_HANDLE
;
1444 if (dwSecurityInformation
== 0 ||
1445 dwSecurityInformation
& ~(OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
1446 | DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
))
1447 return ERROR_INVALID_PARAMETER
;
1449 if (!RtlValidSecurityDescriptor((PSECURITY_DESCRIPTOR
)lpSecurityDescriptor
))
1450 return ERROR_INVALID_PARAMETER
;
1452 if (dwSecurityInformation
& SACL_SECURITY_INFORMATION
)
1453 DesiredAccess
|= ACCESS_SYSTEM_SECURITY
;
1455 if (dwSecurityInformation
& DACL_SECURITY_INFORMATION
)
1456 DesiredAccess
|= WRITE_DAC
;
1458 if (dwSecurityInformation
& (OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
))
1459 DesiredAccess
|= WRITE_OWNER
;
1461 if ((dwSecurityInformation
& OWNER_SECURITY_INFORMATION
) &&
1462 (((PISECURITY_DESCRIPTOR
)lpSecurityDescriptor
)->Owner
== NULL
))
1463 return ERROR_INVALID_PARAMETER
;
1465 if ((dwSecurityInformation
& GROUP_SECURITY_INFORMATION
) &&
1466 (((PISECURITY_DESCRIPTOR
)lpSecurityDescriptor
)->Group
== NULL
))
1467 return ERROR_INVALID_PARAMETER
;
1469 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
1472 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
1473 return ERROR_ACCESS_DENIED
;
1476 lpService
= hSvc
->ServiceEntry
;
1477 if (lpService
== NULL
)
1479 DPRINT("lpService == NULL!\n");
1480 return ERROR_INVALID_HANDLE
;
1483 if (lpService
->bDeleted
)
1484 return ERROR_SERVICE_MARKED_FOR_DELETE
;
1487 RpcImpersonateClient(NULL
);
1489 Status
= NtOpenThreadToken(NtCurrentThread(),
1493 if (!NT_SUCCESS(Status
))
1494 return RtlNtStatusToDosError(Status
);
1499 /* Lock the service database exclusive */
1500 ScmLockDatabaseExclusive();
1503 Status
= RtlSetSecurityObject(dwSecurityInformation
,
1504 (PSECURITY_DESCRIPTOR
)lpSecurityDescriptor
,
1505 &lpService
->lpSecurityDescriptor
,
1508 if (!NT_SUCCESS(Status
))
1510 dwError
= RtlNtStatusToDosError(Status
);
1515 dwError
= ScmOpenServiceKey(lpService
->lpServiceName
,
1516 READ_CONTROL
| KEY_CREATE_SUB_KEY
| KEY_SET_VALUE
,
1518 if (dwError
!= ERROR_SUCCESS
)
1522 dwError
= ERROR_SUCCESS
;
1523 // dwError = ScmWriteSecurityDescriptor(hServiceKey,
1524 // lpService->lpSecurityDescriptor);
1526 RegFlushKey(hServiceKey
);
1527 RegCloseKey(hServiceKey
);
1536 /* Unlock service database */
1537 ScmUnlockDatabase();
1539 DPRINT("RSetServiceObjectSecurity() done (Error %lu)\n", dwError
);
1546 DWORD
RQueryServiceStatus(
1547 SC_RPC_HANDLE hService
,
1548 LPSERVICE_STATUS lpServiceStatus
)
1550 PSERVICE_HANDLE hSvc
;
1553 DPRINT("RQueryServiceStatus() called\n");
1556 return ERROR_SHUTDOWN_IN_PROGRESS
;
1558 hSvc
= ScmGetServiceFromHandle(hService
);
1561 DPRINT1("Invalid service handle!\n");
1562 return ERROR_INVALID_HANDLE
;
1565 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
1566 SERVICE_QUERY_STATUS
))
1568 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
1569 return ERROR_ACCESS_DENIED
;
1572 lpService
= hSvc
->ServiceEntry
;
1573 if (lpService
== NULL
)
1575 DPRINT("lpService == NULL!\n");
1576 return ERROR_INVALID_HANDLE
;
1579 /* Lock the service database shared */
1580 ScmLockDatabaseShared();
1582 /* Return service status information */
1583 RtlCopyMemory(lpServiceStatus
,
1585 sizeof(SERVICE_STATUS
));
1587 /* Unlock the service database */
1588 ScmUnlockDatabase();
1590 return ERROR_SUCCESS
;
1595 ScmIsValidServiceState(DWORD dwCurrentState
)
1597 switch (dwCurrentState
)
1599 case SERVICE_STOPPED
:
1600 case SERVICE_START_PENDING
:
1601 case SERVICE_STOP_PENDING
:
1602 case SERVICE_RUNNING
:
1603 case SERVICE_CONTINUE_PENDING
:
1604 case SERVICE_PAUSE_PENDING
:
1605 case SERVICE_PAUSED
:
1615 DWORD
RSetServiceStatus(
1616 RPC_SERVICE_STATUS_HANDLE hServiceStatus
,
1617 LPSERVICE_STATUS lpServiceStatus
)
1620 DWORD dwPreviousState
;
1621 LPCWSTR lpErrorStrings
[2];
1622 WCHAR szErrorBuffer
[32];
1624 DPRINT("RSetServiceStatus() called\n");
1625 DPRINT("hServiceStatus = %p\n", hServiceStatus
);
1626 DPRINT("dwServiceType = %lu\n", lpServiceStatus
->dwServiceType
);
1627 DPRINT("dwCurrentState = %lu\n", lpServiceStatus
->dwCurrentState
);
1628 DPRINT("dwControlsAccepted = %lu\n", lpServiceStatus
->dwControlsAccepted
);
1629 DPRINT("dwWin32ExitCode = %lu\n", lpServiceStatus
->dwWin32ExitCode
);
1630 DPRINT("dwServiceSpecificExitCode = %lu\n", lpServiceStatus
->dwServiceSpecificExitCode
);
1631 DPRINT("dwCheckPoint = %lu\n", lpServiceStatus
->dwCheckPoint
);
1632 DPRINT("dwWaitHint = %lu\n", lpServiceStatus
->dwWaitHint
);
1634 if (hServiceStatus
== 0)
1636 DPRINT("hServiceStatus == NULL!\n");
1637 return ERROR_INVALID_HANDLE
;
1640 lpService
= (PSERVICE
)hServiceStatus
;
1641 if (lpService
== NULL
)
1643 DPRINT("lpService == NULL!\n");
1644 return ERROR_INVALID_HANDLE
;
1647 /* Check current state */
1648 if (!ScmIsValidServiceState(lpServiceStatus
->dwCurrentState
))
1650 DPRINT("Invalid service state!\n");
1651 return ERROR_INVALID_DATA
;
1654 /* Check service type */
1655 if (!(lpServiceStatus
->dwServiceType
& SERVICE_WIN32
) &&
1656 (lpServiceStatus
->dwServiceType
& SERVICE_DRIVER
))
1658 DPRINT("Invalid service type!\n");
1659 return ERROR_INVALID_DATA
;
1662 /* Check accepted controls */
1663 if (lpServiceStatus
->dwControlsAccepted
& ~0xFF)
1665 DPRINT("Invalid controls accepted!\n");
1666 return ERROR_INVALID_DATA
;
1669 /* Lock the service database exclusively */
1670 ScmLockDatabaseExclusive();
1672 /* Save the current service state */
1673 dwPreviousState
= lpService
->Status
.dwCurrentState
;
1675 RtlCopyMemory(&lpService
->Status
,
1677 sizeof(SERVICE_STATUS
));
1679 /* Unlock the service database */
1680 ScmUnlockDatabase();
1682 /* Log a failed service stop */
1683 if ((lpServiceStatus
->dwCurrentState
== SERVICE_STOPPED
) &&
1684 (dwPreviousState
!= SERVICE_STOPPED
))
1686 if (lpServiceStatus
->dwWin32ExitCode
!= ERROR_SUCCESS
)
1688 swprintf(szErrorBuffer
, L
"%lu", lpServiceStatus
->dwWin32ExitCode
);
1689 lpErrorStrings
[0] = lpService
->lpDisplayName
;
1690 lpErrorStrings
[1] = szErrorBuffer
;
1692 ScmLogError(EVENT_SERVICE_EXIT_FAILED
,
1698 DPRINT("Set %S to %lu\n", lpService
->lpDisplayName
, lpService
->Status
.dwCurrentState
);
1699 DPRINT("RSetServiceStatus() done\n");
1701 return ERROR_SUCCESS
;
1706 DWORD
RUnlockServiceDatabase(
1710 return ERROR_SUCCESS
;
1715 DWORD
RNotifyBootConfigStatus(
1716 SVCCTL_HANDLEW lpMachineName
,
1717 DWORD BootAcceptable
)
1719 DPRINT1("RNotifyBootConfigStatus(%p %lu) called\n", lpMachineName
, BootAcceptable
);
1720 return ERROR_SUCCESS
;
1723 // return ERROR_CALL_NOT_IMPLEMENTED;
1728 DWORD
RI_ScSetServiceBitsW(
1729 RPC_SERVICE_STATUS_HANDLE hServiceStatus
,
1730 DWORD dwServiceBits
,
1732 int bUpdateImmediately
,
1736 return ERROR_CALL_NOT_IMPLEMENTED
;
1741 DWORD
RChangeServiceConfigW(
1742 SC_RPC_HANDLE hService
,
1743 DWORD dwServiceType
,
1745 DWORD dwErrorControl
,
1746 LPWSTR lpBinaryPathName
,
1747 LPWSTR lpLoadOrderGroup
,
1749 LPBYTE lpDependencies
,
1751 LPWSTR lpServiceStartName
,
1754 LPWSTR lpDisplayName
)
1756 DWORD dwError
= ERROR_SUCCESS
;
1757 PSERVICE_HANDLE hSvc
;
1758 PSERVICE lpService
= NULL
;
1759 HKEY hServiceKey
= NULL
;
1760 LPWSTR lpDisplayNameW
= NULL
;
1761 LPWSTR lpImagePathW
= NULL
;
1763 DPRINT("RChangeServiceConfigW() called\n");
1764 DPRINT("dwServiceType = %lu\n", dwServiceType
);
1765 DPRINT("dwStartType = %lu\n", dwStartType
);
1766 DPRINT("dwErrorControl = %lu\n", dwErrorControl
);
1767 DPRINT("lpBinaryPathName = %S\n", lpBinaryPathName
);
1768 DPRINT("lpLoadOrderGroup = %S\n", lpLoadOrderGroup
);
1769 DPRINT("lpDisplayName = %S\n", lpDisplayName
);
1772 return ERROR_SHUTDOWN_IN_PROGRESS
;
1774 hSvc
= ScmGetServiceFromHandle(hService
);
1777 DPRINT1("Invalid service handle!\n");
1778 return ERROR_INVALID_HANDLE
;
1781 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
1782 SERVICE_CHANGE_CONFIG
))
1784 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
1785 return ERROR_ACCESS_DENIED
;
1788 lpService
= hSvc
->ServiceEntry
;
1789 if (lpService
== NULL
)
1791 DPRINT("lpService == NULL!\n");
1792 return ERROR_INVALID_HANDLE
;
1795 /* Lock the service database exclusively */
1796 ScmLockDatabaseExclusive();
1798 if (lpService
->bDeleted
)
1800 DPRINT("The service has already been marked for delete!\n");
1801 dwError
= ERROR_SERVICE_MARKED_FOR_DELETE
;
1805 /* Open the service key */
1806 dwError
= ScmOpenServiceKey(lpService
->szServiceName
,
1809 if (dwError
!= ERROR_SUCCESS
)
1812 /* Write service data to the registry */
1813 /* Set the display name */
1814 if (lpDisplayName
!= NULL
&& *lpDisplayName
!= 0)
1816 RegSetValueExW(hServiceKey
,
1820 (LPBYTE
)lpDisplayName
,
1821 (wcslen(lpDisplayName
) + 1) * sizeof(WCHAR
));
1823 /* Update the display name */
1824 lpDisplayNameW
= HeapAlloc(GetProcessHeap(),
1826 (wcslen(lpDisplayName
) + 1) * sizeof(WCHAR
));
1827 if (lpDisplayNameW
== NULL
)
1829 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
1833 if (lpService
->lpDisplayName
!= lpService
->lpServiceName
)
1834 HeapFree(GetProcessHeap(), 0, lpService
->lpDisplayName
);
1836 lpService
->lpDisplayName
= lpDisplayNameW
;
1839 if (dwServiceType
!= SERVICE_NO_CHANGE
)
1841 /* Set the service type */
1842 dwError
= RegSetValueExW(hServiceKey
,
1846 (LPBYTE
)&dwServiceType
,
1848 if (dwError
!= ERROR_SUCCESS
)
1851 lpService
->Status
.dwServiceType
= dwServiceType
;
1854 if (dwStartType
!= SERVICE_NO_CHANGE
)
1856 /* Set the start value */
1857 dwError
= RegSetValueExW(hServiceKey
,
1861 (LPBYTE
)&dwStartType
,
1863 if (dwError
!= ERROR_SUCCESS
)
1866 lpService
->dwStartType
= dwStartType
;
1869 if (dwErrorControl
!= SERVICE_NO_CHANGE
)
1871 /* Set the error control value */
1872 dwError
= RegSetValueExW(hServiceKey
,
1876 (LPBYTE
)&dwErrorControl
,
1878 if (dwError
!= ERROR_SUCCESS
)
1881 lpService
->dwErrorControl
= dwErrorControl
;
1884 if (lpBinaryPathName
!= NULL
&& *lpBinaryPathName
!= 0)
1886 /* Set the image path */
1887 lpImagePathW
= lpBinaryPathName
;
1889 if (lpService
->Status
.dwServiceType
& SERVICE_DRIVER
)
1891 dwError
= ScmCanonDriverImagePath(lpService
->dwStartType
,
1895 if (dwError
!= ERROR_SUCCESS
)
1899 dwError
= RegSetValueExW(hServiceKey
,
1903 (LPBYTE
)lpImagePathW
,
1904 (wcslen(lpImagePathW
) + 1) * sizeof(WCHAR
));
1906 if (lpImagePathW
!= lpBinaryPathName
)
1907 HeapFree(GetProcessHeap(), 0, lpImagePathW
);
1909 if (dwError
!= ERROR_SUCCESS
)
1913 /* Set the group name */
1914 if (lpLoadOrderGroup
!= NULL
&& *lpLoadOrderGroup
!= 0)
1916 dwError
= RegSetValueExW(hServiceKey
,
1920 (LPBYTE
)lpLoadOrderGroup
,
1921 (wcslen(lpLoadOrderGroup
) + 1) * sizeof(WCHAR
));
1922 if (dwError
!= ERROR_SUCCESS
)
1925 dwError
= ScmSetServiceGroup(lpService
,
1927 if (dwError
!= ERROR_SUCCESS
)
1931 if (lpdwTagId
!= NULL
)
1933 dwError
= ScmAssignNewTag(lpService
);
1934 if (dwError
!= ERROR_SUCCESS
)
1937 dwError
= RegSetValueExW(hServiceKey
,
1941 (LPBYTE
)&lpService
->dwTag
,
1943 if (dwError
!= ERROR_SUCCESS
)
1946 *lpdwTagId
= lpService
->dwTag
;
1949 /* Write dependencies */
1950 if (lpDependencies
!= NULL
&& *lpDependencies
!= 0)
1952 dwError
= ScmWriteDependencies(hServiceKey
,
1953 (LPWSTR
)lpDependencies
,
1955 if (dwError
!= ERROR_SUCCESS
)
1959 if (lpPassword
!= NULL
)
1961 /* FIXME: Write password */
1965 if (hServiceKey
!= NULL
)
1966 RegCloseKey(hServiceKey
);
1968 /* Unlock the service database */
1969 ScmUnlockDatabase();
1971 DPRINT("RChangeServiceConfigW() done (Error %lu)\n", dwError
);
1978 DWORD
RCreateServiceW(
1979 SC_RPC_HANDLE hSCManager
,
1980 LPCWSTR lpServiceName
,
1981 LPCWSTR lpDisplayName
,
1982 DWORD dwDesiredAccess
,
1983 DWORD dwServiceType
,
1985 DWORD dwErrorControl
,
1986 LPCWSTR lpBinaryPathName
,
1987 LPCWSTR lpLoadOrderGroup
,
1989 LPBYTE lpDependencies
,
1991 LPCWSTR lpServiceStartName
,
1994 LPSC_RPC_HANDLE lpServiceHandle
)
1996 PMANAGER_HANDLE hManager
;
1997 DWORD dwError
= ERROR_SUCCESS
;
1998 PSERVICE lpService
= NULL
;
1999 SC_HANDLE hServiceHandle
= NULL
;
2000 LPWSTR lpImagePath
= NULL
;
2001 HKEY hServiceKey
= NULL
;
2002 LPWSTR lpObjectName
;
2004 DPRINT("RCreateServiceW() called\n");
2005 DPRINT("lpServiceName = %S\n", lpServiceName
);
2006 DPRINT("lpDisplayName = %S\n", lpDisplayName
);
2007 DPRINT("dwDesiredAccess = %lx\n", dwDesiredAccess
);
2008 DPRINT("dwServiceType = %lu\n", dwServiceType
);
2009 DPRINT("dwStartType = %lu\n", dwStartType
);
2010 DPRINT("dwErrorControl = %lu\n", dwErrorControl
);
2011 DPRINT("lpBinaryPathName = %S\n", lpBinaryPathName
);
2012 DPRINT("lpLoadOrderGroup = %S\n", lpLoadOrderGroup
);
2013 DPRINT("lpdwTagId = %p\n", lpdwTagId
);
2016 return ERROR_SHUTDOWN_IN_PROGRESS
;
2018 hManager
= ScmGetServiceManagerFromHandle(hSCManager
);
2019 if (hManager
== NULL
)
2021 DPRINT1("Invalid service manager handle!\n");
2022 return ERROR_INVALID_HANDLE
;
2025 /* Check access rights */
2026 if (!RtlAreAllAccessesGranted(hManager
->Handle
.DesiredAccess
,
2027 SC_MANAGER_CREATE_SERVICE
))
2029 DPRINT("Insufficient access rights! 0x%lx\n",
2030 hManager
->Handle
.DesiredAccess
);
2031 return ERROR_ACCESS_DENIED
;
2034 if (wcslen(lpServiceName
) == 0)
2036 return ERROR_INVALID_NAME
;
2039 if (wcslen(lpBinaryPathName
) == 0)
2041 return ERROR_INVALID_PARAMETER
;
2044 /* Check for invalid service type value */
2045 if ((dwServiceType
!= SERVICE_KERNEL_DRIVER
) &&
2046 (dwServiceType
!= SERVICE_FILE_SYSTEM_DRIVER
) &&
2047 ((dwServiceType
& ~SERVICE_INTERACTIVE_PROCESS
) != SERVICE_WIN32_OWN_PROCESS
) &&
2048 ((dwServiceType
& ~SERVICE_INTERACTIVE_PROCESS
) != SERVICE_WIN32_SHARE_PROCESS
))
2049 return ERROR_INVALID_PARAMETER
;
2051 /* Check for invalid start type value */
2052 if ((dwStartType
!= SERVICE_BOOT_START
) &&
2053 (dwStartType
!= SERVICE_SYSTEM_START
) &&
2054 (dwStartType
!= SERVICE_AUTO_START
) &&
2055 (dwStartType
!= SERVICE_DEMAND_START
) &&
2056 (dwStartType
!= SERVICE_DISABLED
))
2057 return ERROR_INVALID_PARAMETER
;
2059 /* Only drivers can be boot start or system start services */
2060 if ((dwStartType
== SERVICE_BOOT_START
) ||
2061 (dwStartType
== SERVICE_SYSTEM_START
))
2063 if ((dwServiceType
!= SERVICE_KERNEL_DRIVER
) &&
2064 (dwServiceType
!= SERVICE_FILE_SYSTEM_DRIVER
))
2065 return ERROR_INVALID_PARAMETER
;
2068 /* Check for invalid error control value */
2069 if ((dwErrorControl
!= SERVICE_ERROR_IGNORE
) &&
2070 (dwErrorControl
!= SERVICE_ERROR_NORMAL
) &&
2071 (dwErrorControl
!= SERVICE_ERROR_SEVERE
) &&
2072 (dwErrorControl
!= SERVICE_ERROR_CRITICAL
))
2073 return ERROR_INVALID_PARAMETER
;
2075 if ((dwServiceType
== (SERVICE_WIN32_OWN_PROCESS
| SERVICE_INTERACTIVE_PROCESS
)) &&
2076 (lpServiceStartName
))
2078 return ERROR_INVALID_PARAMETER
;
2081 if (lpdwTagId
&& (!lpLoadOrderGroup
|| !*lpLoadOrderGroup
))
2083 return ERROR_INVALID_PARAMETER
;
2086 /* Lock the service database exclusively */
2087 ScmLockDatabaseExclusive();
2089 lpService
= ScmGetServiceEntryByName(lpServiceName
);
2092 /* Unlock the service database */
2093 ScmUnlockDatabase();
2095 /* Check if it is marked for deletion */
2096 if (lpService
->bDeleted
)
2097 return ERROR_SERVICE_MARKED_FOR_DELETE
;
2099 /* Return Error exist */
2100 return ERROR_SERVICE_EXISTS
;
2103 if (lpDisplayName
!= NULL
&&
2104 ScmGetServiceEntryByDisplayName(lpDisplayName
) != NULL
)
2106 /* Unlock the service database */
2107 ScmUnlockDatabase();
2109 return ERROR_DUPLICATE_SERVICE_NAME
;
2112 if (dwServiceType
& SERVICE_DRIVER
)
2114 dwError
= ScmCanonDriverImagePath(dwStartType
,
2117 if (dwError
!= ERROR_SUCCESS
)
2122 if (dwStartType
== SERVICE_BOOT_START
||
2123 dwStartType
== SERVICE_SYSTEM_START
)
2125 /* Unlock the service database */
2126 ScmUnlockDatabase();
2128 return ERROR_INVALID_PARAMETER
;
2132 /* Allocate a new service entry */
2133 dwError
= ScmCreateNewServiceRecord(lpServiceName
,
2135 if (dwError
!= ERROR_SUCCESS
)
2138 /* Fill the new service entry */
2139 lpService
->Status
.dwServiceType
= dwServiceType
;
2140 lpService
->dwStartType
= dwStartType
;
2141 lpService
->dwErrorControl
= dwErrorControl
;
2143 /* Fill the display name */
2144 if (lpDisplayName
!= NULL
&&
2145 *lpDisplayName
!= 0 &&
2146 _wcsicmp(lpService
->lpDisplayName
, lpDisplayName
) != 0)
2148 lpService
->lpDisplayName
= HeapAlloc(GetProcessHeap(), 0,
2149 (wcslen(lpDisplayName
) + 1) * sizeof(WCHAR
));
2150 if (lpService
->lpDisplayName
== NULL
)
2152 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
2155 wcscpy(lpService
->lpDisplayName
, lpDisplayName
);
2158 /* Assign the service to a group */
2159 if (lpLoadOrderGroup
!= NULL
&& *lpLoadOrderGroup
!= 0)
2161 dwError
= ScmSetServiceGroup(lpService
,
2163 if (dwError
!= ERROR_SUCCESS
)
2167 /* Assign a new tag */
2168 if (lpdwTagId
!= NULL
)
2170 dwError
= ScmAssignNewTag(lpService
);
2171 if (dwError
!= ERROR_SUCCESS
)
2175 /* Write service data to the registry */
2176 /* Create the service key */
2177 dwError
= ScmCreateServiceKey(lpServiceName
,
2180 if (dwError
!= ERROR_SUCCESS
)
2183 /* Set the display name */
2184 if (lpDisplayName
!= NULL
&& *lpDisplayName
!= 0)
2186 RegSetValueExW(hServiceKey
,
2190 (LPBYTE
)lpDisplayName
,
2191 (wcslen(lpDisplayName
) + 1) * sizeof(WCHAR
));
2194 /* Set the service type */
2195 dwError
= RegSetValueExW(hServiceKey
,
2199 (LPBYTE
)&dwServiceType
,
2201 if (dwError
!= ERROR_SUCCESS
)
2204 /* Set the start value */
2205 dwError
= RegSetValueExW(hServiceKey
,
2209 (LPBYTE
)&dwStartType
,
2211 if (dwError
!= ERROR_SUCCESS
)
2214 /* Set the error control value */
2215 dwError
= RegSetValueExW(hServiceKey
,
2219 (LPBYTE
)&dwErrorControl
,
2221 if (dwError
!= ERROR_SUCCESS
)
2224 /* Set the image path */
2225 if (dwServiceType
& SERVICE_WIN32
)
2227 dwError
= RegSetValueExW(hServiceKey
,
2231 (LPBYTE
)lpBinaryPathName
,
2232 (wcslen(lpBinaryPathName
) + 1) * sizeof(WCHAR
));
2233 if (dwError
!= ERROR_SUCCESS
)
2236 else if (dwServiceType
& SERVICE_DRIVER
)
2238 dwError
= RegSetValueExW(hServiceKey
,
2242 (LPBYTE
)lpImagePath
,
2243 (wcslen(lpImagePath
) + 1) * sizeof(WCHAR
));
2244 if (dwError
!= ERROR_SUCCESS
)
2248 /* Set the group name */
2249 if (lpLoadOrderGroup
!= NULL
&& *lpLoadOrderGroup
!= 0)
2251 dwError
= RegSetValueExW(hServiceKey
,
2255 (LPBYTE
)lpLoadOrderGroup
,
2256 (wcslen(lpLoadOrderGroup
) + 1) * sizeof(WCHAR
));
2257 if (dwError
!= ERROR_SUCCESS
)
2261 if (lpdwTagId
!= NULL
)
2263 dwError
= RegSetValueExW(hServiceKey
,
2267 (LPBYTE
)&lpService
->dwTag
,
2269 if (dwError
!= ERROR_SUCCESS
)
2273 /* Write dependencies */
2274 if (lpDependencies
!= NULL
&& *lpDependencies
!= 0)
2276 dwError
= ScmWriteDependencies(hServiceKey
,
2277 (LPCWSTR
)lpDependencies
,
2279 if (dwError
!= ERROR_SUCCESS
)
2283 /* Write service start name */
2284 if (dwServiceType
& SERVICE_WIN32
)
2286 lpObjectName
= (lpServiceStartName
!= NULL
) ? (LPWSTR
)lpServiceStartName
: L
"LocalSystem";
2287 dwError
= RegSetValueExW(hServiceKey
,
2291 (LPBYTE
)lpObjectName
,
2292 (wcslen(lpObjectName
) + 1) * sizeof(WCHAR
));
2293 if (dwError
!= ERROR_SUCCESS
)
2297 if (lpPassword
!= NULL
)
2299 /* FIXME: Write password */
2302 dwError
= ScmCreateServiceHandle(lpService
,
2304 if (dwError
!= ERROR_SUCCESS
)
2307 dwError
= ScmCheckAccess(hServiceHandle
,
2309 if (dwError
!= ERROR_SUCCESS
)
2312 lpService
->dwRefCount
= 1;
2313 DPRINT("CreateService - lpService->dwRefCount %u\n", lpService
->dwRefCount
);
2316 /* Unlock the service database */
2317 ScmUnlockDatabase();
2319 if (hServiceKey
!= NULL
)
2320 RegCloseKey(hServiceKey
);
2322 if (dwError
== ERROR_SUCCESS
)
2324 DPRINT("hService %p\n", hServiceHandle
);
2325 *lpServiceHandle
= (SC_RPC_HANDLE
)hServiceHandle
;
2327 if (lpdwTagId
!= NULL
)
2328 *lpdwTagId
= lpService
->dwTag
;
2332 if (lpService
!= NULL
&&
2333 lpService
->lpServiceName
!= NULL
)
2335 /* Release the display name buffer */
2336 HeapFree(GetProcessHeap(), 0, lpService
->lpDisplayName
);
2341 /* Remove the service handle */
2342 HeapFree(GetProcessHeap(), 0, hServiceHandle
);
2345 if (lpService
!= NULL
)
2347 /* FIXME: remove the service entry */
2351 if (lpImagePath
!= NULL
)
2352 HeapFree(GetProcessHeap(), 0, lpImagePath
);
2354 DPRINT("RCreateServiceW() done (Error %lu)\n", dwError
);
2361 DWORD
REnumDependentServicesW(
2362 SC_RPC_HANDLE hService
,
2363 DWORD dwServiceState
,
2366 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
2367 LPBOUNDED_DWORD_256K lpServicesReturned
)
2369 DWORD dwError
= ERROR_SUCCESS
;
2370 DWORD dwServicesReturned
= 0;
2371 DWORD dwServiceCount
;
2372 HKEY hServicesKey
= NULL
;
2373 PSERVICE_HANDLE hSvc
;
2374 PSERVICE lpService
= NULL
;
2375 PSERVICE
*lpServicesArray
= NULL
;
2376 LPENUM_SERVICE_STATUSW lpServicesPtr
= NULL
;
2379 *pcbBytesNeeded
= 0;
2380 *lpServicesReturned
= 0;
2382 DPRINT("REnumDependentServicesW() called\n");
2384 hSvc
= ScmGetServiceFromHandle(hService
);
2387 DPRINT1("Invalid service handle!\n");
2388 return ERROR_INVALID_HANDLE
;
2391 lpService
= hSvc
->ServiceEntry
;
2393 /* Check access rights */
2394 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
2395 SC_MANAGER_ENUMERATE_SERVICE
))
2397 DPRINT("Insufficient access rights! 0x%lx\n",
2398 hSvc
->Handle
.DesiredAccess
);
2399 return ERROR_ACCESS_DENIED
;
2402 /* Open the Services Reg key */
2403 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
2404 L
"System\\CurrentControlSet\\Services",
2408 if (dwError
!= ERROR_SUCCESS
)
2411 /* First determine the bytes needed and get the number of dependent services */
2412 dwError
= Int_EnumDependentServicesW(hServicesKey
,
2417 &dwServicesReturned
);
2418 if (dwError
!= ERROR_SUCCESS
)
2421 /* If buffer size is less than the bytes needed or pointer is null */
2422 if ((!lpServices
) || (cbBufSize
< *pcbBytesNeeded
))
2424 dwError
= ERROR_MORE_DATA
;
2428 /* Allocate memory for array of service pointers */
2429 lpServicesArray
= HeapAlloc(GetProcessHeap(),
2431 (dwServicesReturned
+ 1) * sizeof(PSERVICE
));
2432 if (!lpServicesArray
)
2434 DPRINT1("Could not allocate a buffer!!\n");
2435 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
2439 dwServicesReturned
= 0;
2440 *pcbBytesNeeded
= 0;
2442 dwError
= Int_EnumDependentServicesW(hServicesKey
,
2447 &dwServicesReturned
);
2448 if (dwError
!= ERROR_SUCCESS
)
2453 lpServicesPtr
= (LPENUM_SERVICE_STATUSW
) lpServices
;
2454 lpStr
= (LPWSTR
)(lpServices
+ (dwServicesReturned
* sizeof(ENUM_SERVICE_STATUSW
)));
2456 /* Copy EnumDepenedentService to Buffer */
2457 for (dwServiceCount
= 0; dwServiceCount
< dwServicesReturned
; dwServiceCount
++)
2459 lpService
= lpServicesArray
[dwServiceCount
];
2461 /* Copy status info */
2462 memcpy(&lpServicesPtr
->ServiceStatus
,
2464 sizeof(SERVICE_STATUS
));
2466 /* Copy display name */
2467 wcscpy(lpStr
, lpService
->lpDisplayName
);
2468 lpServicesPtr
->lpDisplayName
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServices
);
2469 lpStr
+= (wcslen(lpService
->lpDisplayName
) + 1);
2471 /* Copy service name */
2472 wcscpy(lpStr
, lpService
->lpServiceName
);
2473 lpServicesPtr
->lpServiceName
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServices
);
2474 lpStr
+= (wcslen(lpService
->lpServiceName
) + 1);
2479 *lpServicesReturned
= dwServicesReturned
;
2482 if (lpServicesArray
!= NULL
)
2483 HeapFree(GetProcessHeap(), 0, lpServicesArray
);
2485 RegCloseKey(hServicesKey
);
2487 DPRINT("REnumDependentServicesW() done (Error %lu)\n", dwError
);
2494 DWORD
REnumServicesStatusW(
2495 SC_RPC_HANDLE hSCManager
,
2496 DWORD dwServiceType
,
2497 DWORD dwServiceState
,
2500 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
2501 LPBOUNDED_DWORD_256K lpServicesReturned
,
2502 LPBOUNDED_DWORD_256K lpResumeHandle
)
2504 PMANAGER_HANDLE hManager
;
2506 DWORD dwError
= ERROR_SUCCESS
;
2507 PLIST_ENTRY ServiceEntry
;
2508 PSERVICE CurrentService
;
2510 DWORD dwRequiredSize
;
2511 DWORD dwServiceCount
;
2513 DWORD dwLastResumeCount
= 0;
2514 LPENUM_SERVICE_STATUSW lpStatusPtr
;
2517 DPRINT("REnumServicesStatusW() called\n");
2520 return ERROR_SHUTDOWN_IN_PROGRESS
;
2522 hManager
= ScmGetServiceManagerFromHandle(hSCManager
);
2523 if (hManager
== NULL
)
2525 DPRINT1("Invalid service manager handle!\n");
2526 return ERROR_INVALID_HANDLE
;
2530 *pcbBytesNeeded
= 0;
2531 *lpServicesReturned
= 0;
2533 if ((dwServiceType
== 0) ||
2534 ((dwServiceType
& ~(SERVICE_DRIVER
| SERVICE_WIN32
)) != 0))
2536 DPRINT("Not a valid Service Type!\n");
2537 return ERROR_INVALID_PARAMETER
;
2540 if ((dwServiceState
!= SERVICE_ACTIVE
) &&
2541 (dwServiceState
!= SERVICE_INACTIVE
) &&
2542 (dwServiceState
!= SERVICE_STATE_ALL
))
2544 DPRINT("Not a valid Service State!\n");
2545 return ERROR_INVALID_PARAMETER
;
2548 /* Check access rights */
2549 if (!RtlAreAllAccessesGranted(hManager
->Handle
.DesiredAccess
,
2550 SC_MANAGER_ENUMERATE_SERVICE
))
2552 DPRINT("Insufficient access rights! 0x%lx\n",
2553 hManager
->Handle
.DesiredAccess
);
2554 return ERROR_ACCESS_DENIED
;
2558 dwLastResumeCount
= *lpResumeHandle
;
2560 /* Lock the service database shared */
2561 ScmLockDatabaseShared();
2563 lpService
= ScmGetServiceEntryByResumeCount(dwLastResumeCount
);
2564 if (lpService
== NULL
)
2566 dwError
= ERROR_SUCCESS
;
2573 for (ServiceEntry
= &lpService
->ServiceListEntry
;
2574 ServiceEntry
!= &ServiceListHead
;
2575 ServiceEntry
= ServiceEntry
->Flink
)
2577 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
2581 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
2584 dwState
= SERVICE_ACTIVE
;
2585 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
2586 dwState
= SERVICE_INACTIVE
;
2588 if ((dwState
& dwServiceState
) == 0)
2591 dwSize
= sizeof(ENUM_SERVICE_STATUSW
) +
2592 ((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
2593 ((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
2595 if (dwRequiredSize
+ dwSize
> dwBufSize
)
2597 DPRINT("Service name: %S no fit\n", CurrentService
->lpServiceName
);
2601 DPRINT("Service name: %S fit\n", CurrentService
->lpServiceName
);
2602 dwRequiredSize
+= dwSize
;
2604 dwLastResumeCount
= CurrentService
->dwResumeCount
;
2607 DPRINT("dwRequiredSize: %lu\n", dwRequiredSize
);
2608 DPRINT("dwServiceCount: %lu\n", dwServiceCount
);
2611 ServiceEntry
!= &ServiceListHead
;
2612 ServiceEntry
= ServiceEntry
->Flink
)
2614 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
2618 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
2621 dwState
= SERVICE_ACTIVE
;
2622 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
2623 dwState
= SERVICE_INACTIVE
;
2625 if ((dwState
& dwServiceState
) == 0)
2628 dwRequiredSize
+= (sizeof(ENUM_SERVICE_STATUSW
) +
2629 ((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
2630 ((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
)));
2632 dwError
= ERROR_MORE_DATA
;
2635 DPRINT("*pcbBytesNeeded: %lu\n", dwRequiredSize
);
2638 *lpResumeHandle
= dwLastResumeCount
;
2640 *lpServicesReturned
= dwServiceCount
;
2641 *pcbBytesNeeded
= dwRequiredSize
;
2643 lpStatusPtr
= (LPENUM_SERVICE_STATUSW
)lpBuffer
;
2644 lpStringPtr
= (LPWSTR
)((ULONG_PTR
)lpBuffer
+
2645 dwServiceCount
* sizeof(ENUM_SERVICE_STATUSW
));
2648 for (ServiceEntry
= &lpService
->ServiceListEntry
;
2649 ServiceEntry
!= &ServiceListHead
;
2650 ServiceEntry
= ServiceEntry
->Flink
)
2652 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
2656 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
2659 dwState
= SERVICE_ACTIVE
;
2660 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
2661 dwState
= SERVICE_INACTIVE
;
2663 if ((dwState
& dwServiceState
) == 0)
2666 dwSize
= sizeof(ENUM_SERVICE_STATUSW
) +
2667 ((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
2668 ((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
2670 if (dwRequiredSize
+ dwSize
> dwBufSize
)
2673 /* Copy the service name */
2674 wcscpy(lpStringPtr
, CurrentService
->lpServiceName
);
2675 lpStatusPtr
->lpServiceName
= (LPWSTR
)((ULONG_PTR
)lpStringPtr
- (ULONG_PTR
)lpBuffer
);
2676 lpStringPtr
+= (wcslen(CurrentService
->lpServiceName
) + 1);
2678 /* Copy the display name */
2679 wcscpy(lpStringPtr
, CurrentService
->lpDisplayName
);
2680 lpStatusPtr
->lpDisplayName
= (LPWSTR
)((ULONG_PTR
)lpStringPtr
- (ULONG_PTR
)lpBuffer
);
2681 lpStringPtr
+= (wcslen(CurrentService
->lpDisplayName
) + 1);
2683 /* Copy the status information */
2684 memcpy(&lpStatusPtr
->ServiceStatus
,
2685 &CurrentService
->Status
,
2686 sizeof(SERVICE_STATUS
));
2689 dwRequiredSize
+= dwSize
;
2692 if (dwError
== ERROR_SUCCESS
)
2694 *pcbBytesNeeded
= 0;
2695 if (lpResumeHandle
) *lpResumeHandle
= 0;
2699 /* Unlock the service database */
2700 ScmUnlockDatabase();
2702 DPRINT("REnumServicesStatusW() done (Error %lu)\n", dwError
);
2709 DWORD
ROpenSCManagerW(
2710 LPWSTR lpMachineName
,
2711 LPWSTR lpDatabaseName
,
2712 DWORD dwDesiredAccess
,
2713 LPSC_RPC_HANDLE lpScHandle
)
2718 DPRINT("ROpenSCManagerW() called\n");
2719 DPRINT("lpMachineName = %p\n", lpMachineName
);
2720 DPRINT("lpMachineName: %S\n", lpMachineName
);
2721 DPRINT("lpDataBaseName = %p\n", lpDatabaseName
);
2722 DPRINT("lpDataBaseName: %S\n", lpDatabaseName
);
2723 DPRINT("dwDesiredAccess = %x\n", dwDesiredAccess
);
2726 return ERROR_SHUTDOWN_IN_PROGRESS
;
2729 return ERROR_INVALID_PARAMETER
;
2731 dwError
= ScmCreateManagerHandle(lpDatabaseName
,
2733 if (dwError
!= ERROR_SUCCESS
)
2735 DPRINT("ScmCreateManagerHandle() failed (Error %lu)\n", dwError
);
2739 /* Check the desired access */
2740 dwError
= ScmCheckAccess(hHandle
,
2741 dwDesiredAccess
| SC_MANAGER_CONNECT
);
2742 if (dwError
!= ERROR_SUCCESS
)
2744 DPRINT("ScmCheckAccess() failed (Error %lu)\n", dwError
);
2745 HeapFree(GetProcessHeap(), 0, hHandle
);
2749 *lpScHandle
= (SC_RPC_HANDLE
)hHandle
;
2750 DPRINT("*hScm = %p\n", *lpScHandle
);
2752 DPRINT("ROpenSCManagerW() done\n");
2754 return ERROR_SUCCESS
;
2759 DWORD
ROpenServiceW(
2760 SC_RPC_HANDLE hSCManager
,
2761 LPWSTR lpServiceName
,
2762 DWORD dwDesiredAccess
,
2763 LPSC_RPC_HANDLE lpServiceHandle
)
2766 PMANAGER_HANDLE hManager
;
2768 DWORD dwError
= ERROR_SUCCESS
;
2770 DPRINT("ROpenServiceW() called\n");
2771 DPRINT("hSCManager = %p\n", hSCManager
);
2772 DPRINT("lpServiceName = %p\n", lpServiceName
);
2773 DPRINT("lpServiceName: %S\n", lpServiceName
);
2774 DPRINT("dwDesiredAccess = %x\n", dwDesiredAccess
);
2777 return ERROR_SHUTDOWN_IN_PROGRESS
;
2779 hManager
= ScmGetServiceManagerFromHandle(hSCManager
);
2780 if (hManager
== NULL
)
2782 DPRINT1("Invalid service manager handle!\n");
2783 return ERROR_INVALID_HANDLE
;
2786 if (!lpServiceHandle
)
2787 return ERROR_INVALID_PARAMETER
;
2790 return ERROR_INVALID_ADDRESS
;
2792 /* Lock the service database exclusive */
2793 ScmLockDatabaseExclusive();
2795 /* Get service database entry */
2796 lpService
= ScmGetServiceEntryByName(lpServiceName
);
2797 if (lpService
== NULL
)
2799 DPRINT("Could not find a service!\n");
2800 dwError
= ERROR_SERVICE_DOES_NOT_EXIST
;
2804 /* Create a service handle */
2805 dwError
= ScmCreateServiceHandle(lpService
,
2807 if (dwError
!= ERROR_SUCCESS
)
2809 DPRINT("ScmCreateServiceHandle() failed (Error %lu)\n", dwError
);
2813 /* Check the desired access */
2814 dwError
= ScmCheckAccess(hHandle
,
2816 if (dwError
!= ERROR_SUCCESS
)
2818 DPRINT("ScmCheckAccess() failed (Error %lu)\n", dwError
);
2819 HeapFree(GetProcessHeap(), 0, hHandle
);
2823 lpService
->dwRefCount
++;
2824 DPRINT("OpenService - lpService->dwRefCount %u\n",lpService
->dwRefCount
);
2826 *lpServiceHandle
= (SC_RPC_HANDLE
)hHandle
;
2827 DPRINT("*hService = %p\n", *lpServiceHandle
);
2830 /* Unlock the service database */
2831 ScmUnlockDatabase();
2833 DPRINT("ROpenServiceW() done\n");
2840 DWORD
RQueryServiceConfigW(
2841 SC_RPC_HANDLE hService
,
2842 LPBYTE lpBuf
, //LPQUERY_SERVICE_CONFIGW lpServiceConfig,
2844 LPBOUNDED_DWORD_8K pcbBytesNeeded
)
2846 LPQUERY_SERVICE_CONFIGW lpServiceConfig
= (LPQUERY_SERVICE_CONFIGW
)lpBuf
;
2847 DWORD dwError
= ERROR_SUCCESS
;
2848 PSERVICE_HANDLE hSvc
;
2849 PSERVICE lpService
= NULL
;
2850 HKEY hServiceKey
= NULL
;
2851 LPWSTR lpImagePath
= NULL
;
2852 LPWSTR lpServiceStartName
= NULL
;
2853 LPWSTR lpDependencies
= NULL
;
2854 DWORD dwDependenciesLength
= 0;
2855 DWORD dwRequiredSize
;
2856 LPQUERY_SERVICE_CONFIGW lpConfig
= NULL
;
2857 WCHAR lpEmptyString
[] = {0,0};
2860 DPRINT("RQueryServiceConfigW() called\n");
2863 return ERROR_SHUTDOWN_IN_PROGRESS
;
2865 hSvc
= ScmGetServiceFromHandle(hService
);
2868 DPRINT1("Invalid service handle!\n");
2869 return ERROR_INVALID_HANDLE
;
2872 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
2873 SERVICE_QUERY_CONFIG
))
2875 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
2876 return ERROR_ACCESS_DENIED
;
2879 lpService
= hSvc
->ServiceEntry
;
2880 if (lpService
== NULL
)
2882 DPRINT("lpService == NULL!\n");
2883 return ERROR_INVALID_HANDLE
;
2886 /* Lock the service database shared */
2887 ScmLockDatabaseShared();
2889 dwError
= ScmOpenServiceKey(lpService
->lpServiceName
,
2892 if (dwError
!= ERROR_SUCCESS
)
2895 /* Read the image path */
2896 dwError
= ScmReadString(hServiceKey
,
2899 if (dwError
!= ERROR_SUCCESS
)
2902 /* Read the service start name */
2903 ScmReadString(hServiceKey
,
2905 &lpServiceStartName
);
2907 /* Read the dependencies */
2908 ScmReadDependencies(hServiceKey
,
2910 &dwDependenciesLength
);
2912 dwRequiredSize
= sizeof(QUERY_SERVICE_CONFIGW
);
2914 if (lpImagePath
!= NULL
)
2915 dwRequiredSize
+= ((wcslen(lpImagePath
) + 1) * sizeof(WCHAR
));
2917 dwRequiredSize
+= 2 * sizeof(WCHAR
);
2919 if (lpService
->lpGroup
!= NULL
)
2920 dwRequiredSize
+= ((wcslen(lpService
->lpGroup
->lpGroupName
) + 1) * sizeof(WCHAR
));
2922 dwRequiredSize
+= 2 * sizeof(WCHAR
);
2924 if (lpDependencies
!= NULL
)
2925 dwRequiredSize
+= dwDependenciesLength
* sizeof(WCHAR
);
2927 dwRequiredSize
+= 2 * sizeof(WCHAR
);
2929 if (lpServiceStartName
!= NULL
)
2930 dwRequiredSize
+= ((wcslen(lpServiceStartName
) + 1) * sizeof(WCHAR
));
2932 dwRequiredSize
+= 2 * sizeof(WCHAR
);
2934 if (lpService
->lpDisplayName
!= NULL
)
2935 dwRequiredSize
+= ((wcslen(lpService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
2937 dwRequiredSize
+= 2 * sizeof(WCHAR
);
2939 if (lpServiceConfig
== NULL
|| cbBufSize
< dwRequiredSize
)
2941 dwError
= ERROR_INSUFFICIENT_BUFFER
;
2945 lpConfig
= (LPQUERY_SERVICE_CONFIGW
)lpServiceConfig
;
2946 lpConfig
->dwServiceType
= lpService
->Status
.dwServiceType
;
2947 lpConfig
->dwStartType
= lpService
->dwStartType
;
2948 lpConfig
->dwErrorControl
= lpService
->dwErrorControl
;
2949 lpConfig
->dwTagId
= lpService
->dwTag
;
2951 lpStr
= (LPWSTR
)(lpConfig
+ 1);
2953 /* Append the image path */
2954 if (lpImagePath
!= NULL
)
2956 wcscpy(lpStr
, lpImagePath
);
2960 wcscpy(lpStr
, lpEmptyString
);
2963 lpConfig
->lpBinaryPathName
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpConfig
);
2964 lpStr
+= (wcslen(lpStr
) + 1);
2966 /* Append the group name */
2967 if (lpService
->lpGroup
!= NULL
)
2969 wcscpy(lpStr
, lpService
->lpGroup
->lpGroupName
);
2973 wcscpy(lpStr
, lpEmptyString
);
2976 lpConfig
->lpLoadOrderGroup
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpConfig
);
2977 lpStr
+= (wcslen(lpStr
) + 1);
2979 /* Append Dependencies */
2980 if (lpDependencies
!= NULL
)
2984 dwDependenciesLength
* sizeof(WCHAR
));
2988 wcscpy(lpStr
, lpEmptyString
);
2991 lpConfig
->lpDependencies
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpConfig
);
2992 if (lpDependencies
!= NULL
)
2993 lpStr
+= dwDependenciesLength
;
2995 lpStr
+= (wcslen(lpStr
) + 1);
2997 /* Append the service start name */
2998 if (lpServiceStartName
!= NULL
)
3000 wcscpy(lpStr
, lpServiceStartName
);
3004 wcscpy(lpStr
, lpEmptyString
);
3007 lpConfig
->lpServiceStartName
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpConfig
);
3008 lpStr
+= (wcslen(lpStr
) + 1);
3010 /* Append the display name */
3011 if (lpService
->lpDisplayName
!= NULL
)
3013 wcscpy(lpStr
, lpService
->lpDisplayName
);
3017 wcscpy(lpStr
, lpEmptyString
);
3020 lpConfig
->lpDisplayName
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpConfig
);
3023 if (pcbBytesNeeded
!= NULL
)
3024 *pcbBytesNeeded
= dwRequiredSize
;
3027 /* Unlock the service database */
3028 ScmUnlockDatabase();
3030 if (lpImagePath
!= NULL
)
3031 HeapFree(GetProcessHeap(), 0, lpImagePath
);
3033 if (lpServiceStartName
!= NULL
)
3034 HeapFree(GetProcessHeap(), 0, lpServiceStartName
);
3036 if (lpDependencies
!= NULL
)
3037 HeapFree(GetProcessHeap(), 0, lpDependencies
);
3039 if (hServiceKey
!= NULL
)
3040 RegCloseKey(hServiceKey
);
3042 DPRINT("RQueryServiceConfigW() done\n");
3049 DWORD
RQueryServiceLockStatusW(
3050 SC_RPC_HANDLE hSCManager
,
3051 LPQUERY_SERVICE_LOCK_STATUSW lpLockStatus
,
3053 LPBOUNDED_DWORD_4K pcbBytesNeeded
)
3056 return ERROR_CALL_NOT_IMPLEMENTED
;
3061 DWORD
RStartServiceW(
3062 SC_RPC_HANDLE hService
,
3064 LPSTRING_PTRSW argv
)
3066 DWORD dwError
= ERROR_SUCCESS
;
3067 PSERVICE_HANDLE hSvc
;
3068 PSERVICE lpService
= NULL
;
3071 DPRINT("RStartServiceW(%p %lu %p) called\n", hService
, argc
, argv
);
3072 DPRINT(" argc: %lu\n", argc
);
3075 for (i
= 0; i
< argc
; i
++)
3077 DPRINT(" argv[%lu]: %S\n", i
, argv
[i
]);
3082 return ERROR_SHUTDOWN_IN_PROGRESS
;
3084 hSvc
= ScmGetServiceFromHandle(hService
);
3087 DPRINT1("Invalid service handle!\n");
3088 return ERROR_INVALID_HANDLE
;
3091 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
3094 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
3095 return ERROR_ACCESS_DENIED
;
3098 lpService
= hSvc
->ServiceEntry
;
3099 if (lpService
== NULL
)
3101 DPRINT("lpService == NULL!\n");
3102 return ERROR_INVALID_HANDLE
;
3105 if (lpService
->dwStartType
== SERVICE_DISABLED
)
3106 return ERROR_SERVICE_DISABLED
;
3108 if (lpService
->bDeleted
)
3109 return ERROR_SERVICE_MARKED_FOR_DELETE
;
3111 /* Start the service */
3112 dwError
= ScmStartService(lpService
, argc
, (LPWSTR
*)argv
);
3119 DWORD
RGetServiceDisplayNameW(
3120 SC_RPC_HANDLE hSCManager
,
3121 LPCWSTR lpServiceName
,
3122 LPWSTR lpDisplayName
,
3125 // PMANAGER_HANDLE hManager;
3130 DPRINT("RGetServiceDisplayNameW() called\n");
3131 DPRINT("hSCManager = %p\n", hSCManager
);
3132 DPRINT("lpServiceName: %S\n", lpServiceName
);
3133 DPRINT("lpDisplayName: %p\n", lpDisplayName
);
3134 DPRINT("*lpcchBuffer: %lu\n", *lpcchBuffer
);
3136 // hManager = (PMANAGER_HANDLE)hSCManager;
3137 // if (hManager->Handle.Tag != MANAGER_TAG)
3139 // DPRINT("Invalid manager handle!\n");
3140 // return ERROR_INVALID_HANDLE;
3143 /* Get service database entry */
3144 lpService
= ScmGetServiceEntryByName(lpServiceName
);
3145 if (lpService
== NULL
)
3147 DPRINT("Could not find a service!\n");
3149 /* If the service could not be found and lpcchBuffer is less than 2, windows
3150 puts null in lpDisplayName and puts 2 in lpcchBuffer */
3151 if (*lpcchBuffer
< 2)
3154 if (lpDisplayName
!= NULL
)
3156 *lpDisplayName
= '\0';
3160 return ERROR_SERVICE_DOES_NOT_EXIST
;
3163 if (!lpService
->lpDisplayName
)
3165 dwLength
= wcslen(lpService
->lpServiceName
);
3167 if (lpDisplayName
!= NULL
&&
3168 *lpcchBuffer
> dwLength
)
3170 wcscpy(lpDisplayName
, lpService
->lpServiceName
);
3175 dwLength
= wcslen(lpService
->lpDisplayName
);
3177 if (lpDisplayName
!= NULL
&&
3178 *lpcchBuffer
> dwLength
)
3180 wcscpy(lpDisplayName
, lpService
->lpDisplayName
);
3184 dwError
= (*lpcchBuffer
> dwLength
) ? ERROR_SUCCESS
: ERROR_INSUFFICIENT_BUFFER
;
3186 *lpcchBuffer
= dwLength
;
3193 DWORD
RGetServiceKeyNameW(
3194 SC_RPC_HANDLE hSCManager
,
3195 LPCWSTR lpDisplayName
,
3196 LPWSTR lpServiceName
,
3199 // PMANAGER_HANDLE hManager;
3204 DPRINT("RGetServiceKeyNameW() called\n");
3205 DPRINT("hSCManager = %p\n", hSCManager
);
3206 DPRINT("lpDisplayName: %S\n", lpDisplayName
);
3207 DPRINT("lpServiceName: %p\n", lpServiceName
);
3208 DPRINT("*lpcchBuffer: %lu\n", *lpcchBuffer
);
3210 // hManager = (PMANAGER_HANDLE)hSCManager;
3211 // if (hManager->Handle.Tag != MANAGER_TAG)
3213 // DPRINT("Invalid manager handle!\n");
3214 // return ERROR_INVALID_HANDLE;
3217 /* Get service database entry */
3218 lpService
= ScmGetServiceEntryByDisplayName(lpDisplayName
);
3219 if (lpService
== NULL
)
3221 DPRINT("Could not find a service!\n");
3223 /* If the service could not be found and lpcchBuffer is less than 2, windows
3224 puts null in lpDisplayName and puts 2 in lpcchBuffer */
3225 if (*lpcchBuffer
< 2)
3228 if (lpServiceName
!= NULL
)
3230 *lpServiceName
= '\0';
3234 return ERROR_SERVICE_DOES_NOT_EXIST
;
3237 dwLength
= wcslen(lpService
->lpServiceName
);
3239 if (lpServiceName
!= NULL
&&
3240 *lpcchBuffer
> dwLength
)
3242 wcscpy(lpServiceName
, lpService
->lpServiceName
);
3243 *lpcchBuffer
= dwLength
;
3244 return ERROR_SUCCESS
;
3247 dwError
= (*lpcchBuffer
> dwLength
) ? ERROR_SUCCESS
: ERROR_INSUFFICIENT_BUFFER
;
3249 *lpcchBuffer
= dwLength
;
3256 DWORD
RI_ScSetServiceBitsA(
3257 RPC_SERVICE_STATUS_HANDLE hServiceStatus
,
3258 DWORD dwServiceBits
,
3260 int bUpdateImmediately
,
3264 return ERROR_CALL_NOT_IMPLEMENTED
;
3269 DWORD
RChangeServiceConfigA(
3270 SC_RPC_HANDLE hService
,
3271 DWORD dwServiceType
,
3273 DWORD dwErrorControl
,
3274 LPSTR lpBinaryPathName
,
3275 LPSTR lpLoadOrderGroup
,
3277 LPSTR lpDependencies
,
3279 LPSTR lpServiceStartName
,
3282 LPSTR lpDisplayName
)
3284 DWORD dwError
= ERROR_SUCCESS
;
3285 PSERVICE_HANDLE hSvc
;
3286 PSERVICE lpService
= NULL
;
3287 HKEY hServiceKey
= NULL
;
3288 LPWSTR lpDisplayNameW
= NULL
;
3289 LPWSTR lpBinaryPathNameW
= NULL
;
3290 LPWSTR lpCanonicalImagePathW
= NULL
;
3291 LPWSTR lpLoadOrderGroupW
= NULL
;
3292 LPWSTR lpDependenciesW
= NULL
;
3293 // LPWSTR lpPasswordW = NULL;
3295 DPRINT("RChangeServiceConfigA() called\n");
3296 DPRINT("dwServiceType = %lu\n", dwServiceType
);
3297 DPRINT("dwStartType = %lu\n", dwStartType
);
3298 DPRINT("dwErrorControl = %lu\n", dwErrorControl
);
3299 DPRINT("lpBinaryPathName = %s\n", lpBinaryPathName
);
3300 DPRINT("lpLoadOrderGroup = %s\n", lpLoadOrderGroup
);
3301 DPRINT("lpDisplayName = %s\n", lpDisplayName
);
3304 return ERROR_SHUTDOWN_IN_PROGRESS
;
3306 hSvc
= ScmGetServiceFromHandle(hService
);
3309 DPRINT1("Invalid service handle!\n");
3310 return ERROR_INVALID_HANDLE
;
3313 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
3314 SERVICE_CHANGE_CONFIG
))
3316 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
3317 return ERROR_ACCESS_DENIED
;
3320 lpService
= hSvc
->ServiceEntry
;
3321 if (lpService
== NULL
)
3323 DPRINT("lpService == NULL!\n");
3324 return ERROR_INVALID_HANDLE
;
3327 /* Lock the service database exclusively */
3328 ScmLockDatabaseExclusive();
3330 if (lpService
->bDeleted
)
3332 DPRINT("The service has already been marked for delete!\n");
3333 dwError
= ERROR_SERVICE_MARKED_FOR_DELETE
;
3337 /* Open the service key */
3338 dwError
= ScmOpenServiceKey(lpService
->szServiceName
,
3341 if (dwError
!= ERROR_SUCCESS
)
3344 /* Write service data to the registry */
3346 if (lpDisplayName
!= NULL
&& *lpDisplayName
!= 0)
3348 /* Set the display name */
3349 lpDisplayNameW
= HeapAlloc(GetProcessHeap(),
3351 (strlen(lpDisplayName
) + 1) * sizeof(WCHAR
));
3352 if (lpDisplayNameW
== NULL
)
3354 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
3358 MultiByteToWideChar(CP_ACP
,
3363 strlen(lpDisplayName
) + 1);
3365 RegSetValueExW(hServiceKey
,
3369 (LPBYTE
)lpDisplayNameW
,
3370 (wcslen(lpDisplayNameW
) + 1) * sizeof(WCHAR
));
3372 /* Update lpService->lpDisplayName */
3373 if (lpService
->lpDisplayName
)
3374 HeapFree(GetProcessHeap(), 0, lpService
->lpDisplayName
);
3376 lpService
->lpDisplayName
= lpDisplayNameW
;
3379 if (dwServiceType
!= SERVICE_NO_CHANGE
)
3381 /* Set the service type */
3382 dwError
= RegSetValueExW(hServiceKey
,
3386 (LPBYTE
)&dwServiceType
,
3388 if (dwError
!= ERROR_SUCCESS
)
3391 lpService
->Status
.dwServiceType
= dwServiceType
;
3394 if (dwStartType
!= SERVICE_NO_CHANGE
)
3396 /* Set the start value */
3397 dwError
= RegSetValueExW(hServiceKey
,
3401 (LPBYTE
)&dwStartType
,
3403 if (dwError
!= ERROR_SUCCESS
)
3406 lpService
->dwStartType
= dwStartType
;
3409 if (dwErrorControl
!= SERVICE_NO_CHANGE
)
3411 /* Set the error control value */
3412 dwError
= RegSetValueExW(hServiceKey
,
3416 (LPBYTE
)&dwErrorControl
,
3418 if (dwError
!= ERROR_SUCCESS
)
3421 lpService
->dwErrorControl
= dwErrorControl
;
3424 if (lpBinaryPathName
!= NULL
&& *lpBinaryPathName
!= 0)
3426 /* Set the image path */
3427 lpBinaryPathNameW
= HeapAlloc(GetProcessHeap(),
3429 (strlen(lpBinaryPathName
) + 1) * sizeof(WCHAR
));
3430 if (lpBinaryPathNameW
== NULL
)
3432 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
3436 MultiByteToWideChar(CP_ACP
,
3441 strlen(lpBinaryPathName
) + 1);
3443 if (lpService
->Status
.dwServiceType
& SERVICE_DRIVER
)
3445 dwError
= ScmCanonDriverImagePath(lpService
->dwStartType
,
3447 &lpCanonicalImagePathW
);
3449 HeapFree(GetProcessHeap(), 0, lpBinaryPathNameW
);
3451 if (dwError
!= ERROR_SUCCESS
)
3454 lpBinaryPathNameW
= lpCanonicalImagePathW
;
3457 dwError
= RegSetValueExW(hServiceKey
,
3461 (LPBYTE
)lpBinaryPathNameW
,
3462 (wcslen(lpBinaryPathNameW
) + 1) * sizeof(WCHAR
));
3464 HeapFree(GetProcessHeap(), 0, lpBinaryPathNameW
);
3466 if (dwError
!= ERROR_SUCCESS
)
3470 /* Set the group name */
3471 if (lpLoadOrderGroup
!= NULL
&& *lpLoadOrderGroup
!= 0)
3473 lpLoadOrderGroupW
= HeapAlloc(GetProcessHeap(),
3475 (strlen(lpLoadOrderGroup
) + 1) * sizeof(WCHAR
));
3476 if (lpLoadOrderGroupW
== NULL
)
3478 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
3482 MultiByteToWideChar(CP_ACP
,
3487 strlen(lpLoadOrderGroup
) + 1);
3489 dwError
= RegSetValueExW(hServiceKey
,
3493 (LPBYTE
)lpLoadOrderGroupW
,
3494 (wcslen(lpLoadOrderGroupW
) + 1) * sizeof(WCHAR
));
3495 if (dwError
!= ERROR_SUCCESS
)
3497 HeapFree(GetProcessHeap(), 0, lpLoadOrderGroupW
);
3501 dwError
= ScmSetServiceGroup(lpService
,
3504 HeapFree(GetProcessHeap(), 0, lpLoadOrderGroupW
);
3506 if (dwError
!= ERROR_SUCCESS
)
3510 if (lpdwTagId
!= NULL
)
3512 dwError
= ScmAssignNewTag(lpService
);
3513 if (dwError
!= ERROR_SUCCESS
)
3516 dwError
= RegSetValueExW(hServiceKey
,
3520 (LPBYTE
)&lpService
->dwTag
,
3522 if (dwError
!= ERROR_SUCCESS
)
3525 *lpdwTagId
= lpService
->dwTag
;
3528 /* Write dependencies */
3529 if (lpDependencies
!= NULL
&& *lpDependencies
!= 0)
3531 lpDependenciesW
= HeapAlloc(GetProcessHeap(),
3533 (strlen(lpDependencies
) + 1) * sizeof(WCHAR
));
3534 if (lpDependenciesW
== NULL
)
3536 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
3540 MultiByteToWideChar(CP_ACP
,
3545 strlen(lpDependencies
) + 1);
3547 dwError
= ScmWriteDependencies(hServiceKey
,
3548 (LPWSTR
)lpDependenciesW
,
3551 HeapFree(GetProcessHeap(), 0, lpDependenciesW
);
3554 if (lpPassword
!= NULL
)
3556 /* FIXME: Write password */
3560 /* Unlock the service database */
3561 ScmUnlockDatabase();
3563 if (hServiceKey
!= NULL
)
3564 RegCloseKey(hServiceKey
);
3566 DPRINT("RChangeServiceConfigA() done (Error %lu)\n", dwError
);
3573 DWORD
RCreateServiceA(
3574 SC_RPC_HANDLE hSCManager
,
3575 LPSTR lpServiceName
,
3576 LPSTR lpDisplayName
,
3577 DWORD dwDesiredAccess
,
3578 DWORD dwServiceType
,
3580 DWORD dwErrorControl
,
3581 LPSTR lpBinaryPathName
,
3582 LPSTR lpLoadOrderGroup
,
3584 LPBYTE lpDependencies
,
3586 LPSTR lpServiceStartName
,
3589 LPSC_RPC_HANDLE lpServiceHandle
)
3591 DWORD dwError
= ERROR_SUCCESS
;
3592 LPWSTR lpServiceNameW
= NULL
;
3593 LPWSTR lpDisplayNameW
= NULL
;
3594 LPWSTR lpBinaryPathNameW
= NULL
;
3595 LPWSTR lpLoadOrderGroupW
= NULL
;
3596 LPWSTR lpDependenciesW
= NULL
;
3597 LPWSTR lpServiceStartNameW
= NULL
;
3598 DWORD dwDependenciesLength
= 0;
3605 len
= MultiByteToWideChar(CP_ACP
, 0, lpServiceName
, -1, NULL
, 0);
3606 lpServiceNameW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
3607 if (!lpServiceNameW
)
3609 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3612 MultiByteToWideChar(CP_ACP
, 0, lpServiceName
, -1, lpServiceNameW
, len
);
3617 len
= MultiByteToWideChar(CP_ACP
, 0, lpDisplayName
, -1, NULL
, 0);
3618 lpDisplayNameW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
3619 if (!lpDisplayNameW
)
3621 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3624 MultiByteToWideChar(CP_ACP
, 0, lpDisplayName
, -1, lpDisplayNameW
, len
);
3627 if (lpBinaryPathName
)
3629 len
= MultiByteToWideChar(CP_ACP
, 0, lpBinaryPathName
, -1, NULL
, 0);
3630 lpBinaryPathNameW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
3631 if (!lpBinaryPathNameW
)
3633 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3636 MultiByteToWideChar(CP_ACP
, 0, lpBinaryPathName
, -1, lpBinaryPathNameW
, len
);
3639 if (lpLoadOrderGroup
)
3641 len
= MultiByteToWideChar(CP_ACP
, 0, lpLoadOrderGroup
, -1, NULL
, 0);
3642 lpLoadOrderGroupW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
3643 if (!lpLoadOrderGroupW
)
3645 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3648 MultiByteToWideChar(CP_ACP
, 0, lpLoadOrderGroup
, -1, lpLoadOrderGroupW
, len
);
3653 lpStr
= (LPCSTR
)lpDependencies
;
3656 dwLength
= strlen(lpStr
) + 1;
3657 dwDependenciesLength
+= dwLength
;
3658 lpStr
= lpStr
+ dwLength
;
3660 dwDependenciesLength
++;
3662 lpDependenciesW
= HeapAlloc(GetProcessHeap(), 0, dwDependenciesLength
* sizeof(WCHAR
));
3663 if (!lpDependenciesW
)
3665 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3668 MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)lpDependencies
, dwDependenciesLength
, lpDependenciesW
, dwDependenciesLength
);
3671 if (lpServiceStartName
)
3673 len
= MultiByteToWideChar(CP_ACP
, 0, lpServiceStartName
, -1, NULL
, 0);
3674 lpServiceStartNameW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
3675 if (!lpServiceStartNameW
)
3677 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3680 MultiByteToWideChar(CP_ACP
, 0, lpServiceStartName
, -1, lpServiceStartNameW
, len
);
3683 dwError
= RCreateServiceW(hSCManager
,
3693 (LPBYTE
)lpDependenciesW
,
3694 dwDependenciesLength
,
3695 lpServiceStartNameW
,
3701 if (lpServiceNameW
!=NULL
)
3702 HeapFree(GetProcessHeap(), 0, lpServiceNameW
);
3704 if (lpDisplayNameW
!= NULL
)
3705 HeapFree(GetProcessHeap(), 0, lpDisplayNameW
);
3707 if (lpBinaryPathNameW
!= NULL
)
3708 HeapFree(GetProcessHeap(), 0, lpBinaryPathNameW
);
3710 if (lpLoadOrderGroupW
!= NULL
)
3711 HeapFree(GetProcessHeap(), 0, lpLoadOrderGroupW
);
3713 if (lpDependenciesW
!= NULL
)
3714 HeapFree(GetProcessHeap(), 0, lpDependenciesW
);
3716 if (lpServiceStartNameW
!= NULL
)
3717 HeapFree(GetProcessHeap(), 0, lpServiceStartNameW
);
3724 DWORD
REnumDependentServicesA(
3725 SC_RPC_HANDLE hService
,
3726 DWORD dwServiceState
,
3729 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
3730 LPBOUNDED_DWORD_256K lpServicesReturned
)
3732 DWORD dwError
= ERROR_SUCCESS
;
3733 DWORD dwServicesReturned
= 0;
3734 DWORD dwServiceCount
;
3735 HKEY hServicesKey
= NULL
;
3736 PSERVICE_HANDLE hSvc
;
3737 PSERVICE lpService
= NULL
;
3738 PSERVICE
*lpServicesArray
= NULL
;
3739 LPENUM_SERVICE_STATUSA lpServicesPtr
= NULL
;
3742 *pcbBytesNeeded
= 0;
3743 *lpServicesReturned
= 0;
3745 DPRINT("REnumDependentServicesA() called\n");
3747 hSvc
= ScmGetServiceFromHandle(hService
);
3750 DPRINT1("Invalid service handle!\n");
3751 return ERROR_INVALID_HANDLE
;
3754 lpService
= hSvc
->ServiceEntry
;
3756 /* Check access rights */
3757 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
3758 SC_MANAGER_ENUMERATE_SERVICE
))
3760 DPRINT("Insufficient access rights! 0x%lx\n",
3761 hSvc
->Handle
.DesiredAccess
);
3762 return ERROR_ACCESS_DENIED
;
3765 /* Open the Services Reg key */
3766 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
3767 L
"System\\CurrentControlSet\\Services",
3772 if (dwError
!= ERROR_SUCCESS
)
3775 /* NOTE: Windows calculates the pcbBytesNeeded based on WCHAR strings for
3776 both EnumDependentServicesA and EnumDependentServicesW. So returned pcbBytesNeeded
3777 are the same for both. Verified in WINXP. */
3779 /* First determine the bytes needed and get the number of dependent services*/
3780 dwError
= Int_EnumDependentServicesW(hServicesKey
,
3785 &dwServicesReturned
);
3786 if (dwError
!= ERROR_SUCCESS
)
3789 /* If buffer size is less than the bytes needed or pointer is null*/
3790 if ((!lpServices
) || (cbBufSize
< *pcbBytesNeeded
))
3792 dwError
= ERROR_MORE_DATA
;
3796 /* Allocate memory for array of service pointers */
3797 lpServicesArray
= HeapAlloc(GetProcessHeap(),
3799 (dwServicesReturned
+ 1) * sizeof(PSERVICE
));
3800 if (!lpServicesArray
)
3802 DPRINT("Could not allocate a buffer!!\n");
3803 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
3807 dwServicesReturned
= 0;
3808 *pcbBytesNeeded
= 0;
3810 dwError
= Int_EnumDependentServicesW(hServicesKey
,
3815 &dwServicesReturned
);
3816 if (dwError
!= ERROR_SUCCESS
)
3821 lpServicesPtr
= (LPENUM_SERVICE_STATUSA
)lpServices
;
3822 lpStr
= (LPSTR
)(lpServices
+ (dwServicesReturned
* sizeof(ENUM_SERVICE_STATUSA
)));
3824 /* Copy EnumDepenedentService to Buffer */
3825 for (dwServiceCount
= 0; dwServiceCount
< dwServicesReturned
; dwServiceCount
++)
3827 lpService
= lpServicesArray
[dwServiceCount
];
3829 /* Copy the status info */
3830 memcpy(&lpServicesPtr
->ServiceStatus
,
3832 sizeof(SERVICE_STATUS
));
3834 /* Copy display name */
3835 WideCharToMultiByte(CP_ACP
,
3837 lpService
->lpDisplayName
,
3840 wcslen(lpService
->lpDisplayName
),
3843 lpServicesPtr
->lpDisplayName
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServices
);
3844 lpStr
+= strlen(lpStr
) + 1;
3846 /* Copy service name */
3847 WideCharToMultiByte(CP_ACP
,
3849 lpService
->lpServiceName
,
3852 wcslen(lpService
->lpServiceName
),
3855 lpServicesPtr
->lpServiceName
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServices
);
3856 lpStr
+= strlen(lpStr
) + 1;
3861 *lpServicesReturned
= dwServicesReturned
;
3864 if (lpServicesArray
)
3865 HeapFree(GetProcessHeap(), 0, lpServicesArray
);
3867 RegCloseKey(hServicesKey
);
3869 DPRINT("REnumDependentServicesA() done (Error %lu)\n", dwError
);
3876 DWORD
REnumServicesStatusA(
3877 SC_RPC_HANDLE hSCManager
,
3878 DWORD dwServiceType
,
3879 DWORD dwServiceState
,
3882 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
3883 LPBOUNDED_DWORD_256K lpServicesReturned
,
3884 LPBOUNDED_DWORD_256K lpResumeHandle
)
3886 LPENUM_SERVICE_STATUSW lpStatusPtrW
= NULL
;
3887 LPENUM_SERVICE_STATUSA lpStatusPtrA
= NULL
;
3888 LPWSTR lpStringPtrW
;
3891 DWORD dwServiceCount
;
3893 DPRINT("REnumServicesStatusA() called\n");
3895 if ((dwBufSize
> 0) && (lpBuffer
))
3897 lpStatusPtrW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, dwBufSize
);
3900 DPRINT("Failed to allocate buffer!\n");
3901 return ERROR_NOT_ENOUGH_MEMORY
;
3905 dwError
= REnumServicesStatusW(hSCManager
,
3908 (LPBYTE
)lpStatusPtrW
,
3914 /* if no services were returned then we are Done */
3915 if (*lpServicesReturned
== 0)
3918 lpStatusPtrA
= (LPENUM_SERVICE_STATUSA
)lpBuffer
;
3919 lpStringPtrA
= (LPSTR
)((ULONG_PTR
)lpBuffer
+
3920 *lpServicesReturned
* sizeof(ENUM_SERVICE_STATUSA
));
3921 lpStringPtrW
= (LPWSTR
)((ULONG_PTR
)lpStatusPtrW
+
3922 *lpServicesReturned
* sizeof(ENUM_SERVICE_STATUSW
));
3924 for (dwServiceCount
= 0; dwServiceCount
< *lpServicesReturned
; dwServiceCount
++)
3926 /* Copy the service name */
3927 WideCharToMultiByte(CP_ACP
,
3932 wcslen(lpStringPtrW
),
3936 lpStatusPtrA
->lpServiceName
= (LPSTR
)((ULONG_PTR
)lpStringPtrA
- (ULONG_PTR
)lpBuffer
);
3937 lpStringPtrA
+= wcslen(lpStringPtrW
) + 1;
3938 lpStringPtrW
+= wcslen(lpStringPtrW
) + 1;
3940 /* Copy the display name */
3941 WideCharToMultiByte(CP_ACP
,
3946 wcslen(lpStringPtrW
),
3950 lpStatusPtrA
->lpDisplayName
= (LPSTR
)((ULONG_PTR
)lpStringPtrA
- (ULONG_PTR
)lpBuffer
);
3951 lpStringPtrA
+= wcslen(lpStringPtrW
) + 1;
3952 lpStringPtrW
+= wcslen(lpStringPtrW
) + 1;
3954 /* Copy the status information */
3955 memcpy(&lpStatusPtrA
->ServiceStatus
,
3956 &lpStatusPtrW
->ServiceStatus
,
3957 sizeof(SERVICE_STATUS
));
3964 HeapFree(GetProcessHeap(), 0, lpStatusPtrW
);
3966 DPRINT("REnumServicesStatusA() done (Error %lu)\n", dwError
);
3973 DWORD
ROpenSCManagerA(
3974 LPSTR lpMachineName
,
3975 LPSTR lpDatabaseName
,
3976 DWORD dwDesiredAccess
,
3977 LPSC_RPC_HANDLE lpScHandle
)
3979 UNICODE_STRING MachineName
;
3980 UNICODE_STRING DatabaseName
;
3983 DPRINT("ROpenSCManagerA() called\n");
3986 RtlCreateUnicodeStringFromAsciiz(&MachineName
,
3990 RtlCreateUnicodeStringFromAsciiz(&DatabaseName
,
3993 dwError
= ROpenSCManagerW(lpMachineName
? MachineName
.Buffer
: NULL
,
3994 lpDatabaseName
? DatabaseName
.Buffer
: NULL
,
3999 RtlFreeUnicodeString(&MachineName
);
4002 RtlFreeUnicodeString(&DatabaseName
);
4009 DWORD
ROpenServiceA(
4010 SC_RPC_HANDLE hSCManager
,
4011 LPSTR lpServiceName
,
4012 DWORD dwDesiredAccess
,
4013 LPSC_RPC_HANDLE lpServiceHandle
)
4015 UNICODE_STRING ServiceName
;
4018 DPRINT("ROpenServiceA() called\n");
4021 RtlCreateUnicodeStringFromAsciiz(&ServiceName
,
4024 dwError
= ROpenServiceW(hSCManager
,
4025 lpServiceName
? ServiceName
.Buffer
: NULL
,
4030 RtlFreeUnicodeString(&ServiceName
);
4037 DWORD
RQueryServiceConfigA(
4038 SC_RPC_HANDLE hService
,
4039 LPBYTE lpBuf
, //LPQUERY_SERVICE_CONFIGA lpServiceConfig,
4041 LPBOUNDED_DWORD_8K pcbBytesNeeded
)
4043 LPQUERY_SERVICE_CONFIGA lpServiceConfig
= (LPQUERY_SERVICE_CONFIGA
)lpBuf
;
4044 DWORD dwError
= ERROR_SUCCESS
;
4045 PSERVICE_HANDLE hSvc
;
4046 PSERVICE lpService
= NULL
;
4047 HKEY hServiceKey
= NULL
;
4048 LPWSTR lpImagePath
= NULL
;
4049 LPWSTR lpServiceStartName
= NULL
;
4050 LPWSTR lpDependencies
= NULL
;
4051 DWORD dwDependenciesLength
= 0;
4052 DWORD dwRequiredSize
;
4053 LPQUERY_SERVICE_CONFIGA lpConfig
= NULL
;
4054 CHAR lpEmptyString
[]={0,0};
4057 DPRINT("RQueryServiceConfigA() called\n");
4060 return ERROR_SHUTDOWN_IN_PROGRESS
;
4062 hSvc
= ScmGetServiceFromHandle(hService
);
4065 DPRINT1("Invalid service handle!\n");
4066 return ERROR_INVALID_HANDLE
;
4069 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
4070 SERVICE_QUERY_CONFIG
))
4072 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
4073 return ERROR_ACCESS_DENIED
;
4076 lpService
= hSvc
->ServiceEntry
;
4077 if (lpService
== NULL
)
4079 DPRINT("lpService == NULL!\n");
4080 return ERROR_INVALID_HANDLE
;
4083 /* Lock the service database shared */
4084 ScmLockDatabaseShared();
4086 dwError
= ScmOpenServiceKey(lpService
->lpServiceName
,
4089 if (dwError
!= ERROR_SUCCESS
)
4092 /* Read the image path */
4093 dwError
= ScmReadString(hServiceKey
,
4096 if (dwError
!= ERROR_SUCCESS
)
4099 /* Read the service start name */
4100 ScmReadString(hServiceKey
,
4102 &lpServiceStartName
);
4104 /* Read the dependencies */
4105 ScmReadDependencies(hServiceKey
,
4107 &dwDependenciesLength
);
4109 dwRequiredSize
= sizeof(QUERY_SERVICE_CONFIGA
);
4111 if (lpImagePath
!= NULL
)
4112 dwRequiredSize
+= wcslen(lpImagePath
) + 1;
4114 dwRequiredSize
+= 2;
4116 if ((lpService
->lpGroup
!= NULL
) && (lpService
->lpGroup
->lpGroupName
!= NULL
))
4117 dwRequiredSize
+= wcslen(lpService
->lpGroup
->lpGroupName
) + 1;
4119 dwRequiredSize
+= 2;
4121 /* Add Dependencies length */
4122 if (lpDependencies
!= NULL
)
4123 dwRequiredSize
+= dwDependenciesLength
;
4125 dwRequiredSize
+= 2;
4127 if (lpServiceStartName
!= NULL
)
4128 dwRequiredSize
+= wcslen(lpServiceStartName
) + 1;
4130 dwRequiredSize
+= 2;
4132 if (lpService
->lpDisplayName
!= NULL
)
4133 dwRequiredSize
+= wcslen(lpService
->lpDisplayName
) + 1;
4135 dwRequiredSize
+= 2;
4137 if (lpServiceConfig
== NULL
|| cbBufSize
< dwRequiredSize
)
4139 dwError
= ERROR_INSUFFICIENT_BUFFER
;
4143 lpConfig
= (LPQUERY_SERVICE_CONFIGA
)lpServiceConfig
;
4144 lpConfig
->dwServiceType
= lpService
->Status
.dwServiceType
;
4145 lpConfig
->dwStartType
= lpService
->dwStartType
;
4146 lpConfig
->dwErrorControl
= lpService
->dwErrorControl
;
4147 lpConfig
->dwTagId
= lpService
->dwTag
;
4149 lpStr
= (LPSTR
)(lpServiceConfig
+ 1);
4151 /* NOTE: Strings that are NULL for QUERY_SERVICE_CONFIG are pointers to empty strings.
4152 Verified in WINXP */
4156 WideCharToMultiByte(CP_ACP
,
4161 wcslen(lpImagePath
) + 1,
4167 strcpy(lpStr
, lpEmptyString
);
4170 lpConfig
->lpBinaryPathName
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpConfig
);
4171 lpStr
+= (strlen((LPSTR
)lpStr
) + 1);
4173 if (lpService
->lpGroup
&& lpService
->lpGroup
->lpGroupName
)
4175 WideCharToMultiByte(CP_ACP
,
4177 lpService
->lpGroup
->lpGroupName
,
4180 wcslen(lpService
->lpGroup
->lpGroupName
) + 1,
4186 strcpy(lpStr
, lpEmptyString
);
4189 lpConfig
->lpLoadOrderGroup
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpConfig
);
4190 lpStr
+= (strlen(lpStr
) + 1);
4192 /* Append Dependencies */
4195 WideCharToMultiByte(CP_ACP
,
4198 dwDependenciesLength
,
4200 dwDependenciesLength
,
4206 strcpy(lpStr
, lpEmptyString
);
4209 lpConfig
->lpDependencies
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpConfig
);
4211 lpStr
+= dwDependenciesLength
;
4213 lpStr
+= (strlen(lpStr
) + 1);
4215 if (lpServiceStartName
)
4217 WideCharToMultiByte(CP_ACP
,
4222 wcslen(lpServiceStartName
) + 1,
4228 strcpy(lpStr
, lpEmptyString
);
4231 lpConfig
->lpServiceStartName
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpConfig
);
4232 lpStr
+= (strlen(lpStr
) + 1);
4234 if (lpService
->lpDisplayName
)
4236 WideCharToMultiByte(CP_ACP
,
4238 lpService
->lpDisplayName
,
4241 wcslen(lpService
->lpDisplayName
) + 1,
4247 strcpy(lpStr
, lpEmptyString
);
4250 lpConfig
->lpDisplayName
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpConfig
);
4253 if (pcbBytesNeeded
!= NULL
)
4254 *pcbBytesNeeded
= dwRequiredSize
;
4257 /* Unlock the service database */
4258 ScmUnlockDatabase();
4260 if (lpImagePath
!= NULL
)
4261 HeapFree(GetProcessHeap(), 0, lpImagePath
);
4263 if (lpServiceStartName
!= NULL
)
4264 HeapFree(GetProcessHeap(), 0, lpServiceStartName
);
4266 if (lpDependencies
!= NULL
)
4267 HeapFree(GetProcessHeap(), 0, lpDependencies
);
4269 if (hServiceKey
!= NULL
)
4270 RegCloseKey(hServiceKey
);
4272 DPRINT("RQueryServiceConfigA() done\n");
4279 DWORD
RQueryServiceLockStatusA(
4280 SC_RPC_HANDLE hSCManager
,
4281 LPQUERY_SERVICE_LOCK_STATUSA lpLockStatus
,
4283 LPBOUNDED_DWORD_4K pcbBytesNeeded
)
4286 return ERROR_CALL_NOT_IMPLEMENTED
;
4291 DWORD
RStartServiceA(
4292 SC_RPC_HANDLE hService
,
4294 LPSTRING_PTRSA argv
)
4296 DWORD dwError
= ERROR_SUCCESS
;
4297 PSERVICE_HANDLE hSvc
;
4298 PSERVICE lpService
= NULL
;
4299 LPWSTR
*lpVector
= NULL
;
4303 DPRINT("RStartServiceA() called\n");
4306 return ERROR_SHUTDOWN_IN_PROGRESS
;
4308 hSvc
= ScmGetServiceFromHandle(hService
);
4311 DPRINT1("Invalid service handle!\n");
4312 return ERROR_INVALID_HANDLE
;
4315 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
4318 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
4319 return ERROR_ACCESS_DENIED
;
4322 lpService
= hSvc
->ServiceEntry
;
4323 if (lpService
== NULL
)
4325 DPRINT("lpService == NULL!\n");
4326 return ERROR_INVALID_HANDLE
;
4329 if (lpService
->dwStartType
== SERVICE_DISABLED
)
4330 return ERROR_SERVICE_DISABLED
;
4332 if (lpService
->bDeleted
)
4333 return ERROR_SERVICE_MARKED_FOR_DELETE
;
4335 /* Build a Unicode argument vector */
4338 lpVector
= HeapAlloc(GetProcessHeap(),
4340 argc
* sizeof(LPWSTR
));
4341 if (lpVector
== NULL
)
4342 return ERROR_NOT_ENOUGH_MEMORY
;
4344 for (i
= 0; i
< argc
; i
++)
4346 dwLength
= MultiByteToWideChar(CP_ACP
,
4353 lpVector
[i
] = HeapAlloc(GetProcessHeap(),
4355 dwLength
* sizeof(WCHAR
));
4356 if (lpVector
[i
] == NULL
)
4358 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
4362 MultiByteToWideChar(CP_ACP
,
4371 /* Start the service */
4372 dwError
= ScmStartService(lpService
, argc
, lpVector
);
4375 /* Free the Unicode argument vector */
4376 if (lpVector
!= NULL
)
4378 for (i
= 0; i
< argc
; i
++)
4380 if (lpVector
[i
] != NULL
)
4381 HeapFree(GetProcessHeap(), 0, lpVector
[i
]);
4383 HeapFree(GetProcessHeap(), 0, lpVector
);
4391 DWORD
RGetServiceDisplayNameA(
4392 SC_RPC_HANDLE hSCManager
,
4393 LPCSTR lpServiceName
,
4394 LPSTR lpDisplayName
,
4395 LPBOUNDED_DWORD_4K lpcchBuffer
)
4397 // PMANAGER_HANDLE hManager;
4398 PSERVICE lpService
= NULL
;
4401 LPWSTR lpServiceNameW
;
4403 DPRINT("RGetServiceDisplayNameA() called\n");
4404 DPRINT("hSCManager = %p\n", hSCManager
);
4405 DPRINT("lpServiceName: %s\n", lpServiceName
);
4406 DPRINT("lpDisplayName: %p\n", lpDisplayName
);
4407 DPRINT("*lpcchBuffer: %lu\n", *lpcchBuffer
);
4409 // hManager = (PMANAGER_HANDLE)hSCManager;
4410 // if (hManager->Handle.Tag != MANAGER_TAG)
4412 // DPRINT("Invalid manager handle!\n");
4413 // return ERROR_INVALID_HANDLE;
4416 if (lpServiceName
!= NULL
)
4418 dwLength
= strlen(lpServiceName
) + 1;
4419 lpServiceNameW
= HeapAlloc(GetProcessHeap(),
4421 dwLength
* sizeof(WCHAR
));
4422 if (!lpServiceNameW
)
4423 return ERROR_NOT_ENOUGH_MEMORY
;
4425 MultiByteToWideChar(CP_ACP
,
4432 lpService
= ScmGetServiceEntryByName(lpServiceNameW
);
4434 HeapFree(GetProcessHeap(), 0, lpServiceNameW
);
4437 if (lpService
== NULL
)
4439 DPRINT("Could not find a service!\n");
4441 /* If the service could not be found and lpcchBuffer is 0, windows
4442 puts null in lpDisplayName and puts 1 in lpcchBuffer */
4443 if (*lpcchBuffer
== 0)
4446 if (lpDisplayName
!= NULL
)
4448 *lpDisplayName
= '\0';
4451 return ERROR_SERVICE_DOES_NOT_EXIST
;
4454 if (!lpService
->lpDisplayName
)
4456 dwLength
= wcslen(lpService
->lpServiceName
);
4457 if (lpDisplayName
!= NULL
&&
4458 *lpcchBuffer
> dwLength
)
4460 WideCharToMultiByte(CP_ACP
,
4462 lpService
->lpServiceName
,
4463 wcslen(lpService
->lpServiceName
),
4468 return ERROR_SUCCESS
;
4473 dwLength
= wcslen(lpService
->lpDisplayName
);
4474 if (lpDisplayName
!= NULL
&&
4475 *lpcchBuffer
> dwLength
)
4477 WideCharToMultiByte(CP_ACP
,
4479 lpService
->lpDisplayName
,
4480 wcslen(lpService
->lpDisplayName
),
4485 return ERROR_SUCCESS
;
4489 dwError
= (*lpcchBuffer
> dwLength
) ? ERROR_SUCCESS
: ERROR_INSUFFICIENT_BUFFER
;
4491 *lpcchBuffer
= dwLength
* 2;
4498 DWORD
RGetServiceKeyNameA(
4499 SC_RPC_HANDLE hSCManager
,
4500 LPCSTR lpDisplayName
,
4501 LPSTR lpServiceName
,
4502 LPBOUNDED_DWORD_4K lpcchBuffer
)
4507 LPWSTR lpDisplayNameW
;
4509 DPRINT("RGetServiceKeyNameA() called\n");
4510 DPRINT("hSCManager = %p\n", hSCManager
);
4511 DPRINT("lpDisplayName: %s\n", lpDisplayName
);
4512 DPRINT("lpServiceName: %p\n", lpServiceName
);
4513 DPRINT("*lpcchBuffer: %lu\n", *lpcchBuffer
);
4515 dwLength
= strlen(lpDisplayName
) + 1;
4516 lpDisplayNameW
= HeapAlloc(GetProcessHeap(),
4518 dwLength
* sizeof(WCHAR
));
4519 if (!lpDisplayNameW
)
4520 return ERROR_NOT_ENOUGH_MEMORY
;
4522 MultiByteToWideChar(CP_ACP
,
4529 lpService
= ScmGetServiceEntryByDisplayName(lpDisplayNameW
);
4531 HeapFree(GetProcessHeap(), 0, lpDisplayNameW
);
4533 if (lpService
== NULL
)
4535 DPRINT("Could not find the service!\n");
4537 /* If the service could not be found and lpcchBuffer is 0,
4538 put null in lpDisplayName and puts 1 in lpcchBuffer, verified WINXP. */
4539 if (*lpcchBuffer
== 0)
4542 if (lpServiceName
!= NULL
)
4544 *lpServiceName
= '\0';
4548 return ERROR_SERVICE_DOES_NOT_EXIST
;
4551 dwLength
= wcslen(lpService
->lpServiceName
);
4552 if (lpServiceName
!= NULL
&&
4553 *lpcchBuffer
> dwLength
)
4555 WideCharToMultiByte(CP_ACP
,
4557 lpService
->lpServiceName
,
4558 wcslen(lpService
->lpServiceName
),
4563 return ERROR_SUCCESS
;
4566 dwError
= (*lpcchBuffer
> dwLength
) ? ERROR_SUCCESS
: ERROR_INSUFFICIENT_BUFFER
;
4568 *lpcchBuffer
= dwLength
* 2;
4575 DWORD
RI_ScGetCurrentGroupStateW(
4576 SC_RPC_HANDLE hSCManager
,
4577 LPWSTR lpLoadOrderGroup
,
4581 return ERROR_CALL_NOT_IMPLEMENTED
;
4586 DWORD
REnumServiceGroupW(
4587 SC_RPC_HANDLE hSCManager
,
4588 DWORD dwServiceType
,
4589 DWORD dwServiceState
,
4592 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
4593 LPBOUNDED_DWORD_256K lpServicesReturned
,
4594 LPBOUNDED_DWORD_256K lpResumeIndex
,
4595 LPCWSTR pszGroupName
)
4598 return ERROR_CALL_NOT_IMPLEMENTED
;
4603 // WARNING: This function is untested
4606 DWORD
RChangeServiceConfig2A(
4607 SC_RPC_HANDLE hService
,
4608 SC_RPC_CONFIG_INFOA Info
)
4610 SC_RPC_CONFIG_INFOW InfoW
;
4611 DWORD dwRet
, dwLength
;
4614 DPRINT("RChangeServiceConfig2A() called\n");
4615 DPRINT("dwInfoLevel = %lu\n", Info
.dwInfoLevel
);
4617 InfoW
.dwInfoLevel
= Info
.dwInfoLevel
;
4619 if (InfoW
.dwInfoLevel
== SERVICE_CONFIG_DESCRIPTION
)
4621 LPSERVICE_DESCRIPTIONW lpServiceDescriptonW
;
4622 //LPSERVICE_DESCRIPTIONA lpServiceDescriptonA;
4624 //lpServiceDescriptonA = Info.psd;
4626 ///if (lpServiceDescriptonA &&
4627 ///lpServiceDescriptonA->lpDescription)
4629 dwLength
= (strlen(Info
.lpDescription
) + 1) * sizeof(WCHAR
);
4631 lpServiceDescriptonW
= HeapAlloc(GetProcessHeap(),
4633 dwLength
+ sizeof(SERVICE_DESCRIPTIONW
));
4634 if (!lpServiceDescriptonW
)
4636 return ERROR_NOT_ENOUGH_MEMORY
;
4639 lpServiceDescriptonW
->lpDescription
= (LPWSTR
)(lpServiceDescriptonW
+ 1);
4641 MultiByteToWideChar(CP_ACP
,
4645 lpServiceDescriptonW
->lpDescription
,
4648 ptr
= lpServiceDescriptonW
;
4649 InfoW
.psd
= lpServiceDescriptonW
;
4652 else if (Info
.dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
4654 LPSERVICE_FAILURE_ACTIONSW lpServiceFailureActionsW
;
4655 LPSERVICE_FAILURE_ACTIONSA lpServiceFailureActionsA
;
4656 DWORD dwRebootLen
= 0;
4657 DWORD dwCommandLen
= 0;
4659 lpServiceFailureActionsA
= Info
.psfa
;
4661 if (lpServiceFailureActionsA
)
4663 if (lpServiceFailureActionsA
->lpRebootMsg
)
4665 dwRebootLen
= (strlen(lpServiceFailureActionsA
->lpRebootMsg
) + 1) * sizeof(WCHAR
);
4667 if (lpServiceFailureActionsA
->lpCommand
)
4669 dwCommandLen
= (strlen(lpServiceFailureActionsA
->lpCommand
) + 1) * sizeof(WCHAR
);
4671 dwLength
= dwRebootLen
+ dwCommandLen
+ sizeof(SERVICE_FAILURE_ACTIONSW
);
4673 lpServiceFailureActionsW
= HeapAlloc(GetProcessHeap(),
4676 if (!lpServiceFailureActionsW
)
4678 return ERROR_NOT_ENOUGH_MEMORY
;
4681 lpServiceFailureActionsW
->cActions
= lpServiceFailureActionsA
->cActions
;
4682 lpServiceFailureActionsW
->dwResetPeriod
= lpServiceFailureActionsA
->dwResetPeriod
;
4683 CopyMemory(lpServiceFailureActionsW
->lpsaActions
, lpServiceFailureActionsA
->lpsaActions
, sizeof(SC_ACTION
));
4685 if (lpServiceFailureActionsA
->lpRebootMsg
)
4687 MultiByteToWideChar(CP_ACP
,
4689 lpServiceFailureActionsA
->lpRebootMsg
,
4691 lpServiceFailureActionsW
->lpRebootMsg
,
4695 if (lpServiceFailureActionsA
->lpCommand
)
4697 MultiByteToWideChar(CP_ACP
,
4699 lpServiceFailureActionsA
->lpCommand
,
4701 lpServiceFailureActionsW
->lpCommand
,
4705 ptr
= lpServiceFailureActionsW
;
4709 dwRet
= RChangeServiceConfig2W(hService
, InfoW
);
4711 HeapFree(GetProcessHeap(), 0, ptr
);
4718 DWORD
RChangeServiceConfig2W(
4719 SC_RPC_HANDLE hService
,
4720 SC_RPC_CONFIG_INFOW Info
)
4722 DWORD dwError
= ERROR_SUCCESS
;
4723 PSERVICE_HANDLE hSvc
;
4724 PSERVICE lpService
= NULL
;
4725 HKEY hServiceKey
= NULL
;
4727 DPRINT("RChangeServiceConfig2W() called\n");
4728 DPRINT("dwInfoLevel = %lu\n", Info
.dwInfoLevel
);
4731 return ERROR_SHUTDOWN_IN_PROGRESS
;
4733 hSvc
= ScmGetServiceFromHandle(hService
);
4736 DPRINT1("Invalid service handle!\n");
4737 return ERROR_INVALID_HANDLE
;
4740 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
4741 SERVICE_CHANGE_CONFIG
))
4743 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
4744 return ERROR_ACCESS_DENIED
;
4747 lpService
= hSvc
->ServiceEntry
;
4748 if (lpService
== NULL
)
4750 DPRINT("lpService == NULL!\n");
4751 return ERROR_INVALID_HANDLE
;
4754 /* Lock the service database exclusively */
4755 ScmLockDatabaseExclusive();
4757 if (lpService
->bDeleted
)
4759 DPRINT("The service has already been marked for delete!\n");
4760 dwError
= ERROR_SERVICE_MARKED_FOR_DELETE
;
4764 /* Open the service key */
4765 dwError
= ScmOpenServiceKey(lpService
->szServiceName
,
4768 if (dwError
!= ERROR_SUCCESS
)
4771 if (Info
.dwInfoLevel
== SERVICE_CONFIG_DESCRIPTION
)
4773 LPSERVICE_DESCRIPTIONW lpServiceDescription
;
4775 lpServiceDescription
= (LPSERVICE_DESCRIPTIONW
)Info
.psd
;
4777 if (lpServiceDescription
!= NULL
&&
4778 lpServiceDescription
->lpDescription
!= NULL
)
4780 DPRINT("Setting value %S\n", lpServiceDescription
->lpDescription
);
4781 dwError
= RegSetValueExW(hServiceKey
,
4785 (LPBYTE
)lpServiceDescription
->lpDescription
,
4786 (wcslen(lpServiceDescription
->lpDescription
) + 1) * sizeof(WCHAR
));
4787 if (dwError
!= ERROR_SUCCESS
)
4791 else if (Info
.dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
4794 dwError
= ERROR_CALL_NOT_IMPLEMENTED
;
4799 /* Unlock the service database */
4800 ScmUnlockDatabase();
4802 if (hServiceKey
!= NULL
)
4803 RegCloseKey(hServiceKey
);
4805 DPRINT("RChangeServiceConfig2W() done (Error %lu)\n", dwError
);
4812 DWORD
RQueryServiceConfig2A(
4813 SC_RPC_HANDLE hService
,
4817 LPBOUNDED_DWORD_8K pcbBytesNeeded
)
4819 DWORD dwError
= ERROR_SUCCESS
;
4820 PSERVICE_HANDLE hSvc
;
4821 PSERVICE lpService
= NULL
;
4822 HKEY hServiceKey
= NULL
;
4823 DWORD dwRequiredSize
= 0;
4825 LPWSTR lpDescriptionW
= NULL
;
4826 LPWSTR lpRebootMessageW
= NULL
;
4827 LPWSTR lpFailureCommandW
= NULL
;
4829 DPRINT("RQueryServiceConfig2A() called hService %p dwInfoLevel %u, lpBuffer %p cbBufSize %u pcbBytesNeeded %p\n",
4830 hService
, dwInfoLevel
, lpBuffer
, cbBufSize
, pcbBytesNeeded
);
4833 return ERROR_INVALID_ADDRESS
;
4836 return ERROR_SHUTDOWN_IN_PROGRESS
;
4838 hSvc
= ScmGetServiceFromHandle(hService
);
4841 DPRINT1("Invalid service handle!\n");
4842 return ERROR_INVALID_HANDLE
;
4845 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
4846 SERVICE_QUERY_CONFIG
))
4848 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
4849 return ERROR_ACCESS_DENIED
;
4852 lpService
= hSvc
->ServiceEntry
;
4853 if (lpService
== NULL
)
4855 DPRINT("lpService == NULL!\n");
4856 return ERROR_INVALID_HANDLE
;
4859 /* Lock the service database shared */
4860 ScmLockDatabaseShared();
4862 dwError
= ScmOpenServiceKey(lpService
->lpServiceName
,
4865 if (dwError
!= ERROR_SUCCESS
)
4868 if (dwInfoLevel
== SERVICE_CONFIG_DESCRIPTION
)
4870 LPSERVICE_DESCRIPTIONA lpServiceDescription
= (LPSERVICE_DESCRIPTIONA
)lpBuffer
;
4873 dwError
= ScmReadString(hServiceKey
,
4876 if (dwError
!= ERROR_SUCCESS
&& dwError
!= ERROR_FILE_NOT_FOUND
)
4879 *pcbBytesNeeded
= sizeof(SERVICE_DESCRIPTIONA
);
4880 if (dwError
== ERROR_SUCCESS
)
4881 *pcbBytesNeeded
+= ((wcslen(lpDescriptionW
) + 1) * sizeof(WCHAR
));
4883 if (cbBufSize
< *pcbBytesNeeded
)
4885 dwError
= ERROR_INSUFFICIENT_BUFFER
;
4889 if (dwError
== ERROR_SUCCESS
)
4891 lpStr
= (LPSTR
)(lpServiceDescription
+ 1);
4893 WideCharToMultiByte(CP_ACP
,
4898 wcslen(lpDescriptionW
),
4901 lpServiceDescription
->lpDescription
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceDescription
);
4905 lpServiceDescription
->lpDescription
= NULL
;
4906 dwError
= ERROR_SUCCESS
;
4909 else if (dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
4911 LPSERVICE_FAILURE_ACTIONSA lpFailureActions
= (LPSERVICE_FAILURE_ACTIONSA
)lpBuffer
;
4914 /* Query value length */
4916 dwError
= RegQueryValueExW(hServiceKey
,
4922 if (dwError
!= ERROR_SUCCESS
&& dwError
!= ERROR_MORE_DATA
)
4925 if (dwType
!= REG_BINARY
)
4927 dwError
= ERROR_UNSUPPORTED_TYPE
;
4931 dwRequiredSize
= max(sizeof(SERVICE_FAILURE_ACTIONSA
), dwRequiredSize
);
4933 dwError
= ScmReadString(hServiceKey
,
4935 &lpFailureCommandW
);
4937 dwError
= ScmReadString(hServiceKey
,
4941 if (lpRebootMessageW
)
4942 dwRequiredSize
+= (wcslen(lpRebootMessageW
) + 1) * sizeof(WCHAR
);
4944 if (lpFailureCommandW
)
4945 dwRequiredSize
+= (wcslen(lpFailureCommandW
) + 1) * sizeof(WCHAR
);
4947 if (cbBufSize
< dwRequiredSize
)
4949 *pcbBytesNeeded
= dwRequiredSize
;
4950 dwError
= ERROR_INSUFFICIENT_BUFFER
;
4954 /* Now we can fill the buffer */
4955 dwError
= RegQueryValueExW(hServiceKey
,
4959 (LPBYTE
)lpFailureActions
,
4961 if (dwError
!= ERROR_SUCCESS
)
4964 if ((dwRequiredSize
< sizeof(SERVICE_FAILURE_ACTIONSA
)) ||
4965 (dwRequiredSize
> cbBufSize
))
4967 dwError
= ERROR_BUFFER_OVERFLOW
;
4971 if (dwError
== ERROR_SUCCESS
)
4973 lpFailureActions
->cActions
= min(lpFailureActions
->cActions
, (dwRequiredSize
- FIELD_OFFSET(SERVICE_FAILURE_ACTIONSA
, lpsaActions
)) / sizeof(SC_ACTION
));
4974 lpFailureActions
->lpsaActions
= (lpFailureActions
->cActions
> 0) ? (LPSC_ACTION
)(lpFailureActions
+ 1) : NULL
;
4975 lpStr
= (LPSTR
)((ULONG_PTR
)(lpFailureActions
+ 1) + lpFailureActions
->cActions
* sizeof(SC_ACTION
));
4979 lpFailureActions
->dwResetPeriod
= 0;
4980 lpFailureActions
->cActions
= 0;
4981 lpFailureActions
->lpsaActions
= NULL
;
4982 lpStr
= (LPSTR
)(lpFailureActions
+ 1);
4985 lpFailureActions
->lpRebootMsg
= NULL
;
4986 lpFailureActions
->lpCommand
= NULL
;
4988 if (lpRebootMessageW
)
4990 WideCharToMultiByte(CP_ACP
,
4995 wcslen(lpRebootMessageW
),
4998 lpFailureActions
->lpRebootMsg
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpFailureActions
);
4999 lpStr
+= strlen(lpStr
) + 1;
5002 if (lpFailureCommandW
)
5004 WideCharToMultiByte(CP_ACP
,
5009 wcslen(lpFailureCommandW
),
5012 lpFailureActions
->lpCommand
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpFailureActions
);
5013 /* lpStr += strlen(lpStr) + 1; */
5016 dwError
= ERROR_SUCCESS
;
5020 /* Unlock the service database */
5021 ScmUnlockDatabase();
5023 if (lpDescriptionW
!= NULL
)
5024 HeapFree(GetProcessHeap(), 0, lpDescriptionW
);
5026 if (lpRebootMessageW
!= NULL
)
5027 HeapFree(GetProcessHeap(), 0, lpRebootMessageW
);
5029 if (lpFailureCommandW
!= NULL
)
5030 HeapFree(GetProcessHeap(), 0, lpFailureCommandW
);
5032 if (hServiceKey
!= NULL
)
5033 RegCloseKey(hServiceKey
);
5035 DPRINT("RQueryServiceConfig2A() done (Error %lu)\n", dwError
);
5042 DWORD
RQueryServiceConfig2W(
5043 SC_RPC_HANDLE hService
,
5047 LPBOUNDED_DWORD_8K pcbBytesNeeded
)
5049 DWORD dwError
= ERROR_SUCCESS
;
5050 PSERVICE_HANDLE hSvc
;
5051 PSERVICE lpService
= NULL
;
5052 HKEY hServiceKey
= NULL
;
5053 DWORD dwRequiredSize
= 0;
5055 LPWSTR lpDescription
= NULL
;
5056 LPWSTR lpRebootMessage
= NULL
;
5057 LPWSTR lpFailureCommand
= NULL
;
5059 DPRINT("RQueryServiceConfig2W() called\n");
5062 return ERROR_INVALID_ADDRESS
;
5065 return ERROR_SHUTDOWN_IN_PROGRESS
;
5067 hSvc
= ScmGetServiceFromHandle(hService
);
5070 DPRINT1("Invalid service handle!\n");
5071 return ERROR_INVALID_HANDLE
;
5074 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
5075 SERVICE_QUERY_CONFIG
))
5077 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
5078 return ERROR_ACCESS_DENIED
;
5081 lpService
= hSvc
->ServiceEntry
;
5082 if (lpService
== NULL
)
5084 DPRINT("lpService == NULL!\n");
5085 return ERROR_INVALID_HANDLE
;
5088 /* Lock the service database shared */
5089 ScmLockDatabaseShared();
5091 dwError
= ScmOpenServiceKey(lpService
->lpServiceName
,
5094 if (dwError
!= ERROR_SUCCESS
)
5097 if (dwInfoLevel
== SERVICE_CONFIG_DESCRIPTION
)
5099 LPSERVICE_DESCRIPTIONW lpServiceDescription
= (LPSERVICE_DESCRIPTIONW
)lpBuffer
;
5102 dwError
= ScmReadString(hServiceKey
,
5105 if (dwError
!= ERROR_SUCCESS
&& dwError
!= ERROR_FILE_NOT_FOUND
)
5108 *pcbBytesNeeded
= sizeof(SERVICE_DESCRIPTIONW
);
5109 if (dwError
== ERROR_SUCCESS
)
5110 *pcbBytesNeeded
+= ((wcslen(lpDescription
) + 1) * sizeof(WCHAR
));
5112 if (cbBufSize
< *pcbBytesNeeded
)
5114 dwError
= ERROR_INSUFFICIENT_BUFFER
;
5118 if (dwError
== ERROR_SUCCESS
)
5120 lpStr
= (LPWSTR
)(lpServiceDescription
+ 1);
5121 wcscpy(lpStr
, lpDescription
);
5122 lpServiceDescription
->lpDescription
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceDescription
);
5126 lpServiceDescription
->lpDescription
= NULL
;
5127 dwError
= ERROR_SUCCESS
;
5130 else if (dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
5132 LPSERVICE_FAILURE_ACTIONSW lpFailureActions
= (LPSERVICE_FAILURE_ACTIONSW
)lpBuffer
;
5135 /* Query value length */
5137 dwError
= RegQueryValueExW(hServiceKey
,
5143 if (dwError
!= ERROR_SUCCESS
&& dwError
!= ERROR_MORE_DATA
)
5146 if (dwType
!= REG_BINARY
)
5148 dwError
= ERROR_UNSUPPORTED_TYPE
;
5152 dwRequiredSize
= max(sizeof(SERVICE_FAILURE_ACTIONSW
), dwRequiredSize
);
5154 dwError
= ScmReadString(hServiceKey
,
5158 dwError
= ScmReadString(hServiceKey
,
5162 if (lpRebootMessage
)
5163 dwRequiredSize
+= (wcslen(lpRebootMessage
) + 1) * sizeof(WCHAR
);
5165 if (lpFailureCommand
)
5166 dwRequiredSize
+= (wcslen(lpFailureCommand
) + 1) * sizeof(WCHAR
);
5168 if (cbBufSize
< dwRequiredSize
)
5170 *pcbBytesNeeded
= dwRequiredSize
;
5171 dwError
= ERROR_INSUFFICIENT_BUFFER
;
5175 /* Now we can fill the buffer */
5176 dwError
= RegQueryValueExW(hServiceKey
,
5180 (LPBYTE
)lpFailureActions
,
5182 if (dwError
!= ERROR_SUCCESS
)
5185 if ((dwRequiredSize
< sizeof(SERVICE_FAILURE_ACTIONSW
)) ||
5186 (dwRequiredSize
> cbBufSize
))
5188 dwError
= ERROR_BUFFER_OVERFLOW
;
5192 if (dwError
== ERROR_SUCCESS
)
5194 lpFailureActions
->cActions
= min(lpFailureActions
->cActions
, (dwRequiredSize
- FIELD_OFFSET(SERVICE_FAILURE_ACTIONSW
, lpsaActions
)) / sizeof(SC_ACTION
));
5195 lpFailureActions
->lpsaActions
= (lpFailureActions
->cActions
> 0 ? (LPSC_ACTION
)(lpFailureActions
+ 1) : NULL
);
5196 lpStr
= (LPWSTR
)((ULONG_PTR
)(lpFailureActions
+ 1) + lpFailureActions
->cActions
* sizeof(SC_ACTION
));
5200 lpFailureActions
->dwResetPeriod
= 0;
5201 lpFailureActions
->cActions
= 0;
5202 lpFailureActions
->lpsaActions
= NULL
;
5203 lpStr
= (LPWSTR
)(lpFailureActions
+ 1);
5206 lpFailureActions
->lpRebootMsg
= NULL
;
5207 lpFailureActions
->lpCommand
= NULL
;
5209 if (lpRebootMessage
)
5211 wcscpy(lpStr
, lpRebootMessage
);
5212 lpFailureActions
->lpRebootMsg
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpFailureActions
);
5213 lpStr
+= wcslen(lpStr
) + 1;
5216 if (lpFailureCommand
)
5218 wcscpy(lpStr
, lpFailureCommand
);
5219 lpFailureActions
->lpCommand
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpFailureActions
);
5220 /* lpStr += wcslen(lpStr) + 1; */
5223 dwError
= ERROR_SUCCESS
;
5227 /* Unlock the service database */
5228 ScmUnlockDatabase();
5230 if (lpDescription
!= NULL
)
5231 HeapFree(GetProcessHeap(), 0, lpDescription
);
5233 if (lpRebootMessage
!= NULL
)
5234 HeapFree(GetProcessHeap(), 0, lpRebootMessage
);
5236 if (lpFailureCommand
!= NULL
)
5237 HeapFree(GetProcessHeap(), 0, lpFailureCommand
);
5239 if (hServiceKey
!= NULL
)
5240 RegCloseKey(hServiceKey
);
5242 DPRINT("RQueryServiceConfig2W() done (Error %lu)\n", dwError
);
5249 DWORD
RQueryServiceStatusEx(
5250 SC_RPC_HANDLE hService
,
5251 SC_STATUS_TYPE InfoLevel
,
5254 LPBOUNDED_DWORD_8K pcbBytesNeeded
)
5256 LPSERVICE_STATUS_PROCESS lpStatus
;
5257 PSERVICE_HANDLE hSvc
;
5260 DPRINT("RQueryServiceStatusEx() called\n");
5263 return ERROR_SHUTDOWN_IN_PROGRESS
;
5265 if (InfoLevel
!= SC_STATUS_PROCESS_INFO
)
5266 return ERROR_INVALID_LEVEL
;
5268 *pcbBytesNeeded
= sizeof(SERVICE_STATUS_PROCESS
);
5270 if (cbBufSize
< sizeof(SERVICE_STATUS_PROCESS
))
5271 return ERROR_INSUFFICIENT_BUFFER
;
5273 hSvc
= ScmGetServiceFromHandle(hService
);
5276 DPRINT1("Invalid service handle!\n");
5277 return ERROR_INVALID_HANDLE
;
5280 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
5281 SERVICE_QUERY_STATUS
))
5283 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
5284 return ERROR_ACCESS_DENIED
;
5287 lpService
= hSvc
->ServiceEntry
;
5288 if (lpService
== NULL
)
5290 DPRINT("lpService == NULL!\n");
5291 return ERROR_INVALID_HANDLE
;
5294 /* Lock the service database shared */
5295 ScmLockDatabaseShared();
5297 lpStatus
= (LPSERVICE_STATUS_PROCESS
)lpBuffer
;
5299 /* Return service status information */
5300 RtlCopyMemory(lpStatus
,
5302 sizeof(SERVICE_STATUS
));
5304 lpStatus
->dwProcessId
= (lpService
->lpImage
!= NULL
) ? lpService
->lpImage
->dwProcessId
: 0; /* FIXME */
5305 lpStatus
->dwServiceFlags
= 0; /* FIXME */
5307 /* Unlock the service database */
5308 ScmUnlockDatabase();
5310 return ERROR_SUCCESS
;
5315 DWORD
REnumServicesStatusExA(
5316 SC_RPC_HANDLE hSCManager
,
5317 SC_ENUM_TYPE InfoLevel
,
5318 DWORD dwServiceType
,
5319 DWORD dwServiceState
,
5322 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
5323 LPBOUNDED_DWORD_256K lpServicesReturned
,
5324 LPBOUNDED_DWORD_256K lpResumeIndex
,
5325 LPCSTR pszGroupName
)
5327 LPENUM_SERVICE_STATUS_PROCESSW lpStatusPtrW
= NULL
;
5328 LPENUM_SERVICE_STATUS_PROCESSA lpStatusPtrA
= NULL
;
5329 LPWSTR lpStringPtrW
;
5331 LPWSTR pszGroupNameW
= NULL
;
5333 DWORD dwServiceCount
;
5335 DPRINT("REnumServicesStatusExA() called\n");
5339 pszGroupNameW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (strlen(pszGroupName
) + 1) * sizeof(WCHAR
));
5342 DPRINT("Failed to allocate buffer!\n");
5343 return ERROR_NOT_ENOUGH_MEMORY
;
5346 MultiByteToWideChar(CP_ACP
,
5351 strlen(pszGroupName
) + 1);
5354 if ((cbBufSize
> 0) && (lpBuffer
))
5356 lpStatusPtrW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, cbBufSize
);
5359 DPRINT("Failed to allocate buffer!\n");
5360 return ERROR_NOT_ENOUGH_MEMORY
;
5364 dwError
= REnumServicesStatusExW(hSCManager
,
5368 (LPBYTE
)lpStatusPtrW
,
5375 /* if no services were returned then we are Done */
5376 if (*lpServicesReturned
== 0)
5379 lpStatusPtrA
= (LPENUM_SERVICE_STATUS_PROCESSA
)lpBuffer
;
5380 lpStringPtrA
= (LPSTR
)((ULONG_PTR
)lpBuffer
+
5381 *lpServicesReturned
* sizeof(ENUM_SERVICE_STATUS_PROCESSA
));
5382 lpStringPtrW
= (LPWSTR
)((ULONG_PTR
)lpStatusPtrW
+
5383 *lpServicesReturned
* sizeof(ENUM_SERVICE_STATUS_PROCESSW
));
5385 for (dwServiceCount
= 0; dwServiceCount
< *lpServicesReturned
; dwServiceCount
++)
5387 /* Copy the service name */
5388 WideCharToMultiByte(CP_ACP
,
5393 wcslen(lpStringPtrW
),
5397 lpStatusPtrA
->lpServiceName
= (LPSTR
)((ULONG_PTR
)lpStringPtrA
- (ULONG_PTR
)lpBuffer
);
5398 lpStringPtrA
+= wcslen(lpStringPtrW
) + 1;
5399 lpStringPtrW
+= wcslen(lpStringPtrW
) + 1;
5401 /* Copy the display name */
5402 WideCharToMultiByte(CP_ACP
,
5407 wcslen(lpStringPtrW
),
5411 lpStatusPtrA
->lpDisplayName
= (LPSTR
)((ULONG_PTR
)lpStringPtrA
- (ULONG_PTR
)lpBuffer
);
5412 lpStringPtrA
+= wcslen(lpStringPtrW
) + 1;
5413 lpStringPtrW
+= wcslen(lpStringPtrW
) + 1;
5415 /* Copy the status information */
5416 memcpy(&lpStatusPtrA
->ServiceStatusProcess
,
5417 &lpStatusPtrW
->ServiceStatusProcess
,
5418 sizeof(SERVICE_STATUS
));
5420 lpStatusPtrA
->ServiceStatusProcess
.dwProcessId
= lpStatusPtrW
->ServiceStatusProcess
.dwProcessId
; /* FIXME */
5421 lpStatusPtrA
->ServiceStatusProcess
.dwServiceFlags
= 0; /* FIXME */
5427 HeapFree(GetProcessHeap(), 0, pszGroupNameW
);
5430 HeapFree(GetProcessHeap(), 0, lpStatusPtrW
);
5432 DPRINT("REnumServicesStatusExA() done (Error %lu)\n", dwError
);
5439 DWORD
REnumServicesStatusExW(
5440 SC_RPC_HANDLE hSCManager
,
5441 SC_ENUM_TYPE InfoLevel
,
5442 DWORD dwServiceType
,
5443 DWORD dwServiceState
,
5446 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
5447 LPBOUNDED_DWORD_256K lpServicesReturned
,
5448 LPBOUNDED_DWORD_256K lpResumeIndex
,
5449 LPCWSTR pszGroupName
)
5451 PMANAGER_HANDLE hManager
;
5453 DWORD dwError
= ERROR_SUCCESS
;
5454 PLIST_ENTRY ServiceEntry
;
5455 PSERVICE CurrentService
;
5457 DWORD dwRequiredSize
;
5458 DWORD dwServiceCount
;
5460 DWORD dwLastResumeCount
= 0;
5461 LPENUM_SERVICE_STATUS_PROCESSW lpStatusPtr
;
5464 DPRINT("REnumServicesStatusExW() called\n");
5467 return ERROR_SHUTDOWN_IN_PROGRESS
;
5469 if (InfoLevel
!= SC_ENUM_PROCESS_INFO
)
5470 return ERROR_INVALID_LEVEL
;
5472 hManager
= ScmGetServiceManagerFromHandle(hSCManager
);
5473 if (hManager
== NULL
)
5475 DPRINT1("Invalid service manager handle!\n");
5476 return ERROR_INVALID_HANDLE
;
5479 *pcbBytesNeeded
= 0;
5480 *lpServicesReturned
= 0;
5482 if ((dwServiceType
== 0) ||
5483 ((dwServiceType
& ~(SERVICE_DRIVER
| SERVICE_WIN32
)) != 0))
5485 DPRINT("Not a valid Service Type!\n");
5486 return ERROR_INVALID_PARAMETER
;
5489 if ((dwServiceState
!= SERVICE_ACTIVE
) &&
5490 (dwServiceState
!= SERVICE_INACTIVE
) &&
5491 (dwServiceState
!= SERVICE_STATE_ALL
))
5493 DPRINT("Not a valid Service State!\n");
5494 return ERROR_INVALID_PARAMETER
;
5497 /* Check access rights */
5498 if (!RtlAreAllAccessesGranted(hManager
->Handle
.DesiredAccess
,
5499 SC_MANAGER_ENUMERATE_SERVICE
))
5501 DPRINT("Insufficient access rights! 0x%lx\n",
5502 hManager
->Handle
.DesiredAccess
);
5503 return ERROR_ACCESS_DENIED
;
5507 dwLastResumeCount
= *lpResumeIndex
;
5509 /* Lock the service database shared */
5510 ScmLockDatabaseShared();
5512 lpService
= ScmGetServiceEntryByResumeCount(dwLastResumeCount
);
5513 if (lpService
== NULL
)
5515 dwError
= ERROR_SUCCESS
;
5522 for (ServiceEntry
= &lpService
->ServiceListEntry
;
5523 ServiceEntry
!= &ServiceListHead
;
5524 ServiceEntry
= ServiceEntry
->Flink
)
5526 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
5530 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
5533 dwState
= SERVICE_ACTIVE
;
5534 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
5535 dwState
= SERVICE_INACTIVE
;
5537 if ((dwState
& dwServiceState
) == 0)
5542 if (*pszGroupName
== 0)
5544 if (CurrentService
->lpGroup
!= NULL
)
5549 if ((CurrentService
->lpGroup
== NULL
) ||
5550 _wcsicmp(pszGroupName
, CurrentService
->lpGroup
->lpGroupName
))
5555 dwSize
= sizeof(ENUM_SERVICE_STATUS_PROCESSW
) +
5556 ((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
5557 ((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
5559 if (dwRequiredSize
+ dwSize
<= cbBufSize
)
5561 DPRINT("Service name: %S fit\n", CurrentService
->lpServiceName
);
5562 dwRequiredSize
+= dwSize
;
5564 dwLastResumeCount
= CurrentService
->dwResumeCount
;
5568 DPRINT("Service name: %S no fit\n", CurrentService
->lpServiceName
);
5574 DPRINT("dwRequiredSize: %lu\n", dwRequiredSize
);
5575 DPRINT("dwServiceCount: %lu\n", dwServiceCount
);
5578 ServiceEntry
!= &ServiceListHead
;
5579 ServiceEntry
= ServiceEntry
->Flink
)
5581 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
5585 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
5588 dwState
= SERVICE_ACTIVE
;
5589 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
5590 dwState
= SERVICE_INACTIVE
;
5592 if ((dwState
& dwServiceState
) == 0)
5597 if (*pszGroupName
== 0)
5599 if (CurrentService
->lpGroup
!= NULL
)
5604 if ((CurrentService
->lpGroup
== NULL
) ||
5605 _wcsicmp(pszGroupName
, CurrentService
->lpGroup
->lpGroupName
))
5610 dwRequiredSize
+= (sizeof(ENUM_SERVICE_STATUS_PROCESSW
) +
5611 ((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
5612 ((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
)));
5614 dwError
= ERROR_MORE_DATA
;
5617 DPRINT("*pcbBytesNeeded: %lu\n", dwRequiredSize
);
5620 *lpResumeIndex
= dwLastResumeCount
;
5622 *lpServicesReturned
= dwServiceCount
;
5623 *pcbBytesNeeded
= dwRequiredSize
;
5625 /* If there was no services that matched */
5626 if ((!dwServiceCount
) && (dwError
!= ERROR_MORE_DATA
))
5628 dwError
= ERROR_SERVICE_DOES_NOT_EXIST
;
5632 lpStatusPtr
= (LPENUM_SERVICE_STATUS_PROCESSW
)lpBuffer
;
5633 lpStringPtr
= (LPWSTR
)((ULONG_PTR
)lpBuffer
+
5634 dwServiceCount
* sizeof(ENUM_SERVICE_STATUS_PROCESSW
));
5637 for (ServiceEntry
= &lpService
->ServiceListEntry
;
5638 ServiceEntry
!= &ServiceListHead
;
5639 ServiceEntry
= ServiceEntry
->Flink
)
5641 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
5645 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
5648 dwState
= SERVICE_ACTIVE
;
5649 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
5650 dwState
= SERVICE_INACTIVE
;
5652 if ((dwState
& dwServiceState
) == 0)
5657 if (*pszGroupName
== 0)
5659 if (CurrentService
->lpGroup
!= NULL
)
5664 if ((CurrentService
->lpGroup
== NULL
) ||
5665 _wcsicmp(pszGroupName
, CurrentService
->lpGroup
->lpGroupName
))
5670 dwSize
= sizeof(ENUM_SERVICE_STATUS_PROCESSW
) +
5671 ((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
5672 ((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
5674 if (dwRequiredSize
+ dwSize
<= cbBufSize
)
5676 /* Copy the service name */
5678 CurrentService
->lpServiceName
);
5679 lpStatusPtr
->lpServiceName
= (LPWSTR
)((ULONG_PTR
)lpStringPtr
- (ULONG_PTR
)lpBuffer
);
5680 lpStringPtr
+= (wcslen(CurrentService
->lpServiceName
) + 1);
5682 /* Copy the display name */
5684 CurrentService
->lpDisplayName
);
5685 lpStatusPtr
->lpDisplayName
= (LPWSTR
)((ULONG_PTR
)lpStringPtr
- (ULONG_PTR
)lpBuffer
);
5686 lpStringPtr
+= (wcslen(CurrentService
->lpDisplayName
) + 1);
5688 /* Copy the status information */
5689 memcpy(&lpStatusPtr
->ServiceStatusProcess
,
5690 &CurrentService
->Status
,
5691 sizeof(SERVICE_STATUS
));
5692 lpStatusPtr
->ServiceStatusProcess
.dwProcessId
=
5693 (CurrentService
->lpImage
!= NULL
) ? CurrentService
->lpImage
->dwProcessId
: 0; /* FIXME */
5694 lpStatusPtr
->ServiceStatusProcess
.dwServiceFlags
= 0; /* FIXME */
5697 dwRequiredSize
+= dwSize
;
5707 *pcbBytesNeeded
= 0;
5713 /* Unlock the service database */
5714 ScmUnlockDatabase();
5716 DPRINT("REnumServicesStatusExW() done (Error %lu)\n", dwError
);
5723 DWORD
RSendTSMessage(
5724 handle_t BindingHandle
) /* FIXME */
5727 return ERROR_CALL_NOT_IMPLEMENTED
;
5732 DWORD
RCreateServiceWOW64A(
5733 handle_t BindingHandle
,
5734 LPSTR lpServiceName
,
5735 LPSTR lpDisplayName
,
5736 DWORD dwDesiredAccess
,
5737 DWORD dwServiceType
,
5739 DWORD dwErrorControl
,
5740 LPSTR lpBinaryPathName
,
5741 LPSTR lpLoadOrderGroup
,
5743 LPBYTE lpDependencies
,
5745 LPSTR lpServiceStartName
,
5748 LPSC_RPC_HANDLE lpServiceHandle
)
5751 return ERROR_CALL_NOT_IMPLEMENTED
;
5756 DWORD
RCreateServiceWOW64W(
5757 handle_t BindingHandle
,
5758 LPWSTR lpServiceName
,
5759 LPWSTR lpDisplayName
,
5760 DWORD dwDesiredAccess
,
5761 DWORD dwServiceType
,
5763 DWORD dwErrorControl
,
5764 LPWSTR lpBinaryPathName
,
5765 LPWSTR lpLoadOrderGroup
,
5767 LPBYTE lpDependencies
,
5769 LPWSTR lpServiceStartName
,
5772 LPSC_RPC_HANDLE lpServiceHandle
)
5775 return ERROR_CALL_NOT_IMPLEMENTED
;
5780 DWORD
RQueryServiceTagInfo(
5781 handle_t BindingHandle
) /* FIXME */
5784 return ERROR_CALL_NOT_IMPLEMENTED
;
5789 DWORD
RNotifyServiceStatusChange(
5790 SC_RPC_HANDLE hService
,
5791 SC_RPC_NOTIFY_PARAMS NotifyParams
,
5792 GUID
*pClientProcessGuid
,
5793 GUID
*pSCMProcessGuid
,
5794 PBOOL pfCreateRemoteQueue
,
5795 LPSC_NOTIFY_RPC_HANDLE phNotify
)
5798 return ERROR_CALL_NOT_IMPLEMENTED
;
5803 DWORD
RGetNotifyResults(
5804 SC_NOTIFY_RPC_HANDLE hNotify
,
5805 PSC_RPC_NOTIFY_PARAMS_LIST
*ppNotifyParams
)
5808 return ERROR_CALL_NOT_IMPLEMENTED
;
5813 DWORD
RCloseNotifyHandle(
5814 LPSC_NOTIFY_RPC_HANDLE phNotify
,
5818 return ERROR_CALL_NOT_IMPLEMENTED
;
5823 DWORD
RControlServiceExA(
5824 SC_RPC_HANDLE hService
,
5829 return ERROR_CALL_NOT_IMPLEMENTED
;
5834 DWORD
RControlServiceExW(
5835 SC_RPC_HANDLE hService
,
5840 return ERROR_CALL_NOT_IMPLEMENTED
;
5845 DWORD
RSendPnPMessage(
5846 handle_t BindingHandle
) /* FIXME */
5849 return ERROR_CALL_NOT_IMPLEMENTED
;
5854 DWORD
RValidatePnPService(
5855 handle_t BindingHandle
) /* FIXME */
5858 return ERROR_CALL_NOT_IMPLEMENTED
;
5863 DWORD
ROpenServiceStatusHandle(
5864 handle_t BindingHandle
) /* FIXME */
5867 return ERROR_CALL_NOT_IMPLEMENTED
;
5873 handle_t BindingHandle
) /* FIXME */
5876 return ERROR_CALL_NOT_IMPLEMENTED
;
5880 void __RPC_FAR
* __RPC_USER
midl_user_allocate(SIZE_T len
)
5882 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
);
5886 void __RPC_USER
midl_user_free(void __RPC_FAR
* ptr
)
5888 HeapFree(GetProcessHeap(), 0, ptr
);
5892 void __RPC_USER
SC_RPC_HANDLE_rundown(SC_RPC_HANDLE hSCObject
)
5897 void __RPC_USER
SC_RPC_LOCK_rundown(SC_RPC_LOCK Lock
)
5902 void __RPC_USER
SC_NOTIFY_RPC_HANDLE_rundown(SC_NOTIFY_RPC_HANDLE hNotify
)