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 ****************************************************************/
19 /* GLOBALS *****************************************************************/
21 #define MANAGER_TAG 0x72674D68 /* 'hMgr' */
22 #define SERVICE_TAG 0x63765368 /* 'hSvc' */
24 typedef struct _SCMGR_HANDLE
31 typedef struct _MANAGER_HANDLE
34 WCHAR DatabaseName
[1];
35 } MANAGER_HANDLE
, *PMANAGER_HANDLE
;
38 typedef struct _SERVICE_HANDLE
41 PSERVICE ServiceEntry
;
42 } SERVICE_HANDLE
, *PSERVICE_HANDLE
;
45 #define SC_MANAGER_READ \
46 (STANDARD_RIGHTS_READ | \
47 SC_MANAGER_QUERY_LOCK_STATUS | \
48 SC_MANAGER_ENUMERATE_SERVICE)
50 #define SC_MANAGER_WRITE \
51 (STANDARD_RIGHTS_WRITE | \
52 SC_MANAGER_MODIFY_BOOT_CONFIG | \
53 SC_MANAGER_CREATE_SERVICE)
55 #define SC_MANAGER_EXECUTE \
56 (STANDARD_RIGHTS_EXECUTE | \
58 SC_MANAGER_ENUMERATE_SERVICE | \
59 SC_MANAGER_CONNECT | \
60 SC_MANAGER_CREATE_SERVICE)
63 #define SERVICE_READ \
64 (STANDARD_RIGHTS_READ | \
65 SERVICE_INTERROGATE | \
66 SERVICE_ENUMERATE_DEPENDENTS | \
67 SERVICE_QUERY_STATUS | \
70 #define SERVICE_WRITE \
71 (STANDARD_RIGHTS_WRITE | \
72 SERVICE_CHANGE_CONFIG)
74 #define SERVICE_EXECUTE \
75 (STANDARD_RIGHTS_EXECUTE | \
76 SERVICE_USER_DEFINED_CONTROL | \
77 SERVICE_PAUSE_CONTINUE | \
82 /* VARIABLES ***************************************************************/
84 static GENERIC_MAPPING
85 ScmManagerMapping
= {SC_MANAGER_READ
,
88 SC_MANAGER_ALL_ACCESS
};
90 static GENERIC_MAPPING
91 ScmServiceMapping
= {SERVICE_READ
,
94 SC_MANAGER_ALL_ACCESS
};
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
= (MANAGER_HANDLE
*) HeapAlloc(GetProcessHeap(),
158 sizeof(MANAGER_HANDLE
) + (wcslen(lpDatabaseName
) + 1) * sizeof(WCHAR
));
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
= (SERVICE_HANDLE
*) 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
)
268 DPRINT("Assigning new tag to service %S\n", lpService
->lpServiceName
);
269 lpService
->dwTag
= 0;
270 return ERROR_SUCCESS
;
274 /* Internal recursive function */
275 /* Need to search for every dependency on every service */
277 Int_EnumDependentServicesW(HKEY hServicesKey
,
279 DWORD dwServiceState
,
280 PSERVICE
*lpServices
,
281 LPDWORD pcbBytesNeeded
,
282 LPDWORD lpServicesReturned
)
284 DWORD dwError
= ERROR_SUCCESS
;
285 WCHAR szNameBuf
[MAX_PATH
];
286 WCHAR szValueBuf
[MAX_PATH
];
287 WCHAR
*lpszNameBuf
= szNameBuf
;
288 WCHAR
*lpszValueBuf
= szValueBuf
;
292 PSERVICE lpCurrentService
;
293 HKEY hServiceEnumKey
;
294 DWORD dwCurrentServiceState
= SERVICE_ACTIVE
;
295 DWORD dwDependServiceStrPtr
= 0;
296 DWORD dwRequiredSize
= 0;
298 /* Get the number of service keys */
299 dwError
= RegQueryInfoKeyW(hServicesKey
,
311 if (dwError
!= ERROR_SUCCESS
)
313 DPRINT("ERROR! Unable to get number of services keys.\n");
317 /* Iterate the service keys to see if another service depends on the this service */
318 for (dwIteration
= 0; dwIteration
< dwNumSubKeys
; dwIteration
++)
321 dwError
= RegEnumKeyExW(hServicesKey
,
329 if (dwError
!= ERROR_SUCCESS
)
332 /* Open the Service key */
333 dwError
= RegOpenKeyExW(hServicesKey
,
338 if (dwError
!= ERROR_SUCCESS
)
343 /* Check for the DependOnService Value */
344 dwError
= RegQueryValueExW(hServiceEnumKey
,
348 (LPBYTE
)lpszValueBuf
,
351 /* FIXME: Handle load order. */
353 /* If the service found has a DependOnService value */
354 if (dwError
== ERROR_SUCCESS
)
356 dwDependServiceStrPtr
= 0;
358 /* Can be more than one Dependencies in the DependOnService string */
359 while (wcslen(lpszValueBuf
+ dwDependServiceStrPtr
) > 0)
361 if (_wcsicmp(lpszValueBuf
+ dwDependServiceStrPtr
, lpService
->lpServiceName
) == 0)
363 /* Get the current enumed service pointer */
364 lpCurrentService
= ScmGetServiceEntryByName(lpszNameBuf
);
366 /* Check for valid Service */
367 if (!lpCurrentService
)
369 /* This should never happen! */
370 DPRINT("This should not happen at this point, report to Developer\n");
371 return ERROR_NOT_FOUND
;
374 /* Determine state the service is in */
375 if (lpCurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
376 dwCurrentServiceState
= SERVICE_INACTIVE
;
378 /* If the ServiceState matches that requested or searching for SERVICE_STATE_ALL */
379 if ((dwCurrentServiceState
== dwServiceState
) ||
380 (dwServiceState
== SERVICE_STATE_ALL
))
382 /* Calculate the required size */
383 dwRequiredSize
+= sizeof(SERVICE_STATUS
);
384 dwRequiredSize
+= ((wcslen(lpCurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
));
385 dwRequiredSize
+= ((wcslen(lpCurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
387 /* Add the size for service name and display name pointers */
388 dwRequiredSize
+= (2 * sizeof(PVOID
));
390 /* increase the BytesNeeded size */
391 *pcbBytesNeeded
= *pcbBytesNeeded
+ dwRequiredSize
;
393 /* Don't fill callers buffer yet, as MSDN read that the last service with dependency
396 /* Recursive call to check for its dependencies */
397 Int_EnumDependentServicesW(hServicesKey
,
404 /* If the lpServices is valid set the service pointer */
406 lpServices
[*lpServicesReturned
] = lpCurrentService
;
408 *lpServicesReturned
= *lpServicesReturned
+ 1;
412 dwDependServiceStrPtr
+= (wcslen(lpszValueBuf
+ dwDependServiceStrPtr
) + 1);
415 else if (*pcbBytesNeeded
)
417 dwError
= ERROR_SUCCESS
;
420 RegCloseKey(hServiceEnumKey
);
428 DWORD
RCloseServiceHandle(
429 LPSC_RPC_HANDLE hSCObject
)
431 PMANAGER_HANDLE hManager
;
432 PSERVICE_HANDLE hService
;
436 DWORD pcbBytesNeeded
= 0;
437 DWORD dwServicesReturned
= 0;
439 DPRINT("RCloseServiceHandle() called\n");
441 DPRINT("hSCObject = %p\n", *hSCObject
);
444 return ERROR_INVALID_HANDLE
;
446 hManager
= ScmGetServiceManagerFromHandle(*hSCObject
);
447 hService
= ScmGetServiceFromHandle(*hSCObject
);
449 if (hManager
!= NULL
)
451 DPRINT("Found manager handle\n");
453 /* FIXME: add handle cleanup code */
455 HeapFree(GetProcessHeap(), 0, hManager
);
458 DPRINT("RCloseServiceHandle() done\n");
459 return ERROR_SUCCESS
;
461 else if (hService
!= NULL
)
463 DPRINT("Found service handle\n");
465 /* Get the pointer to the service record */
466 lpService
= hService
->ServiceEntry
;
468 /* FIXME: add handle cleanup code */
470 /* Free the handle */
471 HeapFree(GetProcessHeap(), 0, hService
);
474 ASSERT(lpService
->dwRefCount
> 0);
476 lpService
->dwRefCount
--;
477 DPRINT("CloseServiceHandle - lpService->dwRefCount %u\n",
478 lpService
->dwRefCount
);
480 if (lpService
->dwRefCount
== 0)
482 /* If this service has been marked for deletion */
483 if (lpService
->bDeleted
)
485 /* Open the Services Reg key */
486 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
487 L
"System\\CurrentControlSet\\Services",
489 KEY_SET_VALUE
| KEY_READ
,
491 if (dwError
!= ERROR_SUCCESS
)
493 DPRINT("Failed to open services key\n");
497 /* Call the internal function with NULL, just to get bytes we need */
498 Int_EnumDependentServicesW(hServicesKey
,
503 &dwServicesReturned
);
505 /* if pcbBytesNeeded returned a value then there are services running that are dependent on this service*/
508 DPRINT("Deletion failed due to running dependencies.\n");
509 RegCloseKey(hServicesKey
);
510 return ERROR_SUCCESS
;
513 /* There are no references and no runnning dependencies,
514 it is now safe to delete the service */
516 /* Delete the Service Key */
517 dwError
= RegDeleteKeyW(hServicesKey
,
518 lpService
->lpServiceName
);
520 RegCloseKey(hServicesKey
);
522 if (dwError
!= ERROR_SUCCESS
)
524 DPRINT("Failed to Delete the Service Registry key\n");
528 /* Delete the Service */
529 ScmDeleteServiceRecord(lpService
);
533 DPRINT("RCloseServiceHandle() done\n");
534 return ERROR_SUCCESS
;
537 DPRINT("Invalid handle tag (Tag %lx)\n", hManager
->Handle
.Tag
);
539 return ERROR_INVALID_HANDLE
;
544 DWORD
RControlService(
545 SC_RPC_HANDLE hService
,
547 LPSERVICE_STATUS lpServiceStatus
)
549 PSERVICE_HANDLE hSvc
;
551 ACCESS_MASK DesiredAccess
;
552 DWORD dwError
= ERROR_SUCCESS
;
553 DWORD pcbBytesNeeded
= 0;
554 DWORD dwServicesReturned
= 0;
555 DWORD dwControlsAccepted
;
556 DWORD dwCurrentState
;
557 HKEY hServicesKey
= NULL
;
559 DPRINT("RControlService() called\n");
562 return ERROR_SHUTDOWN_IN_PROGRESS
;
564 /* Check the service handle */
565 hSvc
= ScmGetServiceFromHandle(hService
);
568 DPRINT1("Invalid service handle!\n");
569 return ERROR_INVALID_HANDLE
;
573 /* Check the service entry point */
574 lpService
= hSvc
->ServiceEntry
;
575 if (lpService
== NULL
)
577 DPRINT1("lpService == NULL!\n");
578 return ERROR_INVALID_HANDLE
;
581 /* Check access rights */
584 case SERVICE_CONTROL_STOP
:
585 DesiredAccess
= SERVICE_STOP
;
588 case SERVICE_CONTROL_PAUSE
:
589 case SERVICE_CONTROL_CONTINUE
:
590 DesiredAccess
= SERVICE_PAUSE_CONTINUE
;
593 case SERVICE_INTERROGATE
:
594 DesiredAccess
= SERVICE_INTERROGATE
;
598 if (dwControl
>= 128 && dwControl
<= 255)
599 DesiredAccess
= SERVICE_USER_DEFINED_CONTROL
;
601 DesiredAccess
= SERVICE_QUERY_CONFIG
|
602 SERVICE_CHANGE_CONFIG
|
603 SERVICE_QUERY_STATUS
|
605 SERVICE_PAUSE_CONTINUE
;
609 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
611 return ERROR_ACCESS_DENIED
;
613 if (dwControl
== SERVICE_CONTROL_STOP
)
615 /* Check if the service has dependencies running as windows
616 doesn't stop a service that does */
618 /* Open the Services Reg key */
619 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
620 L
"System\\CurrentControlSet\\Services",
624 if (dwError
!= ERROR_SUCCESS
)
626 DPRINT("Failed to open services key\n");
630 /* Call the internal function with NULL, just to get bytes we need */
631 Int_EnumDependentServicesW(hServicesKey
,
636 &dwServicesReturned
);
638 RegCloseKey(hServicesKey
);
640 /* If pcbBytesNeeded is not zero then there are services running that
641 are dependent on this service */
642 if (pcbBytesNeeded
!= 0)
644 DPRINT("Service has running dependencies. Failed to stop service.\n");
645 return ERROR_DEPENDENT_SERVICES_RUNNING
;
649 if (lpService
->Status
.dwServiceType
& SERVICE_DRIVER
)
651 /* Send control code to the driver */
652 dwError
= ScmControlDriver(lpService
,
658 dwControlsAccepted
= lpService
->Status
.dwControlsAccepted
;
659 dwCurrentState
= lpService
->Status
.dwCurrentState
;
661 /* Check the current state before sending a control request */
662 switch (dwCurrentState
)
664 case SERVICE_STOP_PENDING
:
665 case SERVICE_STOPPED
:
666 return ERROR_SERVICE_CANNOT_ACCEPT_CTRL
;
668 case SERVICE_START_PENDING
:
671 case SERVICE_CONTROL_STOP
:
674 case SERVICE_CONTROL_INTERROGATE
:
675 RtlCopyMemory(lpServiceStatus
,
677 sizeof(SERVICE_STATUS
));
678 return ERROR_SUCCESS
;
681 return ERROR_SERVICE_CANNOT_ACCEPT_CTRL
;
686 /* Check if the control code is acceptable to the service */
689 case SERVICE_CONTROL_STOP
:
690 if ((dwControlsAccepted
& SERVICE_ACCEPT_STOP
) == 0)
691 return ERROR_INVALID_SERVICE_CONTROL
;
694 case SERVICE_CONTROL_PAUSE
:
695 case SERVICE_CONTROL_CONTINUE
:
696 if ((dwControlsAccepted
& SERVICE_ACCEPT_PAUSE_CONTINUE
) == 0)
697 return ERROR_INVALID_SERVICE_CONTROL
;
701 /* Send control code to the service */
702 dwError
= ScmControlService(lpService
,
705 /* Return service status information */
706 RtlCopyMemory(lpServiceStatus
,
708 sizeof(SERVICE_STATUS
));
711 if ((dwError
== ERROR_SUCCESS
) && (pcbBytesNeeded
))
712 dwError
= ERROR_DEPENDENT_SERVICES_RUNNING
;
714 if (dwError
== ERROR_SUCCESS
&&
715 dwControl
== SERVICE_CONTROL_STOP
&&
716 lpServiceStatus
->dwCurrentState
== SERVICE_STOPPED
)
718 lpService
->ProcessId
= 0; /* FIXME */
719 lpService
->ThreadId
= 0;
728 DWORD
RDeleteService(
729 SC_RPC_HANDLE hService
)
731 PSERVICE_HANDLE hSvc
;
735 DPRINT("RDeleteService() called\n");
738 return ERROR_SHUTDOWN_IN_PROGRESS
;
740 hSvc
= ScmGetServiceFromHandle(hService
);
743 DPRINT1("Invalid service handle!\n");
744 return ERROR_INVALID_HANDLE
;
747 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
749 return ERROR_ACCESS_DENIED
;
751 lpService
= hSvc
->ServiceEntry
;
752 if (lpService
== NULL
)
754 DPRINT("lpService == NULL!\n");
755 return ERROR_INVALID_HANDLE
;
758 /* FIXME: Acquire service database lock exclusively */
760 if (lpService
->bDeleted
)
762 DPRINT("The service has already been marked for delete!\n");
763 return ERROR_SERVICE_MARKED_FOR_DELETE
;
766 /* Mark service for delete */
767 lpService
->bDeleted
= TRUE
;
769 dwError
= ScmMarkServiceForDelete(lpService
);
771 /* FIXME: Release service database lock */
773 DPRINT("RDeleteService() done\n");
780 DWORD
RLockServiceDatabase(
781 SC_RPC_HANDLE hSCManager
,
782 LPSC_RPC_LOCK lpLock
)
784 PMANAGER_HANDLE hMgr
;
786 DPRINT("RLockServiceDatabase() called\n");
790 hMgr
= ScmGetServiceManagerFromHandle(hSCManager
);
793 DPRINT1("Invalid service manager handle!\n");
794 return ERROR_INVALID_HANDLE
;
797 if (!RtlAreAllAccessesGranted(hMgr
->Handle
.DesiredAccess
,
799 return ERROR_ACCESS_DENIED
;
801 // return ScmLockDatabase(0, hMgr->0xC, hLock);
803 /* FIXME: Lock the database */
804 *lpLock
= (SC_RPC_LOCK
)0x12345678; /* Dummy! */
806 return ERROR_SUCCESS
;
811 DWORD
RQueryServiceObjectSecurity(
812 SC_RPC_HANDLE hService
,
813 SECURITY_INFORMATION dwSecurityInformation
,
814 LPBYTE lpSecurityDescriptor
,
816 LPBOUNDED_DWORD_256K pcbBytesNeeded
)
818 PSERVICE_HANDLE hSvc
;
820 ULONG DesiredAccess
= 0;
826 SECURITY_DESCRIPTOR ObjectDescriptor
;
828 DPRINT("RQueryServiceObjectSecurity() called\n");
830 hSvc
= ScmGetServiceFromHandle(hService
);
833 DPRINT1("Invalid service handle!\n");
834 return ERROR_INVALID_HANDLE
;
837 if (dwSecurityInformation
& (DACL_SECURITY_INFORMATION
||
838 GROUP_SECURITY_INFORMATION
||
839 OWNER_SECURITY_INFORMATION
))
840 DesiredAccess
|= READ_CONTROL
;
842 if (dwSecurityInformation
& SACL_SECURITY_INFORMATION
)
843 DesiredAccess
|= ACCESS_SYSTEM_SECURITY
;
845 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
848 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
849 return ERROR_ACCESS_DENIED
;
852 lpService
= hSvc
->ServiceEntry
;
853 if (lpService
== NULL
)
855 DPRINT("lpService == NULL!\n");
856 return ERROR_INVALID_HANDLE
;
859 /* FIXME: Lock the service list */
862 Status
= RtlCreateSecurityDescriptor(&ObjectDescriptor
, SECURITY_DESCRIPTOR_REVISION
);
864 Status
= RtlQuerySecurityObject(&ObjectDescriptor
/* lpService->lpSecurityDescriptor */,
865 dwSecurityInformation
,
866 (PSECURITY_DESCRIPTOR
)lpSecurityDescriptor
,
870 /* FIXME: Unlock the service list */
872 if (NT_SUCCESS(Status
))
874 *pcbBytesNeeded
= dwBytesNeeded
;
875 dwError
= STATUS_SUCCESS
;
877 else if (Status
== STATUS_BUFFER_TOO_SMALL
)
879 *pcbBytesNeeded
= dwBytesNeeded
;
880 dwError
= ERROR_INSUFFICIENT_BUFFER
;
882 else if (Status
== STATUS_BAD_DESCRIPTOR_FORMAT
)
884 dwError
= ERROR_GEN_FAILURE
;
888 dwError
= RtlNtStatusToDosError(Status
);
896 DWORD
RSetServiceObjectSecurity(
897 SC_RPC_HANDLE hService
,
898 DWORD dwSecurityInformation
,
899 LPBYTE lpSecurityDescriptor
,
900 DWORD dwSecuityDescriptorSize
)
902 PSERVICE_HANDLE hSvc
;
904 ULONG DesiredAccess
= 0;
905 /* HANDLE hToken = NULL; */
907 /* NTSTATUS Status; */
910 DPRINT("RSetServiceObjectSecurity() called\n");
912 hSvc
= ScmGetServiceFromHandle(hService
);
915 DPRINT1("Invalid service handle!\n");
916 return ERROR_INVALID_HANDLE
;
919 if (dwSecurityInformation
== 0 ||
920 dwSecurityInformation
& ~(OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
921 | DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
))
922 return ERROR_INVALID_PARAMETER
;
924 if (!RtlValidSecurityDescriptor((PSECURITY_DESCRIPTOR
)lpSecurityDescriptor
))
925 return ERROR_INVALID_PARAMETER
;
927 if (dwSecurityInformation
& SACL_SECURITY_INFORMATION
)
928 DesiredAccess
|= ACCESS_SYSTEM_SECURITY
;
930 if (dwSecurityInformation
& DACL_SECURITY_INFORMATION
)
931 DesiredAccess
|= WRITE_DAC
;
933 if (dwSecurityInformation
& (OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
))
934 DesiredAccess
|= WRITE_OWNER
;
936 if ((dwSecurityInformation
& OWNER_SECURITY_INFORMATION
) &&
937 (((PISECURITY_DESCRIPTOR
)lpSecurityDescriptor
)->Owner
== NULL
))
938 return ERROR_INVALID_PARAMETER
;
940 if ((dwSecurityInformation
& GROUP_SECURITY_INFORMATION
) &&
941 (((PISECURITY_DESCRIPTOR
)lpSecurityDescriptor
)->Group
== NULL
))
942 return ERROR_INVALID_PARAMETER
;
944 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
947 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
948 return ERROR_ACCESS_DENIED
;
951 lpService
= hSvc
->ServiceEntry
;
952 if (lpService
== NULL
)
954 DPRINT("lpService == NULL!\n");
955 return ERROR_INVALID_HANDLE
;
958 if (lpService
->bDeleted
)
959 return ERROR_SERVICE_MARKED_FOR_DELETE
;
962 RpcImpersonateClient(NULL
);
964 Status
= NtOpenThreadToken(NtCurrentThread(),
968 if (!NT_SUCCESS(Status
))
969 return RtlNtStatusToDosError(Status
);
973 /* FIXME: Lock service database */
975 Status
= RtlSetSecurityObject(dwSecurityInformation
,
976 (PSECURITY_DESCRIPTOR
)lpSecurityDescriptor
,
977 &lpService
->lpSecurityDescriptor
,
980 if (!NT_SUCCESS(Status
))
982 dwError
= RtlNtStatusToDosError(Status
);
987 dwError
= ScmOpenServiceKey(lpService
->lpServiceName
,
988 READ_CONTROL
| KEY_CREATE_SUB_KEY
| KEY_SET_VALUE
,
990 if (dwError
!= ERROR_SUCCESS
)
994 dwError
= ERROR_SUCCESS
;
995 // dwError = ScmWriteSecurityDescriptor(hServiceKey,
996 // lpService->lpSecurityDescriptor);
998 RegFlushKey(hServiceKey
);
999 RegCloseKey(hServiceKey
);
1008 /* FIXME: Unlock service database */
1010 DPRINT("RSetServiceObjectSecurity() done (Error %lu)\n", dwError
);
1017 DWORD
RQueryServiceStatus(
1018 SC_RPC_HANDLE hService
,
1019 LPSERVICE_STATUS lpServiceStatus
)
1021 PSERVICE_HANDLE hSvc
;
1024 DPRINT("RQueryServiceStatus() called\n");
1027 return ERROR_SHUTDOWN_IN_PROGRESS
;
1029 hSvc
= ScmGetServiceFromHandle(hService
);
1032 DPRINT1("Invalid service handle!\n");
1033 return ERROR_INVALID_HANDLE
;
1036 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
1037 SERVICE_QUERY_STATUS
))
1039 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
1040 return ERROR_ACCESS_DENIED
;
1043 lpService
= hSvc
->ServiceEntry
;
1044 if (lpService
== NULL
)
1046 DPRINT("lpService == NULL!\n");
1047 return ERROR_INVALID_HANDLE
;
1050 ScmLockDatabaseShared();
1052 /* Return service status information */
1053 RtlCopyMemory(lpServiceStatus
,
1055 sizeof(SERVICE_STATUS
));
1057 ScmUnlockDatabase();
1059 return ERROR_SUCCESS
;
1064 ScmIsValidServiceState(DWORD dwCurrentState
)
1066 switch (dwCurrentState
)
1068 case SERVICE_STOPPED
:
1069 case SERVICE_START_PENDING
:
1070 case SERVICE_STOP_PENDING
:
1071 case SERVICE_RUNNING
:
1072 case SERVICE_CONTINUE_PENDING
:
1073 case SERVICE_PAUSE_PENDING
:
1074 case SERVICE_PAUSED
:
1084 DWORD
RSetServiceStatus(
1085 RPC_SERVICE_STATUS_HANDLE hServiceStatus
,
1086 LPSERVICE_STATUS lpServiceStatus
)
1090 DPRINT("RSetServiceStatus() called\n");
1091 DPRINT("hServiceStatus = %p\n", hServiceStatus
);
1092 DPRINT("dwServiceType = %lu\n", lpServiceStatus
->dwServiceType
);
1093 DPRINT("dwCurrentState = %lu\n", lpServiceStatus
->dwCurrentState
);
1094 DPRINT("dwControlsAccepted = %lu\n", lpServiceStatus
->dwControlsAccepted
);
1095 DPRINT("dwWin32ExitCode = %lu\n", lpServiceStatus
->dwWin32ExitCode
);
1096 DPRINT("dwServiceSpecificExitCode = %lu\n", lpServiceStatus
->dwServiceSpecificExitCode
);
1097 DPRINT("dwCheckPoint = %lu\n", lpServiceStatus
->dwCheckPoint
);
1098 DPRINT("dwWaitHint = %lu\n", lpServiceStatus
->dwWaitHint
);
1100 if (hServiceStatus
== 0)
1102 DPRINT("hServiceStatus == NULL!\n");
1103 return ERROR_INVALID_HANDLE
;
1106 lpService
= (PSERVICE
)hServiceStatus
;
1107 if (lpService
== NULL
)
1109 DPRINT("lpService == NULL!\n");
1110 return ERROR_INVALID_HANDLE
;
1113 /* Check current state */
1114 if (!ScmIsValidServiceState(lpServiceStatus
->dwCurrentState
))
1116 DPRINT("Invalid service state!\n");
1117 return ERROR_INVALID_DATA
;
1120 /* Check service type */
1121 if (!(lpServiceStatus
->dwServiceType
& SERVICE_WIN32
) &&
1122 (lpServiceStatus
->dwServiceType
& SERVICE_DRIVER
))
1124 DPRINT("Invalid service type!\n");
1125 return ERROR_INVALID_DATA
;
1128 /* Check accepted controls */
1129 if (lpServiceStatus
->dwControlsAccepted
& ~0xFF)
1131 DPRINT("Invalid controls accepted!\n");
1132 return ERROR_INVALID_DATA
;
1135 ScmLockDatabaseExclusive();
1137 RtlCopyMemory(&lpService
->Status
,
1139 sizeof(SERVICE_STATUS
));
1141 ScmUnlockDatabase();
1143 DPRINT("Set %S to %lu\n", lpService
->lpDisplayName
, lpService
->Status
.dwCurrentState
);
1144 DPRINT("RSetServiceStatus() done\n");
1146 return ERROR_SUCCESS
;
1151 DWORD
RUnlockServiceDatabase(
1155 return ERROR_SUCCESS
;
1160 DWORD
RNotifyBootConfigStatus(
1161 SVCCTL_HANDLEW lpMachineName
,
1162 DWORD BootAcceptable
)
1165 return ERROR_CALL_NOT_IMPLEMENTED
;
1170 DWORD
RI_ScSetServiceBitsW(
1171 RPC_SERVICE_STATUS_HANDLE hServiceStatus
,
1172 DWORD dwServiceBits
,
1174 int bUpdateImmediately
,
1178 return ERROR_CALL_NOT_IMPLEMENTED
;
1183 DWORD
RChangeServiceConfigW(
1184 SC_RPC_HANDLE hService
,
1185 DWORD dwServiceType
,
1187 DWORD dwErrorControl
,
1188 LPWSTR lpBinaryPathName
,
1189 LPWSTR lpLoadOrderGroup
,
1191 LPBYTE lpDependencies
,
1193 LPWSTR lpServiceStartName
,
1196 LPWSTR lpDisplayName
)
1198 DWORD dwError
= ERROR_SUCCESS
;
1199 PSERVICE_HANDLE hSvc
;
1200 PSERVICE lpService
= NULL
;
1201 HKEY hServiceKey
= NULL
;
1202 LPWSTR lpDisplayNameW
= NULL
;
1204 DPRINT("RChangeServiceConfigW() called\n");
1205 DPRINT("dwServiceType = %lu\n", dwServiceType
);
1206 DPRINT("dwStartType = %lu\n", dwStartType
);
1207 DPRINT("dwErrorControl = %lu\n", dwErrorControl
);
1208 DPRINT("lpBinaryPathName = %S\n", lpBinaryPathName
);
1209 DPRINT("lpLoadOrderGroup = %S\n", lpLoadOrderGroup
);
1210 DPRINT("lpDisplayName = %S\n", lpDisplayName
);
1213 return ERROR_SHUTDOWN_IN_PROGRESS
;
1215 hSvc
= ScmGetServiceFromHandle(hService
);
1218 DPRINT1("Invalid service handle!\n");
1219 return ERROR_INVALID_HANDLE
;
1222 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
1223 SERVICE_CHANGE_CONFIG
))
1225 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
1226 return ERROR_ACCESS_DENIED
;
1229 lpService
= hSvc
->ServiceEntry
;
1230 if (lpService
== NULL
)
1232 DPRINT("lpService == NULL!\n");
1233 return ERROR_INVALID_HANDLE
;
1236 /* FIXME: Lock database exclusively */
1238 if (lpService
->bDeleted
)
1240 /* FIXME: Unlock database */
1241 DPRINT("The service has already been marked for delete!\n");
1242 return ERROR_SERVICE_MARKED_FOR_DELETE
;
1245 /* Open the service key */
1246 dwError
= ScmOpenServiceKey(lpService
->szServiceName
,
1249 if (dwError
!= ERROR_SUCCESS
)
1252 /* Write service data to the registry */
1253 /* Set the display name */
1254 if (lpDisplayName
!= NULL
&& *lpDisplayName
!= 0)
1256 RegSetValueExW(hServiceKey
,
1260 (LPBYTE
)lpDisplayName
,
1261 (wcslen(lpDisplayName
) + 1) * sizeof(WCHAR
));
1263 /* Update the display name */
1264 lpDisplayNameW
= (LPWSTR
)HeapAlloc(GetProcessHeap(),
1266 (wcslen(lpDisplayName
) + 1) * sizeof(WCHAR
));
1267 if (lpDisplayNameW
== NULL
)
1269 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
1273 if (lpService
->lpDisplayName
!= lpService
->lpServiceName
)
1274 HeapFree(GetProcessHeap(), 0, lpService
->lpDisplayName
);
1276 lpService
->lpDisplayName
= lpDisplayNameW
;
1279 if (dwServiceType
!= SERVICE_NO_CHANGE
)
1281 /* Set the service type */
1282 dwError
= RegSetValueExW(hServiceKey
,
1286 (LPBYTE
)&dwServiceType
,
1288 if (dwError
!= ERROR_SUCCESS
)
1291 lpService
->Status
.dwServiceType
= dwServiceType
;
1294 if (dwStartType
!= SERVICE_NO_CHANGE
)
1296 /* Set the start value */
1297 dwError
= RegSetValueExW(hServiceKey
,
1301 (LPBYTE
)&dwStartType
,
1303 if (dwError
!= ERROR_SUCCESS
)
1306 lpService
->dwStartType
= dwStartType
;
1309 if (dwErrorControl
!= SERVICE_NO_CHANGE
)
1311 /* Set the error control value */
1312 dwError
= RegSetValueExW(hServiceKey
,
1316 (LPBYTE
)&dwErrorControl
,
1318 if (dwError
!= ERROR_SUCCESS
)
1321 lpService
->dwErrorControl
= dwErrorControl
;
1325 /* FIXME: set the new ImagePath value */
1327 /* Set the image path */
1328 if (dwServiceType
& SERVICE_WIN32
)
1330 if (lpBinaryPathName
!= NULL
&& *lpBinaryPathName
!= 0)
1332 dwError
= RegSetValueExW(hServiceKey
,
1336 (LPBYTE
)lpBinaryPathName
,
1337 (wcslen(lpBinaryPathName
) + 1) * sizeof(WCHAR
));
1338 if (dwError
!= ERROR_SUCCESS
)
1342 else if (dwServiceType
& SERVICE_DRIVER
)
1344 if (lpImagePath
!= NULL
&& *lpImagePath
!= 0)
1346 dwError
= RegSetValueExW(hServiceKey
,
1350 (LPBYTE
)lpImagePath
,
1351 (wcslen(lpImagePath
) + 1) *sizeof(WCHAR
));
1352 if (dwError
!= ERROR_SUCCESS
)
1358 /* Set the group name */
1359 if (lpLoadOrderGroup
!= NULL
&& *lpLoadOrderGroup
!= 0)
1361 dwError
= RegSetValueExW(hServiceKey
,
1365 (LPBYTE
)lpLoadOrderGroup
,
1366 (wcslen(lpLoadOrderGroup
) + 1) * sizeof(WCHAR
));
1367 if (dwError
!= ERROR_SUCCESS
)
1370 dwError
= ScmSetServiceGroup(lpService
,
1372 if (dwError
!= ERROR_SUCCESS
)
1376 if (lpdwTagId
!= NULL
)
1378 dwError
= ScmAssignNewTag(lpService
);
1379 if (dwError
!= ERROR_SUCCESS
)
1382 dwError
= RegSetValueExW(hServiceKey
,
1386 (LPBYTE
)&lpService
->dwTag
,
1388 if (dwError
!= ERROR_SUCCESS
)
1391 *lpdwTagId
= lpService
->dwTag
;
1394 /* Write dependencies */
1395 if (lpDependencies
!= NULL
&& *lpDependencies
!= 0)
1397 dwError
= ScmWriteDependencies(hServiceKey
,
1398 (LPWSTR
)lpDependencies
,
1400 if (dwError
!= ERROR_SUCCESS
)
1404 if (lpPassword
!= NULL
)
1406 /* FIXME: Write password */
1409 /* FIXME: Unlock database */
1412 if (hServiceKey
!= NULL
)
1413 RegCloseKey(hServiceKey
);
1415 DPRINT("RChangeServiceConfigW() done (Error %lu)\n", dwError
);
1421 /* Create a path suitable for the bootloader out of the full path */
1423 ScmConvertToBootPathName(wchar_t *CanonName
, wchar_t **RelativeName
)
1425 DWORD ServiceNameLen
, BufferSize
, ExpandedLen
;
1428 UNICODE_STRING NtPathName
, SystemRoot
, LinkTarget
;
1429 OBJECT_ATTRIBUTES ObjectAttributes
;
1431 HANDLE SymbolicLinkHandle
;
1433 DPRINT("ScmConvertToBootPathName %S\n", CanonName
);
1435 ServiceNameLen
= wcslen(CanonName
);
1437 /* First check, if it's already good */
1438 if (ServiceNameLen
> 12 &&
1439 !_wcsnicmp(L
"\\SystemRoot\\", CanonName
, 12))
1441 *RelativeName
= LocalAlloc(LMEM_ZEROINIT
, ServiceNameLen
* sizeof(WCHAR
) + sizeof(WCHAR
));
1442 if (*RelativeName
== NULL
)
1444 DPRINT("Error allocating memory for boot driver name!\n");
1445 return ERROR_NOT_ENOUGH_MEMORY
;
1449 wcscpy(*RelativeName
, CanonName
);
1451 DPRINT("Bootdriver name %S\n", *RelativeName
);
1452 return ERROR_SUCCESS
;
1455 /* If it has %SystemRoot% prefix, substitute it to \System*/
1456 if (ServiceNameLen
> 13 &&
1457 !_wcsnicmp(L
"%SystemRoot%\\", CanonName
, 13))
1459 /* There is no +sizeof(wchar_t) because the name is less by 1 wchar */
1460 *RelativeName
= LocalAlloc(LMEM_ZEROINIT
, ServiceNameLen
* sizeof(WCHAR
));
1462 if (*RelativeName
== NULL
)
1464 DPRINT("Error allocating memory for boot driver name!\n");
1465 return ERROR_NOT_ENOUGH_MEMORY
;
1469 wcscpy(*RelativeName
, L
"\\SystemRoot\\");
1470 wcscat(*RelativeName
, CanonName
+ 13);
1472 DPRINT("Bootdriver name %S\n", *RelativeName
);
1473 return ERROR_SUCCESS
;
1476 /* Get buffer size needed for expanding env strings */
1477 BufferSize
= ExpandEnvironmentStringsW(L
"%SystemRoot%\\", &Dest
, 1);
1479 if (BufferSize
<= 1)
1481 DPRINT("Error during a call to ExpandEnvironmentStringsW()\n");
1482 return ERROR_INVALID_ENVIRONMENT
;
1485 /* Allocate memory, since the size is known now */
1486 Expanded
= LocalAlloc(LMEM_ZEROINIT
, BufferSize
* sizeof(WCHAR
) + sizeof(WCHAR
));
1489 DPRINT("Error allocating memory for boot driver name!\n");
1490 return ERROR_NOT_ENOUGH_MEMORY
;
1494 if (ExpandEnvironmentStringsW(L
"%SystemRoot%\\", Expanded
, BufferSize
) >
1497 DPRINT("Error during a call to ExpandEnvironmentStringsW()\n");
1498 LocalFree(Expanded
);
1499 return ERROR_NOT_ENOUGH_MEMORY
;
1502 /* Convert to NY-style path */
1503 if (!RtlDosPathNameToNtPathName_U(Expanded
, &NtPathName
, NULL
, NULL
))
1505 DPRINT("Error during a call to RtlDosPathNameToNtPathName_U()\n");
1506 return ERROR_INVALID_ENVIRONMENT
;
1509 DPRINT("Converted to NT-style %wZ\n", &NtPathName
);
1511 /* No need to keep the dos-path anymore */
1512 LocalFree(Expanded
);
1514 /* Copy it to the allocated place */
1515 Expanded
= LocalAlloc(LMEM_ZEROINIT
, NtPathName
.Length
+ sizeof(WCHAR
));
1518 DPRINT("Error allocating memory for boot driver name!\n");
1519 return ERROR_NOT_ENOUGH_MEMORY
;
1522 ExpandedLen
= NtPathName
.Length
/ sizeof(WCHAR
);
1523 wcsncpy(Expanded
, NtPathName
.Buffer
, ExpandedLen
);
1524 Expanded
[ExpandedLen
] = 0;
1526 if (ServiceNameLen
> ExpandedLen
&&
1527 !_wcsnicmp(Expanded
, CanonName
, ExpandedLen
))
1529 /* Only \SystemRoot\ is missing */
1530 *RelativeName
= LocalAlloc(LMEM_ZEROINIT
,
1531 (ServiceNameLen
- ExpandedLen
) * sizeof(WCHAR
) + 13*sizeof(WCHAR
));
1532 if (*RelativeName
== NULL
)
1534 DPRINT("Error allocating memory for boot driver name!\n");
1535 LocalFree(Expanded
);
1536 return ERROR_NOT_ENOUGH_MEMORY
;
1539 wcscpy(*RelativeName
, L
"\\SystemRoot\\");
1540 wcscat(*RelativeName
, CanonName
+ ExpandedLen
);
1542 RtlFreeUnicodeString(&NtPathName
);
1543 return ERROR_SUCCESS
;
1546 /* The most complex case starts here */
1547 RtlInitUnicodeString(&SystemRoot
, L
"\\SystemRoot");
1548 InitializeObjectAttributes(&ObjectAttributes
,
1550 OBJ_CASE_INSENSITIVE
,
1554 /* Open this symlink */
1555 Status
= NtOpenSymbolicLinkObject(&SymbolicLinkHandle
, SYMBOLIC_LINK_QUERY
, &ObjectAttributes
);
1557 if (NT_SUCCESS(Status
))
1559 LinkTarget
.Length
= 0;
1560 LinkTarget
.MaximumLength
= 0;
1562 DPRINT("Opened symbolic link object\n");
1564 Status
= NtQuerySymbolicLinkObject(SymbolicLinkHandle
, &LinkTarget
, &BufferSize
);
1565 if (NT_SUCCESS(Status
) || Status
== STATUS_BUFFER_TOO_SMALL
)
1567 /* Check if required buffer size is sane */
1568 if (BufferSize
> 0xFFFD)
1570 DPRINT("Too large buffer required\n");
1573 if (SymbolicLinkHandle
) NtClose(SymbolicLinkHandle
);
1574 LocalFree(Expanded
);
1575 return ERROR_NOT_ENOUGH_MEMORY
;
1578 /* Alloc the string */
1579 LinkTarget
.Buffer
= LocalAlloc(LMEM_ZEROINIT
, BufferSize
+ sizeof(WCHAR
));
1580 if (!LinkTarget
.Buffer
)
1582 DPRINT("Unable to alloc buffer\n");
1583 if (SymbolicLinkHandle
) NtClose(SymbolicLinkHandle
);
1584 LocalFree(Expanded
);
1585 return ERROR_NOT_ENOUGH_MEMORY
;
1588 /* Do a real query now */
1589 LinkTarget
.Length
= BufferSize
;
1590 LinkTarget
.MaximumLength
= LinkTarget
.Length
+ sizeof(WCHAR
);
1592 Status
= NtQuerySymbolicLinkObject(SymbolicLinkHandle
, &LinkTarget
, &BufferSize
);
1593 if (NT_SUCCESS(Status
))
1595 DPRINT("LinkTarget: %wZ\n", &LinkTarget
);
1597 ExpandedLen
= LinkTarget
.Length
/ sizeof(WCHAR
);
1598 if ((ServiceNameLen
> ExpandedLen
) &&
1599 !_wcsnicmp(LinkTarget
.Buffer
, CanonName
, ExpandedLen
))
1601 *RelativeName
= LocalAlloc(LMEM_ZEROINIT
,
1602 (ServiceNameLen
- ExpandedLen
) * sizeof(WCHAR
) + 13*sizeof(WCHAR
));
1604 if (*RelativeName
== NULL
)
1606 DPRINT("Unable to alloc buffer\n");
1607 if (SymbolicLinkHandle
) NtClose(SymbolicLinkHandle
);
1608 LocalFree(Expanded
);
1609 RtlFreeUnicodeString(&NtPathName
);
1610 return ERROR_NOT_ENOUGH_MEMORY
;
1613 /* Copy it over, substituting the first part
1615 wcscpy(*RelativeName
, L
"\\SystemRoot\\");
1616 wcscat(*RelativeName
, CanonName
+ExpandedLen
+1);
1619 if (SymbolicLinkHandle
) NtClose(SymbolicLinkHandle
);
1620 LocalFree(Expanded
);
1621 RtlFreeUnicodeString(&NtPathName
);
1623 /* Return success */
1624 return ERROR_SUCCESS
;
1628 if (SymbolicLinkHandle
) NtClose(SymbolicLinkHandle
);
1629 LocalFree(Expanded
);
1630 RtlFreeUnicodeString(&NtPathName
);
1631 return ERROR_INVALID_PARAMETER
;
1636 DPRINT("Error, Status = %08X\n", Status
);
1637 if (SymbolicLinkHandle
) NtClose(SymbolicLinkHandle
);
1638 LocalFree(Expanded
);
1639 RtlFreeUnicodeString(&NtPathName
);
1640 return ERROR_INVALID_PARAMETER
;
1645 DPRINT("Error, Status = %08X\n", Status
);
1646 if (SymbolicLinkHandle
) NtClose(SymbolicLinkHandle
);
1647 LocalFree(Expanded
);
1648 RtlFreeUnicodeString(&NtPathName
);
1649 return ERROR_INVALID_PARAMETER
;
1654 DPRINT("Error, Status = %08X\n", Status
);
1655 LocalFree(Expanded
);
1656 return ERROR_INVALID_PARAMETER
;
1660 *RelativeName
= NULL
;
1661 return ERROR_INVALID_PARAMETER
;
1665 ScmCanonDriverImagePath(DWORD dwStartType
,
1666 const wchar_t *lpServiceName
,
1667 wchar_t **lpCanonName
)
1669 DWORD ServiceNameLen
, Result
;
1670 UNICODE_STRING NtServiceName
;
1671 WCHAR
*RelativeName
;
1672 const WCHAR
*SourceName
= lpServiceName
;
1674 /* Calculate the length of the service's name */
1675 ServiceNameLen
= wcslen(lpServiceName
);
1677 /* 12 is wcslen(L"\\SystemRoot\\") */
1678 if (ServiceNameLen
> 12 &&
1679 !_wcsnicmp(L
"\\SystemRoot\\", lpServiceName
, 12))
1681 /* SystemRoot prefix is already included */
1683 *lpCanonName
= LocalAlloc(LMEM_ZEROINIT
, ServiceNameLen
* sizeof(WCHAR
) + sizeof(WCHAR
));
1685 if (*lpCanonName
== NULL
)
1687 DPRINT("Error allocating memory for canonized service name!\n");
1688 return ERROR_NOT_ENOUGH_MEMORY
;
1691 /* If it's a boot-time driver, it must be systemroot relative */
1692 if (dwStartType
== SERVICE_BOOT_START
)
1696 wcscpy(*lpCanonName
, SourceName
);
1698 DPRINT("Canonicalized name %S\n", *lpCanonName
);
1702 /* Check if it has %SystemRoot% (len=13) */
1703 if (ServiceNameLen
> 13 &&
1704 !_wcsnicmp(L
"%%SystemRoot%%\\", lpServiceName
, 13))
1706 /* Substitute %SystemRoot% with \\SystemRoot\\ */
1707 *lpCanonName
= LocalAlloc(LMEM_ZEROINIT
, ServiceNameLen
* sizeof(WCHAR
) + sizeof(WCHAR
));
1709 if (*lpCanonName
== NULL
)
1711 DPRINT("Error allocating memory for canonized service name!\n");
1712 return ERROR_NOT_ENOUGH_MEMORY
;
1715 /* If it's a boot-time driver, it must be systemroot relative */
1716 if (dwStartType
== SERVICE_BOOT_START
)
1717 wcscpy(*lpCanonName
, L
"\\SystemRoot\\");
1719 wcscat(*lpCanonName
, lpServiceName
+ 13);
1721 DPRINT("Canonicalized name %S\n", *lpCanonName
);
1725 /* Check if it's a relative path name */
1726 if (lpServiceName
[0] != L
'\\' && lpServiceName
[1] != L
':')
1728 *lpCanonName
= LocalAlloc(LMEM_ZEROINIT
, ServiceNameLen
* sizeof(WCHAR
) + sizeof(WCHAR
));
1730 if (*lpCanonName
== NULL
)
1732 DPRINT("Error allocating memory for canonized service name!\n");
1733 return ERROR_NOT_ENOUGH_MEMORY
;
1736 /* Just copy it over without changing */
1737 wcscpy(*lpCanonName
, lpServiceName
);
1742 /* It seems to be a DOS path, convert it */
1743 if (!RtlDosPathNameToNtPathName_U(lpServiceName
, &NtServiceName
, NULL
, NULL
))
1745 DPRINT("RtlDosPathNameToNtPathName_U() failed!\n");
1746 return ERROR_INVALID_PARAMETER
;
1749 *lpCanonName
= LocalAlloc(LMEM_ZEROINIT
, NtServiceName
.Length
+ sizeof(WCHAR
));
1751 if (*lpCanonName
== NULL
)
1753 DPRINT("Error allocating memory for canonized service name!\n");
1754 RtlFreeUnicodeString(&NtServiceName
);
1755 return ERROR_NOT_ENOUGH_MEMORY
;
1758 /* Copy the string */
1759 wcsncpy(*lpCanonName
, NtServiceName
.Buffer
, NtServiceName
.Length
/ sizeof(WCHAR
));
1761 /* The unicode string is not needed anymore */
1762 RtlFreeUnicodeString(&NtServiceName
);
1764 if (dwStartType
!= SERVICE_BOOT_START
)
1766 DPRINT("Canonicalized name %S\n", *lpCanonName
);
1770 /* The service is boot-started, so must be relative */
1771 Result
= ScmConvertToBootPathName(*lpCanonName
, &RelativeName
);
1774 /* There is a problem, free name and return */
1775 LocalFree(*lpCanonName
);
1776 DPRINT("Error converting named!\n");
1780 ASSERT(RelativeName
);
1782 /* Copy that string */
1783 wcscpy(*lpCanonName
, RelativeName
+ 12);
1785 /* Free the allocated buffer */
1786 LocalFree(RelativeName
);
1788 DPRINT("Canonicalized name %S\n", *lpCanonName
);
1796 DWORD
RCreateServiceW(
1797 SC_RPC_HANDLE hSCManager
,
1798 LPCWSTR lpServiceName
,
1799 LPCWSTR lpDisplayName
,
1800 DWORD dwDesiredAccess
,
1801 DWORD dwServiceType
,
1803 DWORD dwErrorControl
,
1804 LPCWSTR lpBinaryPathName
,
1805 LPCWSTR lpLoadOrderGroup
,
1807 LPBYTE lpDependencies
,
1809 LPCWSTR lpServiceStartName
,
1812 LPSC_RPC_HANDLE lpServiceHandle
)
1814 PMANAGER_HANDLE hManager
;
1815 DWORD dwError
= ERROR_SUCCESS
;
1816 PSERVICE lpService
= NULL
;
1817 SC_HANDLE hServiceHandle
= NULL
;
1818 LPWSTR lpImagePath
= NULL
;
1819 HKEY hServiceKey
= NULL
;
1820 LPWSTR lpObjectName
;
1822 DPRINT("RCreateServiceW() called\n");
1823 DPRINT("lpServiceName = %S\n", lpServiceName
);
1824 DPRINT("lpDisplayName = %S\n", lpDisplayName
);
1825 DPRINT("dwDesiredAccess = %lx\n", dwDesiredAccess
);
1826 DPRINT("dwServiceType = %lu\n", dwServiceType
);
1827 DPRINT("dwStartType = %lu\n", dwStartType
);
1828 DPRINT("dwErrorControl = %lu\n", dwErrorControl
);
1829 DPRINT("lpBinaryPathName = %S\n", lpBinaryPathName
);
1830 DPRINT("lpLoadOrderGroup = %S\n", lpLoadOrderGroup
);
1833 return ERROR_SHUTDOWN_IN_PROGRESS
;
1835 hManager
= ScmGetServiceManagerFromHandle(hSCManager
);
1836 if (hManager
== NULL
)
1838 DPRINT1("Invalid service manager handle!\n");
1839 return ERROR_INVALID_HANDLE
;
1842 /* Check access rights */
1843 if (!RtlAreAllAccessesGranted(hManager
->Handle
.DesiredAccess
,
1844 SC_MANAGER_CREATE_SERVICE
))
1846 DPRINT("Insufficient access rights! 0x%lx\n",
1847 hManager
->Handle
.DesiredAccess
);
1848 return ERROR_ACCESS_DENIED
;
1851 if (wcslen(lpServiceName
) == 0)
1853 return ERROR_INVALID_NAME
;
1856 if (wcslen(lpBinaryPathName
) == 0)
1858 return ERROR_INVALID_PARAMETER
;
1861 if ((dwServiceType
== (SERVICE_WIN32_OWN_PROCESS
| SERVICE_INTERACTIVE_PROCESS
)) &&
1862 (lpServiceStartName
))
1864 return ERROR_INVALID_PARAMETER
;
1867 if ((dwServiceType
> SERVICE_WIN32_SHARE_PROCESS
) &&
1868 (dwServiceType
!= (SERVICE_WIN32_OWN_PROCESS
| SERVICE_INTERACTIVE_PROCESS
)) &&
1869 (dwServiceType
!= (SERVICE_WIN32_SHARE_PROCESS
| SERVICE_INTERACTIVE_PROCESS
)))
1871 return ERROR_INVALID_PARAMETER
;
1874 if (dwStartType
> SERVICE_DISABLED
)
1876 return ERROR_INVALID_PARAMETER
;
1879 lpService
= ScmGetServiceEntryByName(lpServiceName
);
1882 /* check if it is marked for deletion */
1883 if (lpService
->bDeleted
)
1884 return ERROR_SERVICE_MARKED_FOR_DELETE
;
1885 /* Return Error exist */
1886 return ERROR_SERVICE_EXISTS
;
1889 if (lpDisplayName
!= NULL
&&
1890 ScmGetServiceEntryByDisplayName(lpDisplayName
) != NULL
)
1891 return ERROR_DUPLICATE_SERVICE_NAME
;
1893 if (dwServiceType
& SERVICE_DRIVER
)
1895 dwError
= ScmCanonDriverImagePath(dwStartType
,
1898 if (dwError
!= ERROR_SUCCESS
)
1903 if (dwStartType
== SERVICE_BOOT_START
||
1904 dwStartType
== SERVICE_SYSTEM_START
)
1906 return ERROR_INVALID_PARAMETER
;
1910 /* Allocate a new service entry */
1911 dwError
= ScmCreateNewServiceRecord(lpServiceName
,
1913 if (dwError
!= ERROR_SUCCESS
)
1916 /* Fill the new service entry */
1917 lpService
->Status
.dwServiceType
= dwServiceType
;
1918 lpService
->dwStartType
= dwStartType
;
1919 lpService
->dwErrorControl
= dwErrorControl
;
1921 /* Fill the display name */
1922 if (lpDisplayName
!= NULL
&&
1923 *lpDisplayName
!= 0 &&
1924 _wcsicmp(lpService
->lpDisplayName
, lpDisplayName
) != 0)
1926 lpService
->lpDisplayName
= (WCHAR
*) HeapAlloc(GetProcessHeap(), 0,
1927 (wcslen(lpDisplayName
) + 1) * sizeof(WCHAR
));
1928 if (lpService
->lpDisplayName
== NULL
)
1930 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
1933 wcscpy(lpService
->lpDisplayName
, lpDisplayName
);
1936 /* Assign the service to a group */
1937 if (lpLoadOrderGroup
!= NULL
&& *lpLoadOrderGroup
!= 0)
1939 dwError
= ScmSetServiceGroup(lpService
,
1941 if (dwError
!= ERROR_SUCCESS
)
1945 /* Assign a new tag */
1946 if (lpdwTagId
!= NULL
)
1948 dwError
= ScmAssignNewTag(lpService
);
1949 if (dwError
!= ERROR_SUCCESS
)
1953 /* Write service data to the registry */
1954 /* Create the service key */
1955 dwError
= ScmCreateServiceKey(lpServiceName
,
1958 if (dwError
!= ERROR_SUCCESS
)
1961 /* Set the display name */
1962 if (lpDisplayName
!= NULL
&& *lpDisplayName
!= 0)
1964 RegSetValueExW(hServiceKey
,
1968 (LPBYTE
)lpDisplayName
,
1969 (wcslen(lpDisplayName
) + 1) * sizeof(WCHAR
));
1972 /* Set the service type */
1973 dwError
= RegSetValueExW(hServiceKey
,
1977 (LPBYTE
)&dwServiceType
,
1979 if (dwError
!= ERROR_SUCCESS
)
1982 /* Set the start value */
1983 dwError
= RegSetValueExW(hServiceKey
,
1987 (LPBYTE
)&dwStartType
,
1989 if (dwError
!= ERROR_SUCCESS
)
1992 /* Set the error control value */
1993 dwError
= RegSetValueExW(hServiceKey
,
1997 (LPBYTE
)&dwErrorControl
,
1999 if (dwError
!= ERROR_SUCCESS
)
2002 /* Set the image path */
2003 if (dwServiceType
& SERVICE_WIN32
)
2005 dwError
= RegSetValueExW(hServiceKey
,
2009 (LPBYTE
)lpBinaryPathName
,
2010 (wcslen(lpBinaryPathName
) + 1) * sizeof(WCHAR
));
2011 if (dwError
!= ERROR_SUCCESS
)
2014 else if (dwServiceType
& SERVICE_DRIVER
)
2016 dwError
= RegSetValueExW(hServiceKey
,
2020 (LPBYTE
)lpImagePath
,
2021 (wcslen(lpImagePath
) + 1) * sizeof(WCHAR
));
2022 if (dwError
!= ERROR_SUCCESS
)
2026 /* Set the group name */
2027 if (lpLoadOrderGroup
!= NULL
&& *lpLoadOrderGroup
!= 0)
2029 dwError
= RegSetValueExW(hServiceKey
,
2033 (LPBYTE
)lpLoadOrderGroup
,
2034 (wcslen(lpLoadOrderGroup
) + 1) * sizeof(WCHAR
));
2035 if (dwError
!= ERROR_SUCCESS
)
2039 if (lpdwTagId
!= NULL
)
2041 dwError
= RegSetValueExW(hServiceKey
,
2045 (LPBYTE
)&lpService
->dwTag
,
2047 if (dwError
!= ERROR_SUCCESS
)
2051 /* Write dependencies */
2052 if (lpDependencies
!= NULL
&& *lpDependencies
!= 0)
2054 dwError
= ScmWriteDependencies(hServiceKey
,
2055 (LPWSTR
)lpDependencies
,
2057 if (dwError
!= ERROR_SUCCESS
)
2061 /* Write service start name */
2062 if (dwServiceType
& SERVICE_WIN32
)
2064 lpObjectName
= (lpServiceStartName
!= NULL
) ? (LPWSTR
)lpServiceStartName
: L
"LocalSystem";
2065 dwError
= RegSetValueExW(hServiceKey
,
2069 (LPBYTE
)lpObjectName
,
2070 (wcslen(lpObjectName
) + 1) * sizeof(WCHAR
));
2071 if (dwError
!= ERROR_SUCCESS
)
2075 if (lpPassword
!= NULL
)
2077 /* FIXME: Write password */
2080 dwError
= ScmCreateServiceHandle(lpService
,
2082 if (dwError
!= ERROR_SUCCESS
)
2085 dwError
= ScmCheckAccess(hServiceHandle
,
2087 if (dwError
!= ERROR_SUCCESS
)
2090 lpService
->dwRefCount
= 1;
2091 DPRINT("CreateService - lpService->dwRefCount %u\n", lpService
->dwRefCount
);
2094 if (hServiceKey
!= NULL
)
2095 RegCloseKey(hServiceKey
);
2097 if (dwError
== ERROR_SUCCESS
)
2099 DPRINT("hService %p\n", hServiceHandle
);
2100 *lpServiceHandle
= (SC_RPC_HANDLE
)hServiceHandle
;
2102 if (lpdwTagId
!= NULL
)
2103 *lpdwTagId
= lpService
->dwTag
;
2107 /* Release the display name buffer */
2108 if (lpService
->lpServiceName
!= NULL
)
2109 HeapFree(GetProcessHeap(), 0, lpService
->lpDisplayName
);
2113 /* Remove the service handle */
2114 HeapFree(GetProcessHeap(), 0, hServiceHandle
);
2117 if (lpService
!= NULL
)
2119 /* FIXME: remove the service entry */
2123 if (lpImagePath
!= NULL
)
2124 HeapFree(GetProcessHeap(), 0, lpImagePath
);
2126 DPRINT("RCreateServiceW() done (Error %lu)\n", dwError
);
2133 DWORD
REnumDependentServicesW(
2134 SC_RPC_HANDLE hService
,
2135 DWORD dwServiceState
,
2138 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
2139 LPBOUNDED_DWORD_256K lpServicesReturned
)
2141 DWORD dwError
= ERROR_SUCCESS
;
2142 DWORD dwServicesReturned
= 0;
2143 DWORD dwServiceCount
;
2144 HKEY hServicesKey
= NULL
;
2145 PSERVICE_HANDLE hSvc
;
2146 PSERVICE lpService
= NULL
;
2147 PSERVICE
*lpServicesArray
= NULL
;
2148 LPENUM_SERVICE_STATUSW lpServicesPtr
= NULL
;
2151 *pcbBytesNeeded
= 0;
2152 *lpServicesReturned
= 0;
2154 DPRINT("REnumDependentServicesW() called\n");
2156 hSvc
= ScmGetServiceFromHandle(hService
);
2159 DPRINT1("Invalid service handle!\n");
2160 return ERROR_INVALID_HANDLE
;
2163 lpService
= hSvc
->ServiceEntry
;
2165 /* Check access rights */
2166 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
2167 SC_MANAGER_ENUMERATE_SERVICE
))
2169 DPRINT("Insufficient access rights! 0x%lx\n",
2170 hSvc
->Handle
.DesiredAccess
);
2171 return ERROR_ACCESS_DENIED
;
2174 /* Open the Services Reg key */
2175 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
2176 L
"System\\CurrentControlSet\\Services",
2180 if (dwError
!= ERROR_SUCCESS
)
2183 /* First determine the bytes needed and get the number of dependent services */
2184 dwError
= Int_EnumDependentServicesW(hServicesKey
,
2189 &dwServicesReturned
);
2190 if (dwError
!= ERROR_SUCCESS
)
2193 /* If buffer size is less than the bytes needed or pointer is null */
2194 if ((!lpServices
) || (cbBufSize
< *pcbBytesNeeded
))
2196 dwError
= ERROR_MORE_DATA
;
2200 /* Allocate memory for array of service pointers */
2201 lpServicesArray
= HeapAlloc(GetProcessHeap(),
2203 (dwServicesReturned
+ 1) * sizeof(PSERVICE
));
2204 if (!lpServicesArray
)
2206 DPRINT("Could not allocate a buffer!!\n");
2207 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
2211 dwServicesReturned
= 0;
2212 *pcbBytesNeeded
= 0;
2214 dwError
= Int_EnumDependentServicesW(hServicesKey
,
2219 &dwServicesReturned
);
2220 if (dwError
!= ERROR_SUCCESS
)
2225 lpServicesPtr
= (LPENUM_SERVICE_STATUSW
) lpServices
;
2226 lpStr
= (LPWSTR
)(lpServices
+ (dwServicesReturned
* sizeof(ENUM_SERVICE_STATUSW
)));
2228 /* Copy EnumDepenedentService to Buffer */
2229 for (dwServiceCount
= 0; dwServiceCount
< dwServicesReturned
; dwServiceCount
++)
2231 lpService
= lpServicesArray
[dwServiceCount
];
2233 /* Copy status info */
2234 memcpy(&lpServicesPtr
->ServiceStatus
,
2236 sizeof(SERVICE_STATUS
));
2238 /* Copy display name */
2239 wcscpy(lpStr
, lpService
->lpDisplayName
);
2240 lpServicesPtr
->lpDisplayName
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServices
);
2241 lpStr
+= (wcslen(lpService
->lpDisplayName
) + 1);
2243 /* Copy service name */
2244 wcscpy(lpStr
, lpService
->lpServiceName
);
2245 lpServicesPtr
->lpServiceName
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServices
);
2246 lpStr
+= (wcslen(lpService
->lpServiceName
) + 1);
2251 *lpServicesReturned
= dwServicesReturned
;
2254 if (lpServicesArray
!= NULL
)
2255 HeapFree(GetProcessHeap(), 0, lpServicesArray
);
2257 RegCloseKey(hServicesKey
);
2259 DPRINT("REnumDependentServicesW() done (Error %lu)\n", dwError
);
2266 DWORD
REnumServicesStatusW(
2267 SC_RPC_HANDLE hSCManager
,
2268 DWORD dwServiceType
,
2269 DWORD dwServiceState
,
2272 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
2273 LPBOUNDED_DWORD_256K lpServicesReturned
,
2274 LPBOUNDED_DWORD_256K lpResumeHandle
)
2276 PMANAGER_HANDLE hManager
;
2278 DWORD dwError
= ERROR_SUCCESS
;
2279 PLIST_ENTRY ServiceEntry
;
2280 PSERVICE CurrentService
;
2282 DWORD dwRequiredSize
;
2283 DWORD dwServiceCount
;
2285 DWORD dwLastResumeCount
= 0;
2286 LPENUM_SERVICE_STATUSW lpStatusPtr
;
2289 DPRINT("REnumServicesStatusW() called\n");
2292 return ERROR_SHUTDOWN_IN_PROGRESS
;
2294 hManager
= ScmGetServiceManagerFromHandle(hSCManager
);
2295 if (hManager
== NULL
)
2297 DPRINT1("Invalid service manager handle!\n");
2298 return ERROR_INVALID_HANDLE
;
2302 *pcbBytesNeeded
= 0;
2303 *lpServicesReturned
= 0;
2305 if ((dwServiceType
!=SERVICE_DRIVER
) && (dwServiceType
!=SERVICE_WIN32
))
2307 DPRINT("Not a valid Service Type!\n");
2308 return ERROR_INVALID_PARAMETER
;
2311 if ((dwServiceState
<SERVICE_ACTIVE
) || (dwServiceState
>SERVICE_STATE_ALL
))
2313 DPRINT("Not a valid Service State!\n");
2314 return ERROR_INVALID_PARAMETER
;
2317 /* Check access rights */
2318 if (!RtlAreAllAccessesGranted(hManager
->Handle
.DesiredAccess
,
2319 SC_MANAGER_ENUMERATE_SERVICE
))
2321 DPRINT("Insufficient access rights! 0x%lx\n",
2322 hManager
->Handle
.DesiredAccess
);
2323 return ERROR_ACCESS_DENIED
;
2327 dwLastResumeCount
= *lpResumeHandle
;
2329 /* FIXME: Lock the service list shared */
2331 lpService
= ScmGetServiceEntryByResumeCount(dwLastResumeCount
);
2332 if (lpService
== NULL
)
2334 dwError
= ERROR_SUCCESS
;
2341 for (ServiceEntry
= &lpService
->ServiceListEntry
;
2342 ServiceEntry
!= &ServiceListHead
;
2343 ServiceEntry
= ServiceEntry
->Flink
)
2345 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
2349 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
2352 dwState
= SERVICE_ACTIVE
;
2353 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
2354 dwState
= SERVICE_INACTIVE
;
2356 if ((dwState
& dwServiceState
) == 0)
2359 dwSize
= sizeof(ENUM_SERVICE_STATUSW
) +
2360 ((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
2361 ((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
2363 if (dwRequiredSize
+ dwSize
> dwBufSize
)
2365 DPRINT("Service name: %S no fit\n", CurrentService
->lpServiceName
);
2369 DPRINT("Service name: %S fit\n", CurrentService
->lpServiceName
);
2370 dwRequiredSize
+= dwSize
;
2372 dwLastResumeCount
= CurrentService
->dwResumeCount
;
2375 DPRINT("dwRequiredSize: %lu\n", dwRequiredSize
);
2376 DPRINT("dwServiceCount: %lu\n", dwServiceCount
);
2379 ServiceEntry
!= &ServiceListHead
;
2380 ServiceEntry
= ServiceEntry
->Flink
)
2382 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
2386 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
2389 dwState
= SERVICE_ACTIVE
;
2390 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
2391 dwState
= SERVICE_INACTIVE
;
2393 if ((dwState
& dwServiceState
) == 0)
2396 dwRequiredSize
+= (sizeof(ENUM_SERVICE_STATUSW
) +
2397 ((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
2398 ((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
)));
2400 dwError
= ERROR_MORE_DATA
;
2403 DPRINT("*pcbBytesNeeded: %lu\n", dwRequiredSize
);
2406 *lpResumeHandle
= dwLastResumeCount
;
2408 *lpServicesReturned
= dwServiceCount
;
2409 *pcbBytesNeeded
= dwRequiredSize
;
2411 lpStatusPtr
= (LPENUM_SERVICE_STATUSW
)lpBuffer
;
2412 lpStringPtr
= (LPWSTR
)((ULONG_PTR
)lpBuffer
+
2413 dwServiceCount
* sizeof(ENUM_SERVICE_STATUSW
));
2416 for (ServiceEntry
= &lpService
->ServiceListEntry
;
2417 ServiceEntry
!= &ServiceListHead
;
2418 ServiceEntry
= ServiceEntry
->Flink
)
2420 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
2424 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
2427 dwState
= SERVICE_ACTIVE
;
2428 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
2429 dwState
= SERVICE_INACTIVE
;
2431 if ((dwState
& dwServiceState
) == 0)
2434 dwSize
= sizeof(ENUM_SERVICE_STATUSW
) +
2435 ((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
2436 ((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
2438 if (dwRequiredSize
+ dwSize
> dwBufSize
)
2441 /* Copy the service name */
2442 wcscpy(lpStringPtr
, CurrentService
->lpServiceName
);
2443 lpStatusPtr
->lpServiceName
= (LPWSTR
)((ULONG_PTR
)lpStringPtr
- (ULONG_PTR
)lpBuffer
);
2444 lpStringPtr
+= (wcslen(CurrentService
->lpServiceName
) + 1);
2446 /* Copy the display name */
2447 wcscpy(lpStringPtr
, CurrentService
->lpDisplayName
);
2448 lpStatusPtr
->lpDisplayName
= (LPWSTR
)((ULONG_PTR
)lpStringPtr
- (ULONG_PTR
)lpBuffer
);
2449 lpStringPtr
+= (wcslen(CurrentService
->lpDisplayName
) + 1);
2451 /* Copy the status information */
2452 memcpy(&lpStatusPtr
->ServiceStatus
,
2453 &CurrentService
->Status
,
2454 sizeof(SERVICE_STATUS
));
2457 dwRequiredSize
+= dwSize
;
2462 *pcbBytesNeeded
= 0;
2463 if (lpResumeHandle
) *lpResumeHandle
= 0;
2467 /* FIXME: Unlock the service list */
2469 DPRINT("REnumServicesStatusW() done (Error %lu)\n", dwError
);
2476 DWORD
ROpenSCManagerW(
2477 LPWSTR lpMachineName
,
2478 LPWSTR lpDatabaseName
,
2479 DWORD dwDesiredAccess
,
2480 LPSC_RPC_HANDLE lpScHandle
)
2485 DPRINT("ROpenSCManagerW() called\n");
2486 DPRINT("lpMachineName = %p\n", lpMachineName
);
2487 DPRINT("lpMachineName: %S\n", lpMachineName
);
2488 DPRINT("lpDataBaseName = %p\n", lpDatabaseName
);
2489 DPRINT("lpDataBaseName: %S\n", lpDatabaseName
);
2490 DPRINT("dwDesiredAccess = %x\n", dwDesiredAccess
);
2493 return ERROR_SHUTDOWN_IN_PROGRESS
;
2496 return ERROR_INVALID_PARAMETER
;
2498 dwError
= ScmCreateManagerHandle(lpDatabaseName
,
2500 if (dwError
!= ERROR_SUCCESS
)
2502 DPRINT("ScmCreateManagerHandle() failed (Error %lu)\n", dwError
);
2506 /* Check the desired access */
2507 dwError
= ScmCheckAccess(hHandle
,
2508 dwDesiredAccess
| SC_MANAGER_CONNECT
);
2509 if (dwError
!= ERROR_SUCCESS
)
2511 DPRINT("ScmCheckAccess() failed (Error %lu)\n", dwError
);
2512 HeapFree(GetProcessHeap(), 0, hHandle
);
2516 *lpScHandle
= (SC_RPC_HANDLE
)hHandle
;
2517 DPRINT("*hScm = %p\n", *lpScHandle
);
2519 DPRINT("ROpenSCManagerW() done\n");
2521 return ERROR_SUCCESS
;
2526 DWORD
ROpenServiceW(
2527 SC_RPC_HANDLE hSCManager
,
2528 LPWSTR lpServiceName
,
2529 DWORD dwDesiredAccess
,
2530 LPSC_RPC_HANDLE lpServiceHandle
)
2533 PMANAGER_HANDLE hManager
;
2537 DPRINT("ROpenServiceW() called\n");
2538 DPRINT("hSCManager = %p\n", hSCManager
);
2539 DPRINT("lpServiceName = %p\n", lpServiceName
);
2540 DPRINT("lpServiceName: %S\n", lpServiceName
);
2541 DPRINT("dwDesiredAccess = %x\n", dwDesiredAccess
);
2544 return ERROR_SHUTDOWN_IN_PROGRESS
;
2546 hManager
= ScmGetServiceManagerFromHandle(hSCManager
);
2547 if (hManager
== NULL
)
2549 DPRINT1("Invalid service manager handle!\n");
2550 return ERROR_INVALID_HANDLE
;
2553 if (!lpServiceHandle
)
2554 return ERROR_INVALID_PARAMETER
;
2557 return ERROR_INVALID_ADDRESS
;
2559 /* FIXME: Lock the service list */
2561 /* Get service database entry */
2562 lpService
= ScmGetServiceEntryByName(lpServiceName
);
2563 if (lpService
== NULL
)
2565 DPRINT("Could not find a service!\n");
2566 return ERROR_SERVICE_DOES_NOT_EXIST
;
2569 /* Create a service handle */
2570 dwError
= ScmCreateServiceHandle(lpService
,
2572 if (dwError
!= ERROR_SUCCESS
)
2574 DPRINT("ScmCreateServiceHandle() failed (Error %lu)\n", dwError
);
2578 /* Check the desired access */
2579 dwError
= ScmCheckAccess(hHandle
,
2581 if (dwError
!= ERROR_SUCCESS
)
2583 DPRINT("ScmCheckAccess() failed (Error %lu)\n", dwError
);
2584 HeapFree(GetProcessHeap(), 0, hHandle
);
2588 lpService
->dwRefCount
++;
2589 DPRINT("OpenService - lpService->dwRefCount %u\n",lpService
->dwRefCount
);
2591 *lpServiceHandle
= (SC_RPC_HANDLE
)hHandle
;
2592 DPRINT("*hService = %p\n", *lpServiceHandle
);
2594 DPRINT("ROpenServiceW() done\n");
2596 return ERROR_SUCCESS
;
2601 DWORD
RQueryServiceConfigW(
2602 SC_RPC_HANDLE hService
,
2603 LPQUERY_SERVICE_CONFIGW lpServiceConfig
,
2605 LPBOUNDED_DWORD_8K pcbBytesNeeded
)
2607 DWORD dwError
= ERROR_SUCCESS
;
2608 PSERVICE_HANDLE hSvc
;
2609 PSERVICE lpService
= NULL
;
2610 HKEY hServiceKey
= NULL
;
2611 LPWSTR lpImagePath
= NULL
;
2612 LPWSTR lpServiceStartName
= NULL
;
2613 LPWSTR lpDependencies
= NULL
;
2614 DWORD dwDependenciesLength
= 0;
2615 DWORD dwRequiredSize
;
2616 LPQUERY_SERVICE_CONFIGW lpConfig
= NULL
;
2617 WCHAR lpEmptyString
[] = {0,0};
2620 DPRINT("RQueryServiceConfigW() called\n");
2623 return ERROR_SHUTDOWN_IN_PROGRESS
;
2625 hSvc
= ScmGetServiceFromHandle(hService
);
2628 DPRINT1("Invalid service handle!\n");
2629 return ERROR_INVALID_HANDLE
;
2632 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
2633 SERVICE_QUERY_CONFIG
))
2635 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
2636 return ERROR_ACCESS_DENIED
;
2639 lpService
= hSvc
->ServiceEntry
;
2640 if (lpService
== NULL
)
2642 DPRINT("lpService == NULL!\n");
2643 return ERROR_INVALID_HANDLE
;
2646 /* FIXME: Lock the service database shared */
2648 dwError
= ScmOpenServiceKey(lpService
->lpServiceName
,
2651 if (dwError
!= ERROR_SUCCESS
)
2654 /* Read the image path */
2655 dwError
= ScmReadString(hServiceKey
,
2658 if (dwError
!= ERROR_SUCCESS
)
2661 /* Read the service start name */
2662 ScmReadString(hServiceKey
,
2664 &lpServiceStartName
);
2666 /* Read the dependencies */
2667 ScmReadDependencies(hServiceKey
,
2669 &dwDependenciesLength
);
2671 dwRequiredSize
= sizeof(QUERY_SERVICE_CONFIGW
);
2673 if (lpImagePath
!= NULL
)
2674 dwRequiredSize
+= ((wcslen(lpImagePath
) + 1) * sizeof(WCHAR
));
2676 dwRequiredSize
+= 2 * sizeof(WCHAR
);
2678 if (lpService
->lpGroup
!= NULL
)
2679 dwRequiredSize
+= ((wcslen(lpService
->lpGroup
->lpGroupName
) + 1) * sizeof(WCHAR
));
2681 dwRequiredSize
+= 2 * sizeof(WCHAR
);
2683 if (lpDependencies
!= NULL
)
2684 dwRequiredSize
+= dwDependenciesLength
* sizeof(WCHAR
);
2686 dwRequiredSize
+= 2 * sizeof(WCHAR
);
2688 if (lpServiceStartName
!= NULL
)
2689 dwRequiredSize
+= ((wcslen(lpServiceStartName
) + 1) * sizeof(WCHAR
));
2691 dwRequiredSize
+= 2 * sizeof(WCHAR
);
2693 if (lpService
->lpDisplayName
!= NULL
)
2694 dwRequiredSize
+= ((wcslen(lpService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
2696 dwRequiredSize
+= 2 * sizeof(WCHAR
);
2698 if (lpServiceConfig
== NULL
|| cbBufSize
< dwRequiredSize
)
2700 dwError
= ERROR_INSUFFICIENT_BUFFER
;
2704 lpConfig
= (LPQUERY_SERVICE_CONFIGW
)lpServiceConfig
;
2705 lpConfig
->dwServiceType
= lpService
->Status
.dwServiceType
;
2706 lpConfig
->dwStartType
= lpService
->dwStartType
;
2707 lpConfig
->dwErrorControl
= lpService
->dwErrorControl
;
2708 lpConfig
->dwTagId
= lpService
->dwTag
;
2710 lpStr
= (LPWSTR
)(lpConfig
+ 1);
2712 /* Append the image path */
2713 if (lpImagePath
!= NULL
)
2715 wcscpy(lpStr
, lpImagePath
);
2719 wcscpy(lpStr
, lpEmptyString
);
2722 lpConfig
->lpBinaryPathName
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpConfig
);
2723 lpStr
+= (wcslen(lpStr
) + 1);
2725 /* Append the group name */
2726 if (lpService
->lpGroup
!= NULL
)
2728 wcscpy(lpStr
, lpService
->lpGroup
->lpGroupName
);
2732 wcscpy(lpStr
, lpEmptyString
);
2735 lpConfig
->lpLoadOrderGroup
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpConfig
);
2736 lpStr
+= (wcslen(lpStr
) + 1);
2738 /* Append Dependencies */
2739 if (lpDependencies
!= NULL
)
2743 dwDependenciesLength
* sizeof(WCHAR
));
2747 wcscpy(lpStr
, lpEmptyString
);
2750 lpConfig
->lpDependencies
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpConfig
);
2751 if (lpDependencies
!= NULL
)
2752 lpStr
+= dwDependenciesLength
* sizeof(WCHAR
);
2754 lpStr
+= (wcslen(lpStr
) + 1);
2756 /* Append the service start name */
2757 if (lpServiceStartName
!= NULL
)
2759 wcscpy(lpStr
, lpServiceStartName
);
2763 wcscpy(lpStr
, lpEmptyString
);
2766 lpConfig
->lpServiceStartName
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpConfig
);
2767 lpStr
+= (wcslen(lpStr
) + 1);
2769 /* Append the display name */
2770 if (lpService
->lpDisplayName
!= NULL
)
2772 wcscpy(lpStr
, lpService
->lpDisplayName
);
2776 wcscpy(lpStr
, lpEmptyString
);
2779 lpConfig
->lpDisplayName
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpConfig
);
2782 if (pcbBytesNeeded
!= NULL
)
2783 *pcbBytesNeeded
= dwRequiredSize
;
2786 if (lpImagePath
!= NULL
)
2787 HeapFree(GetProcessHeap(), 0, lpImagePath
);
2789 if (lpServiceStartName
!= NULL
)
2790 HeapFree(GetProcessHeap(), 0, lpServiceStartName
);
2792 if (lpDependencies
!= NULL
)
2793 HeapFree(GetProcessHeap(), 0, lpDependencies
);
2795 if (hServiceKey
!= NULL
)
2796 RegCloseKey(hServiceKey
);
2798 /* FIXME: Unlock the service database */
2800 DPRINT("RQueryServiceConfigW() done\n");
2807 DWORD
RQueryServiceLockStatusW(
2808 SC_RPC_HANDLE hSCManager
,
2809 LPQUERY_SERVICE_LOCK_STATUSW lpLockStatus
,
2811 LPBOUNDED_DWORD_4K pcbBytesNeeded
)
2814 return ERROR_CALL_NOT_IMPLEMENTED
;
2819 DWORD
RStartServiceW(
2820 SC_RPC_HANDLE hService
,
2822 LPSTRING_PTRSW argv
)
2824 DWORD dwError
= ERROR_SUCCESS
;
2825 PSERVICE_HANDLE hSvc
;
2826 PSERVICE lpService
= NULL
;
2828 DPRINT("RStartServiceW() called\n");
2831 return ERROR_SHUTDOWN_IN_PROGRESS
;
2833 hSvc
= ScmGetServiceFromHandle(hService
);
2836 DPRINT1("Invalid service handle!\n");
2837 return ERROR_INVALID_HANDLE
;
2840 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
2843 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
2844 return ERROR_ACCESS_DENIED
;
2847 lpService
= hSvc
->ServiceEntry
;
2848 if (lpService
== NULL
)
2850 DPRINT("lpService == NULL!\n");
2851 return ERROR_INVALID_HANDLE
;
2854 if (lpService
->dwStartType
== SERVICE_DISABLED
)
2855 return ERROR_SERVICE_DISABLED
;
2857 if (lpService
->bDeleted
)
2858 return ERROR_SERVICE_MARKED_FOR_DELETE
;
2865 /* Start the service */
2866 dwError
= ScmStartService(lpService
, argc
, (LPWSTR
*)argv
);
2873 DWORD
RGetServiceDisplayNameW(
2874 SC_RPC_HANDLE hSCManager
,
2875 LPCWSTR lpServiceName
,
2876 LPWSTR lpDisplayName
,
2879 // PMANAGER_HANDLE hManager;
2884 DPRINT("RGetServiceDisplayNameW() called\n");
2885 DPRINT("hSCManager = %p\n", hSCManager
);
2886 DPRINT("lpServiceName: %S\n", lpServiceName
);
2887 DPRINT("lpDisplayName: %p\n", lpDisplayName
);
2888 DPRINT("*lpcchBuffer: %lu\n", *lpcchBuffer
);
2890 // hManager = (PMANAGER_HANDLE)hSCManager;
2891 // if (hManager->Handle.Tag != MANAGER_TAG)
2893 // DPRINT("Invalid manager handle!\n");
2894 // return ERROR_INVALID_HANDLE;
2897 /* Get service database entry */
2898 lpService
= ScmGetServiceEntryByName(lpServiceName
);
2899 if (lpService
== NULL
)
2901 DPRINT("Could not find a service!\n");
2903 /* If the service could not be found and lpcchBuffer is less than 2, windows
2904 puts null in lpDisplayName and puts 2 in lpcchBuffer */
2905 if (*lpcchBuffer
< 2)
2908 if (lpDisplayName
!= NULL
)
2910 *lpDisplayName
= '\0';
2914 return ERROR_SERVICE_DOES_NOT_EXIST
;
2917 if (!lpService
->lpDisplayName
)
2919 dwLength
= wcslen(lpService
->lpServiceName
);
2921 if (lpDisplayName
!= NULL
&&
2922 *lpcchBuffer
> dwLength
)
2924 wcscpy(lpDisplayName
, lpService
->lpServiceName
);
2929 dwLength
= wcslen(lpService
->lpDisplayName
);
2931 if (lpDisplayName
!= NULL
&&
2932 *lpcchBuffer
> dwLength
)
2934 wcscpy(lpDisplayName
, lpService
->lpDisplayName
);
2938 dwError
= (*lpcchBuffer
> dwLength
) ? ERROR_SUCCESS
: ERROR_INSUFFICIENT_BUFFER
;
2940 *lpcchBuffer
= dwLength
;
2947 DWORD
RGetServiceKeyNameW(
2948 SC_RPC_HANDLE hSCManager
,
2949 LPCWSTR lpDisplayName
,
2950 LPWSTR lpServiceName
,
2953 // PMANAGER_HANDLE hManager;
2958 DPRINT("RGetServiceKeyNameW() called\n");
2959 DPRINT("hSCManager = %p\n", hSCManager
);
2960 DPRINT("lpDisplayName: %S\n", lpDisplayName
);
2961 DPRINT("lpServiceName: %p\n", lpServiceName
);
2962 DPRINT("*lpcchBuffer: %lu\n", *lpcchBuffer
);
2964 // hManager = (PMANAGER_HANDLE)hSCManager;
2965 // if (hManager->Handle.Tag != MANAGER_TAG)
2967 // DPRINT("Invalid manager handle!\n");
2968 // return ERROR_INVALID_HANDLE;
2971 /* Get service database entry */
2972 lpService
= ScmGetServiceEntryByDisplayName(lpDisplayName
);
2973 if (lpService
== NULL
)
2975 DPRINT("Could not find a service!\n");
2977 /* If the service could not be found and lpcchBuffer is less than 2, windows
2978 puts null in lpDisplayName and puts 2 in lpcchBuffer */
2979 if (*lpcchBuffer
< 2)
2982 if (lpServiceName
!= NULL
)
2984 *lpServiceName
= '\0';
2988 return ERROR_SERVICE_DOES_NOT_EXIST
;
2991 dwLength
= wcslen(lpService
->lpServiceName
);
2993 if (lpServiceName
!= NULL
&&
2994 *lpcchBuffer
> dwLength
)
2996 wcscpy(lpServiceName
, lpService
->lpServiceName
);
2997 *lpcchBuffer
= dwLength
;
2998 return ERROR_SUCCESS
;
3001 dwError
= (*lpcchBuffer
> dwLength
) ? ERROR_SUCCESS
: ERROR_INSUFFICIENT_BUFFER
;
3003 *lpcchBuffer
= dwLength
;
3010 DWORD
RI_ScSetServiceBitsA(
3011 RPC_SERVICE_STATUS_HANDLE hServiceStatus
,
3012 DWORD dwServiceBits
,
3014 int bUpdateImmediately
,
3018 return ERROR_CALL_NOT_IMPLEMENTED
;
3023 DWORD
RChangeServiceConfigA(
3024 SC_RPC_HANDLE hService
,
3025 DWORD dwServiceType
,
3027 DWORD dwErrorControl
,
3028 LPSTR lpBinaryPathName
,
3029 LPSTR lpLoadOrderGroup
,
3031 LPSTR lpDependencies
,
3033 LPSTR lpServiceStartName
,
3036 LPSTR lpDisplayName
)
3038 DWORD dwError
= ERROR_SUCCESS
;
3039 PSERVICE_HANDLE hSvc
;
3040 PSERVICE lpService
= NULL
;
3041 HKEY hServiceKey
= NULL
;
3042 LPWSTR lpDisplayNameW
= NULL
;
3043 // LPWSTR lpBinaryPathNameW = NULL;
3044 LPWSTR lpLoadOrderGroupW
= NULL
;
3045 LPWSTR lpDependenciesW
= NULL
;
3046 // LPWSTR lpPasswordW = NULL;
3048 DPRINT("RChangeServiceConfigA() called\n");
3049 DPRINT("dwServiceType = %lu\n", dwServiceType
);
3050 DPRINT("dwStartType = %lu\n", dwStartType
);
3051 DPRINT("dwErrorControl = %lu\n", dwErrorControl
);
3052 DPRINT("lpBinaryPathName = %s\n", lpBinaryPathName
);
3053 DPRINT("lpLoadOrderGroup = %s\n", lpLoadOrderGroup
);
3054 DPRINT("lpDisplayName = %s\n", lpDisplayName
);
3057 return ERROR_SHUTDOWN_IN_PROGRESS
;
3059 hSvc
= ScmGetServiceFromHandle(hService
);
3062 DPRINT1("Invalid service handle!\n");
3063 return ERROR_INVALID_HANDLE
;
3066 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
3067 SERVICE_CHANGE_CONFIG
))
3069 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
3070 return ERROR_ACCESS_DENIED
;
3073 lpService
= hSvc
->ServiceEntry
;
3074 if (lpService
== NULL
)
3076 DPRINT("lpService == NULL!\n");
3077 return ERROR_INVALID_HANDLE
;
3080 /* FIXME: Lock database exclusively */
3082 if (lpService
->bDeleted
)
3084 /* FIXME: Unlock database */
3085 DPRINT("The service has already been marked for delete!\n");
3086 return ERROR_SERVICE_MARKED_FOR_DELETE
;
3089 /* Open the service key */
3090 dwError
= ScmOpenServiceKey(lpService
->szServiceName
,
3093 if (dwError
!= ERROR_SUCCESS
)
3096 /* Write service data to the registry */
3098 if (lpDisplayName
!= NULL
&& *lpDisplayName
!= 0)
3100 /* Set the display name */
3101 lpDisplayNameW
= HeapAlloc(GetProcessHeap(),
3103 (strlen(lpDisplayName
) + 1) * sizeof(WCHAR
));
3104 if (lpDisplayNameW
== NULL
)
3106 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
3110 MultiByteToWideChar(CP_ACP
,
3115 strlen(lpDisplayName
) + 1);
3117 RegSetValueExW(hServiceKey
,
3121 (LPBYTE
)lpDisplayNameW
,
3122 (wcslen(lpDisplayNameW
) + 1) * sizeof(WCHAR
));
3124 /* Update lpService->lpDisplayName */
3125 if (lpService
->lpDisplayName
)
3126 HeapFree(GetProcessHeap(), 0, lpService
->lpDisplayName
);
3128 lpService
->lpDisplayName
= lpDisplayNameW
;
3131 if (dwServiceType
!= SERVICE_NO_CHANGE
)
3133 /* Set the service type */
3134 dwError
= RegSetValueExW(hServiceKey
,
3138 (LPBYTE
)&dwServiceType
,
3140 if (dwError
!= ERROR_SUCCESS
)
3143 lpService
->Status
.dwServiceType
= dwServiceType
;
3146 if (dwStartType
!= SERVICE_NO_CHANGE
)
3148 /* Set the start value */
3149 dwError
= RegSetValueExW(hServiceKey
,
3153 (LPBYTE
)&dwStartType
,
3155 if (dwError
!= ERROR_SUCCESS
)
3158 lpService
->dwStartType
= dwStartType
;
3161 if (dwErrorControl
!= SERVICE_NO_CHANGE
)
3163 /* Set the error control value */
3164 dwError
= RegSetValueExW(hServiceKey
,
3168 (LPBYTE
)&dwErrorControl
,
3170 if (dwError
!= ERROR_SUCCESS
)
3173 lpService
->dwErrorControl
= dwErrorControl
;
3177 /* FIXME: set the new ImagePath value */
3179 /* Set the image path */
3180 if (dwServiceType
& SERVICE_WIN32
)
3182 if (lpBinaryPathName
!= NULL
&& *lpBinaryPathName
!= 0)
3184 lpBinaryPathNameW
=HeapAlloc(GetProcessHeap(),0, (strlen(lpBinaryPathName
)+1) * sizeof(WCHAR
));
3185 MultiByteToWideChar(CP_ACP
, 0, lpBinaryPathName
, -1, lpBinaryPathNameW
, strlen(lpBinaryPathName
)+1);
3186 dwError
= RegSetValueExW(hServiceKey
,
3190 (LPBYTE
)lpBinaryPathNameW
,
3191 (wcslen(lpBinaryPathNameW
) + 1) * sizeof(WCHAR
));
3192 if (dwError
!= ERROR_SUCCESS
)
3196 else if (dwServiceType
& SERVICE_DRIVER
)
3198 if (lpImagePath
!= NULL
&& *lpImagePath
!= 0)
3200 dwError
= RegSetValueExW(hServiceKey
,
3204 (LPBYTE
)lpImagePath
,
3205 (wcslen(lpImagePath
) + 1) *sizeof(WCHAR
));
3206 if (dwError
!= ERROR_SUCCESS
)
3212 /* Set the group name */
3213 if (lpLoadOrderGroup
!= NULL
&& *lpLoadOrderGroup
!= 0)
3215 lpLoadOrderGroupW
= HeapAlloc(GetProcessHeap(),
3217 (strlen(lpLoadOrderGroup
) + 1) * sizeof(WCHAR
));
3218 if (lpLoadOrderGroupW
== NULL
)
3220 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
3224 MultiByteToWideChar(CP_ACP
,
3229 strlen(lpLoadOrderGroup
) + 1);
3231 dwError
= RegSetValueExW(hServiceKey
,
3235 (LPBYTE
)lpLoadOrderGroupW
,
3236 (wcslen(lpLoadOrderGroupW
) + 1) * sizeof(WCHAR
));
3237 if (dwError
!= ERROR_SUCCESS
)
3239 HeapFree(GetProcessHeap(), 0, lpLoadOrderGroupW
);
3243 dwError
= ScmSetServiceGroup(lpService
,
3246 HeapFree(GetProcessHeap(), 0, lpLoadOrderGroupW
);
3248 if (dwError
!= ERROR_SUCCESS
)
3252 if (lpdwTagId
!= NULL
)
3254 dwError
= ScmAssignNewTag(lpService
);
3255 if (dwError
!= ERROR_SUCCESS
)
3258 dwError
= RegSetValueExW(hServiceKey
,
3262 (LPBYTE
)&lpService
->dwTag
,
3264 if (dwError
!= ERROR_SUCCESS
)
3267 *lpdwTagId
= lpService
->dwTag
;
3270 /* Write dependencies */
3271 if (lpDependencies
!= NULL
&& *lpDependencies
!= 0)
3273 lpDependenciesW
= HeapAlloc(GetProcessHeap(),
3275 (strlen(lpDependencies
) + 1) * sizeof(WCHAR
));
3276 if (lpDependenciesW
== NULL
)
3278 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
3282 MultiByteToWideChar(CP_ACP
,
3287 strlen(lpDependencies
) + 1);
3289 dwError
= ScmWriteDependencies(hServiceKey
,
3290 (LPWSTR
)lpDependenciesW
,
3293 HeapFree(GetProcessHeap(), 0, lpDependenciesW
);
3296 if (lpPassword
!= NULL
)
3298 /* FIXME: Write password */
3301 /* FIXME: Unlock database */
3304 if (hServiceKey
!= NULL
)
3305 RegCloseKey(hServiceKey
);
3307 DPRINT("RChangeServiceConfigA() done (Error %lu)\n", dwError
);
3314 DWORD
RCreateServiceA(
3315 SC_RPC_HANDLE hSCManager
,
3316 LPSTR lpServiceName
,
3317 LPSTR lpDisplayName
,
3318 DWORD dwDesiredAccess
,
3319 DWORD dwServiceType
,
3321 DWORD dwErrorControl
,
3322 LPSTR lpBinaryPathName
,
3323 LPSTR lpLoadOrderGroup
,
3325 LPBYTE lpDependencies
,
3327 LPSTR lpServiceStartName
,
3330 LPSC_RPC_HANDLE lpServiceHandle
)
3332 DWORD dwError
= ERROR_SUCCESS
;
3333 LPWSTR lpServiceNameW
= NULL
;
3334 LPWSTR lpDisplayNameW
= NULL
;
3335 LPWSTR lpBinaryPathNameW
= NULL
;
3336 LPWSTR lpLoadOrderGroupW
= NULL
;
3337 LPWSTR lpDependenciesW
= NULL
;
3338 LPWSTR lpServiceStartNameW
= NULL
;
3339 DWORD dwDependenciesLength
= 0;
3346 len
= MultiByteToWideChar(CP_ACP
, 0, lpServiceName
, -1, NULL
, 0);
3347 lpServiceNameW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
3348 if (!lpServiceNameW
)
3350 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3353 MultiByteToWideChar(CP_ACP
, 0, lpServiceName
, -1, lpServiceNameW
, len
);
3358 len
= MultiByteToWideChar(CP_ACP
, 0, lpDisplayName
, -1, NULL
, 0);
3359 lpDisplayNameW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
3360 if (!lpDisplayNameW
)
3362 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3365 MultiByteToWideChar(CP_ACP
, 0, lpDisplayName
, -1, lpDisplayNameW
, len
);
3368 if (lpBinaryPathName
)
3370 len
= MultiByteToWideChar(CP_ACP
, 0, lpBinaryPathName
, -1, NULL
, 0);
3371 lpBinaryPathNameW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
3372 if (!lpBinaryPathNameW
)
3374 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3377 MultiByteToWideChar(CP_ACP
, 0, lpBinaryPathName
, -1, lpBinaryPathNameW
, len
);
3380 if (lpLoadOrderGroup
)
3382 len
= MultiByteToWideChar(CP_ACP
, 0, lpLoadOrderGroup
, -1, NULL
, 0);
3383 lpLoadOrderGroupW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
3384 if (!lpLoadOrderGroupW
)
3386 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3389 MultiByteToWideChar(CP_ACP
, 0, lpLoadOrderGroup
, -1, lpLoadOrderGroupW
, len
);
3394 lpStr
= (LPSTR
)lpDependencies
;
3397 dwLength
= strlen(lpStr
) + 1;
3398 dwDependenciesLength
+= dwLength
;
3399 lpStr
= lpStr
+ dwLength
;
3401 dwDependenciesLength
++;
3403 lpDependenciesW
= HeapAlloc(GetProcessHeap(), 0, dwDependenciesLength
* sizeof(WCHAR
));
3404 if (!lpDependenciesW
)
3406 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3409 MultiByteToWideChar(CP_ACP
, 0, (LPSTR
)lpDependencies
, dwDependenciesLength
, lpDependenciesW
, dwDependenciesLength
);
3412 if (lpServiceStartName
)
3414 len
= MultiByteToWideChar(CP_ACP
, 0, lpServiceStartName
, -1, NULL
, 0);
3415 lpServiceStartNameW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
3416 if (!lpServiceStartNameW
)
3418 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
3421 MultiByteToWideChar(CP_ACP
, 0, lpServiceStartName
, -1, lpServiceStartNameW
, len
);
3424 dwError
= RCreateServiceW(hSCManager
,
3434 (LPBYTE
)lpDependenciesW
,
3435 dwDependenciesLength
,
3436 lpServiceStartNameW
,
3442 if (lpServiceNameW
!=NULL
)
3443 HeapFree(GetProcessHeap(), 0, lpServiceNameW
);
3445 if (lpDisplayNameW
!= NULL
)
3446 HeapFree(GetProcessHeap(), 0, lpDisplayNameW
);
3448 if (lpBinaryPathNameW
!= NULL
)
3449 HeapFree(GetProcessHeap(), 0, lpBinaryPathNameW
);
3451 if (lpLoadOrderGroupW
!= NULL
)
3452 HeapFree(GetProcessHeap(), 0, lpLoadOrderGroupW
);
3454 if (lpDependenciesW
!= NULL
)
3455 HeapFree(GetProcessHeap(), 0, lpDependenciesW
);
3457 if (lpServiceStartNameW
!= NULL
)
3458 HeapFree(GetProcessHeap(), 0, lpServiceStartNameW
);
3465 DWORD
REnumDependentServicesA(
3466 SC_RPC_HANDLE hService
,
3467 DWORD dwServiceState
,
3470 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
3471 LPBOUNDED_DWORD_256K lpServicesReturned
)
3473 DWORD dwError
= ERROR_SUCCESS
;
3474 DWORD dwServicesReturned
= 0;
3475 DWORD dwServiceCount
;
3476 HKEY hServicesKey
= NULL
;
3477 PSERVICE_HANDLE hSvc
;
3478 PSERVICE lpService
= NULL
;
3479 PSERVICE
*lpServicesArray
= NULL
;
3480 LPENUM_SERVICE_STATUSA lpServicesPtr
= NULL
;
3483 *pcbBytesNeeded
= 0;
3484 *lpServicesReturned
= 0;
3486 DPRINT("REnumDependentServicesA() called\n");
3488 hSvc
= ScmGetServiceFromHandle(hService
);
3491 DPRINT1("Invalid service handle!\n");
3492 return ERROR_INVALID_HANDLE
;
3495 lpService
= hSvc
->ServiceEntry
;
3497 /* Check access rights */
3498 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
3499 SC_MANAGER_ENUMERATE_SERVICE
))
3501 DPRINT("Insufficient access rights! 0x%lx\n",
3502 hSvc
->Handle
.DesiredAccess
);
3503 return ERROR_ACCESS_DENIED
;
3506 /* Open the Services Reg key */
3507 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
3508 L
"System\\CurrentControlSet\\Services",
3513 if (dwError
!= ERROR_SUCCESS
)
3516 /* NOTE: Windows calculates the pcbBytesNeeded based on WCHAR strings for
3517 both EnumDependentServicesA and EnumDependentServicesW. So returned pcbBytesNeeded
3518 are the same for both. Verified in WINXP. */
3520 /* First determine the bytes needed and get the number of dependent services*/
3521 dwError
= Int_EnumDependentServicesW(hServicesKey
,
3526 &dwServicesReturned
);
3527 if (dwError
!= ERROR_SUCCESS
)
3530 /* If buffer size is less than the bytes needed or pointer is null*/
3531 if ((!lpServices
) || (cbBufSize
< *pcbBytesNeeded
))
3533 dwError
= ERROR_MORE_DATA
;
3537 /* Allocate memory for array of service pointers */
3538 lpServicesArray
= HeapAlloc(GetProcessHeap(),
3540 (dwServicesReturned
+ 1) * sizeof(PSERVICE
));
3541 if (!lpServicesArray
)
3543 DPRINT("Could not allocate a buffer!!\n");
3544 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
3548 dwServicesReturned
= 0;
3549 *pcbBytesNeeded
= 0;
3551 dwError
= Int_EnumDependentServicesW(hServicesKey
,
3556 &dwServicesReturned
);
3557 if (dwError
!= ERROR_SUCCESS
)
3562 lpServicesPtr
= (LPENUM_SERVICE_STATUSA
)lpServices
;
3563 lpStr
= (LPSTR
)(lpServices
+ (dwServicesReturned
* sizeof(ENUM_SERVICE_STATUSA
)));
3565 /* Copy EnumDepenedentService to Buffer */
3566 for (dwServiceCount
= 0; dwServiceCount
< dwServicesReturned
; dwServiceCount
++)
3568 lpService
= lpServicesArray
[dwServiceCount
];
3570 /* Copy the status info */
3571 memcpy(&lpServicesPtr
->ServiceStatus
,
3573 sizeof(SERVICE_STATUS
));
3575 /* Copy display name */
3576 WideCharToMultiByte(CP_ACP
,
3578 lpService
->lpDisplayName
,
3581 wcslen(lpService
->lpDisplayName
),
3584 lpServicesPtr
->lpDisplayName
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServices
);
3585 lpStr
+= strlen(lpStr
) + 1;
3587 /* Copy service name */
3588 WideCharToMultiByte(CP_ACP
,
3590 lpService
->lpServiceName
,
3593 wcslen(lpService
->lpServiceName
),
3596 lpServicesPtr
->lpServiceName
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServices
);
3597 lpStr
+= strlen(lpStr
) + 1;
3602 *lpServicesReturned
= dwServicesReturned
;
3605 if (lpServicesArray
)
3606 HeapFree(GetProcessHeap(), 0, lpServicesArray
);
3608 RegCloseKey(hServicesKey
);
3610 DPRINT("REnumDependentServicesA() done (Error %lu)\n", dwError
);
3617 DWORD
REnumServicesStatusA(
3618 SC_RPC_HANDLE hSCManager
,
3619 DWORD dwServiceType
,
3620 DWORD dwServiceState
,
3623 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
3624 LPBOUNDED_DWORD_256K lpServicesReturned
,
3625 LPBOUNDED_DWORD_256K lpResumeHandle
)
3627 LPENUM_SERVICE_STATUSW lpStatusPtrW
= NULL
;
3628 LPENUM_SERVICE_STATUSA lpStatusPtrA
= NULL
;
3629 LPWSTR lpStringPtrW
;
3632 DWORD dwServiceCount
;
3634 DPRINT("REnumServicesStatusA() called\n");
3636 if ((dwBufSize
> 0) && (lpBuffer
))
3638 lpStatusPtrW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, dwBufSize
);
3641 DPRINT("Failed to allocate buffer!\n");
3642 return ERROR_NOT_ENOUGH_MEMORY
;
3646 dwError
= REnumServicesStatusW(hSCManager
,
3649 (LPBYTE
)lpStatusPtrW
,
3655 /* if no services were returned then we are Done */
3656 if (*lpServicesReturned
== 0)
3659 lpStatusPtrA
= (LPENUM_SERVICE_STATUSA
)lpBuffer
;
3660 lpStringPtrA
= (LPSTR
)((ULONG_PTR
)lpBuffer
+
3661 *lpServicesReturned
* sizeof(ENUM_SERVICE_STATUSA
));
3662 lpStringPtrW
= (LPWSTR
)((ULONG_PTR
)lpStatusPtrW
+
3663 *lpServicesReturned
* sizeof(ENUM_SERVICE_STATUSW
));
3665 for (dwServiceCount
= 0; dwServiceCount
< *lpServicesReturned
; dwServiceCount
++)
3667 /* Copy the service name */
3668 WideCharToMultiByte(CP_ACP
,
3673 wcslen(lpStringPtrW
),
3677 lpStatusPtrA
->lpServiceName
= (LPSTR
)((ULONG_PTR
)lpStringPtrA
- (ULONG_PTR
)lpBuffer
);
3678 lpStringPtrA
+= wcslen(lpStringPtrW
) + 1;
3679 lpStringPtrW
+= wcslen(lpStringPtrW
) + 1;
3681 /* Copy the display name */
3682 WideCharToMultiByte(CP_ACP
,
3687 wcslen(lpStringPtrW
),
3691 lpStatusPtrA
->lpDisplayName
= (LPSTR
)((ULONG_PTR
)lpStringPtrA
- (ULONG_PTR
)lpBuffer
);
3692 lpStringPtrA
+= wcslen(lpStringPtrW
) + 1;
3693 lpStringPtrW
+= wcslen(lpStringPtrW
) + 1;
3695 /* Copy the status information */
3696 memcpy(&lpStatusPtrA
->ServiceStatus
,
3697 &lpStatusPtrW
->ServiceStatus
,
3698 sizeof(SERVICE_STATUS
));
3705 HeapFree(GetProcessHeap(), 0, lpStatusPtrW
);
3707 DPRINT("REnumServicesStatusA() done (Error %lu)\n", dwError
);
3714 DWORD
ROpenSCManagerA(
3715 LPSTR lpMachineName
,
3716 LPSTR lpDatabaseName
,
3717 DWORD dwDesiredAccess
,
3718 LPSC_RPC_HANDLE lpScHandle
)
3720 UNICODE_STRING MachineName
;
3721 UNICODE_STRING DatabaseName
;
3724 DPRINT("ROpenSCManagerA() called\n");
3727 RtlCreateUnicodeStringFromAsciiz(&MachineName
,
3731 RtlCreateUnicodeStringFromAsciiz(&DatabaseName
,
3734 dwError
= ROpenSCManagerW(lpMachineName
? MachineName
.Buffer
: NULL
,
3735 lpDatabaseName
? DatabaseName
.Buffer
: NULL
,
3740 RtlFreeUnicodeString(&MachineName
);
3743 RtlFreeUnicodeString(&DatabaseName
);
3750 DWORD
ROpenServiceA(
3751 SC_RPC_HANDLE hSCManager
,
3752 LPSTR lpServiceName
,
3753 DWORD dwDesiredAccess
,
3754 LPSC_RPC_HANDLE lpServiceHandle
)
3756 UNICODE_STRING ServiceName
;
3759 DPRINT("ROpenServiceA() called\n");
3762 RtlCreateUnicodeStringFromAsciiz(&ServiceName
,
3765 dwError
= ROpenServiceW(hSCManager
,
3766 lpServiceName
? ServiceName
.Buffer
: NULL
,
3771 RtlFreeUnicodeString(&ServiceName
);
3778 DWORD
RQueryServiceConfigA(
3779 SC_RPC_HANDLE hService
,
3780 LPQUERY_SERVICE_CONFIGA lpServiceConfig
,
3782 LPBOUNDED_DWORD_8K pcbBytesNeeded
)
3784 DWORD dwError
= ERROR_SUCCESS
;
3785 PSERVICE_HANDLE hSvc
;
3786 PSERVICE lpService
= NULL
;
3787 HKEY hServiceKey
= NULL
;
3788 LPWSTR lpImagePath
= NULL
;
3789 LPWSTR lpServiceStartName
= NULL
;
3790 LPWSTR lpDependencies
= NULL
;
3791 DWORD dwDependenciesLength
= 0;
3792 DWORD dwRequiredSize
;
3793 LPQUERY_SERVICE_CONFIGA lpConfig
= NULL
;
3794 CHAR lpEmptyString
[]={0,0};
3797 DPRINT("RQueryServiceConfigA() called\n");
3800 return ERROR_SHUTDOWN_IN_PROGRESS
;
3802 hSvc
= ScmGetServiceFromHandle(hService
);
3805 DPRINT1("Invalid service handle!\n");
3806 return ERROR_INVALID_HANDLE
;
3809 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
3810 SERVICE_QUERY_CONFIG
))
3812 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
3813 return ERROR_ACCESS_DENIED
;
3816 lpService
= hSvc
->ServiceEntry
;
3817 if (lpService
== NULL
)
3819 DPRINT("lpService == NULL!\n");
3820 return ERROR_INVALID_HANDLE
;
3823 /* FIXME: Lock the service database shared */
3825 dwError
= ScmOpenServiceKey(lpService
->lpServiceName
,
3828 if (dwError
!= ERROR_SUCCESS
)
3831 /* Read the image path */
3832 dwError
= ScmReadString(hServiceKey
,
3835 if (dwError
!= ERROR_SUCCESS
)
3838 /* Read the service start name */
3839 ScmReadString(hServiceKey
,
3841 &lpServiceStartName
);
3843 /* Read the dependencies */
3844 ScmReadDependencies(hServiceKey
,
3846 &dwDependenciesLength
);
3848 dwRequiredSize
= sizeof(QUERY_SERVICE_CONFIGW
);
3850 if (lpImagePath
!= NULL
)
3851 dwRequiredSize
+= wcslen(lpImagePath
) + 1;
3853 dwRequiredSize
+= 2;
3855 if ((lpService
->lpGroup
!= NULL
) && (lpService
->lpGroup
->lpGroupName
!= NULL
))
3856 dwRequiredSize
+= wcslen(lpService
->lpGroup
->lpGroupName
) + 1;
3858 dwRequiredSize
+= 2;
3860 /* Add Dependencies length */
3861 if (lpDependencies
!= NULL
)
3862 dwRequiredSize
+= dwDependenciesLength
;
3864 dwRequiredSize
+= 2;
3866 if (lpServiceStartName
!= NULL
)
3867 dwRequiredSize
+= wcslen(lpServiceStartName
) + 1;
3869 dwRequiredSize
+= 2;
3871 if (lpService
->lpDisplayName
!= NULL
)
3872 dwRequiredSize
+= wcslen(lpService
->lpDisplayName
) + 1;
3874 dwRequiredSize
+= 2;
3876 if (lpServiceConfig
== NULL
|| cbBufSize
< dwRequiredSize
)
3878 dwError
= ERROR_INSUFFICIENT_BUFFER
;
3882 lpConfig
= (LPQUERY_SERVICE_CONFIGA
)lpServiceConfig
;
3883 lpConfig
->dwServiceType
= lpService
->Status
.dwServiceType
;
3884 lpConfig
->dwStartType
= lpService
->dwStartType
;
3885 lpConfig
->dwErrorControl
= lpService
->dwErrorControl
;
3886 lpConfig
->dwTagId
= lpService
->dwTag
;
3888 lpStr
= (LPSTR
)(lpServiceConfig
+ 1);
3890 /* NOTE: Strings that are NULL for QUERY_SERVICE_CONFIG are pointers to empty strings.
3895 WideCharToMultiByte(CP_ACP
,
3900 wcslen(lpImagePath
) + 1,
3906 strcpy(lpStr
, lpEmptyString
);
3909 lpConfig
->lpBinaryPathName
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpConfig
);
3910 lpStr
+= (strlen((LPSTR
)lpStr
) + 1);
3912 if (lpService
->lpGroup
&& lpService
->lpGroup
->lpGroupName
)
3914 WideCharToMultiByte(CP_ACP
,
3916 lpService
->lpGroup
->lpGroupName
,
3919 wcslen(lpService
->lpGroup
->lpGroupName
) + 1,
3925 strcpy(lpStr
, lpEmptyString
);
3928 lpConfig
->lpLoadOrderGroup
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpConfig
);
3929 lpStr
+= (strlen(lpStr
) + 1);
3931 /* Append Dependencies */
3934 WideCharToMultiByte(CP_ACP
,
3937 dwDependenciesLength
,
3939 dwDependenciesLength
,
3945 strcpy(lpStr
, lpEmptyString
);
3948 lpConfig
->lpDependencies
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpConfig
);
3950 lpStr
+= dwDependenciesLength
;
3952 lpStr
+= (strlen(lpStr
) + 1);
3954 if (lpServiceStartName
)
3956 WideCharToMultiByte(CP_ACP
,
3961 wcslen(lpServiceStartName
) + 1,
3967 strcpy(lpStr
, lpEmptyString
);
3970 lpConfig
->lpServiceStartName
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpConfig
);
3971 lpStr
+= (strlen(lpStr
) + 1);
3973 if (lpService
->lpDisplayName
)
3975 WideCharToMultiByte(CP_ACP
,
3977 lpService
->lpDisplayName
,
3980 wcslen(lpService
->lpDisplayName
) + 1,
3986 strcpy(lpStr
, lpEmptyString
);
3989 lpConfig
->lpDisplayName
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpConfig
);
3992 if (pcbBytesNeeded
!= NULL
)
3993 *pcbBytesNeeded
= dwRequiredSize
;
3996 if (lpImagePath
!= NULL
)
3997 HeapFree(GetProcessHeap(), 0, lpImagePath
);
3999 if (lpServiceStartName
!= NULL
)
4000 HeapFree(GetProcessHeap(), 0, lpServiceStartName
);
4002 if (lpDependencies
!= NULL
)
4003 HeapFree(GetProcessHeap(), 0, lpDependencies
);
4005 if (hServiceKey
!= NULL
)
4006 RegCloseKey(hServiceKey
);
4008 /* FIXME: Unlock the service database */
4010 DPRINT("RQueryServiceConfigA() done\n");
4017 DWORD
RQueryServiceLockStatusA(
4018 SC_RPC_HANDLE hSCManager
,
4019 LPQUERY_SERVICE_LOCK_STATUSA lpLockStatus
,
4021 LPBOUNDED_DWORD_4K pcbBytesNeeded
)
4024 return ERROR_CALL_NOT_IMPLEMENTED
;
4029 DWORD
RStartServiceA(
4030 SC_RPC_HANDLE hService
,
4032 LPSTRING_PTRSA argv
)
4034 DWORD dwError
= ERROR_SUCCESS
;
4035 PSERVICE_HANDLE hSvc
;
4036 PSERVICE lpService
= NULL
;
4038 DPRINT("RStartServiceA() called\n");
4041 return ERROR_SHUTDOWN_IN_PROGRESS
;
4043 hSvc
= ScmGetServiceFromHandle(hService
);
4046 DPRINT1("Invalid service handle!\n");
4047 return ERROR_INVALID_HANDLE
;
4050 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
4053 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
4054 return ERROR_ACCESS_DENIED
;
4057 lpService
= hSvc
->ServiceEntry
;
4058 if (lpService
== NULL
)
4060 DPRINT("lpService == NULL!\n");
4061 return ERROR_INVALID_HANDLE
;
4064 if (lpService
->dwStartType
== SERVICE_DISABLED
)
4065 return ERROR_SERVICE_DISABLED
;
4067 if (lpService
->bDeleted
)
4068 return ERROR_SERVICE_MARKED_FOR_DELETE
;
4070 /* FIXME: Convert argument vector to Unicode */
4072 /* Start the service */
4073 dwError
= ScmStartService(lpService
, 0, NULL
);
4075 /* FIXME: Free argument vector */
4082 DWORD
RGetServiceDisplayNameA(
4083 SC_RPC_HANDLE hSCManager
,
4084 LPCSTR lpServiceName
,
4085 LPSTR lpDisplayName
,
4086 LPBOUNDED_DWORD_4K lpcchBuffer
)
4088 // PMANAGER_HANDLE hManager;
4089 PSERVICE lpService
= NULL
;
4092 LPWSTR lpServiceNameW
;
4094 DPRINT("RGetServiceDisplayNameA() called\n");
4095 DPRINT("hSCManager = %p\n", hSCManager
);
4096 DPRINT("lpServiceName: %s\n", lpServiceName
);
4097 DPRINT("lpDisplayName: %p\n", lpDisplayName
);
4098 DPRINT("*lpcchBuffer: %lu\n", *lpcchBuffer
);
4100 // hManager = (PMANAGER_HANDLE)hSCManager;
4101 // if (hManager->Handle.Tag != MANAGER_TAG)
4103 // DPRINT("Invalid manager handle!\n");
4104 // return ERROR_INVALID_HANDLE;
4107 if (lpServiceName
!= NULL
)
4109 dwLength
= strlen(lpServiceName
) + 1;
4110 lpServiceNameW
= HeapAlloc(GetProcessHeap(),
4112 dwLength
* sizeof(WCHAR
));
4113 if (!lpServiceNameW
)
4114 return ERROR_NOT_ENOUGH_MEMORY
;
4116 MultiByteToWideChar(CP_ACP
,
4123 lpService
= ScmGetServiceEntryByName(lpServiceNameW
);
4125 HeapFree(GetProcessHeap(), 0, lpServiceNameW
);
4128 if (lpService
== NULL
)
4130 DPRINT("Could not find a service!\n");
4132 /* If the service could not be found and lpcchBuffer is 0, windows
4133 puts null in lpDisplayName and puts 1 in lpcchBuffer */
4134 if (*lpcchBuffer
== 0)
4137 if (lpDisplayName
!= NULL
)
4139 *lpDisplayName
= '\0';
4142 return ERROR_SERVICE_DOES_NOT_EXIST
;
4145 if (!lpService
->lpDisplayName
)
4147 dwLength
= wcslen(lpService
->lpServiceName
);
4148 if (lpDisplayName
!= NULL
&&
4149 *lpcchBuffer
> dwLength
)
4151 WideCharToMultiByte(CP_ACP
,
4153 lpService
->lpServiceName
,
4154 wcslen(lpService
->lpServiceName
),
4159 return ERROR_SUCCESS
;
4164 dwLength
= wcslen(lpService
->lpDisplayName
);
4165 if (lpDisplayName
!= NULL
&&
4166 *lpcchBuffer
> dwLength
)
4168 WideCharToMultiByte(CP_ACP
,
4170 lpService
->lpDisplayName
,
4171 wcslen(lpService
->lpDisplayName
),
4176 return ERROR_SUCCESS
;
4180 dwError
= (*lpcchBuffer
> dwLength
) ? ERROR_SUCCESS
: ERROR_INSUFFICIENT_BUFFER
;
4182 *lpcchBuffer
= dwLength
* 2;
4189 DWORD
RGetServiceKeyNameA(
4190 SC_RPC_HANDLE hSCManager
,
4191 LPCSTR lpDisplayName
,
4192 LPSTR lpServiceName
,
4193 LPBOUNDED_DWORD_4K lpcchBuffer
)
4198 LPWSTR lpDisplayNameW
;
4200 DPRINT("RGetServiceKeyNameA() called\n");
4201 DPRINT("hSCManager = %p\n", hSCManager
);
4202 DPRINT("lpDisplayName: %s\n", lpDisplayName
);
4203 DPRINT("lpServiceName: %p\n", lpServiceName
);
4204 DPRINT("*lpcchBuffer: %lu\n", *lpcchBuffer
);
4206 dwLength
= strlen(lpDisplayName
) + 1;
4207 lpDisplayNameW
= HeapAlloc(GetProcessHeap(),
4209 dwLength
* sizeof(WCHAR
));
4210 if (!lpDisplayNameW
)
4211 return ERROR_NOT_ENOUGH_MEMORY
;
4213 MultiByteToWideChar(CP_ACP
,
4220 lpService
= ScmGetServiceEntryByDisplayName(lpDisplayNameW
);
4222 HeapFree(GetProcessHeap(), 0, lpDisplayNameW
);
4224 if (lpService
== NULL
)
4226 DPRINT("Could not find the service!\n");
4228 /* If the service could not be found and lpcchBuffer is 0,
4229 put null in lpDisplayName and puts 1 in lpcchBuffer, verified WINXP. */
4230 if (*lpcchBuffer
== 0)
4233 if (lpServiceName
!= NULL
)
4235 *lpServiceName
= '\0';
4239 return ERROR_SERVICE_DOES_NOT_EXIST
;
4242 dwLength
= wcslen(lpService
->lpServiceName
);
4243 if (lpServiceName
!= NULL
&&
4244 *lpcchBuffer
> dwLength
)
4246 WideCharToMultiByte(CP_ACP
,
4248 lpService
->lpServiceName
,
4249 wcslen(lpService
->lpServiceName
),
4254 return ERROR_SUCCESS
;
4257 dwError
= (*lpcchBuffer
> dwLength
) ? ERROR_SUCCESS
: ERROR_INSUFFICIENT_BUFFER
;
4259 *lpcchBuffer
= dwLength
* 2;
4266 DWORD
RI_ScGetCurrentGroupStateW(
4267 SC_RPC_HANDLE hSCManager
,
4268 LPWSTR lpLoadOrderGroup
,
4272 return ERROR_CALL_NOT_IMPLEMENTED
;
4277 DWORD
REnumServiceGroupW(
4278 SC_RPC_HANDLE hSCManager
,
4279 DWORD dwServiceType
,
4280 DWORD dwServiceState
,
4283 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
4284 LPBOUNDED_DWORD_256K lpServicesReturned
,
4285 LPBOUNDED_DWORD_256K lpResumeIndex
,
4286 LPCWSTR pszGroupName
)
4289 return ERROR_CALL_NOT_IMPLEMENTED
;
4294 // WARNING: This function is untested
4297 DWORD
RChangeServiceConfig2A(
4298 SC_RPC_HANDLE hService
,
4299 SC_RPC_CONFIG_INFOA Info
)
4301 SC_RPC_CONFIG_INFOW InfoW
;
4302 DWORD dwRet
, dwLength
;
4305 DPRINT("RChangeServiceConfig2A() called\n");
4306 DPRINT("dwInfoLevel = %lu\n", Info
.dwInfoLevel
);
4308 InfoW
.dwInfoLevel
= Info
.dwInfoLevel
;
4310 if (InfoW
.dwInfoLevel
== SERVICE_CONFIG_DESCRIPTION
)
4312 LPSERVICE_DESCRIPTIONW lpServiceDescriptonW
;
4313 LPSERVICE_DESCRIPTIONA lpServiceDescriptonA
;
4315 lpServiceDescriptonA
= Info
.psd
;
4317 ///if (lpServiceDescriptonA &&
4318 ///lpServiceDescriptonA->lpDescription)
4320 dwLength
= (strlen(Info
.lpDescription
) + 1) * sizeof(WCHAR
);
4322 lpServiceDescriptonW
= HeapAlloc(GetProcessHeap(),
4324 dwLength
+ sizeof(SERVICE_DESCRIPTIONW
));
4325 if (!lpServiceDescriptonW
)
4327 return ERROR_NOT_ENOUGH_MEMORY
;
4330 lpServiceDescriptonW
->lpDescription
= (LPWSTR
)(lpServiceDescriptonW
+ 1);
4332 MultiByteToWideChar(CP_ACP
,
4336 lpServiceDescriptonW
->lpDescription
,
4339 ptr
= lpServiceDescriptonW
;
4340 InfoW
.psd
= lpServiceDescriptonW
;
4343 else if (Info
.dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
4345 LPSERVICE_FAILURE_ACTIONSW lpServiceFailureActionsW
;
4346 LPSERVICE_FAILURE_ACTIONSA lpServiceFailureActionsA
;
4347 DWORD dwRebootLen
= 0;
4348 DWORD dwCommandLen
= 0;
4350 lpServiceFailureActionsA
= Info
.psfa
;
4352 if (lpServiceFailureActionsA
)
4354 if (lpServiceFailureActionsA
->lpRebootMsg
)
4356 dwRebootLen
= (strlen(lpServiceFailureActionsA
->lpRebootMsg
) + 1) * sizeof(WCHAR
);
4358 if (lpServiceFailureActionsA
->lpCommand
)
4360 dwCommandLen
= (strlen(lpServiceFailureActionsA
->lpCommand
) + 1) * sizeof(WCHAR
);
4362 dwLength
= dwRebootLen
+ dwCommandLen
+ sizeof(SERVICE_FAILURE_ACTIONSW
);
4364 lpServiceFailureActionsW
= HeapAlloc(GetProcessHeap(),
4367 if (!lpServiceFailureActionsW
)
4369 return ERROR_NOT_ENOUGH_MEMORY
;
4372 lpServiceFailureActionsW
->cActions
= lpServiceFailureActionsA
->cActions
;
4373 lpServiceFailureActionsW
->dwResetPeriod
= lpServiceFailureActionsA
->dwResetPeriod
;
4374 CopyMemory(lpServiceFailureActionsW
->lpsaActions
, lpServiceFailureActionsA
->lpsaActions
, sizeof(SC_ACTION
));
4376 if (lpServiceFailureActionsA
->lpRebootMsg
)
4378 MultiByteToWideChar(CP_ACP
,
4380 lpServiceFailureActionsA
->lpRebootMsg
,
4382 lpServiceFailureActionsW
->lpRebootMsg
,
4386 if (lpServiceFailureActionsA
->lpCommand
)
4388 MultiByteToWideChar(CP_ACP
,
4390 lpServiceFailureActionsA
->lpCommand
,
4392 lpServiceFailureActionsW
->lpCommand
,
4396 ptr
= lpServiceFailureActionsW
;
4400 dwRet
= RChangeServiceConfig2W(hService
, InfoW
);
4402 HeapFree(GetProcessHeap(), 0, ptr
);
4409 DWORD
RChangeServiceConfig2W(
4410 SC_RPC_HANDLE hService
,
4411 SC_RPC_CONFIG_INFOW Info
)
4413 DWORD dwError
= ERROR_SUCCESS
;
4414 PSERVICE_HANDLE hSvc
;
4415 PSERVICE lpService
= NULL
;
4416 HKEY hServiceKey
= NULL
;
4418 DPRINT("RChangeServiceConfig2W() called\n");
4419 DPRINT("dwInfoLevel = %lu\n", Info
.dwInfoLevel
);
4422 return ERROR_SHUTDOWN_IN_PROGRESS
;
4424 hSvc
= ScmGetServiceFromHandle(hService
);
4427 DPRINT1("Invalid service handle!\n");
4428 return ERROR_INVALID_HANDLE
;
4431 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
4432 SERVICE_CHANGE_CONFIG
))
4434 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
4435 return ERROR_ACCESS_DENIED
;
4438 lpService
= hSvc
->ServiceEntry
;
4439 if (lpService
== NULL
)
4441 DPRINT("lpService == NULL!\n");
4442 return ERROR_INVALID_HANDLE
;
4445 /* FIXME: Lock database exclusively */
4447 if (lpService
->bDeleted
)
4449 /* FIXME: Unlock database */
4450 DPRINT("The service has already been marked for delete!\n");
4451 return ERROR_SERVICE_MARKED_FOR_DELETE
;
4454 /* Open the service key */
4455 dwError
= ScmOpenServiceKey(lpService
->szServiceName
,
4458 if (dwError
!= ERROR_SUCCESS
)
4461 if (Info
.dwInfoLevel
== SERVICE_CONFIG_DESCRIPTION
)
4463 LPSERVICE_DESCRIPTIONW lpServiceDescription
;
4465 lpServiceDescription
= (LPSERVICE_DESCRIPTIONW
)Info
.psd
;
4466 lpServiceDescription
->lpDescription
= (LPWSTR
)((ULONG_PTR
)lpServiceDescription
+ sizeof(LPSERVICE_DESCRIPTIONW
));
4468 if (lpServiceDescription
!= NULL
&&
4469 lpServiceDescription
->lpDescription
!= NULL
)
4471 DPRINT("Setting value %S\n", lpServiceDescription
->lpDescription
);
4472 RegSetValueExW(hServiceKey
,
4476 (LPBYTE
)lpServiceDescription
->lpDescription
,
4477 (wcslen(lpServiceDescription
->lpDescription
) + 1) * sizeof(WCHAR
));
4479 if (dwError
!= ERROR_SUCCESS
)
4483 else if (Info
.dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
4486 dwError
= ERROR_CALL_NOT_IMPLEMENTED
;
4491 /* FIXME: Unlock database */
4492 if (hServiceKey
!= NULL
)
4493 RegCloseKey(hServiceKey
);
4495 DPRINT("RChangeServiceConfig2W() done (Error %lu)\n", dwError
);
4502 DWORD
RQueryServiceConfig2A(
4503 SC_RPC_HANDLE hService
,
4507 LPBOUNDED_DWORD_8K pcbBytesNeeded
)
4509 DWORD dwError
= ERROR_SUCCESS
;
4510 PSERVICE_HANDLE hSvc
;
4511 PSERVICE lpService
= NULL
;
4512 HKEY hServiceKey
= NULL
;
4513 LPWSTR lpDescriptionW
= NULL
;
4514 LPSTR lpDescription
= NULL
;
4516 DPRINT("RQueryServiceConfig2A() called hService %p dwInfoLevel %u, lpBuffer %p cbBufSize %u pcbBytesNeeded %p\n",
4517 hService
, dwInfoLevel
, lpBuffer
, cbBufSize
, pcbBytesNeeded
);
4520 return ERROR_INVALID_ADDRESS
;
4523 return ERROR_SHUTDOWN_IN_PROGRESS
;
4525 hSvc
= ScmGetServiceFromHandle(hService
);
4528 DPRINT1("Invalid service handle!\n");
4529 return ERROR_INVALID_HANDLE
;
4532 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
4533 SERVICE_QUERY_CONFIG
))
4535 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
4536 return ERROR_ACCESS_DENIED
;
4539 lpService
= hSvc
->ServiceEntry
;
4540 if (lpService
== NULL
)
4542 DPRINT("lpService == NULL!\n");
4543 return ERROR_INVALID_HANDLE
;
4546 /* FIXME: Lock the service database shared */
4548 dwError
= ScmOpenServiceKey(lpService
->lpServiceName
,
4551 if (dwError
!= ERROR_SUCCESS
)
4554 if (dwInfoLevel
== SERVICE_CONFIG_DESCRIPTION
)
4556 LPSERVICE_DESCRIPTIONA lpServiceDescription
= (LPSERVICE_DESCRIPTIONA
)lpBuffer
;
4559 *pcbBytesNeeded
= sizeof(SERVICE_DESCRIPTIONA
);
4561 dwError
= ScmReadString(hServiceKey
,
4564 if (dwError
== ERROR_SUCCESS
)
4566 *pcbBytesNeeded
+= ((wcslen(lpDescriptionW
) + 1) * sizeof(WCHAR
));
4569 if (cbBufSize
>= *pcbBytesNeeded
)
4572 if (dwError
== ERROR_SUCCESS
)
4574 lpStr
= (LPSTR
)(lpServiceDescription
+ 1);
4576 WideCharToMultiByte(CP_ACP
,
4581 wcslen(lpDescriptionW
),
4584 lpServiceDescription
->lpDescription
= (LPSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceDescription
);
4588 lpServiceDescription
->lpDescription
= NULL
;
4594 dwError
= ERROR_INSUFFICIENT_BUFFER
;
4598 else if (dwInfoLevel
& SERVICE_CONFIG_FAILURE_ACTIONS
)
4601 dwError
= ERROR_CALL_NOT_IMPLEMENTED
;
4606 if (lpDescription
!= NULL
)
4607 HeapFree(GetProcessHeap(), 0, lpDescription
);
4609 if (hServiceKey
!= NULL
)
4610 RegCloseKey(hServiceKey
);
4612 /* FIXME: Unlock database */
4614 DPRINT("RQueryServiceConfig2W() done (Error %lu)\n", dwError
);
4621 DWORD
RQueryServiceConfig2W(
4622 SC_RPC_HANDLE hService
,
4626 LPBOUNDED_DWORD_8K pcbBytesNeeded
)
4628 DWORD dwError
= ERROR_SUCCESS
;
4629 PSERVICE_HANDLE hSvc
;
4630 PSERVICE lpService
= NULL
;
4631 HKEY hServiceKey
= NULL
;
4632 DWORD dwRequiredSize
;
4633 LPWSTR lpDescription
= NULL
;
4634 LPWSTR lpFailureCommand
= NULL
;
4635 LPWSTR lpRebootMessage
= NULL
;
4637 DPRINT("RQueryServiceConfig2W() called\n");
4640 return ERROR_INVALID_ADDRESS
;
4643 return ERROR_SHUTDOWN_IN_PROGRESS
;
4645 hSvc
= ScmGetServiceFromHandle(hService
);
4648 DPRINT1("Invalid service handle!\n");
4649 return ERROR_INVALID_HANDLE
;
4652 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
4653 SERVICE_QUERY_CONFIG
))
4655 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
4656 return ERROR_ACCESS_DENIED
;
4659 lpService
= hSvc
->ServiceEntry
;
4660 if (lpService
== NULL
)
4662 DPRINT("lpService == NULL!\n");
4663 return ERROR_INVALID_HANDLE
;
4666 /* FIXME: Lock the service database shared */
4668 dwError
= ScmOpenServiceKey(lpService
->lpServiceName
,
4671 if (dwError
!= ERROR_SUCCESS
)
4674 if (dwInfoLevel
== SERVICE_CONFIG_DESCRIPTION
)
4676 LPSERVICE_DESCRIPTIONW lpServiceDescription
= (LPSERVICE_DESCRIPTIONW
)lpBuffer
;
4679 dwError
= ScmReadString(hServiceKey
,
4682 if (dwError
!= ERROR_SUCCESS
)
4685 dwRequiredSize
= sizeof(SERVICE_DESCRIPTIONW
) + ((wcslen(lpDescription
) + 1) * sizeof(WCHAR
));
4687 if (cbBufSize
< dwRequiredSize
)
4689 *pcbBytesNeeded
= dwRequiredSize
;
4690 dwError
= ERROR_INSUFFICIENT_BUFFER
;
4694 lpStr
= (LPWSTR
)(lpServiceDescription
+ 1);
4695 wcscpy(lpStr
, lpDescription
);
4696 lpServiceDescription
->lpDescription
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpServiceDescription
);
4698 else if (dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
4701 LPSERVICE_FAILURE_ACTIONSW lpFailureActions
= (LPSERVICE_FAILURE_ACTIONSW
)lpBuffer
;
4705 dwError
= ScmReadString(hServiceKey
,
4709 dwError
= ScmReadString(hServiceKey
,
4713 dwRequiredSize
= sizeof(SERVICE_FAILURE_ACTIONSW
);
4715 if (lpFailureCommand
)
4716 dwRequiredSize
+= (wcslen(lpFailureCommand
) + 1) * sizeof(WCHAR
);
4718 if (lpRebootMessage
)
4719 dwRequiredSize
+= (wcslen(lpRebootMessage
) + 1) * sizeof(WCHAR
);
4721 if (cbBufSize
< dwRequiredSize
)
4723 *pcbBytesNeeded
= dwRequiredSize
;
4724 dwError
= ERROR_INSUFFICIENT_BUFFER
;
4728 lpFailureActions
->cActions
= 0;
4729 lpFailureActions
->dwResetPeriod
= 0;
4730 lpFailureActions
->lpCommand
= NULL
;
4731 lpFailureActions
->lpRebootMsg
= NULL
;
4732 lpFailureActions
->lpsaActions
= NULL
;
4734 lpStr
= (LPWSTR
)(lpFailureActions
+ 1);
4735 if (lpRebootMessage
)
4737 wcscpy(lpStr
, lpRebootMessage
);
4738 lpFailureActions
->lpRebootMsg
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpRebootMessage
);
4739 lpStr
+= wcslen(lpRebootMessage
) + 1;
4742 if (lpFailureCommand
)
4744 wcscpy(lpStr
, lpFailureCommand
);
4745 lpFailureActions
->lpCommand
= (LPWSTR
)((ULONG_PTR
)lpStr
- (ULONG_PTR
)lpFailureCommand
);
4746 lpStr
+= wcslen(lpRebootMessage
) + 1;
4748 dwError
= STATUS_SUCCESS
;
4753 if (lpDescription
!= NULL
)
4754 HeapFree(GetProcessHeap(), 0, lpDescription
);
4756 if (lpRebootMessage
!= NULL
)
4757 HeapFree(GetProcessHeap(), 0, lpRebootMessage
);
4759 if (lpFailureCommand
!= NULL
)
4760 HeapFree(GetProcessHeap(), 0, lpFailureCommand
);
4762 if (hServiceKey
!= NULL
)
4763 RegCloseKey(hServiceKey
);
4765 /* FIXME: Unlock database */
4767 DPRINT("RQueryServiceConfig2W() done (Error %lu)\n", dwError
);
4774 DWORD
RQueryServiceStatusEx(
4775 SC_RPC_HANDLE hService
,
4776 SC_STATUS_TYPE InfoLevel
,
4779 LPBOUNDED_DWORD_8K pcbBytesNeeded
)
4781 LPSERVICE_STATUS_PROCESS lpStatus
;
4782 PSERVICE_HANDLE hSvc
;
4785 DPRINT("RQueryServiceStatusEx() called\n");
4788 return ERROR_SHUTDOWN_IN_PROGRESS
;
4790 if (InfoLevel
!= SC_STATUS_PROCESS_INFO
)
4791 return ERROR_INVALID_LEVEL
;
4793 *pcbBytesNeeded
= sizeof(SERVICE_STATUS_PROCESS
);
4795 if (cbBufSize
< sizeof(SERVICE_STATUS_PROCESS
))
4796 return ERROR_INSUFFICIENT_BUFFER
;
4798 hSvc
= ScmGetServiceFromHandle(hService
);
4801 DPRINT1("Invalid service handle!\n");
4802 return ERROR_INVALID_HANDLE
;
4805 if (!RtlAreAllAccessesGranted(hSvc
->Handle
.DesiredAccess
,
4806 SERVICE_QUERY_STATUS
))
4808 DPRINT("Insufficient access rights! 0x%lx\n", hSvc
->Handle
.DesiredAccess
);
4809 return ERROR_ACCESS_DENIED
;
4812 lpService
= hSvc
->ServiceEntry
;
4813 if (lpService
== NULL
)
4815 DPRINT("lpService == NULL!\n");
4816 return ERROR_INVALID_HANDLE
;
4819 lpStatus
= (LPSERVICE_STATUS_PROCESS
)lpBuffer
;
4821 /* Return service status information */
4822 RtlCopyMemory(lpStatus
,
4824 sizeof(SERVICE_STATUS
));
4826 lpStatus
->dwProcessId
= lpService
->ProcessId
; /* FIXME */
4827 lpStatus
->dwServiceFlags
= 0; /* FIXME */
4829 return ERROR_SUCCESS
;
4834 DWORD
REnumServicesStatusExA(
4835 SC_RPC_HANDLE hSCManager
,
4836 SC_ENUM_TYPE InfoLevel
,
4837 DWORD dwServiceType
,
4838 DWORD dwServiceState
,
4841 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
4842 LPBOUNDED_DWORD_256K lpServicesReturned
,
4843 LPBOUNDED_DWORD_256K lpResumeIndex
,
4844 LPCSTR pszGroupName
)
4846 LPENUM_SERVICE_STATUS_PROCESSW lpStatusPtrW
= NULL
;
4847 LPENUM_SERVICE_STATUS_PROCESSA lpStatusPtrA
= NULL
;
4848 LPWSTR lpStringPtrW
;
4850 LPWSTR pszGroupNameW
= NULL
;
4852 DWORD dwServiceCount
;
4854 DPRINT("REnumServicesStatusExA() called\n");
4858 pszGroupNameW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, (strlen(pszGroupName
) + 1) * sizeof(WCHAR
));
4861 DPRINT("Failed to allocate buffer!\n");
4862 return ERROR_NOT_ENOUGH_MEMORY
;
4865 MultiByteToWideChar(CP_ACP
,
4870 strlen(pszGroupName
) + 1);
4873 if ((cbBufSize
> 0) && (lpBuffer
))
4875 lpStatusPtrW
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, cbBufSize
);
4878 DPRINT("Failed to allocate buffer!\n");
4879 return ERROR_NOT_ENOUGH_MEMORY
;
4883 dwError
= REnumServicesStatusExW(hSCManager
,
4887 (LPBYTE
)lpStatusPtrW
,
4894 /* if no services were returned then we are Done */
4895 if (*lpServicesReturned
== 0)
4898 lpStatusPtrA
= (LPENUM_SERVICE_STATUS_PROCESSA
)lpBuffer
;
4899 lpStringPtrA
= (LPSTR
)((ULONG_PTR
)lpBuffer
+
4900 *lpServicesReturned
* sizeof(ENUM_SERVICE_STATUS_PROCESSA
));
4901 lpStringPtrW
= (LPWSTR
)((ULONG_PTR
)lpStatusPtrW
+
4902 *lpServicesReturned
* sizeof(ENUM_SERVICE_STATUS_PROCESSW
));
4904 for (dwServiceCount
= 0; dwServiceCount
< *lpServicesReturned
; dwServiceCount
++)
4906 /* Copy the service name */
4907 WideCharToMultiByte(CP_ACP
,
4912 wcslen(lpStringPtrW
),
4916 lpStatusPtrA
->lpServiceName
= (LPSTR
)((ULONG_PTR
)lpStringPtrA
- (ULONG_PTR
)lpBuffer
);
4917 lpStringPtrA
+= wcslen(lpStringPtrW
) + 1;
4918 lpStringPtrW
+= wcslen(lpStringPtrW
) + 1;
4920 /* Copy the display name */
4921 WideCharToMultiByte(CP_ACP
,
4926 wcslen(lpStringPtrW
),
4930 lpStatusPtrA
->lpDisplayName
= (LPSTR
)((ULONG_PTR
)lpStringPtrA
- (ULONG_PTR
)lpBuffer
);
4931 lpStringPtrA
+= wcslen(lpStringPtrW
) + 1;
4932 lpStringPtrW
+= wcslen(lpStringPtrW
) + 1;
4934 /* Copy the status information */
4935 memcpy(&lpStatusPtrA
->ServiceStatusProcess
,
4936 &lpStatusPtrW
->ServiceStatusProcess
,
4937 sizeof(SERVICE_STATUS
));
4939 lpStatusPtrA
->ServiceStatusProcess
.dwProcessId
= lpStatusPtrW
->ServiceStatusProcess
.dwProcessId
; /* FIXME */
4940 lpStatusPtrA
->ServiceStatusProcess
.dwServiceFlags
= 0; /* FIXME */
4946 HeapFree(GetProcessHeap(), 0, pszGroupNameW
);
4949 HeapFree(GetProcessHeap(), 0, lpStatusPtrW
);
4951 DPRINT("REnumServicesStatusExA() done (Error %lu)\n", dwError
);
4958 DWORD
REnumServicesStatusExW(
4959 SC_RPC_HANDLE hSCManager
,
4960 SC_ENUM_TYPE InfoLevel
,
4961 DWORD dwServiceType
,
4962 DWORD dwServiceState
,
4965 LPBOUNDED_DWORD_256K pcbBytesNeeded
,
4966 LPBOUNDED_DWORD_256K lpServicesReturned
,
4967 LPBOUNDED_DWORD_256K lpResumeIndex
,
4968 LPCWSTR pszGroupName
)
4970 PMANAGER_HANDLE hManager
;
4972 DWORD dwError
= ERROR_SUCCESS
;
4973 PLIST_ENTRY ServiceEntry
;
4974 PSERVICE CurrentService
;
4976 DWORD dwRequiredSize
;
4977 DWORD dwServiceCount
;
4979 DWORD dwLastResumeCount
= 0;
4980 LPENUM_SERVICE_STATUS_PROCESSW lpStatusPtr
;
4983 DPRINT("REnumServicesStatusExW() called\n");
4986 return ERROR_SHUTDOWN_IN_PROGRESS
;
4988 if (InfoLevel
!= SC_ENUM_PROCESS_INFO
)
4989 return ERROR_INVALID_LEVEL
;
4991 hManager
= ScmGetServiceManagerFromHandle(hSCManager
);
4992 if (hManager
== NULL
)
4994 DPRINT1("Invalid service manager handle!\n");
4995 return ERROR_INVALID_HANDLE
;
4998 *pcbBytesNeeded
= 0;
4999 *lpServicesReturned
= 0;
5001 if ((dwServiceType
!=SERVICE_DRIVER
) && (dwServiceType
!=SERVICE_WIN32
))
5003 DPRINT("Not a valid Service Type!\n");
5004 return ERROR_INVALID_PARAMETER
;
5007 if ((dwServiceState
<SERVICE_ACTIVE
) || (dwServiceState
>SERVICE_STATE_ALL
))
5009 DPRINT("Not a valid Service State!\n");
5010 return ERROR_INVALID_PARAMETER
;
5013 /* Check access rights */
5014 if (!RtlAreAllAccessesGranted(hManager
->Handle
.DesiredAccess
,
5015 SC_MANAGER_ENUMERATE_SERVICE
))
5017 DPRINT("Insufficient access rights! 0x%lx\n",
5018 hManager
->Handle
.DesiredAccess
);
5019 return ERROR_ACCESS_DENIED
;
5022 if (lpResumeIndex
) dwLastResumeCount
= *lpResumeIndex
;
5024 /* Lock the service list shared */
5026 lpService
= ScmGetServiceEntryByResumeCount(dwLastResumeCount
);
5027 if (lpService
== NULL
)
5029 dwError
= ERROR_SUCCESS
;
5036 for (ServiceEntry
= &lpService
->ServiceListEntry
;
5037 ServiceEntry
!= &ServiceListHead
;
5038 ServiceEntry
= ServiceEntry
->Flink
)
5040 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
5044 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
5047 dwState
= SERVICE_ACTIVE
;
5048 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
5049 dwState
= SERVICE_INACTIVE
;
5051 if ((dwState
& dwServiceState
) == 0)
5056 if (*pszGroupName
== 0)
5058 if (CurrentService
->lpGroup
!= NULL
)
5063 if ((CurrentService
->lpGroup
== NULL
) ||
5064 _wcsicmp(pszGroupName
, CurrentService
->lpGroup
->lpGroupName
))
5069 dwSize
= sizeof(ENUM_SERVICE_STATUS_PROCESSW
) +
5070 ((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
5071 ((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
5073 if (dwRequiredSize
+ dwSize
<= cbBufSize
)
5075 DPRINT("Service name: %S fit\n", CurrentService
->lpServiceName
);
5076 dwRequiredSize
+= dwSize
;
5078 dwLastResumeCount
= CurrentService
->dwResumeCount
;
5082 DPRINT("Service name: %S no fit\n", CurrentService
->lpServiceName
);
5088 DPRINT("dwRequiredSize: %lu\n", dwRequiredSize
);
5089 DPRINT("dwServiceCount: %lu\n", dwServiceCount
);
5092 ServiceEntry
!= &ServiceListHead
;
5093 ServiceEntry
= ServiceEntry
->Flink
)
5095 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
5099 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
5102 dwState
= SERVICE_ACTIVE
;
5103 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
5104 dwState
= SERVICE_INACTIVE
;
5106 if ((dwState
& dwServiceState
) == 0)
5111 if (*pszGroupName
== 0)
5113 if (CurrentService
->lpGroup
!= NULL
)
5118 if ((CurrentService
->lpGroup
== NULL
) ||
5119 _wcsicmp(pszGroupName
, CurrentService
->lpGroup
->lpGroupName
))
5124 dwRequiredSize
+= (sizeof(ENUM_SERVICE_STATUS_PROCESSW
) +
5125 ((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
5126 ((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
)));
5128 dwError
= ERROR_MORE_DATA
;
5131 DPRINT("*pcbBytesNeeded: %lu\n", dwRequiredSize
);
5134 *lpResumeIndex
= dwLastResumeCount
;
5136 *lpServicesReturned
= dwServiceCount
;
5137 *pcbBytesNeeded
= dwRequiredSize
;
5139 /* If there was no services that matched */
5140 if ((!dwServiceCount
) && (dwError
!= ERROR_MORE_DATA
))
5142 dwError
= ERROR_SERVICE_DOES_NOT_EXIST
;
5146 lpStatusPtr
= (LPENUM_SERVICE_STATUS_PROCESSW
)lpBuffer
;
5147 lpStringPtr
= (LPWSTR
)((ULONG_PTR
)lpBuffer
+
5148 dwServiceCount
* sizeof(ENUM_SERVICE_STATUS_PROCESSW
));
5151 for (ServiceEntry
= &lpService
->ServiceListEntry
;
5152 ServiceEntry
!= &ServiceListHead
;
5153 ServiceEntry
= ServiceEntry
->Flink
)
5155 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
5159 if ((CurrentService
->Status
.dwServiceType
& dwServiceType
) == 0)
5162 dwState
= SERVICE_ACTIVE
;
5163 if (CurrentService
->Status
.dwCurrentState
== SERVICE_STOPPED
)
5164 dwState
= SERVICE_INACTIVE
;
5166 if ((dwState
& dwServiceState
) == 0)
5171 if (*pszGroupName
== 0)
5173 if (CurrentService
->lpGroup
!= NULL
)
5178 if ((CurrentService
->lpGroup
== NULL
) ||
5179 _wcsicmp(pszGroupName
, CurrentService
->lpGroup
->lpGroupName
))
5184 dwSize
= sizeof(ENUM_SERVICE_STATUS_PROCESSW
) +
5185 ((wcslen(CurrentService
->lpServiceName
) + 1) * sizeof(WCHAR
)) +
5186 ((wcslen(CurrentService
->lpDisplayName
) + 1) * sizeof(WCHAR
));
5188 if (dwRequiredSize
+ dwSize
<= cbBufSize
)
5190 /* Copy the service name */
5192 CurrentService
->lpServiceName
);
5193 lpStatusPtr
->lpServiceName
= (LPWSTR
)((ULONG_PTR
)lpStringPtr
- (ULONG_PTR
)lpBuffer
);
5194 lpStringPtr
+= (wcslen(CurrentService
->lpServiceName
) + 1);
5196 /* Copy the display name */
5198 CurrentService
->lpDisplayName
);
5199 lpStatusPtr
->lpDisplayName
= (LPWSTR
)((ULONG_PTR
)lpStringPtr
- (ULONG_PTR
)lpBuffer
);
5200 lpStringPtr
+= (wcslen(CurrentService
->lpDisplayName
) + 1);
5202 /* Copy the status information */
5203 memcpy(&lpStatusPtr
->ServiceStatusProcess
,
5204 &CurrentService
->Status
,
5205 sizeof(SERVICE_STATUS
));
5206 lpStatusPtr
->ServiceStatusProcess
.dwProcessId
= CurrentService
->ProcessId
; /* FIXME */
5207 lpStatusPtr
->ServiceStatusProcess
.dwServiceFlags
= 0; /* FIXME */
5210 dwRequiredSize
+= dwSize
;
5220 *pcbBytesNeeded
= 0;
5226 /* Unlock the service list */
5228 DPRINT("REnumServicesStatusExW() done (Error %lu)\n", dwError
);
5235 DWORD
RSendTSMessage(
5236 handle_t BindingHandle
) /* FIXME */
5239 return ERROR_CALL_NOT_IMPLEMENTED
;
5244 DWORD
RCreateServiceWOW64A(
5245 handle_t BindingHandle
,
5246 LPSTR lpServiceName
,
5247 LPSTR lpDisplayName
,
5248 DWORD dwDesiredAccess
,
5249 DWORD dwServiceType
,
5251 DWORD dwErrorControl
,
5252 LPSTR lpBinaryPathName
,
5253 LPSTR lpLoadOrderGroup
,
5255 LPBYTE lpDependencies
,
5257 LPSTR lpServiceStartName
,
5260 LPSC_RPC_HANDLE lpServiceHandle
)
5263 return ERROR_CALL_NOT_IMPLEMENTED
;
5268 DWORD
RCreateServiceWOW64W(
5269 handle_t BindingHandle
,
5270 LPWSTR lpServiceName
,
5271 LPWSTR lpDisplayName
,
5272 DWORD dwDesiredAccess
,
5273 DWORD dwServiceType
,
5275 DWORD dwErrorControl
,
5276 LPWSTR lpBinaryPathName
,
5277 LPWSTR lpLoadOrderGroup
,
5279 LPBYTE lpDependencies
,
5281 LPWSTR lpServiceStartName
,
5284 LPSC_RPC_HANDLE lpServiceHandle
)
5287 return ERROR_CALL_NOT_IMPLEMENTED
;
5292 DWORD
RQueryServiceTagInfo(
5293 handle_t BindingHandle
) /* FIXME */
5296 return ERROR_CALL_NOT_IMPLEMENTED
;
5301 DWORD
RNotifyServiceStatusChange(
5302 SC_RPC_HANDLE hService
,
5303 SC_RPC_NOTIFY_PARAMS NotifyParams
,
5304 GUID
*pClientProcessGuid
,
5305 GUID
*pSCMProcessGuid
,
5306 PBOOL pfCreateRemoteQueue
,
5307 LPSC_NOTIFY_RPC_HANDLE phNotify
)
5310 return ERROR_CALL_NOT_IMPLEMENTED
;
5315 DWORD
RGetNotifyResults(
5316 SC_NOTIFY_RPC_HANDLE hNotify
,
5317 PSC_RPC_NOTIFY_PARAMS_LIST
*ppNotifyParams
)
5320 return ERROR_CALL_NOT_IMPLEMENTED
;
5325 DWORD
RCloseNotifyHandle(
5326 LPSC_NOTIFY_RPC_HANDLE phNotify
,
5330 return ERROR_CALL_NOT_IMPLEMENTED
;
5335 DWORD
RControlServiceExA(
5336 SC_RPC_HANDLE hService
,
5341 return ERROR_CALL_NOT_IMPLEMENTED
;
5346 DWORD
RControlServiceExW(
5347 SC_RPC_HANDLE hService
,
5352 return ERROR_CALL_NOT_IMPLEMENTED
;
5357 DWORD
RSendPnPMessage(
5358 handle_t BindingHandle
) /* FIXME */
5361 return ERROR_CALL_NOT_IMPLEMENTED
;
5366 DWORD
RValidatePnPService(
5367 handle_t BindingHandle
) /* FIXME */
5370 return ERROR_CALL_NOT_IMPLEMENTED
;
5375 DWORD
ROpenServiceStatusHandle(
5376 handle_t BindingHandle
) /* FIXME */
5379 return ERROR_CALL_NOT_IMPLEMENTED
;
5385 handle_t BindingHandle
) /* FIXME */
5388 return ERROR_CALL_NOT_IMPLEMENTED
;
5392 void __RPC_FAR
* __RPC_USER
midl_user_allocate(size_t len
)
5394 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
);
5398 void __RPC_USER
midl_user_free(void __RPC_FAR
* ptr
)
5400 HeapFree(GetProcessHeap(), 0, ptr
);
5404 void __RPC_USER
SC_RPC_HANDLE_rundown(SC_RPC_HANDLE hSCObject
)
5409 void __RPC_USER
SC_RPC_LOCK_rundown(SC_RPC_LOCK Lock
)
5414 void __RPC_USER
SC_NOTIFY_RPC_HANDLE_rundown(SC_NOTIFY_RPC_HANDLE hNotify
)