2 * Win32 advapi functions
4 * Copyright 1995 Sven Verdoolaege
5 * Copyright 2005 Mike McCormack
6 * Copyright 2007 Rolf Kalbermatter
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
31 #define NONAMELESSUNION
34 #define WIN32_NO_STATUS
40 #include "wine/unicode.h"
41 #include "wine/debug.h"
48 #include "advapi32_misc.h"
50 #include "wine/exception.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(service
);
54 void __RPC_FAR
* __RPC_USER
MIDL_user_allocate(SIZE_T len
)
56 return heap_alloc(len
);
59 void __RPC_USER
MIDL_user_free(void __RPC_FAR
* ptr
)
64 typedef struct service_data_t
66 LPHANDLER_FUNCTION_EX handler
;
70 SC_HANDLE full_access_handle
;
73 LPSERVICE_MAIN_FUNCTIONA a
;
74 LPSERVICE_MAIN_FUNCTIONW w
;
80 typedef struct dispatcher_data_t
86 static CRITICAL_SECTION service_cs
;
87 static CRITICAL_SECTION_DEBUG service_cs_debug
=
90 { &service_cs_debug
.ProcessLocksList
,
91 &service_cs_debug
.ProcessLocksList
},
92 0, 0, { (DWORD_PTR
)(__FILE__
": service_cs") }
94 static CRITICAL_SECTION service_cs
= { &service_cs_debug
, -1, 0, 0, 0, 0 };
96 static service_data
**services
;
97 static unsigned int nb_services
;
98 static HANDLE service_event
;
99 static HANDLE stop_event
;
101 extern HANDLE CDECL
__wine_make_process_system(void);
103 /******************************************************************************
104 * String management functions (same behaviour as strdup)
105 * NOTE: the caller of those functions is responsible for calling HeapFree
106 * in order to release the memory allocated by those functions.
108 LPWSTR
SERV_dup( LPCSTR str
)
115 len
= MultiByteToWideChar( CP_ACP
, 0, str
, -1, NULL
, 0 );
116 wstr
= heap_alloc( len
*sizeof (WCHAR
) );
117 MultiByteToWideChar( CP_ACP
, 0, str
, -1, wstr
, len
);
121 static inline LPWSTR
SERV_dupmulti(LPCSTR str
)
129 len
+= MultiByteToWideChar( CP_ACP
, 0, &str
[n
], -1, NULL
, 0 );
130 n
+= (strlen( &str
[n
] ) + 1);
135 wstr
= heap_alloc( len
*sizeof (WCHAR
) );
136 MultiByteToWideChar( CP_ACP
, 0, str
, n
, wstr
, len
);
140 static inline DWORD
multisz_cb(LPCWSTR wmultisz
)
142 const WCHAR
*wptr
= wmultisz
;
144 if (wmultisz
== NULL
)
148 wptr
+= lstrlenW(wptr
)+1;
149 return (wptr
- wmultisz
+ 1)*sizeof(WCHAR
);
152 /******************************************************************************
153 * RPC connection with services.exe
155 static handle_t
rpc_wstr_bind(RPC_WSTR str
)
157 WCHAR transport
[] = SVCCTL_TRANSPORT
;
158 WCHAR endpoint
[] = SVCCTL_ENDPOINT
;
159 RPC_WSTR binding_str
;
163 status
= RpcStringBindingComposeW(NULL
, transport
, str
, endpoint
, NULL
, &binding_str
);
164 if (status
!= RPC_S_OK
)
166 ERR("RpcStringBindingComposeW failed (%d)\n", (DWORD
)status
);
170 status
= RpcBindingFromStringBindingW(binding_str
, &rpc_handle
);
171 RpcStringFreeW(&binding_str
);
173 if (status
!= RPC_S_OK
)
175 ERR("Couldn't connect to services.exe: error code %u\n", (DWORD
)status
);
182 static handle_t
rpc_cstr_bind(RPC_CSTR str
)
184 RPC_CSTR transport
= (RPC_CSTR
)SVCCTL_TRANSPORTA
;
185 RPC_CSTR endpoint
= (RPC_CSTR
)SVCCTL_ENDPOINTA
;
186 RPC_CSTR binding_str
;
190 status
= RpcStringBindingComposeA(NULL
, transport
, str
, endpoint
, NULL
, &binding_str
);
191 if (status
!= RPC_S_OK
)
193 ERR("RpcStringBindingComposeW failed (%d)\n", (DWORD
)status
);
197 status
= RpcBindingFromStringBindingA(binding_str
, &rpc_handle
);
198 RpcStringFreeA(&binding_str
);
200 if (status
!= RPC_S_OK
)
202 ERR("Couldn't connect to services.exe: error code %u\n", (DWORD
)status
);
209 DECLSPEC_HIDDEN handle_t __RPC_USER
MACHINE_HANDLEA_bind(MACHINE_HANDLEA MachineName
)
211 return rpc_cstr_bind((RPC_CSTR
)MachineName
);
214 DECLSPEC_HIDDEN
void __RPC_USER
MACHINE_HANDLEA_unbind(MACHINE_HANDLEA MachineName
, handle_t h
)
219 DECLSPEC_HIDDEN handle_t __RPC_USER
MACHINE_HANDLEW_bind(MACHINE_HANDLEW MachineName
)
221 return rpc_wstr_bind((RPC_WSTR
)MachineName
);
224 DECLSPEC_HIDDEN
void __RPC_USER
MACHINE_HANDLEW_unbind(MACHINE_HANDLEW MachineName
, handle_t h
)
229 DECLSPEC_HIDDEN handle_t __RPC_USER
SVCCTL_HANDLEW_bind(SVCCTL_HANDLEW MachineName
)
231 return rpc_wstr_bind((RPC_WSTR
)MachineName
);
234 DECLSPEC_HIDDEN
void __RPC_USER
SVCCTL_HANDLEW_unbind(SVCCTL_HANDLEW MachineName
, handle_t h
)
239 static LONG WINAPI
rpc_filter(EXCEPTION_POINTERS
*eptr
)
241 return I_RpcExceptionFilter(eptr
->ExceptionRecord
->ExceptionCode
);
244 static DWORD
map_exception_code(DWORD exception_code
)
246 switch (exception_code
)
248 case RPC_X_NULL_REF_POINTER
:
249 return ERROR_INVALID_ADDRESS
;
250 case RPC_X_ENUM_VALUE_OUT_OF_RANGE
:
251 case RPC_X_BYTE_COUNT_TOO_SMALL
:
252 return ERROR_INVALID_PARAMETER
;
253 case RPC_S_INVALID_BINDING
:
254 case RPC_X_SS_IN_NULL_CONTEXT
:
255 return ERROR_INVALID_HANDLE
;
257 return exception_code
;
261 /******************************************************************************
262 * Service IPC functions
264 static LPWSTR
service_get_pipe_name(void)
266 static const WCHAR format
[] = { '\\','\\','.','\\','p','i','p','e','\\',
267 'n','e','t','\\','N','t','C','o','n','t','r','o','l','P','i','p','e','%','u',0};
268 static const WCHAR service_current_key_str
[] = { 'S','Y','S','T','E','M','\\',
269 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
270 'C','o','n','t','r','o','l','\\',
271 'S','e','r','v','i','c','e','C','u','r','r','e','n','t',0};
274 HKEY service_current_key
;
275 DWORD service_current
;
279 ret
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, service_current_key_str
, 0,
280 KEY_QUERY_VALUE
, &service_current_key
);
281 if (ret
!= ERROR_SUCCESS
)
283 len
= sizeof(service_current
);
284 ret
= RegQueryValueExW(service_current_key
, NULL
, NULL
, &type
,
285 (BYTE
*)&service_current
, &len
);
286 RegCloseKey(service_current_key
);
287 if (ret
!= ERROR_SUCCESS
|| type
!= REG_DWORD
)
289 len
= sizeof(format
)/sizeof(WCHAR
) + 10 /* strlenW("4294967295") */;
290 name
= heap_alloc(len
* sizeof(WCHAR
));
293 snprintfW(name
, len
, format
, service_current
);
297 static HANDLE
service_open_pipe(void)
299 LPWSTR szPipe
= service_get_pipe_name();
300 HANDLE handle
= INVALID_HANDLE_VALUE
;
303 handle
= CreateFileW(szPipe
, GENERIC_READ
|GENERIC_WRITE
,
304 0, NULL
, OPEN_ALWAYS
, 0, NULL
);
305 if (handle
!= INVALID_HANDLE_VALUE
)
307 if (GetLastError() != ERROR_PIPE_BUSY
)
309 } while (WaitNamedPipeW(szPipe
, NMPWAIT_USE_DEFAULT_WAIT
));
315 static service_data
*find_service_by_name( const WCHAR
*name
)
319 if (nb_services
== 1) /* only one service (FIXME: should depend on OWN_PROCESS etc.) */
321 for (i
= 0; i
< nb_services
; i
++)
322 if (!strcmpiW( name
, services
[i
]->name
)) return services
[i
];
326 /******************************************************************************
329 * Call into the main service routine provided by StartServiceCtrlDispatcher.
331 static DWORD WINAPI
service_thread(LPVOID arg
)
333 service_data
*info
= arg
;
334 LPWSTR str
= info
->args
;
335 DWORD argc
= 0, len
= 0;
341 len
+= strlenW(&str
[len
]) + 1;
350 argv
= heap_alloc((argc
+1)*sizeof(LPWSTR
));
351 for (argc
=0, p
=str
; *p
; p
+= strlenW(p
) + 1)
355 info
->proc
.w(argc
, argv
);
360 LPSTR strA
, *argv
, p
;
363 lenA
= WideCharToMultiByte(CP_ACP
,0, str
, len
, NULL
, 0, NULL
, NULL
);
364 strA
= heap_alloc(lenA
);
365 WideCharToMultiByte(CP_ACP
,0, str
, len
, strA
, lenA
, NULL
, NULL
);
367 argv
= heap_alloc((argc
+1)*sizeof(LPSTR
));
368 for (argc
=0, p
=strA
; *p
; p
+= strlen(p
) + 1)
372 info
->proc
.a(argc
, argv
);
379 /******************************************************************************
380 * service_handle_start
382 static DWORD
service_handle_start(service_data
*service
, const WCHAR
*data
, DWORD count
)
384 TRACE("%s argsize %u\n", debugstr_w(service
->name
), count
);
388 WARN("service is not stopped\n");
389 return ERROR_SERVICE_ALREADY_RUNNING
;
392 heap_free(service
->args
);
393 service
->args
= heap_alloc(count
* sizeof(WCHAR
));
394 memcpy( service
->args
, data
, count
* sizeof(WCHAR
) );
395 service
->thread
= CreateThread( NULL
, 0, service_thread
,
397 SetEvent( service_event
); /* notify the main loop */
401 /******************************************************************************
402 * service_handle_control
404 static DWORD
service_handle_control(const service_data
*service
, DWORD dwControl
)
406 DWORD ret
= ERROR_INVALID_SERVICE_CONTROL
;
408 TRACE("%s control %u\n", debugstr_w(service
->name
), dwControl
);
410 if (service
->handler
)
411 ret
= service
->handler(dwControl
, 0, NULL
, service
->context
);
415 /******************************************************************************
416 * service_control_dispatcher
418 static DWORD WINAPI
service_control_dispatcher(LPVOID arg
)
420 dispatcher_data
*disp
= arg
;
422 /* dispatcher loop */
425 service_data
*service
;
426 service_start_info info
;
429 DWORD data_size
= 0, count
, result
;
431 r
= ReadFile( disp
->pipe
, &info
, FIELD_OFFSET(service_start_info
,data
), &count
, NULL
);
434 if (GetLastError() != ERROR_BROKEN_PIPE
)
435 ERR( "pipe read failed error %u\n", GetLastError() );
438 if (count
!= FIELD_OFFSET(service_start_info
,data
))
440 ERR( "partial pipe read %u\n", count
);
443 if (count
< info
.total_size
)
445 data_size
= info
.total_size
- FIELD_OFFSET(service_start_info
,data
);
446 data
= heap_alloc( data_size
);
447 r
= ReadFile( disp
->pipe
, data
, data_size
, &count
, NULL
);
450 if (GetLastError() != ERROR_BROKEN_PIPE
)
451 ERR( "pipe read failed error %u\n", GetLastError() );
455 if (count
!= data_size
)
457 ERR( "partial pipe read %u/%u\n", count
, data_size
);
463 /* find the service */
465 if (!(service
= find_service_by_name( data
)))
467 FIXME( "got request %u for unknown service %s\n", info
.cmd
, debugstr_w(data
));
468 result
= ERROR_INVALID_PARAMETER
;
472 TRACE( "got request %u for service %s\n", info
.cmd
, debugstr_w(data
) );
474 /* handle the request */
477 case WINESERV_STARTINFO
:
478 if (!service
->handle
)
480 if (!(service
->handle
= OpenServiceW( disp
->manager
, data
, SERVICE_SET_STATUS
)) ||
481 !(service
->full_access_handle
= OpenServiceW( disp
->manager
, data
,
482 GENERIC_READ
|GENERIC_WRITE
)))
483 FIXME( "failed to open service %s\n", debugstr_w(data
) );
485 result
= service_handle_start(service
, data
, data_size
/ sizeof(WCHAR
));
487 case WINESERV_SENDCONTROL
:
488 result
= service_handle_control(service
, info
.control
);
491 ERR("received invalid command %u\n", info
.cmd
);
492 result
= ERROR_INVALID_PARAMETER
;
497 WriteFile( disp
->pipe
, &result
, sizeof(result
), &count
, NULL
);
501 CloseHandle( disp
->pipe
);
502 CloseServiceHandle( disp
->manager
);
507 /******************************************************************************
508 * service_run_main_thread
510 static BOOL
service_run_main_thread(void)
513 HANDLE wait_handles
[MAXIMUM_WAIT_OBJECTS
];
514 UINT wait_services
[MAXIMUM_WAIT_OBJECTS
];
515 dispatcher_data
*disp
= heap_alloc( sizeof(*disp
) );
517 disp
->manager
= OpenSCManagerW( NULL
, NULL
, SC_MANAGER_CONNECT
);
520 ERR("failed to open service manager error %u\n", GetLastError());
525 disp
->pipe
= service_open_pipe();
526 if (disp
->pipe
== INVALID_HANDLE_VALUE
)
528 WARN("failed to create control pipe error %u\n", GetLastError());
529 CloseServiceHandle( disp
->manager
);
531 SetLastError( ERROR_FAILED_SERVICE_CONTROLLER_CONNECT
);
535 service_event
= CreateEventW( NULL
, FALSE
, FALSE
, NULL
);
536 stop_event
= CreateEventW( NULL
, FALSE
, FALSE
, NULL
);
538 /* FIXME: service_control_dispatcher should be merged into the main thread */
539 wait_handles
[0] = __wine_make_process_system();
540 wait_handles
[1] = CreateThread( NULL
, 0, service_control_dispatcher
, disp
, 0, NULL
);
541 wait_handles
[2] = service_event
;
542 wait_handles
[3] = stop_event
;
544 TRACE("Starting %d services running as process %d\n",
545 nb_services
, GetCurrentProcessId());
547 /* wait for all the threads to pack up and exit */
550 EnterCriticalSection( &service_cs
);
551 for (i
= 0, n
= 4; i
< nb_services
&& n
< MAXIMUM_WAIT_OBJECTS
; i
++)
553 if (!services
[i
]->thread
) continue;
554 wait_services
[n
] = i
;
555 wait_handles
[n
++] = services
[i
]->thread
;
557 LeaveCriticalSection( &service_cs
);
559 ret
= WaitForMultipleObjects( n
, wait_handles
, FALSE
, INFINITE
);
560 if (!ret
) /* system process event */
563 SERVICE_PRESHUTDOWN_INFO spi
;
564 DWORD timeout
= 5000;
567 EnterCriticalSection( &service_cs
);
569 for (i
= 0; i
< nb_services
&& n
< MAXIMUM_WAIT_OBJECTS
; i
++)
571 if (!services
[i
]->thread
) continue;
573 res
= QueryServiceStatus(services
[i
]->full_access_handle
, &st
);
575 if (res
&& (st
.dwControlsAccepted
& SERVICE_ACCEPT_PRESHUTDOWN
))
577 res
= QueryServiceConfig2W( services
[i
]->full_access_handle
, SERVICE_CONFIG_PRESHUTDOWN_INFO
,
578 (LPBYTE
)&spi
, sizeof(spi
), &i
);
581 FIXME("service should be able to delay shutdown\n");
582 timeout
+= spi
.dwPreshutdownTimeout
;
583 ret
= service_handle_control( services
[i
], SERVICE_CONTROL_PRESHUTDOWN
);
584 wait_handles
[n
++] = services
[i
]->thread
;
587 else if (res
&& (st
.dwControlsAccepted
& SERVICE_ACCEPT_SHUTDOWN
))
589 ret
= service_handle_control( services
[i
], SERVICE_CONTROL_SHUTDOWN
);
590 wait_handles
[n
++] = services
[i
]->thread
;
593 LeaveCriticalSection( &service_cs
);
595 TRACE("last user process exited, shutting down (timeout: %d)\n", timeout
);
596 WaitForMultipleObjects( n
, wait_handles
, TRUE
, timeout
);
601 TRACE( "control dispatcher exited, shutting down\n" );
602 /* FIXME: we should maybe send a shutdown control to running services */
607 continue; /* rebuild the list */
615 services
[wait_services
[ret
]]->thread
= 0;
616 CloseHandle( wait_handles
[ret
] );
622 /******************************************************************************
623 * StartServiceCtrlDispatcherA [ADVAPI32.@]
625 * See StartServiceCtrlDispatcherW.
627 BOOL WINAPI
StartServiceCtrlDispatcherA( const SERVICE_TABLE_ENTRYA
*servent
)
632 TRACE("%p\n", servent
);
636 SetLastError( ERROR_SERVICE_ALREADY_RUNNING
);
639 while (servent
[nb_services
].lpServiceName
) nb_services
++;
642 SetLastError( ERROR_INVALID_PARAMETER
);
646 services
= heap_alloc( nb_services
* sizeof(*services
) );
648 for (i
= 0; i
< nb_services
; i
++)
650 DWORD len
= MultiByteToWideChar(CP_ACP
, 0, servent
[i
].lpServiceName
, -1, NULL
, 0);
651 DWORD sz
= FIELD_OFFSET( service_data
, name
[len
] );
652 info
= heap_alloc_zero( sz
);
653 MultiByteToWideChar(CP_ACP
, 0, servent
[i
].lpServiceName
, -1, info
->name
, len
);
654 info
->proc
.a
= servent
[i
].lpServiceProc
;
655 info
->unicode
= FALSE
;
659 return service_run_main_thread();
662 /******************************************************************************
663 * StartServiceCtrlDispatcherW [ADVAPI32.@]
665 * Connects a process containing one or more services to the service control
669 * servent [I] A list of the service names and service procedures
675 BOOL WINAPI
StartServiceCtrlDispatcherW( const SERVICE_TABLE_ENTRYW
*servent
)
680 TRACE("%p\n", servent
);
684 SetLastError( ERROR_SERVICE_ALREADY_RUNNING
);
687 while (servent
[nb_services
].lpServiceName
) nb_services
++;
690 SetLastError( ERROR_INVALID_PARAMETER
);
694 services
= heap_alloc( nb_services
* sizeof(*services
) );
696 for (i
= 0; i
< nb_services
; i
++)
698 DWORD len
= strlenW(servent
[i
].lpServiceName
) + 1;
699 DWORD sz
= FIELD_OFFSET( service_data
, name
[len
] );
700 info
= heap_alloc_zero( sz
);
701 strcpyW(info
->name
, servent
[i
].lpServiceName
);
702 info
->proc
.w
= servent
[i
].lpServiceProc
;
703 info
->unicode
= TRUE
;
707 return service_run_main_thread();
710 /******************************************************************************
711 * LockServiceDatabase [ADVAPI32.@]
713 SC_LOCK WINAPI
LockServiceDatabase (SC_HANDLE hSCManager
)
715 SC_RPC_LOCK hLock
= NULL
;
718 TRACE("%p\n",hSCManager
);
722 err
= svcctl_LockServiceDatabase(hSCManager
, &hLock
);
726 err
= map_exception_code(GetExceptionCode());
729 if (err
!= ERROR_SUCCESS
)
737 /******************************************************************************
738 * UnlockServiceDatabase [ADVAPI32.@]
740 BOOL WINAPI
UnlockServiceDatabase (SC_LOCK ScLock
)
743 SC_RPC_LOCK hRpcLock
= ScLock
;
745 TRACE("%p\n",ScLock
);
749 err
= svcctl_UnlockServiceDatabase(&hRpcLock
);
753 err
= map_exception_code(GetExceptionCode());
756 if (err
!= ERROR_SUCCESS
)
764 /******************************************************************************
765 * SetServiceStatus [ADVAPI32.@]
772 SetServiceStatus( SERVICE_STATUS_HANDLE hService
, LPSERVICE_STATUS lpStatus
)
776 TRACE("%p %x %x %x %x %x %x %x\n", hService
,
777 lpStatus
->dwServiceType
, lpStatus
->dwCurrentState
,
778 lpStatus
->dwControlsAccepted
, lpStatus
->dwWin32ExitCode
,
779 lpStatus
->dwServiceSpecificExitCode
, lpStatus
->dwCheckPoint
,
780 lpStatus
->dwWaitHint
);
784 err
= svcctl_SetServiceStatus( hService
, lpStatus
);
788 err
= map_exception_code(GetExceptionCode());
791 if (err
!= ERROR_SUCCESS
)
797 if (lpStatus
->dwCurrentState
== SERVICE_STOPPED
) {
798 SetEvent(stop_event
);
799 CloseServiceHandle((SC_HANDLE
)hService
);
806 /******************************************************************************
807 * OpenSCManagerA [ADVAPI32.@]
809 * Establish a connection to the service control manager and open its database.
812 * lpMachineName [I] Pointer to machine name string
813 * lpDatabaseName [I] Pointer to database name string
814 * dwDesiredAccess [I] Type of access
817 * Success: A Handle to the service control manager database
820 SC_HANDLE WINAPI
OpenSCManagerA( LPCSTR lpMachineName
, LPCSTR lpDatabaseName
,
821 DWORD dwDesiredAccess
)
823 LPWSTR machineW
, databaseW
;
826 machineW
= SERV_dup(lpMachineName
);
827 databaseW
= SERV_dup(lpDatabaseName
);
828 ret
= OpenSCManagerW(machineW
, databaseW
, dwDesiredAccess
);
829 heap_free(databaseW
);
834 /******************************************************************************
835 * OpenSCManagerW [ADVAPI32.@]
837 * See OpenSCManagerA.
839 DWORD
SERV_OpenSCManagerW( LPCWSTR lpMachineName
, LPCWSTR lpDatabaseName
,
840 DWORD dwDesiredAccess
, SC_HANDLE
*handle
)
844 TRACE("(%s,%s,0x%08x)\n", debugstr_w(lpMachineName
),
845 debugstr_w(lpDatabaseName
), dwDesiredAccess
);
849 r
= svcctl_OpenSCManagerW(lpMachineName
, lpDatabaseName
, dwDesiredAccess
, (SC_RPC_HANDLE
*)handle
);
853 r
= map_exception_code(GetExceptionCode());
857 if (r
!=ERROR_SUCCESS
)
860 TRACE("returning %p\n", *handle
);
864 SC_HANDLE WINAPI
OpenSCManagerW( LPCWSTR lpMachineName
, LPCWSTR lpDatabaseName
,
865 DWORD dwDesiredAccess
)
867 SC_HANDLE handle
= 0;
870 r
= SERV_OpenSCManagerW(lpMachineName
, lpDatabaseName
, dwDesiredAccess
, &handle
);
871 if (r
!=ERROR_SUCCESS
)
876 /******************************************************************************
877 * ControlService [ADVAPI32.@]
879 * Send a control code to a service.
882 * hService [I] Handle of the service control manager database
883 * dwControl [I] Control code to send (SERVICE_CONTROL_* flags from "winsvc.h")
884 * lpServiceStatus [O] Destination for the status of the service, if available
891 * Unlike M$' implementation, control requests are not serialized and may be
892 * processed asynchronously.
894 BOOL WINAPI
ControlService( SC_HANDLE hService
, DWORD dwControl
,
895 LPSERVICE_STATUS lpServiceStatus
)
899 TRACE("%p %d %p\n", hService
, dwControl
, lpServiceStatus
);
903 err
= svcctl_ControlService(hService
, dwControl
, lpServiceStatus
);
907 err
= map_exception_code(GetExceptionCode());
910 if (err
!= ERROR_SUCCESS
)
919 /******************************************************************************
920 * CloseServiceHandle [ADVAPI32.@]
922 * Close a handle to a service or the service control manager database.
925 * hSCObject [I] Handle to service or service control manager database
932 CloseServiceHandle( SC_HANDLE hSCObject
)
936 TRACE("%p\n", hSCObject
);
940 err
= svcctl_CloseServiceHandle((SC_RPC_HANDLE
*)&hSCObject
);
944 err
= map_exception_code(GetExceptionCode());
948 if (err
!= ERROR_SUCCESS
)
957 /******************************************************************************
958 * OpenServiceA [ADVAPI32.@]
960 * Open a handle to a service.
963 * hSCManager [I] Handle of the service control manager database
964 * lpServiceName [I] Name of the service to open
965 * dwDesiredAccess [I] Access required to the service
968 * Success: Handle to the service
971 SC_HANDLE WINAPI
OpenServiceA( SC_HANDLE hSCManager
, LPCSTR lpServiceName
,
972 DWORD dwDesiredAccess
)
974 LPWSTR lpServiceNameW
;
977 TRACE("%p %s 0x%08x\n", hSCManager
, debugstr_a(lpServiceName
), dwDesiredAccess
);
979 lpServiceNameW
= SERV_dup(lpServiceName
);
980 ret
= OpenServiceW( hSCManager
, lpServiceNameW
, dwDesiredAccess
);
981 heap_free(lpServiceNameW
);
986 /******************************************************************************
987 * OpenServiceW [ADVAPI32.@]
991 DWORD
SERV_OpenServiceW( SC_HANDLE hSCManager
, LPCWSTR lpServiceName
,
992 DWORD dwDesiredAccess
, SC_HANDLE
*handle
)
996 TRACE("%p %s 0x%08x\n", hSCManager
, debugstr_w(lpServiceName
), dwDesiredAccess
);
999 return ERROR_INVALID_HANDLE
;
1003 err
= svcctl_OpenServiceW(hSCManager
, lpServiceName
, dwDesiredAccess
, (SC_RPC_HANDLE
*)handle
);
1005 __EXCEPT(rpc_filter
)
1007 err
= map_exception_code(GetExceptionCode());
1011 if (err
!= ERROR_SUCCESS
)
1014 TRACE("returning %p\n", *handle
);
1018 SC_HANDLE WINAPI
OpenServiceW( SC_HANDLE hSCManager
, LPCWSTR lpServiceName
,
1019 DWORD dwDesiredAccess
)
1021 SC_HANDLE handle
= 0;
1024 err
= SERV_OpenServiceW(hSCManager
, lpServiceName
, dwDesiredAccess
, &handle
);
1025 if (err
!= ERROR_SUCCESS
)
1030 /******************************************************************************
1031 * CreateServiceW [ADVAPI32.@]
1034 CreateServiceW( SC_HANDLE hSCManager
, LPCWSTR lpServiceName
,
1035 LPCWSTR lpDisplayName
, DWORD dwDesiredAccess
,
1036 DWORD dwServiceType
, DWORD dwStartType
,
1037 DWORD dwErrorControl
, LPCWSTR lpBinaryPathName
,
1038 LPCWSTR lpLoadOrderGroup
, LPDWORD lpdwTagId
,
1039 LPCWSTR lpDependencies
, LPCWSTR lpServiceStartName
,
1040 LPCWSTR lpPassword
)
1042 SC_HANDLE handle
= 0;
1046 TRACE("%p %s %s\n", hSCManager
,
1047 debugstr_w(lpServiceName
), debugstr_w(lpDisplayName
));
1051 SetLastError( ERROR_INVALID_HANDLE
);
1056 passwdlen
= (strlenW(lpPassword
) + 1) * sizeof(WCHAR
);
1064 IsWow64Process(GetCurrentProcess(), &is_wow64
);
1067 err
= svcctl_CreateServiceWOW64W(hSCManager
, lpServiceName
,
1068 lpDisplayName
, dwDesiredAccess
, dwServiceType
, dwStartType
, dwErrorControl
,
1069 lpBinaryPathName
, lpLoadOrderGroup
, lpdwTagId
, (const BYTE
*)lpDependencies
,
1070 multisz_cb(lpDependencies
), lpServiceStartName
, (const BYTE
*)lpPassword
, passwdlen
,
1071 (SC_RPC_HANDLE
*)&handle
);
1073 err
= svcctl_CreateServiceW(hSCManager
, lpServiceName
,
1074 lpDisplayName
, dwDesiredAccess
, dwServiceType
, dwStartType
, dwErrorControl
,
1075 lpBinaryPathName
, lpLoadOrderGroup
, lpdwTagId
, (const BYTE
*)lpDependencies
,
1076 multisz_cb(lpDependencies
), lpServiceStartName
, (const BYTE
*)lpPassword
, passwdlen
,
1077 (SC_RPC_HANDLE
*)&handle
);
1079 __EXCEPT(rpc_filter
)
1081 err
= map_exception_code(GetExceptionCode());
1085 if (err
!= ERROR_SUCCESS
)
1094 /******************************************************************************
1095 * CreateServiceA [ADVAPI32.@]
1098 CreateServiceA( SC_HANDLE hSCManager
, LPCSTR lpServiceName
,
1099 LPCSTR lpDisplayName
, DWORD dwDesiredAccess
,
1100 DWORD dwServiceType
, DWORD dwStartType
,
1101 DWORD dwErrorControl
, LPCSTR lpBinaryPathName
,
1102 LPCSTR lpLoadOrderGroup
, LPDWORD lpdwTagId
,
1103 LPCSTR lpDependencies
, LPCSTR lpServiceStartName
,
1106 LPWSTR lpServiceNameW
, lpDisplayNameW
, lpBinaryPathNameW
,
1107 lpLoadOrderGroupW
, lpDependenciesW
, lpServiceStartNameW
, lpPasswordW
;
1110 TRACE("%p %s %s\n", hSCManager
,
1111 debugstr_a(lpServiceName
), debugstr_a(lpDisplayName
));
1113 lpServiceNameW
= SERV_dup( lpServiceName
);
1114 lpDisplayNameW
= SERV_dup( lpDisplayName
);
1115 lpBinaryPathNameW
= SERV_dup( lpBinaryPathName
);
1116 lpLoadOrderGroupW
= SERV_dup( lpLoadOrderGroup
);
1117 lpDependenciesW
= SERV_dupmulti( lpDependencies
);
1118 lpServiceStartNameW
= SERV_dup( lpServiceStartName
);
1119 lpPasswordW
= SERV_dup( lpPassword
);
1121 r
= CreateServiceW( hSCManager
, lpServiceNameW
, lpDisplayNameW
,
1122 dwDesiredAccess
, dwServiceType
, dwStartType
, dwErrorControl
,
1123 lpBinaryPathNameW
, lpLoadOrderGroupW
, lpdwTagId
,
1124 lpDependenciesW
, lpServiceStartNameW
, lpPasswordW
);
1126 heap_free( lpServiceNameW
);
1127 heap_free( lpDisplayNameW
);
1128 heap_free( lpBinaryPathNameW
);
1129 heap_free( lpLoadOrderGroupW
);
1130 heap_free( lpDependenciesW
);
1131 heap_free( lpServiceStartNameW
);
1132 heap_free( lpPasswordW
);
1138 /******************************************************************************
1139 * DeleteService [ADVAPI32.@]
1141 * Delete a service from the service control manager database.
1144 * hService [I] Handle of the service to delete
1150 BOOL WINAPI
DeleteService( SC_HANDLE hService
)
1154 TRACE("%p\n", hService
);
1158 err
= svcctl_DeleteService(hService
);
1160 __EXCEPT(rpc_filter
)
1162 err
= map_exception_code(GetExceptionCode());
1175 /******************************************************************************
1176 * StartServiceA [ADVAPI32.@]
1181 * hService [I] Handle of service
1182 * dwNumServiceArgs [I] Number of arguments
1183 * lpServiceArgVectors [I] Address of array of argument strings
1186 * - NT implements this function using an obscure RPC call.
1187 * - You might need to do a "setenv SystemRoot \\WINNT" in your .cshrc
1188 * to get things like "%SystemRoot%\\System32\\service.exe" to load.
1189 * - This will only work for shared address space. How should the service
1190 * args be transferred when address spaces are separated?
1191 * - Can only start one service at a time.
1192 * - Has no concept of privilege.
1198 BOOL WINAPI
StartServiceA( SC_HANDLE hService
, DWORD dwNumServiceArgs
,
1199 LPCSTR
*lpServiceArgVectors
)
1201 LPWSTR
*lpwstr
=NULL
;
1205 TRACE("(%p,%d,%p)\n",hService
,dwNumServiceArgs
,lpServiceArgVectors
);
1207 if (dwNumServiceArgs
)
1208 lpwstr
= heap_alloc( dwNumServiceArgs
*sizeof(LPWSTR
) );
1210 for(i
=0; i
<dwNumServiceArgs
; i
++)
1211 lpwstr
[i
]=SERV_dup(lpServiceArgVectors
[i
]);
1213 r
= StartServiceW(hService
, dwNumServiceArgs
, (LPCWSTR
*)lpwstr
);
1215 if (dwNumServiceArgs
)
1217 for(i
=0; i
<dwNumServiceArgs
; i
++)
1218 heap_free(lpwstr
[i
]);
1226 /******************************************************************************
1227 * StartServiceW [ADVAPI32.@]
1229 * See StartServiceA.
1231 BOOL WINAPI
StartServiceW(SC_HANDLE hService
, DWORD dwNumServiceArgs
,
1232 LPCWSTR
*lpServiceArgVectors
)
1236 TRACE("%p %d %p\n", hService
, dwNumServiceArgs
, lpServiceArgVectors
);
1240 err
= svcctl_StartServiceW(hService
, dwNumServiceArgs
, lpServiceArgVectors
);
1242 __EXCEPT(rpc_filter
)
1244 err
= map_exception_code(GetExceptionCode());
1247 if (err
!= ERROR_SUCCESS
)
1256 /******************************************************************************
1257 * QueryServiceStatus [ADVAPI32.@]
1260 * hService [I] Handle to service to get information about
1261 * lpservicestatus [O] buffer to receive the status information for the service
1264 BOOL WINAPI
QueryServiceStatus(SC_HANDLE hService
,
1265 LPSERVICE_STATUS lpservicestatus
)
1267 SERVICE_STATUS_PROCESS SvcStatusData
;
1271 TRACE("%p %p\n", hService
, lpservicestatus
);
1275 SetLastError(ERROR_INVALID_HANDLE
);
1278 if (!lpservicestatus
)
1280 SetLastError(ERROR_INVALID_ADDRESS
);
1284 ret
= QueryServiceStatusEx(hService
, SC_STATUS_PROCESS_INFO
, (LPBYTE
)&SvcStatusData
,
1285 sizeof(SERVICE_STATUS_PROCESS
), &dummy
);
1286 if (ret
) memcpy(lpservicestatus
, &SvcStatusData
, sizeof(SERVICE_STATUS
)) ;
1291 /******************************************************************************
1292 * QueryServiceStatusEx [ADVAPI32.@]
1294 * Get information about a service.
1297 * hService [I] Handle to service to get information about
1298 * InfoLevel [I] Level of information to get
1299 * lpBuffer [O] Destination for requested information
1300 * cbBufSize [I] Size of lpBuffer in bytes
1301 * pcbBytesNeeded [O] Destination for number of bytes needed, if cbBufSize is too small
1307 BOOL WINAPI
QueryServiceStatusEx(SC_HANDLE hService
, SC_STATUS_TYPE InfoLevel
,
1308 LPBYTE lpBuffer
, DWORD cbBufSize
,
1309 LPDWORD pcbBytesNeeded
)
1313 TRACE("%p %d %p %d %p\n", hService
, InfoLevel
, lpBuffer
, cbBufSize
, pcbBytesNeeded
);
1315 if (InfoLevel
!= SC_STATUS_PROCESS_INFO
)
1317 err
= ERROR_INVALID_LEVEL
;
1319 else if (cbBufSize
< sizeof(SERVICE_STATUS_PROCESS
))
1321 *pcbBytesNeeded
= sizeof(SERVICE_STATUS_PROCESS
);
1322 err
= ERROR_INSUFFICIENT_BUFFER
;
1328 err
= svcctl_QueryServiceStatusEx(hService
, InfoLevel
, lpBuffer
, cbBufSize
, pcbBytesNeeded
);
1330 __EXCEPT(rpc_filter
)
1332 err
= map_exception_code(GetExceptionCode());
1336 if (err
!= ERROR_SUCCESS
)
1344 /******************************************************************************
1345 * QueryServiceConfigA [ADVAPI32.@]
1347 BOOL WINAPI
QueryServiceConfigA( SC_HANDLE hService
, LPQUERY_SERVICE_CONFIGA config
,
1348 DWORD size
, LPDWORD needed
)
1353 QUERY_SERVICE_CONFIGW
*configW
;
1355 TRACE("%p %p %d %p\n", hService
, config
, size
, needed
);
1357 if (!(buffer
= heap_alloc( 2 * size
)))
1359 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1362 configW
= (QUERY_SERVICE_CONFIGW
*)buffer
;
1363 ret
= QueryServiceConfigW( hService
, configW
, 2 * size
, needed
);
1364 if (!ret
) goto done
;
1366 config
->dwServiceType
= configW
->dwServiceType
;
1367 config
->dwStartType
= configW
->dwStartType
;
1368 config
->dwErrorControl
= configW
->dwErrorControl
;
1369 config
->lpBinaryPathName
= NULL
;
1370 config
->lpLoadOrderGroup
= NULL
;
1371 config
->dwTagId
= configW
->dwTagId
;
1372 config
->lpDependencies
= NULL
;
1373 config
->lpServiceStartName
= NULL
;
1374 config
->lpDisplayName
= NULL
;
1376 p
= (LPSTR
)(config
+ 1);
1377 n
= size
- sizeof(*config
);
1380 #define MAP_STR(str) \
1384 DWORD sz = WideCharToMultiByte( CP_ACP, 0, configW->str, -1, p, n, NULL, NULL ); \
1385 if (!sz) goto done; \
1392 MAP_STR( lpBinaryPathName
);
1393 MAP_STR( lpLoadOrderGroup
);
1394 MAP_STR( lpDependencies
);
1395 MAP_STR( lpServiceStartName
);
1396 MAP_STR( lpDisplayName
);
1399 *needed
= p
- (LPSTR
)config
;
1403 heap_free( buffer
);
1407 static DWORD
move_string_to_buffer(BYTE
**buf
, LPWSTR
*string_ptr
)
1414 memset(*buf
, 0, cb
);
1418 cb
= (strlenW(*string_ptr
) + 1)*sizeof(WCHAR
);
1419 memcpy(*buf
, *string_ptr
, cb
);
1420 MIDL_user_free(*string_ptr
);
1423 *string_ptr
= (LPWSTR
)*buf
;
1429 static DWORD
size_string(LPCWSTR string
)
1431 return (string
? (strlenW(string
) + 1)*sizeof(WCHAR
) : sizeof(WCHAR
));
1434 /******************************************************************************
1435 * QueryServiceConfigW [ADVAPI32.@]
1438 QueryServiceConfigW( SC_HANDLE hService
,
1439 LPQUERY_SERVICE_CONFIGW lpServiceConfig
,
1440 DWORD cbBufSize
, LPDWORD pcbBytesNeeded
)
1442 QUERY_SERVICE_CONFIGW config
;
1447 TRACE("%p %p %d %p\n", hService
, lpServiceConfig
,
1448 cbBufSize
, pcbBytesNeeded
);
1450 memset(&config
, 0, sizeof(config
));
1454 err
= svcctl_QueryServiceConfigW(hService
, &config
, cbBufSize
, pcbBytesNeeded
);
1456 __EXCEPT(rpc_filter
)
1458 err
= map_exception_code(GetExceptionCode());
1462 if (err
!= ERROR_SUCCESS
)
1464 TRACE("services.exe: error %u\n", err
);
1469 /* calculate the size required first */
1470 total
= sizeof (QUERY_SERVICE_CONFIGW
);
1471 total
+= size_string(config
.lpBinaryPathName
);
1472 total
+= size_string(config
.lpLoadOrderGroup
);
1473 total
+= size_string(config
.lpDependencies
);
1474 total
+= size_string(config
.lpServiceStartName
);
1475 total
+= size_string(config
.lpDisplayName
);
1477 *pcbBytesNeeded
= total
;
1479 /* if there's not enough memory, return an error */
1480 if( total
> cbBufSize
)
1482 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1483 MIDL_user_free(config
.lpBinaryPathName
);
1484 MIDL_user_free(config
.lpLoadOrderGroup
);
1485 MIDL_user_free(config
.lpDependencies
);
1486 MIDL_user_free(config
.lpServiceStartName
);
1487 MIDL_user_free(config
.lpDisplayName
);
1491 *lpServiceConfig
= config
;
1492 bufpos
= ((BYTE
*)lpServiceConfig
) + sizeof(QUERY_SERVICE_CONFIGW
);
1493 move_string_to_buffer(&bufpos
, &lpServiceConfig
->lpBinaryPathName
);
1494 move_string_to_buffer(&bufpos
, &lpServiceConfig
->lpLoadOrderGroup
);
1495 move_string_to_buffer(&bufpos
, &lpServiceConfig
->lpDependencies
);
1496 move_string_to_buffer(&bufpos
, &lpServiceConfig
->lpServiceStartName
);
1497 move_string_to_buffer(&bufpos
, &lpServiceConfig
->lpDisplayName
);
1499 TRACE("Image path = %s\n", debugstr_w(lpServiceConfig
->lpBinaryPathName
) );
1500 TRACE("Group = %s\n", debugstr_w(lpServiceConfig
->lpLoadOrderGroup
) );
1501 TRACE("Dependencies = %s\n", debugstr_w(lpServiceConfig
->lpDependencies
) );
1502 TRACE("Service account name = %s\n", debugstr_w(lpServiceConfig
->lpServiceStartName
) );
1503 TRACE("Display name = %s\n", debugstr_w(lpServiceConfig
->lpDisplayName
) );
1508 /******************************************************************************
1509 * QueryServiceConfig2A [ADVAPI32.@]
1512 * observed under win2k:
1513 * The functions QueryServiceConfig2A and QueryServiceConfig2W return the same
1514 * required buffer size (in byte) at least for dwLevel SERVICE_CONFIG_DESCRIPTION
1516 BOOL WINAPI
QueryServiceConfig2A(SC_HANDLE hService
, DWORD dwLevel
, LPBYTE buffer
,
1517 DWORD size
, LPDWORD needed
)
1520 LPBYTE bufferW
= NULL
;
1523 bufferW
= heap_alloc(size
);
1525 ret
= QueryServiceConfig2W(hService
, dwLevel
, bufferW
, size
, needed
);
1526 if(!ret
) goto cleanup
;
1529 case SERVICE_CONFIG_DESCRIPTION
:
1530 if (buffer
&& bufferW
) {
1531 LPSERVICE_DESCRIPTIONA configA
= (LPSERVICE_DESCRIPTIONA
) buffer
;
1532 LPSERVICE_DESCRIPTIONW configW
= (LPSERVICE_DESCRIPTIONW
) bufferW
;
1533 if (configW
->lpDescription
&& (size
> sizeof(SERVICE_DESCRIPTIONA
))) {
1535 configA
->lpDescription
= (LPSTR
)(configA
+ 1);
1536 sz
= WideCharToMultiByte( CP_ACP
, 0, configW
->lpDescription
, -1,
1537 configA
->lpDescription
, size
- sizeof(SERVICE_DESCRIPTIONA
), NULL
, NULL
);
1539 FIXME("WideCharToMultiByte failed for configW->lpDescription\n");
1541 configA
->lpDescription
= NULL
;
1544 else configA
->lpDescription
= NULL
;
1547 case SERVICE_CONFIG_PRESHUTDOWN_INFO
:
1548 if (buffer
&& bufferW
&& *needed
<=size
)
1549 memcpy(buffer
, bufferW
, *needed
);
1552 FIXME("conversation W->A not implemented for level %d\n", dwLevel
);
1558 heap_free( bufferW
);
1562 /******************************************************************************
1563 * QueryServiceConfig2W [ADVAPI32.@]
1565 * See QueryServiceConfig2A.
1567 BOOL WINAPI
QueryServiceConfig2W(SC_HANDLE hService
, DWORD dwLevel
, LPBYTE buffer
,
1568 DWORD size
, LPDWORD needed
)
1572 if(dwLevel
!=SERVICE_CONFIG_DESCRIPTION
&& dwLevel
!=SERVICE_CONFIG_PRESHUTDOWN_INFO
) {
1573 FIXME("Level %d not implemented\n", dwLevel
);
1574 SetLastError(ERROR_INVALID_LEVEL
);
1578 if(!buffer
&& size
) {
1579 SetLastError(ERROR_INVALID_ADDRESS
);
1583 TRACE("%p 0x%d %p 0x%d %p\n", hService
, dwLevel
, buffer
, size
, needed
);
1587 err
= svcctl_QueryServiceConfig2W(hService
, dwLevel
, buffer
, size
, needed
);
1589 __EXCEPT(rpc_filter
)
1591 err
= map_exception_code(GetExceptionCode());
1595 if (err
!= ERROR_SUCCESS
)
1597 SetLastError( err
);
1603 case SERVICE_CONFIG_DESCRIPTION
:
1606 SERVICE_DESCRIPTIONW
*descr
= (SERVICE_DESCRIPTIONW
*)buffer
;
1607 if (descr
->lpDescription
) /* make it an absolute pointer */
1608 descr
->lpDescription
= (WCHAR
*)(buffer
+ (ULONG_PTR
)descr
->lpDescription
);
1616 /******************************************************************************
1617 * EnumServicesStatusA [ADVAPI32.@]
1620 EnumServicesStatusA( SC_HANDLE hmngr
, DWORD type
, DWORD state
, LPENUM_SERVICE_STATUSA
1621 services
, DWORD size
, LPDWORD needed
, LPDWORD returned
,
1622 LPDWORD resume_handle
)
1626 ENUM_SERVICE_STATUSW
*servicesW
= NULL
;
1630 TRACE("%p 0x%x 0x%x %p %u %p %p %p\n", hmngr
, type
, state
, services
, size
, needed
,
1631 returned
, resume_handle
);
1633 sz
= max( 2 * size
, sizeof(*servicesW
) );
1634 if (!(servicesW
= heap_alloc( sz
)))
1636 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1640 ret
= EnumServicesStatusW( hmngr
, type
, state
, servicesW
, sz
, needed
, returned
, resume_handle
);
1641 if (!ret
) goto done
;
1643 p
= (char *)services
+ *returned
* sizeof(ENUM_SERVICE_STATUSA
);
1644 n
= size
- (p
- (char *)services
);
1646 for (i
= 0; i
< *returned
; i
++)
1648 sz
= WideCharToMultiByte( CP_ACP
, 0, servicesW
[i
].lpServiceName
, -1, p
, n
, NULL
, NULL
);
1650 services
[i
].lpServiceName
= p
;
1653 if (servicesW
[i
].lpDisplayName
)
1655 sz
= WideCharToMultiByte( CP_ACP
, 0, servicesW
[i
].lpDisplayName
, -1, p
, n
, NULL
, NULL
);
1657 services
[i
].lpDisplayName
= p
;
1661 else services
[i
].lpDisplayName
= NULL
;
1662 services
[i
].ServiceStatus
= servicesW
[i
].ServiceStatus
;
1668 heap_free( servicesW
);
1672 /******************************************************************************
1673 * EnumServicesStatusW [ADVAPI32.@]
1676 EnumServicesStatusW( SC_HANDLE hmngr
, DWORD type
, DWORD state
, LPENUM_SERVICE_STATUSW
1677 services
, DWORD size
, LPDWORD needed
, LPDWORD returned
,
1678 LPDWORD resume_handle
)
1681 ENUM_SERVICE_STATUSW dummy_status
;
1683 TRACE("%p 0x%x 0x%x %p %u %p %p %p\n", hmngr
, type
, state
, services
, size
, needed
,
1684 returned
, resume_handle
);
1688 SetLastError( ERROR_INVALID_HANDLE
);
1692 /* make sure we pass a valid pointer */
1693 if (!services
|| size
< sizeof(*services
))
1695 services
= &dummy_status
;
1696 size
= sizeof(dummy_status
);
1701 err
= svcctl_EnumServicesStatusW( hmngr
, type
, state
, (BYTE
*)services
, size
, needed
, returned
, resume_handle
);
1703 __EXCEPT(rpc_filter
)
1705 err
= map_exception_code( GetExceptionCode() );
1709 if (err
!= ERROR_SUCCESS
)
1711 SetLastError( err
);
1715 for (i
= 0; i
< *returned
; i
++)
1717 /* convert buffer offsets into pointers */
1718 services
[i
].lpServiceName
= (WCHAR
*)((char *)services
+ (DWORD_PTR
)services
[i
].lpServiceName
);
1719 if (services
[i
].lpDisplayName
)
1720 services
[i
].lpDisplayName
= (WCHAR
*)((char *)services
+ (DWORD_PTR
)services
[i
].lpDisplayName
);
1726 /******************************************************************************
1727 * EnumServicesStatusExA [ADVAPI32.@]
1730 EnumServicesStatusExA( SC_HANDLE hmngr
, SC_ENUM_TYPE level
, DWORD type
, DWORD state
,
1731 LPBYTE buffer
, DWORD size
, LPDWORD needed
, LPDWORD returned
,
1732 LPDWORD resume_handle
, LPCSTR group
)
1736 ENUM_SERVICE_STATUS_PROCESSA
*services
= (ENUM_SERVICE_STATUS_PROCESSA
*)buffer
;
1737 ENUM_SERVICE_STATUS_PROCESSW
*servicesW
= NULL
;
1738 WCHAR
*groupW
= NULL
;
1742 TRACE("%p %u 0x%x 0x%x %p %u %p %p %p %s\n", hmngr
, level
, type
, state
, buffer
,
1743 size
, needed
, returned
, resume_handle
, debugstr_a(group
));
1745 sz
= max( 2 * size
, sizeof(*servicesW
) );
1746 if (!(servicesW
= heap_alloc( sz
)))
1748 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1753 int len
= MultiByteToWideChar( CP_ACP
, 0, group
, -1, NULL
, 0 );
1754 if (!(groupW
= heap_alloc( len
* sizeof(WCHAR
) )))
1756 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1757 heap_free( servicesW
);
1760 MultiByteToWideChar( CP_ACP
, 0, group
, -1, groupW
, len
* sizeof(WCHAR
) );
1763 ret
= EnumServicesStatusExW( hmngr
, level
, type
, state
, (BYTE
*)servicesW
, sz
,
1764 needed
, returned
, resume_handle
, groupW
);
1765 if (!ret
) goto done
;
1767 p
= (char *)services
+ *returned
* sizeof(ENUM_SERVICE_STATUS_PROCESSA
);
1768 n
= size
- (p
- (char *)services
);
1770 for (i
= 0; i
< *returned
; i
++)
1772 sz
= WideCharToMultiByte( CP_ACP
, 0, servicesW
[i
].lpServiceName
, -1, p
, n
, NULL
, NULL
);
1774 services
[i
].lpServiceName
= p
;
1777 if (servicesW
[i
].lpDisplayName
)
1779 sz
= WideCharToMultiByte( CP_ACP
, 0, servicesW
[i
].lpDisplayName
, -1, p
, n
, NULL
, NULL
);
1781 services
[i
].lpDisplayName
= p
;
1785 else services
[i
].lpDisplayName
= NULL
;
1786 services
[i
].ServiceStatusProcess
= servicesW
[i
].ServiceStatusProcess
;
1792 heap_free( servicesW
);
1793 heap_free( groupW
);
1797 /******************************************************************************
1798 * EnumServicesStatusExW [ADVAPI32.@]
1801 EnumServicesStatusExW( SC_HANDLE hmngr
, SC_ENUM_TYPE level
, DWORD type
, DWORD state
,
1802 LPBYTE buffer
, DWORD size
, LPDWORD needed
, LPDWORD returned
,
1803 LPDWORD resume_handle
, LPCWSTR group
)
1806 ENUM_SERVICE_STATUS_PROCESSW dummy_status
;
1807 ENUM_SERVICE_STATUS_PROCESSW
*services
= (ENUM_SERVICE_STATUS_PROCESSW
*)buffer
;
1809 TRACE("%p %u 0x%x 0x%x %p %u %p %p %p %s\n", hmngr
, level
, type
, state
, buffer
,
1810 size
, needed
, returned
, resume_handle
, debugstr_w(group
));
1812 if (level
!= SC_ENUM_PROCESS_INFO
)
1814 SetLastError( ERROR_INVALID_LEVEL
);
1819 SetLastError( ERROR_INVALID_HANDLE
);
1823 /* make sure we pass a valid buffer pointer */
1824 if (!services
|| size
< sizeof(*services
))
1826 buffer
= (BYTE
*)&dummy_status
;
1827 size
= sizeof(dummy_status
);
1832 err
= svcctl_EnumServicesStatusExW( hmngr
, SC_ENUM_PROCESS_INFO
, type
, state
, buffer
, size
, needed
,
1833 returned
, resume_handle
, group
);
1835 __EXCEPT(rpc_filter
)
1837 err
= map_exception_code( GetExceptionCode() );
1841 if (err
!= ERROR_SUCCESS
)
1843 SetLastError( err
);
1847 for (i
= 0; i
< *returned
; i
++)
1849 /* convert buffer offsets into pointers */
1850 services
[i
].lpServiceName
= (WCHAR
*)((char *)services
+ (DWORD_PTR
)services
[i
].lpServiceName
);
1851 if (services
[i
].lpDisplayName
)
1852 services
[i
].lpDisplayName
= (WCHAR
*)((char *)services
+ (DWORD_PTR
)services
[i
].lpDisplayName
);
1858 /******************************************************************************
1859 * GetServiceKeyNameA [ADVAPI32.@]
1861 BOOL WINAPI
GetServiceKeyNameA( SC_HANDLE hSCManager
, LPCSTR lpDisplayName
,
1862 LPSTR lpServiceName
, LPDWORD lpcchBuffer
)
1864 LPWSTR lpDisplayNameW
, lpServiceNameW
;
1868 TRACE("%p %s %p %p\n", hSCManager
,
1869 debugstr_a(lpDisplayName
), lpServiceName
, lpcchBuffer
);
1871 lpDisplayNameW
= SERV_dup(lpDisplayName
);
1873 lpServiceNameW
= heap_alloc(*lpcchBuffer
* sizeof(WCHAR
));
1875 lpServiceNameW
= NULL
;
1877 sizeW
= *lpcchBuffer
;
1878 if (!GetServiceKeyNameW(hSCManager
, lpDisplayNameW
, lpServiceNameW
, &sizeW
))
1880 if (lpServiceName
&& *lpcchBuffer
)
1881 lpServiceName
[0] = 0;
1882 *lpcchBuffer
= sizeW
*2; /* we can only provide an upper estimation of string length */
1886 if (!WideCharToMultiByte(CP_ACP
, 0, lpServiceNameW
, (sizeW
+ 1), lpServiceName
,
1887 *lpcchBuffer
, NULL
, NULL
))
1889 if (*lpcchBuffer
&& lpServiceName
)
1890 lpServiceName
[0] = 0;
1891 *lpcchBuffer
= WideCharToMultiByte(CP_ACP
, 0, lpServiceNameW
, -1, NULL
, 0, NULL
, NULL
);
1895 /* lpcchBuffer not updated - same as in GetServiceDisplayNameA */
1899 heap_free(lpServiceNameW
);
1900 heap_free(lpDisplayNameW
);
1904 /******************************************************************************
1905 * GetServiceKeyNameW [ADVAPI32.@]
1907 BOOL WINAPI
GetServiceKeyNameW( SC_HANDLE hSCManager
, LPCWSTR lpDisplayName
,
1908 LPWSTR lpServiceName
, LPDWORD lpcchBuffer
)
1914 TRACE("%p %s %p %p\n", hSCManager
,
1915 debugstr_w(lpDisplayName
), lpServiceName
, lpcchBuffer
);
1919 SetLastError( ERROR_INVALID_HANDLE
);
1923 /* provide a buffer if the caller didn't */
1924 if (!lpServiceName
|| *lpcchBuffer
< 2)
1926 lpServiceName
= buffer
;
1927 /* A size of 1 would be enough, but tests show that Windows returns 2,
1928 * probably because of a WCHAR/bytes mismatch in their code.
1933 /* RPC call takes size excluding nul-terminator, whereas *lpcchBuffer
1934 * includes the nul-terminator on input. */
1935 size
= *lpcchBuffer
- 1;
1939 err
= svcctl_GetServiceKeyNameW(hSCManager
, lpDisplayName
, lpServiceName
,
1942 __EXCEPT(rpc_filter
)
1944 err
= map_exception_code(GetExceptionCode());
1948 /* The value of *lpcchBuffer excludes nul-terminator on output. */
1949 if (err
== ERROR_SUCCESS
|| err
== ERROR_INSUFFICIENT_BUFFER
)
1950 *lpcchBuffer
= size
;
1954 return err
== ERROR_SUCCESS
;
1957 /******************************************************************************
1958 * QueryServiceLockStatusA [ADVAPI32.@]
1960 BOOL WINAPI
QueryServiceLockStatusA( SC_HANDLE hSCManager
,
1961 LPQUERY_SERVICE_LOCK_STATUSA lpLockStatus
,
1962 DWORD cbBufSize
, LPDWORD pcbBytesNeeded
)
1964 FIXME("%p %p %08x %p\n", hSCManager
, lpLockStatus
, cbBufSize
, pcbBytesNeeded
);
1969 /******************************************************************************
1970 * QueryServiceLockStatusW [ADVAPI32.@]
1972 BOOL WINAPI
QueryServiceLockStatusW( SC_HANDLE hSCManager
,
1973 LPQUERY_SERVICE_LOCK_STATUSW lpLockStatus
,
1974 DWORD cbBufSize
, LPDWORD pcbBytesNeeded
)
1976 FIXME("%p %p %08x %p\n", hSCManager
, lpLockStatus
, cbBufSize
, pcbBytesNeeded
);
1981 /******************************************************************************
1982 * GetServiceDisplayNameA [ADVAPI32.@]
1984 BOOL WINAPI
GetServiceDisplayNameA( SC_HANDLE hSCManager
, LPCSTR lpServiceName
,
1985 LPSTR lpDisplayName
, LPDWORD lpcchBuffer
)
1987 LPWSTR lpServiceNameW
, lpDisplayNameW
;
1991 TRACE("%p %s %p %p\n", hSCManager
,
1992 debugstr_a(lpServiceName
), lpDisplayName
, lpcchBuffer
);
1994 lpServiceNameW
= SERV_dup(lpServiceName
);
1996 lpDisplayNameW
= heap_alloc(*lpcchBuffer
* sizeof(WCHAR
));
1998 lpDisplayNameW
= NULL
;
2000 sizeW
= *lpcchBuffer
;
2001 if (!GetServiceDisplayNameW(hSCManager
, lpServiceNameW
, lpDisplayNameW
, &sizeW
))
2003 if (lpDisplayName
&& *lpcchBuffer
)
2004 lpDisplayName
[0] = 0;
2005 *lpcchBuffer
= sizeW
*2; /* we can only provide an upper estimation of string length */
2009 if (!WideCharToMultiByte(CP_ACP
, 0, lpDisplayNameW
, (sizeW
+ 1), lpDisplayName
,
2010 *lpcchBuffer
, NULL
, NULL
))
2012 if (*lpcchBuffer
&& lpDisplayName
)
2013 lpDisplayName
[0] = 0;
2014 *lpcchBuffer
= WideCharToMultiByte(CP_ACP
, 0, lpDisplayNameW
, -1, NULL
, 0, NULL
, NULL
);
2018 /* probably due to a bug GetServiceDisplayNameA doesn't modify lpcchBuffer on success.
2019 * (but if the function succeeded it means that is a good upper estimation of the size) */
2023 heap_free(lpDisplayNameW
);
2024 heap_free(lpServiceNameW
);
2028 /******************************************************************************
2029 * GetServiceDisplayNameW [ADVAPI32.@]
2031 BOOL WINAPI
GetServiceDisplayNameW( SC_HANDLE hSCManager
, LPCWSTR lpServiceName
,
2032 LPWSTR lpDisplayName
, LPDWORD lpcchBuffer
)
2038 TRACE("%p %s %p %p\n", hSCManager
,
2039 debugstr_w(lpServiceName
), lpDisplayName
, lpcchBuffer
);
2043 SetLastError( ERROR_INVALID_HANDLE
);
2047 /* provide a buffer if the caller didn't */
2048 if (!lpDisplayName
|| *lpcchBuffer
< 2)
2050 lpDisplayName
= buffer
;
2051 /* A size of 1 would be enough, but tests show that Windows returns 2,
2052 * probably because of a WCHAR/bytes mismatch in their code.
2057 /* RPC call takes size excluding nul-terminator, whereas *lpcchBuffer
2058 * includes the nul-terminator on input. */
2059 size
= *lpcchBuffer
- 1;
2063 err
= svcctl_GetServiceDisplayNameW(hSCManager
, lpServiceName
, lpDisplayName
,
2066 __EXCEPT(rpc_filter
)
2068 err
= map_exception_code(GetExceptionCode());
2072 /* The value of *lpcchBuffer excludes nul-terminator on output. */
2073 if (err
== ERROR_SUCCESS
|| err
== ERROR_INSUFFICIENT_BUFFER
)
2074 *lpcchBuffer
= size
;
2078 return err
== ERROR_SUCCESS
;
2081 /******************************************************************************
2082 * ChangeServiceConfigW [ADVAPI32.@]
2084 BOOL WINAPI
ChangeServiceConfigW( SC_HANDLE hService
, DWORD dwServiceType
,
2085 DWORD dwStartType
, DWORD dwErrorControl
, LPCWSTR lpBinaryPathName
,
2086 LPCWSTR lpLoadOrderGroup
, LPDWORD lpdwTagId
, LPCWSTR lpDependencies
,
2087 LPCWSTR lpServiceStartName
, LPCWSTR lpPassword
, LPCWSTR lpDisplayName
)
2092 TRACE("%p %d %d %d %s %s %p %p %s %s %s\n",
2093 hService
, dwServiceType
, dwStartType
, dwErrorControl
,
2094 debugstr_w(lpBinaryPathName
), debugstr_w(lpLoadOrderGroup
),
2095 lpdwTagId
, lpDependencies
, debugstr_w(lpServiceStartName
),
2096 debugstr_w(lpPassword
), debugstr_w(lpDisplayName
) );
2098 cb_pwd
= lpPassword
? (strlenW(lpPassword
) + 1)*sizeof(WCHAR
) : 0;
2102 err
= svcctl_ChangeServiceConfigW(hService
, dwServiceType
,
2103 dwStartType
, dwErrorControl
, lpBinaryPathName
, lpLoadOrderGroup
, lpdwTagId
,
2104 (const BYTE
*)lpDependencies
, multisz_cb(lpDependencies
), lpServiceStartName
,
2105 (const BYTE
*)lpPassword
, cb_pwd
, lpDisplayName
);
2107 __EXCEPT(rpc_filter
)
2109 err
= map_exception_code(GetExceptionCode());
2113 if (err
!= ERROR_SUCCESS
)
2116 return err
== ERROR_SUCCESS
;
2119 /******************************************************************************
2120 * ChangeServiceConfigA [ADVAPI32.@]
2122 BOOL WINAPI
ChangeServiceConfigA( SC_HANDLE hService
, DWORD dwServiceType
,
2123 DWORD dwStartType
, DWORD dwErrorControl
, LPCSTR lpBinaryPathName
,
2124 LPCSTR lpLoadOrderGroup
, LPDWORD lpdwTagId
, LPCSTR lpDependencies
,
2125 LPCSTR lpServiceStartName
, LPCSTR lpPassword
, LPCSTR lpDisplayName
)
2127 LPWSTR wBinaryPathName
, wLoadOrderGroup
, wDependencies
;
2128 LPWSTR wServiceStartName
, wPassword
, wDisplayName
;
2131 TRACE("%p %d %d %d %s %s %p %p %s %s %s\n",
2132 hService
, dwServiceType
, dwStartType
, dwErrorControl
,
2133 debugstr_a(lpBinaryPathName
), debugstr_a(lpLoadOrderGroup
),
2134 lpdwTagId
, lpDependencies
, debugstr_a(lpServiceStartName
),
2135 debugstr_a(lpPassword
), debugstr_a(lpDisplayName
) );
2137 wBinaryPathName
= SERV_dup( lpBinaryPathName
);
2138 wLoadOrderGroup
= SERV_dup( lpLoadOrderGroup
);
2139 wDependencies
= SERV_dupmulti( lpDependencies
);
2140 wServiceStartName
= SERV_dup( lpServiceStartName
);
2141 wPassword
= SERV_dup( lpPassword
);
2142 wDisplayName
= SERV_dup( lpDisplayName
);
2144 r
= ChangeServiceConfigW( hService
, dwServiceType
,
2145 dwStartType
, dwErrorControl
, wBinaryPathName
,
2146 wLoadOrderGroup
, lpdwTagId
, wDependencies
,
2147 wServiceStartName
, wPassword
, wDisplayName
);
2149 heap_free( wBinaryPathName
);
2150 heap_free( wLoadOrderGroup
);
2151 heap_free( wDependencies
);
2152 heap_free( wServiceStartName
);
2153 heap_free( wPassword
);
2154 heap_free( wDisplayName
);
2159 /******************************************************************************
2160 * ChangeServiceConfig2A [ADVAPI32.@]
2162 BOOL WINAPI
ChangeServiceConfig2A( SC_HANDLE hService
, DWORD dwInfoLevel
,
2167 TRACE("%p %d %p\n",hService
, dwInfoLevel
, lpInfo
);
2169 if (dwInfoLevel
== SERVICE_CONFIG_DESCRIPTION
)
2171 LPSERVICE_DESCRIPTIONA sd
= lpInfo
;
2172 SERVICE_DESCRIPTIONW sdw
;
2174 sdw
.lpDescription
= SERV_dup( sd
->lpDescription
);
2176 r
= ChangeServiceConfig2W( hService
, dwInfoLevel
, &sdw
);
2178 heap_free( sdw
.lpDescription
);
2180 else if (dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
2182 LPSERVICE_FAILURE_ACTIONSA fa
= lpInfo
;
2183 SERVICE_FAILURE_ACTIONSW faw
;
2185 faw
.dwResetPeriod
= fa
->dwResetPeriod
;
2186 faw
.lpRebootMsg
= SERV_dup( fa
->lpRebootMsg
);
2187 faw
.lpCommand
= SERV_dup( fa
->lpCommand
);
2188 faw
.cActions
= fa
->cActions
;
2189 faw
.lpsaActions
= fa
->lpsaActions
;
2191 r
= ChangeServiceConfig2W( hService
, dwInfoLevel
, &faw
);
2193 heap_free( faw
.lpRebootMsg
);
2194 heap_free( faw
.lpCommand
);
2196 else if (dwInfoLevel
== SERVICE_CONFIG_PRESHUTDOWN_INFO
)
2198 r
= ChangeServiceConfig2W( hService
, dwInfoLevel
, lpInfo
);
2201 SetLastError( ERROR_INVALID_PARAMETER
);
2206 /******************************************************************************
2207 * ChangeServiceConfig2W [ADVAPI32.@]
2209 BOOL WINAPI
ChangeServiceConfig2W( SC_HANDLE hService
, DWORD dwInfoLevel
,
2216 SC_RPC_CONFIG_INFOW info
;
2218 info
.dwInfoLevel
= dwInfoLevel
;
2219 info
.u
.descr
= lpInfo
;
2220 err
= svcctl_ChangeServiceConfig2W( hService
, info
);
2222 __EXCEPT(rpc_filter
)
2224 err
= map_exception_code(GetExceptionCode());
2228 if (err
!= ERROR_SUCCESS
)
2231 return err
== ERROR_SUCCESS
;
2234 NTSTATUS
SERV_QueryServiceObjectSecurity(SC_HANDLE hService
,
2235 SECURITY_INFORMATION dwSecurityInformation
,
2236 PSECURITY_DESCRIPTOR lpSecurityDescriptor
,
2237 DWORD cbBufSize
, LPDWORD pcbBytesNeeded
)
2239 SECURITY_DESCRIPTOR descriptor
;
2244 FIXME("%p %d %p %u %p - semi-stub\n", hService
, dwSecurityInformation
,
2245 lpSecurityDescriptor
, cbBufSize
, pcbBytesNeeded
);
2247 if (dwSecurityInformation
!= DACL_SECURITY_INFORMATION
)
2248 FIXME("information %d not supported\n", dwSecurityInformation
);
2250 InitializeSecurityDescriptor(&descriptor
, SECURITY_DESCRIPTOR_REVISION
);
2252 InitializeAcl(&acl
, sizeof(ACL
), ACL_REVISION
);
2253 SetSecurityDescriptorDacl(&descriptor
, TRUE
, &acl
, TRUE
);
2256 status
= RtlMakeSelfRelativeSD(&descriptor
, lpSecurityDescriptor
, &size
);
2257 *pcbBytesNeeded
= size
;
2261 /******************************************************************************
2262 * QueryServiceObjectSecurity [ADVAPI32.@]
2264 BOOL WINAPI
QueryServiceObjectSecurity(SC_HANDLE hService
,
2265 SECURITY_INFORMATION dwSecurityInformation
,
2266 PSECURITY_DESCRIPTOR lpSecurityDescriptor
,
2267 DWORD cbBufSize
, LPDWORD pcbBytesNeeded
)
2269 NTSTATUS status
= SERV_QueryServiceObjectSecurity(hService
, dwSecurityInformation
, lpSecurityDescriptor
,
2270 cbBufSize
, pcbBytesNeeded
);
2271 if (status
!= STATUS_SUCCESS
)
2273 SetLastError(RtlNtStatusToDosError(status
));
2279 /******************************************************************************
2280 * SetServiceObjectSecurity [ADVAPI32.@]
2283 * - SetSecurityInfo should be updated to call this function once it's implemented.
2285 BOOL WINAPI
SetServiceObjectSecurity(SC_HANDLE hService
,
2286 SECURITY_INFORMATION dwSecurityInformation
,
2287 PSECURITY_DESCRIPTOR lpSecurityDescriptor
)
2289 FIXME("%p %d %p\n", hService
, dwSecurityInformation
, lpSecurityDescriptor
);
2293 /******************************************************************************
2294 * SetServiceBits [ADVAPI32.@]
2296 BOOL WINAPI
SetServiceBits( SERVICE_STATUS_HANDLE hServiceStatus
,
2297 DWORD dwServiceBits
,
2299 BOOL bUpdateImmediately
)
2301 FIXME("%p %08x %x %x\n", hServiceStatus
, dwServiceBits
,
2302 bSetBitsOn
, bUpdateImmediately
);
2306 /* thunk for calling the RegisterServiceCtrlHandler handler function */
2307 static DWORD WINAPI
ctrl_handler_thunk( DWORD control
, DWORD type
, void *data
, void *context
)
2309 LPHANDLER_FUNCTION func
= context
;
2312 return ERROR_SUCCESS
;
2315 /******************************************************************************
2316 * RegisterServiceCtrlHandlerA [ADVAPI32.@]
2318 SERVICE_STATUS_HANDLE WINAPI
RegisterServiceCtrlHandlerA( LPCSTR name
, LPHANDLER_FUNCTION handler
)
2320 return RegisterServiceCtrlHandlerExA( name
, ctrl_handler_thunk
, handler
);
2323 /******************************************************************************
2324 * RegisterServiceCtrlHandlerW [ADVAPI32.@]
2326 SERVICE_STATUS_HANDLE WINAPI
RegisterServiceCtrlHandlerW( LPCWSTR name
, LPHANDLER_FUNCTION handler
)
2328 return RegisterServiceCtrlHandlerExW( name
, ctrl_handler_thunk
, handler
);
2331 /******************************************************************************
2332 * RegisterServiceCtrlHandlerExA [ADVAPI32.@]
2334 SERVICE_STATUS_HANDLE WINAPI
RegisterServiceCtrlHandlerExA( LPCSTR name
, LPHANDLER_FUNCTION_EX handler
, LPVOID context
)
2337 SERVICE_STATUS_HANDLE ret
;
2339 nameW
= SERV_dup(name
);
2340 ret
= RegisterServiceCtrlHandlerExW( nameW
, handler
, context
);
2345 /******************************************************************************
2346 * RegisterServiceCtrlHandlerExW [ADVAPI32.@]
2348 SERVICE_STATUS_HANDLE WINAPI
RegisterServiceCtrlHandlerExW( LPCWSTR lpServiceName
,
2349 LPHANDLER_FUNCTION_EX lpHandlerProc
, LPVOID lpContext
)
2351 service_data
*service
;
2352 SC_HANDLE hService
= 0;
2355 TRACE("%s %p %p\n", debugstr_w(lpServiceName
), lpHandlerProc
, lpContext
);
2357 EnterCriticalSection( &service_cs
);
2358 if ((service
= find_service_by_name( lpServiceName
)))
2360 service
->handler
= lpHandlerProc
;
2361 service
->context
= lpContext
;
2362 hService
= service
->handle
;
2365 LeaveCriticalSection( &service_cs
);
2367 if (!found
) SetLastError(ERROR_SERVICE_DOES_NOT_EXIST
);
2369 return (SERVICE_STATUS_HANDLE
)hService
;
2372 /******************************************************************************
2373 * EnumDependentServicesA [ADVAPI32.@]
2375 BOOL WINAPI
EnumDependentServicesA( SC_HANDLE hService
, DWORD dwServiceState
,
2376 LPENUM_SERVICE_STATUSA lpServices
, DWORD cbBufSize
,
2377 LPDWORD pcbBytesNeeded
, LPDWORD lpServicesReturned
)
2379 FIXME("%p 0x%08x %p 0x%08x %p %p - stub\n", hService
, dwServiceState
,
2380 lpServices
, cbBufSize
, pcbBytesNeeded
, lpServicesReturned
);
2382 *lpServicesReturned
= 0;
2386 /******************************************************************************
2387 * EnumDependentServicesW [ADVAPI32.@]
2389 BOOL WINAPI
EnumDependentServicesW( SC_HANDLE hService
, DWORD dwServiceState
,
2390 LPENUM_SERVICE_STATUSW lpServices
, DWORD cbBufSize
,
2391 LPDWORD pcbBytesNeeded
, LPDWORD lpServicesReturned
)
2393 FIXME("%p 0x%08x %p 0x%08x %p %p - stub\n", hService
, dwServiceState
,
2394 lpServices
, cbBufSize
, pcbBytesNeeded
, lpServicesReturned
);
2396 *lpServicesReturned
= 0;
2400 /******************************************************************************
2401 * NotifyServiceStatusChangeW [ADVAPI32.@]
2403 DWORD WINAPI
NotifyServiceStatusChangeW(SC_HANDLE hService
, DWORD dwNotifyMask
,
2404 SERVICE_NOTIFYW
*pNotifyBuffer
)
2408 SERVICE_STATUS_PROCESS st
;
2410 FIXME("%p 0x%x %p - semi-stub\n", hService
, dwNotifyMask
, pNotifyBuffer
);
2412 ret
= QueryServiceStatusEx(hService
, SC_STATUS_PROCESS_INFO
, (void*)&st
, sizeof(st
), &dummy
);
2415 /* dwNotifyMask is a set of bitflags in same order as SERVICE_ statuses */
2416 if (dwNotifyMask
& (1 << (st
.dwCurrentState
- SERVICE_STOPPED
)))
2418 pNotifyBuffer
->dwNotificationStatus
= ERROR_SUCCESS
;
2419 memcpy(&pNotifyBuffer
->ServiceStatus
, &st
, sizeof(pNotifyBuffer
->ServiceStatus
));
2420 pNotifyBuffer
->dwNotificationTriggered
= 1 << (st
.dwCurrentState
- SERVICE_STOPPED
);
2421 pNotifyBuffer
->pszServiceNames
= NULL
;
2422 TRACE("Queueing notification: 0x%x\n", pNotifyBuffer
->dwNotificationTriggered
);
2423 QueueUserAPC((PAPCFUNC
)pNotifyBuffer
->pfnNotifyCallback
,
2424 GetCurrentThread(), (ULONG_PTR
)pNotifyBuffer
);
2428 /* TODO: If the service is not currently in a matching state, we should
2429 * tell `services` to monitor it. */
2431 return ERROR_SUCCESS
;