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 BOOL stop_service
;
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 void *data
, DWORD data_size
)
384 DWORD count
= data_size
/ sizeof(WCHAR
);
388 WARN("service is not stopped\n");
389 return ERROR_SERVICE_ALREADY_RUNNING
;
392 heap_free(service
->args
);
393 service
->args
= heap_alloc((count
+ 2) * sizeof(WCHAR
));
394 if (count
) memcpy( service
->args
, data
, count
* sizeof(WCHAR
) );
395 service
->args
[count
++] = 0;
396 service
->args
[count
++] = 0;
398 service
->thread
= CreateThread( NULL
, 0, service_thread
,
400 SetEvent( service_event
); /* notify the main loop */
404 /******************************************************************************
405 * service_handle_control
407 static DWORD
service_handle_control(service_data
*service
, DWORD control
, const void *data
, DWORD data_size
)
409 DWORD ret
= ERROR_INVALID_SERVICE_CONTROL
;
411 TRACE("%s control %u data %p data_size %u\n", debugstr_w(service
->name
), control
, data
, data_size
);
413 if (control
== SERVICE_CONTROL_START
)
414 ret
= service_handle_start(service
, data
, data_size
);
415 else if (service
->handler
)
416 ret
= service
->handler(control
, 0, (void *)data
, service
->context
);
420 /******************************************************************************
421 * service_control_dispatcher
423 static DWORD WINAPI
service_control_dispatcher(LPVOID arg
)
425 dispatcher_data
*disp
= arg
;
427 /* dispatcher loop */
430 service_data
*service
;
431 service_start_info info
;
435 DWORD data_size
= 0, count
, result
;
437 r
= ReadFile( disp
->pipe
, &info
, FIELD_OFFSET(service_start_info
,data
), &count
, NULL
);
440 if (GetLastError() != ERROR_BROKEN_PIPE
)
441 ERR( "pipe read failed error %u\n", GetLastError() );
444 if (count
!= FIELD_OFFSET(service_start_info
,data
))
446 ERR( "partial pipe read %u\n", count
);
449 if (count
< info
.total_size
)
451 data_size
= info
.total_size
- FIELD_OFFSET(service_start_info
,data
);
452 data
= heap_alloc( data_size
);
453 r
= ReadFile( disp
->pipe
, data
, data_size
, &count
, NULL
);
456 if (GetLastError() != ERROR_BROKEN_PIPE
)
457 ERR( "pipe read failed error %u\n", GetLastError() );
461 if (count
!= data_size
)
463 ERR( "partial pipe read %u/%u\n", count
, data_size
);
469 EnterCriticalSection( &service_cs
);
471 /* validate service name */
472 name
= (WCHAR
*)data
;
473 if (!info
.name_size
|| data_size
< info
.name_size
* sizeof(WCHAR
) || name
[info
.name_size
- 1])
475 ERR( "got request without valid service name\n" );
476 result
= ERROR_INVALID_PARAMETER
;
480 if (info
.magic
!= SERVICE_PROTOCOL_MAGIC
)
482 ERR( "received invalid request for service %s\n", debugstr_w(name
) );
483 result
= ERROR_INVALID_PARAMETER
;
487 /* find the service */
488 if (!(service
= find_service_by_name( name
)))
490 FIXME( "got request for unknown service %s\n", debugstr_w(name
) );
491 result
= ERROR_INVALID_PARAMETER
;
495 if (!service
->handle
)
497 if (!(service
->handle
= OpenServiceW( disp
->manager
, name
, SERVICE_SET_STATUS
)) ||
498 !(service
->full_access_handle
= OpenServiceW( disp
->manager
, name
,
499 GENERIC_READ
|GENERIC_WRITE
)))
500 FIXME( "failed to open service %s\n", debugstr_w(name
) );
503 data_size
-= info
.name_size
* sizeof(WCHAR
);
504 result
= service_handle_control(service
, info
.control
, data_size
?
505 &data
[info
.name_size
* sizeof(WCHAR
)] : NULL
, data_size
);
508 LeaveCriticalSection( &service_cs
);
509 WriteFile( disp
->pipe
, &result
, sizeof(result
), &count
, NULL
);
513 CloseHandle( disp
->pipe
);
514 CloseServiceHandle( disp
->manager
);
519 /******************************************************************************
520 * service_run_main_thread
522 static BOOL
service_run_main_thread(void)
525 HANDLE wait_handles
[MAXIMUM_WAIT_OBJECTS
];
526 UINT wait_services
[MAXIMUM_WAIT_OBJECTS
];
527 dispatcher_data
*disp
= heap_alloc( sizeof(*disp
) );
529 disp
->manager
= OpenSCManagerW( NULL
, NULL
, SC_MANAGER_CONNECT
);
532 ERR("failed to open service manager error %u\n", GetLastError());
537 disp
->pipe
= service_open_pipe();
538 if (disp
->pipe
== INVALID_HANDLE_VALUE
)
540 WARN("failed to create control pipe error %u\n", GetLastError());
541 CloseServiceHandle( disp
->manager
);
543 SetLastError( ERROR_FAILED_SERVICE_CONTROLLER_CONNECT
);
547 service_event
= CreateEventW( NULL
, FALSE
, FALSE
, NULL
);
548 stop_service
= FALSE
;
550 /* FIXME: service_control_dispatcher should be merged into the main thread */
551 wait_handles
[0] = __wine_make_process_system();
552 wait_handles
[1] = CreateThread( NULL
, 0, service_control_dispatcher
, disp
, 0, NULL
);
553 wait_handles
[2] = service_event
;
555 TRACE("Starting %d services running as process %d\n",
556 nb_services
, GetCurrentProcessId());
558 /* wait for all the threads to pack up and exit */
559 while (!stop_service
)
561 EnterCriticalSection( &service_cs
);
562 for (i
= 0, n
= 3; i
< nb_services
&& n
< MAXIMUM_WAIT_OBJECTS
; i
++)
564 if (!services
[i
]->thread
) continue;
565 wait_services
[n
] = i
;
566 wait_handles
[n
++] = services
[i
]->thread
;
568 LeaveCriticalSection( &service_cs
);
570 ret
= WaitForMultipleObjects( n
, wait_handles
, FALSE
, INFINITE
);
571 if (!ret
) /* system process event */
574 SERVICE_PRESHUTDOWN_INFO spi
;
575 DWORD timeout
= 5000;
578 EnterCriticalSection( &service_cs
);
580 for (i
= 0; i
< nb_services
&& n
< MAXIMUM_WAIT_OBJECTS
; i
++)
582 if (!services
[i
]->thread
) continue;
584 res
= QueryServiceStatus(services
[i
]->full_access_handle
, &st
);
586 if (res
&& (st
.dwControlsAccepted
& SERVICE_ACCEPT_PRESHUTDOWN
))
588 res
= QueryServiceConfig2W( services
[i
]->full_access_handle
, SERVICE_CONFIG_PRESHUTDOWN_INFO
,
589 (LPBYTE
)&spi
, sizeof(spi
), &i
);
592 FIXME("service should be able to delay shutdown\n");
593 timeout
+= spi
.dwPreshutdownTimeout
;
594 ret
= service_handle_control( services
[i
], SERVICE_CONTROL_PRESHUTDOWN
, NULL
, 0 );
595 wait_handles
[n
++] = services
[i
]->thread
;
598 else if (res
&& (st
.dwControlsAccepted
& SERVICE_ACCEPT_SHUTDOWN
))
600 ret
= service_handle_control( services
[i
], SERVICE_CONTROL_SHUTDOWN
, NULL
, 0 );
601 wait_handles
[n
++] = services
[i
]->thread
;
604 LeaveCriticalSection( &service_cs
);
606 TRACE("last user process exited, shutting down (timeout: %d)\n", timeout
);
607 WaitForMultipleObjects( n
, wait_handles
, TRUE
, timeout
);
612 TRACE( "control dispatcher exited, shutting down\n" );
613 /* FIXME: we should maybe send a shutdown control to running services */
618 continue; /* rebuild the list */
622 i
= wait_services
[ret
];
623 EnterCriticalSection( &service_cs
);
624 CloseHandle( services
[i
]->thread
);
625 services
[i
]->thread
= NULL
;
626 LeaveCriticalSection( &service_cs
);
634 /******************************************************************************
635 * StartServiceCtrlDispatcherA [ADVAPI32.@]
637 * See StartServiceCtrlDispatcherW.
639 BOOL WINAPI
StartServiceCtrlDispatcherA( const SERVICE_TABLE_ENTRYA
*servent
)
644 TRACE("%p\n", servent
);
648 SetLastError( ERROR_SERVICE_ALREADY_RUNNING
);
651 while (servent
[nb_services
].lpServiceName
) nb_services
++;
654 SetLastError( ERROR_INVALID_PARAMETER
);
658 services
= heap_alloc( nb_services
* sizeof(*services
) );
660 for (i
= 0; i
< nb_services
; i
++)
662 DWORD len
= MultiByteToWideChar(CP_ACP
, 0, servent
[i
].lpServiceName
, -1, NULL
, 0);
663 DWORD sz
= FIELD_OFFSET( service_data
, name
[len
] );
664 info
= heap_alloc_zero( sz
);
665 MultiByteToWideChar(CP_ACP
, 0, servent
[i
].lpServiceName
, -1, info
->name
, len
);
666 info
->proc
.a
= servent
[i
].lpServiceProc
;
667 info
->unicode
= FALSE
;
671 return service_run_main_thread();
674 /******************************************************************************
675 * StartServiceCtrlDispatcherW [ADVAPI32.@]
677 * Connects a process containing one or more services to the service control
681 * servent [I] A list of the service names and service procedures
687 BOOL WINAPI
StartServiceCtrlDispatcherW( const SERVICE_TABLE_ENTRYW
*servent
)
692 TRACE("%p\n", servent
);
696 SetLastError( ERROR_SERVICE_ALREADY_RUNNING
);
699 while (servent
[nb_services
].lpServiceName
) nb_services
++;
702 SetLastError( ERROR_INVALID_PARAMETER
);
706 services
= heap_alloc( nb_services
* sizeof(*services
) );
708 for (i
= 0; i
< nb_services
; i
++)
710 DWORD len
= strlenW(servent
[i
].lpServiceName
) + 1;
711 DWORD sz
= FIELD_OFFSET( service_data
, name
[len
] );
712 info
= heap_alloc_zero( sz
);
713 strcpyW(info
->name
, servent
[i
].lpServiceName
);
714 info
->proc
.w
= servent
[i
].lpServiceProc
;
715 info
->unicode
= TRUE
;
719 return service_run_main_thread();
722 /******************************************************************************
723 * LockServiceDatabase [ADVAPI32.@]
725 SC_LOCK WINAPI
LockServiceDatabase (SC_HANDLE hSCManager
)
727 SC_RPC_LOCK hLock
= NULL
;
730 TRACE("%p\n",hSCManager
);
734 err
= svcctl_LockServiceDatabase(hSCManager
, &hLock
);
738 err
= map_exception_code(GetExceptionCode());
741 if (err
!= ERROR_SUCCESS
)
749 /******************************************************************************
750 * UnlockServiceDatabase [ADVAPI32.@]
752 BOOL WINAPI
UnlockServiceDatabase (SC_LOCK ScLock
)
755 SC_RPC_LOCK hRpcLock
= ScLock
;
757 TRACE("%p\n",ScLock
);
761 err
= svcctl_UnlockServiceDatabase(&hRpcLock
);
765 err
= map_exception_code(GetExceptionCode());
768 if (err
!= ERROR_SUCCESS
)
776 /******************************************************************************
777 * SetServiceStatus [ADVAPI32.@]
784 SetServiceStatus( SERVICE_STATUS_HANDLE hService
, LPSERVICE_STATUS lpStatus
)
788 TRACE("%p %x %x %x %x %x %x %x\n", hService
,
789 lpStatus
->dwServiceType
, lpStatus
->dwCurrentState
,
790 lpStatus
->dwControlsAccepted
, lpStatus
->dwWin32ExitCode
,
791 lpStatus
->dwServiceSpecificExitCode
, lpStatus
->dwCheckPoint
,
792 lpStatus
->dwWaitHint
);
796 err
= svcctl_SetServiceStatus( hService
, lpStatus
);
800 err
= map_exception_code(GetExceptionCode());
803 if (err
!= ERROR_SUCCESS
)
809 if (lpStatus
->dwCurrentState
== SERVICE_STOPPED
)
811 unsigned int i
, count
= 0;
812 EnterCriticalSection( &service_cs
);
813 for (i
= 0; i
< nb_services
; i
++)
815 if (services
[i
]->handle
== (SC_HANDLE
)hService
) continue;
816 if (services
[i
]->thread
) count
++;
821 SetEvent( service_event
); /* notify the main loop */
823 LeaveCriticalSection( &service_cs
);
830 /******************************************************************************
831 * OpenSCManagerA [ADVAPI32.@]
833 * Establish a connection to the service control manager and open its database.
836 * lpMachineName [I] Pointer to machine name string
837 * lpDatabaseName [I] Pointer to database name string
838 * dwDesiredAccess [I] Type of access
841 * Success: A Handle to the service control manager database
844 SC_HANDLE WINAPI
OpenSCManagerA( LPCSTR lpMachineName
, LPCSTR lpDatabaseName
,
845 DWORD dwDesiredAccess
)
847 LPWSTR machineW
, databaseW
;
850 machineW
= SERV_dup(lpMachineName
);
851 databaseW
= SERV_dup(lpDatabaseName
);
852 ret
= OpenSCManagerW(machineW
, databaseW
, dwDesiredAccess
);
853 heap_free(databaseW
);
858 /******************************************************************************
859 * OpenSCManagerW [ADVAPI32.@]
861 * See OpenSCManagerA.
863 DWORD
SERV_OpenSCManagerW( LPCWSTR lpMachineName
, LPCWSTR lpDatabaseName
,
864 DWORD dwDesiredAccess
, SC_HANDLE
*handle
)
868 TRACE("(%s,%s,0x%08x)\n", debugstr_w(lpMachineName
),
869 debugstr_w(lpDatabaseName
), dwDesiredAccess
);
873 r
= svcctl_OpenSCManagerW(lpMachineName
, lpDatabaseName
, dwDesiredAccess
, (SC_RPC_HANDLE
*)handle
);
877 r
= map_exception_code(GetExceptionCode());
881 if (r
!=ERROR_SUCCESS
)
884 TRACE("returning %p\n", *handle
);
888 SC_HANDLE WINAPI
OpenSCManagerW( LPCWSTR lpMachineName
, LPCWSTR lpDatabaseName
,
889 DWORD dwDesiredAccess
)
891 SC_HANDLE handle
= 0;
894 r
= SERV_OpenSCManagerW(lpMachineName
, lpDatabaseName
, dwDesiredAccess
, &handle
);
895 if (r
!=ERROR_SUCCESS
)
900 /******************************************************************************
901 * ControlService [ADVAPI32.@]
903 * Send a control code to a service.
906 * hService [I] Handle of the service control manager database
907 * dwControl [I] Control code to send (SERVICE_CONTROL_* flags from "winsvc.h")
908 * lpServiceStatus [O] Destination for the status of the service, if available
915 * Unlike M$' implementation, control requests are not serialized and may be
916 * processed asynchronously.
918 BOOL WINAPI
ControlService( SC_HANDLE hService
, DWORD dwControl
,
919 LPSERVICE_STATUS lpServiceStatus
)
923 TRACE("%p %d %p\n", hService
, dwControl
, lpServiceStatus
);
927 err
= svcctl_ControlService(hService
, dwControl
, lpServiceStatus
);
931 err
= map_exception_code(GetExceptionCode());
934 if (err
!= ERROR_SUCCESS
)
943 /******************************************************************************
944 * CloseServiceHandle [ADVAPI32.@]
946 * Close a handle to a service or the service control manager database.
949 * hSCObject [I] Handle to service or service control manager database
956 CloseServiceHandle( SC_HANDLE hSCObject
)
960 TRACE("%p\n", hSCObject
);
964 err
= svcctl_CloseServiceHandle((SC_RPC_HANDLE
*)&hSCObject
);
968 err
= map_exception_code(GetExceptionCode());
972 if (err
!= ERROR_SUCCESS
)
981 /******************************************************************************
982 * OpenServiceA [ADVAPI32.@]
984 * Open a handle to a service.
987 * hSCManager [I] Handle of the service control manager database
988 * lpServiceName [I] Name of the service to open
989 * dwDesiredAccess [I] Access required to the service
992 * Success: Handle to the service
995 SC_HANDLE WINAPI
OpenServiceA( SC_HANDLE hSCManager
, LPCSTR lpServiceName
,
996 DWORD dwDesiredAccess
)
998 LPWSTR lpServiceNameW
;
1001 TRACE("%p %s 0x%08x\n", hSCManager
, debugstr_a(lpServiceName
), dwDesiredAccess
);
1003 lpServiceNameW
= SERV_dup(lpServiceName
);
1004 ret
= OpenServiceW( hSCManager
, lpServiceNameW
, dwDesiredAccess
);
1005 heap_free(lpServiceNameW
);
1010 /******************************************************************************
1011 * OpenServiceW [ADVAPI32.@]
1015 DWORD
SERV_OpenServiceW( SC_HANDLE hSCManager
, LPCWSTR lpServiceName
,
1016 DWORD dwDesiredAccess
, SC_HANDLE
*handle
)
1020 TRACE("%p %s 0x%08x\n", hSCManager
, debugstr_w(lpServiceName
), dwDesiredAccess
);
1023 return ERROR_INVALID_HANDLE
;
1027 err
= svcctl_OpenServiceW(hSCManager
, lpServiceName
, dwDesiredAccess
, (SC_RPC_HANDLE
*)handle
);
1029 __EXCEPT(rpc_filter
)
1031 err
= map_exception_code(GetExceptionCode());
1035 if (err
!= ERROR_SUCCESS
)
1038 TRACE("returning %p\n", *handle
);
1042 SC_HANDLE WINAPI
OpenServiceW( SC_HANDLE hSCManager
, LPCWSTR lpServiceName
,
1043 DWORD dwDesiredAccess
)
1045 SC_HANDLE handle
= 0;
1048 err
= SERV_OpenServiceW(hSCManager
, lpServiceName
, dwDesiredAccess
, &handle
);
1049 if (err
!= ERROR_SUCCESS
)
1054 /******************************************************************************
1055 * CreateServiceW [ADVAPI32.@]
1058 CreateServiceW( SC_HANDLE hSCManager
, LPCWSTR lpServiceName
,
1059 LPCWSTR lpDisplayName
, DWORD dwDesiredAccess
,
1060 DWORD dwServiceType
, DWORD dwStartType
,
1061 DWORD dwErrorControl
, LPCWSTR lpBinaryPathName
,
1062 LPCWSTR lpLoadOrderGroup
, LPDWORD lpdwTagId
,
1063 LPCWSTR lpDependencies
, LPCWSTR lpServiceStartName
,
1064 LPCWSTR lpPassword
)
1066 SC_HANDLE handle
= 0;
1070 TRACE("%p %s %s\n", hSCManager
,
1071 debugstr_w(lpServiceName
), debugstr_w(lpDisplayName
));
1075 SetLastError( ERROR_INVALID_HANDLE
);
1080 passwdlen
= (strlenW(lpPassword
) + 1) * sizeof(WCHAR
);
1088 IsWow64Process(GetCurrentProcess(), &is_wow64
);
1091 err
= svcctl_CreateServiceWOW64W(hSCManager
, lpServiceName
,
1092 lpDisplayName
, dwDesiredAccess
, dwServiceType
, dwStartType
, dwErrorControl
,
1093 lpBinaryPathName
, lpLoadOrderGroup
, lpdwTagId
, (const BYTE
*)lpDependencies
,
1094 multisz_cb(lpDependencies
), lpServiceStartName
, (const BYTE
*)lpPassword
, passwdlen
,
1095 (SC_RPC_HANDLE
*)&handle
);
1097 err
= svcctl_CreateServiceW(hSCManager
, lpServiceName
,
1098 lpDisplayName
, dwDesiredAccess
, dwServiceType
, dwStartType
, dwErrorControl
,
1099 lpBinaryPathName
, lpLoadOrderGroup
, lpdwTagId
, (const BYTE
*)lpDependencies
,
1100 multisz_cb(lpDependencies
), lpServiceStartName
, (const BYTE
*)lpPassword
, passwdlen
,
1101 (SC_RPC_HANDLE
*)&handle
);
1103 __EXCEPT(rpc_filter
)
1105 err
= map_exception_code(GetExceptionCode());
1109 if (err
!= ERROR_SUCCESS
)
1118 /******************************************************************************
1119 * CreateServiceA [ADVAPI32.@]
1122 CreateServiceA( SC_HANDLE hSCManager
, LPCSTR lpServiceName
,
1123 LPCSTR lpDisplayName
, DWORD dwDesiredAccess
,
1124 DWORD dwServiceType
, DWORD dwStartType
,
1125 DWORD dwErrorControl
, LPCSTR lpBinaryPathName
,
1126 LPCSTR lpLoadOrderGroup
, LPDWORD lpdwTagId
,
1127 LPCSTR lpDependencies
, LPCSTR lpServiceStartName
,
1130 LPWSTR lpServiceNameW
, lpDisplayNameW
, lpBinaryPathNameW
,
1131 lpLoadOrderGroupW
, lpDependenciesW
, lpServiceStartNameW
, lpPasswordW
;
1134 TRACE("%p %s %s\n", hSCManager
,
1135 debugstr_a(lpServiceName
), debugstr_a(lpDisplayName
));
1137 lpServiceNameW
= SERV_dup( lpServiceName
);
1138 lpDisplayNameW
= SERV_dup( lpDisplayName
);
1139 lpBinaryPathNameW
= SERV_dup( lpBinaryPathName
);
1140 lpLoadOrderGroupW
= SERV_dup( lpLoadOrderGroup
);
1141 lpDependenciesW
= SERV_dupmulti( lpDependencies
);
1142 lpServiceStartNameW
= SERV_dup( lpServiceStartName
);
1143 lpPasswordW
= SERV_dup( lpPassword
);
1145 r
= CreateServiceW( hSCManager
, lpServiceNameW
, lpDisplayNameW
,
1146 dwDesiredAccess
, dwServiceType
, dwStartType
, dwErrorControl
,
1147 lpBinaryPathNameW
, lpLoadOrderGroupW
, lpdwTagId
,
1148 lpDependenciesW
, lpServiceStartNameW
, lpPasswordW
);
1150 heap_free( lpServiceNameW
);
1151 heap_free( lpDisplayNameW
);
1152 heap_free( lpBinaryPathNameW
);
1153 heap_free( lpLoadOrderGroupW
);
1154 heap_free( lpDependenciesW
);
1155 heap_free( lpServiceStartNameW
);
1156 heap_free( lpPasswordW
);
1162 /******************************************************************************
1163 * DeleteService [ADVAPI32.@]
1165 * Delete a service from the service control manager database.
1168 * hService [I] Handle of the service to delete
1174 BOOL WINAPI
DeleteService( SC_HANDLE hService
)
1178 TRACE("%p\n", hService
);
1182 err
= svcctl_DeleteService(hService
);
1184 __EXCEPT(rpc_filter
)
1186 err
= map_exception_code(GetExceptionCode());
1199 /******************************************************************************
1200 * StartServiceA [ADVAPI32.@]
1205 * hService [I] Handle of service
1206 * dwNumServiceArgs [I] Number of arguments
1207 * lpServiceArgVectors [I] Address of array of argument strings
1210 * - NT implements this function using an obscure RPC call.
1211 * - You might need to do a "setenv SystemRoot \\WINNT" in your .cshrc
1212 * to get things like "%SystemRoot%\\System32\\service.exe" to load.
1213 * - This will only work for shared address space. How should the service
1214 * args be transferred when address spaces are separated?
1215 * - Can only start one service at a time.
1216 * - Has no concept of privilege.
1222 BOOL WINAPI
StartServiceA( SC_HANDLE hService
, DWORD dwNumServiceArgs
,
1223 LPCSTR
*lpServiceArgVectors
)
1225 LPWSTR
*lpwstr
=NULL
;
1229 TRACE("(%p,%d,%p)\n",hService
,dwNumServiceArgs
,lpServiceArgVectors
);
1231 if (dwNumServiceArgs
)
1232 lpwstr
= heap_alloc( dwNumServiceArgs
*sizeof(LPWSTR
) );
1234 for(i
=0; i
<dwNumServiceArgs
; i
++)
1235 lpwstr
[i
]=SERV_dup(lpServiceArgVectors
[i
]);
1237 r
= StartServiceW(hService
, dwNumServiceArgs
, (LPCWSTR
*)lpwstr
);
1239 if (dwNumServiceArgs
)
1241 for(i
=0; i
<dwNumServiceArgs
; i
++)
1242 heap_free(lpwstr
[i
]);
1250 /******************************************************************************
1251 * StartServiceW [ADVAPI32.@]
1253 * See StartServiceA.
1255 BOOL WINAPI
StartServiceW(SC_HANDLE hService
, DWORD dwNumServiceArgs
,
1256 LPCWSTR
*lpServiceArgVectors
)
1260 TRACE("%p %d %p\n", hService
, dwNumServiceArgs
, lpServiceArgVectors
);
1264 err
= svcctl_StartServiceW(hService
, dwNumServiceArgs
, lpServiceArgVectors
);
1266 __EXCEPT(rpc_filter
)
1268 err
= map_exception_code(GetExceptionCode());
1271 if (err
!= ERROR_SUCCESS
)
1280 /******************************************************************************
1281 * QueryServiceStatus [ADVAPI32.@]
1284 * hService [I] Handle to service to get information about
1285 * lpservicestatus [O] buffer to receive the status information for the service
1288 BOOL WINAPI
QueryServiceStatus(SC_HANDLE hService
,
1289 LPSERVICE_STATUS lpservicestatus
)
1291 SERVICE_STATUS_PROCESS SvcStatusData
;
1295 TRACE("%p %p\n", hService
, lpservicestatus
);
1299 SetLastError(ERROR_INVALID_HANDLE
);
1302 if (!lpservicestatus
)
1304 SetLastError(ERROR_INVALID_ADDRESS
);
1308 ret
= QueryServiceStatusEx(hService
, SC_STATUS_PROCESS_INFO
, (LPBYTE
)&SvcStatusData
,
1309 sizeof(SERVICE_STATUS_PROCESS
), &dummy
);
1310 if (ret
) memcpy(lpservicestatus
, &SvcStatusData
, sizeof(SERVICE_STATUS
)) ;
1315 /******************************************************************************
1316 * QueryServiceStatusEx [ADVAPI32.@]
1318 * Get information about a service.
1321 * hService [I] Handle to service to get information about
1322 * InfoLevel [I] Level of information to get
1323 * lpBuffer [O] Destination for requested information
1324 * cbBufSize [I] Size of lpBuffer in bytes
1325 * pcbBytesNeeded [O] Destination for number of bytes needed, if cbBufSize is too small
1331 BOOL WINAPI
QueryServiceStatusEx(SC_HANDLE hService
, SC_STATUS_TYPE InfoLevel
,
1332 LPBYTE lpBuffer
, DWORD cbBufSize
,
1333 LPDWORD pcbBytesNeeded
)
1337 TRACE("%p %d %p %d %p\n", hService
, InfoLevel
, lpBuffer
, cbBufSize
, pcbBytesNeeded
);
1339 if (InfoLevel
!= SC_STATUS_PROCESS_INFO
)
1341 err
= ERROR_INVALID_LEVEL
;
1343 else if (cbBufSize
< sizeof(SERVICE_STATUS_PROCESS
))
1345 *pcbBytesNeeded
= sizeof(SERVICE_STATUS_PROCESS
);
1346 err
= ERROR_INSUFFICIENT_BUFFER
;
1352 err
= svcctl_QueryServiceStatusEx(hService
, InfoLevel
, lpBuffer
, cbBufSize
, pcbBytesNeeded
);
1354 __EXCEPT(rpc_filter
)
1356 err
= map_exception_code(GetExceptionCode());
1360 if (err
!= ERROR_SUCCESS
)
1368 /******************************************************************************
1369 * QueryServiceConfigA [ADVAPI32.@]
1371 BOOL WINAPI
QueryServiceConfigA( SC_HANDLE hService
, LPQUERY_SERVICE_CONFIGA config
,
1372 DWORD size
, LPDWORD needed
)
1377 QUERY_SERVICE_CONFIGW
*configW
;
1379 TRACE("%p %p %d %p\n", hService
, config
, size
, needed
);
1381 if (!(buffer
= heap_alloc( 2 * size
)))
1383 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1386 configW
= (QUERY_SERVICE_CONFIGW
*)buffer
;
1387 ret
= QueryServiceConfigW( hService
, configW
, 2 * size
, needed
);
1388 if (!ret
) goto done
;
1390 config
->dwServiceType
= configW
->dwServiceType
;
1391 config
->dwStartType
= configW
->dwStartType
;
1392 config
->dwErrorControl
= configW
->dwErrorControl
;
1393 config
->lpBinaryPathName
= NULL
;
1394 config
->lpLoadOrderGroup
= NULL
;
1395 config
->dwTagId
= configW
->dwTagId
;
1396 config
->lpDependencies
= NULL
;
1397 config
->lpServiceStartName
= NULL
;
1398 config
->lpDisplayName
= NULL
;
1400 p
= (LPSTR
)(config
+ 1);
1401 n
= size
- sizeof(*config
);
1404 #define MAP_STR(str) \
1408 DWORD sz = WideCharToMultiByte( CP_ACP, 0, configW->str, -1, p, n, NULL, NULL ); \
1409 if (!sz) goto done; \
1416 MAP_STR( lpBinaryPathName
);
1417 MAP_STR( lpLoadOrderGroup
);
1418 MAP_STR( lpDependencies
);
1419 MAP_STR( lpServiceStartName
);
1420 MAP_STR( lpDisplayName
);
1423 *needed
= p
- (LPSTR
)config
;
1427 heap_free( buffer
);
1431 static DWORD
move_string_to_buffer(BYTE
**buf
, LPWSTR
*string_ptr
)
1438 memset(*buf
, 0, cb
);
1442 cb
= (strlenW(*string_ptr
) + 1)*sizeof(WCHAR
);
1443 memcpy(*buf
, *string_ptr
, cb
);
1444 MIDL_user_free(*string_ptr
);
1447 *string_ptr
= (LPWSTR
)*buf
;
1453 static DWORD
size_string(LPCWSTR string
)
1455 return (string
? (strlenW(string
) + 1)*sizeof(WCHAR
) : sizeof(WCHAR
));
1458 /******************************************************************************
1459 * QueryServiceConfigW [ADVAPI32.@]
1462 QueryServiceConfigW( SC_HANDLE hService
,
1463 LPQUERY_SERVICE_CONFIGW lpServiceConfig
,
1464 DWORD cbBufSize
, LPDWORD pcbBytesNeeded
)
1466 QUERY_SERVICE_CONFIGW config
;
1471 TRACE("%p %p %d %p\n", hService
, lpServiceConfig
,
1472 cbBufSize
, pcbBytesNeeded
);
1474 memset(&config
, 0, sizeof(config
));
1478 err
= svcctl_QueryServiceConfigW(hService
, &config
, cbBufSize
, pcbBytesNeeded
);
1480 __EXCEPT(rpc_filter
)
1482 err
= map_exception_code(GetExceptionCode());
1486 if (err
!= ERROR_SUCCESS
)
1488 TRACE("services.exe: error %u\n", err
);
1493 /* calculate the size required first */
1494 total
= sizeof (QUERY_SERVICE_CONFIGW
);
1495 total
+= size_string(config
.lpBinaryPathName
);
1496 total
+= size_string(config
.lpLoadOrderGroup
);
1497 total
+= size_string(config
.lpDependencies
);
1498 total
+= size_string(config
.lpServiceStartName
);
1499 total
+= size_string(config
.lpDisplayName
);
1501 *pcbBytesNeeded
= total
;
1503 /* if there's not enough memory, return an error */
1504 if( total
> cbBufSize
)
1506 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1507 MIDL_user_free(config
.lpBinaryPathName
);
1508 MIDL_user_free(config
.lpLoadOrderGroup
);
1509 MIDL_user_free(config
.lpDependencies
);
1510 MIDL_user_free(config
.lpServiceStartName
);
1511 MIDL_user_free(config
.lpDisplayName
);
1515 *lpServiceConfig
= config
;
1516 bufpos
= ((BYTE
*)lpServiceConfig
) + sizeof(QUERY_SERVICE_CONFIGW
);
1517 move_string_to_buffer(&bufpos
, &lpServiceConfig
->lpBinaryPathName
);
1518 move_string_to_buffer(&bufpos
, &lpServiceConfig
->lpLoadOrderGroup
);
1519 move_string_to_buffer(&bufpos
, &lpServiceConfig
->lpDependencies
);
1520 move_string_to_buffer(&bufpos
, &lpServiceConfig
->lpServiceStartName
);
1521 move_string_to_buffer(&bufpos
, &lpServiceConfig
->lpDisplayName
);
1523 TRACE("Image path = %s\n", debugstr_w(lpServiceConfig
->lpBinaryPathName
) );
1524 TRACE("Group = %s\n", debugstr_w(lpServiceConfig
->lpLoadOrderGroup
) );
1525 TRACE("Dependencies = %s\n", debugstr_w(lpServiceConfig
->lpDependencies
) );
1526 TRACE("Service account name = %s\n", debugstr_w(lpServiceConfig
->lpServiceStartName
) );
1527 TRACE("Display name = %s\n", debugstr_w(lpServiceConfig
->lpDisplayName
) );
1532 /******************************************************************************
1533 * QueryServiceConfig2A [ADVAPI32.@]
1536 * observed under win2k:
1537 * The functions QueryServiceConfig2A and QueryServiceConfig2W return the same
1538 * required buffer size (in byte) at least for dwLevel SERVICE_CONFIG_DESCRIPTION
1540 BOOL WINAPI
QueryServiceConfig2A(SC_HANDLE hService
, DWORD dwLevel
, LPBYTE buffer
,
1541 DWORD size
, LPDWORD needed
)
1544 LPBYTE bufferW
= NULL
;
1547 bufferW
= heap_alloc(size
);
1549 ret
= QueryServiceConfig2W(hService
, dwLevel
, bufferW
, size
, needed
);
1550 if(!ret
) goto cleanup
;
1553 case SERVICE_CONFIG_DESCRIPTION
:
1554 if (buffer
&& bufferW
) {
1555 LPSERVICE_DESCRIPTIONA configA
= (LPSERVICE_DESCRIPTIONA
) buffer
;
1556 LPSERVICE_DESCRIPTIONW configW
= (LPSERVICE_DESCRIPTIONW
) bufferW
;
1557 if (configW
->lpDescription
&& (size
> sizeof(SERVICE_DESCRIPTIONA
))) {
1559 configA
->lpDescription
= (LPSTR
)(configA
+ 1);
1560 sz
= WideCharToMultiByte( CP_ACP
, 0, configW
->lpDescription
, -1,
1561 configA
->lpDescription
, size
- sizeof(SERVICE_DESCRIPTIONA
), NULL
, NULL
);
1563 FIXME("WideCharToMultiByte failed for configW->lpDescription\n");
1565 configA
->lpDescription
= NULL
;
1568 else configA
->lpDescription
= NULL
;
1571 case SERVICE_CONFIG_PRESHUTDOWN_INFO
:
1572 if (buffer
&& bufferW
&& *needed
<=size
)
1573 memcpy(buffer
, bufferW
, *needed
);
1576 FIXME("conversation W->A not implemented for level %d\n", dwLevel
);
1582 heap_free( bufferW
);
1586 /******************************************************************************
1587 * QueryServiceConfig2W [ADVAPI32.@]
1589 * See QueryServiceConfig2A.
1591 BOOL WINAPI
QueryServiceConfig2W(SC_HANDLE hService
, DWORD dwLevel
, LPBYTE buffer
,
1592 DWORD size
, LPDWORD needed
)
1596 if(dwLevel
!=SERVICE_CONFIG_DESCRIPTION
&& dwLevel
!=SERVICE_CONFIG_PRESHUTDOWN_INFO
) {
1597 FIXME("Level %d not implemented\n", dwLevel
);
1598 SetLastError(ERROR_INVALID_LEVEL
);
1602 if(!buffer
&& size
) {
1603 SetLastError(ERROR_INVALID_ADDRESS
);
1607 TRACE("%p 0x%d %p 0x%d %p\n", hService
, dwLevel
, buffer
, size
, needed
);
1611 err
= svcctl_QueryServiceConfig2W(hService
, dwLevel
, buffer
, size
, needed
);
1613 __EXCEPT(rpc_filter
)
1615 err
= map_exception_code(GetExceptionCode());
1619 if (err
!= ERROR_SUCCESS
)
1621 SetLastError( err
);
1627 case SERVICE_CONFIG_DESCRIPTION
:
1630 SERVICE_DESCRIPTIONW
*descr
= (SERVICE_DESCRIPTIONW
*)buffer
;
1631 if (descr
->lpDescription
) /* make it an absolute pointer */
1632 descr
->lpDescription
= (WCHAR
*)(buffer
+ (ULONG_PTR
)descr
->lpDescription
);
1640 /******************************************************************************
1641 * EnumServicesStatusA [ADVAPI32.@]
1644 EnumServicesStatusA( SC_HANDLE hmngr
, DWORD type
, DWORD state
, LPENUM_SERVICE_STATUSA
1645 services
, DWORD size
, LPDWORD needed
, LPDWORD returned
,
1646 LPDWORD resume_handle
)
1650 ENUM_SERVICE_STATUSW
*servicesW
= NULL
;
1654 TRACE("%p 0x%x 0x%x %p %u %p %p %p\n", hmngr
, type
, state
, services
, size
, needed
,
1655 returned
, resume_handle
);
1657 sz
= max( 2 * size
, sizeof(*servicesW
) );
1658 if (!(servicesW
= heap_alloc( sz
)))
1660 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1664 ret
= EnumServicesStatusW( hmngr
, type
, state
, servicesW
, sz
, needed
, returned
, resume_handle
);
1665 if (!ret
) goto done
;
1667 p
= (char *)services
+ *returned
* sizeof(ENUM_SERVICE_STATUSA
);
1668 n
= size
- (p
- (char *)services
);
1670 for (i
= 0; i
< *returned
; i
++)
1672 sz
= WideCharToMultiByte( CP_ACP
, 0, servicesW
[i
].lpServiceName
, -1, p
, n
, NULL
, NULL
);
1674 services
[i
].lpServiceName
= p
;
1677 if (servicesW
[i
].lpDisplayName
)
1679 sz
= WideCharToMultiByte( CP_ACP
, 0, servicesW
[i
].lpDisplayName
, -1, p
, n
, NULL
, NULL
);
1681 services
[i
].lpDisplayName
= p
;
1685 else services
[i
].lpDisplayName
= NULL
;
1686 services
[i
].ServiceStatus
= servicesW
[i
].ServiceStatus
;
1692 heap_free( servicesW
);
1696 /******************************************************************************
1697 * EnumServicesStatusW [ADVAPI32.@]
1700 EnumServicesStatusW( SC_HANDLE hmngr
, DWORD type
, DWORD state
, LPENUM_SERVICE_STATUSW
1701 services
, DWORD size
, LPDWORD needed
, LPDWORD returned
,
1702 LPDWORD resume_handle
)
1705 ENUM_SERVICE_STATUSW dummy_status
;
1707 TRACE("%p 0x%x 0x%x %p %u %p %p %p\n", hmngr
, type
, state
, services
, size
, needed
,
1708 returned
, resume_handle
);
1712 SetLastError( ERROR_INVALID_HANDLE
);
1716 /* make sure we pass a valid pointer */
1717 if (!services
|| size
< sizeof(*services
))
1719 services
= &dummy_status
;
1720 size
= sizeof(dummy_status
);
1725 err
= svcctl_EnumServicesStatusW( hmngr
, type
, state
, (BYTE
*)services
, size
, needed
, returned
, resume_handle
);
1727 __EXCEPT(rpc_filter
)
1729 err
= map_exception_code( GetExceptionCode() );
1733 if (err
!= ERROR_SUCCESS
)
1735 SetLastError( err
);
1739 for (i
= 0; i
< *returned
; i
++)
1741 /* convert buffer offsets into pointers */
1742 services
[i
].lpServiceName
= (WCHAR
*)((char *)services
+ (DWORD_PTR
)services
[i
].lpServiceName
);
1743 if (services
[i
].lpDisplayName
)
1744 services
[i
].lpDisplayName
= (WCHAR
*)((char *)services
+ (DWORD_PTR
)services
[i
].lpDisplayName
);
1750 /******************************************************************************
1751 * EnumServicesStatusExA [ADVAPI32.@]
1754 EnumServicesStatusExA( SC_HANDLE hmngr
, SC_ENUM_TYPE level
, DWORD type
, DWORD state
,
1755 LPBYTE buffer
, DWORD size
, LPDWORD needed
, LPDWORD returned
,
1756 LPDWORD resume_handle
, LPCSTR group
)
1760 ENUM_SERVICE_STATUS_PROCESSA
*services
= (ENUM_SERVICE_STATUS_PROCESSA
*)buffer
;
1761 ENUM_SERVICE_STATUS_PROCESSW
*servicesW
= NULL
;
1762 WCHAR
*groupW
= NULL
;
1766 TRACE("%p %u 0x%x 0x%x %p %u %p %p %p %s\n", hmngr
, level
, type
, state
, buffer
,
1767 size
, needed
, returned
, resume_handle
, debugstr_a(group
));
1769 sz
= max( 2 * size
, sizeof(*servicesW
) );
1770 if (!(servicesW
= heap_alloc( sz
)))
1772 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1777 int len
= MultiByteToWideChar( CP_ACP
, 0, group
, -1, NULL
, 0 );
1778 if (!(groupW
= heap_alloc( len
* sizeof(WCHAR
) )))
1780 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1781 heap_free( servicesW
);
1784 MultiByteToWideChar( CP_ACP
, 0, group
, -1, groupW
, len
* sizeof(WCHAR
) );
1787 ret
= EnumServicesStatusExW( hmngr
, level
, type
, state
, (BYTE
*)servicesW
, sz
,
1788 needed
, returned
, resume_handle
, groupW
);
1789 if (!ret
) goto done
;
1791 p
= (char *)services
+ *returned
* sizeof(ENUM_SERVICE_STATUS_PROCESSA
);
1792 n
= size
- (p
- (char *)services
);
1794 for (i
= 0; i
< *returned
; i
++)
1796 sz
= WideCharToMultiByte( CP_ACP
, 0, servicesW
[i
].lpServiceName
, -1, p
, n
, NULL
, NULL
);
1798 services
[i
].lpServiceName
= p
;
1801 if (servicesW
[i
].lpDisplayName
)
1803 sz
= WideCharToMultiByte( CP_ACP
, 0, servicesW
[i
].lpDisplayName
, -1, p
, n
, NULL
, NULL
);
1805 services
[i
].lpDisplayName
= p
;
1809 else services
[i
].lpDisplayName
= NULL
;
1810 services
[i
].ServiceStatusProcess
= servicesW
[i
].ServiceStatusProcess
;
1816 heap_free( servicesW
);
1817 heap_free( groupW
);
1821 /******************************************************************************
1822 * EnumServicesStatusExW [ADVAPI32.@]
1825 EnumServicesStatusExW( SC_HANDLE hmngr
, SC_ENUM_TYPE level
, DWORD type
, DWORD state
,
1826 LPBYTE buffer
, DWORD size
, LPDWORD needed
, LPDWORD returned
,
1827 LPDWORD resume_handle
, LPCWSTR group
)
1830 ENUM_SERVICE_STATUS_PROCESSW dummy_status
;
1831 ENUM_SERVICE_STATUS_PROCESSW
*services
= (ENUM_SERVICE_STATUS_PROCESSW
*)buffer
;
1833 TRACE("%p %u 0x%x 0x%x %p %u %p %p %p %s\n", hmngr
, level
, type
, state
, buffer
,
1834 size
, needed
, returned
, resume_handle
, debugstr_w(group
));
1836 if (level
!= SC_ENUM_PROCESS_INFO
)
1838 SetLastError( ERROR_INVALID_LEVEL
);
1843 SetLastError( ERROR_INVALID_HANDLE
);
1847 /* make sure we pass a valid buffer pointer */
1848 if (!services
|| size
< sizeof(*services
))
1850 buffer
= (BYTE
*)&dummy_status
;
1851 size
= sizeof(dummy_status
);
1856 err
= svcctl_EnumServicesStatusExW( hmngr
, SC_ENUM_PROCESS_INFO
, type
, state
, buffer
, size
, needed
,
1857 returned
, resume_handle
, group
);
1859 __EXCEPT(rpc_filter
)
1861 err
= map_exception_code( GetExceptionCode() );
1865 if (err
!= ERROR_SUCCESS
)
1867 SetLastError( err
);
1871 for (i
= 0; i
< *returned
; i
++)
1873 /* convert buffer offsets into pointers */
1874 services
[i
].lpServiceName
= (WCHAR
*)((char *)services
+ (DWORD_PTR
)services
[i
].lpServiceName
);
1875 if (services
[i
].lpDisplayName
)
1876 services
[i
].lpDisplayName
= (WCHAR
*)((char *)services
+ (DWORD_PTR
)services
[i
].lpDisplayName
);
1882 /******************************************************************************
1883 * GetServiceKeyNameA [ADVAPI32.@]
1885 BOOL WINAPI
GetServiceKeyNameA( SC_HANDLE hSCManager
, LPCSTR lpDisplayName
,
1886 LPSTR lpServiceName
, LPDWORD lpcchBuffer
)
1888 LPWSTR lpDisplayNameW
, lpServiceNameW
;
1892 TRACE("%p %s %p %p\n", hSCManager
,
1893 debugstr_a(lpDisplayName
), lpServiceName
, lpcchBuffer
);
1895 lpDisplayNameW
= SERV_dup(lpDisplayName
);
1897 lpServiceNameW
= heap_alloc(*lpcchBuffer
* sizeof(WCHAR
));
1899 lpServiceNameW
= NULL
;
1901 sizeW
= *lpcchBuffer
;
1902 if (!GetServiceKeyNameW(hSCManager
, lpDisplayNameW
, lpServiceNameW
, &sizeW
))
1904 if (lpServiceName
&& *lpcchBuffer
)
1905 lpServiceName
[0] = 0;
1906 *lpcchBuffer
= sizeW
*2; /* we can only provide an upper estimation of string length */
1910 if (!WideCharToMultiByte(CP_ACP
, 0, lpServiceNameW
, (sizeW
+ 1), lpServiceName
,
1911 *lpcchBuffer
, NULL
, NULL
))
1913 if (*lpcchBuffer
&& lpServiceName
)
1914 lpServiceName
[0] = 0;
1915 *lpcchBuffer
= WideCharToMultiByte(CP_ACP
, 0, lpServiceNameW
, -1, NULL
, 0, NULL
, NULL
);
1919 /* lpcchBuffer not updated - same as in GetServiceDisplayNameA */
1923 heap_free(lpServiceNameW
);
1924 heap_free(lpDisplayNameW
);
1928 /******************************************************************************
1929 * GetServiceKeyNameW [ADVAPI32.@]
1931 BOOL WINAPI
GetServiceKeyNameW( SC_HANDLE hSCManager
, LPCWSTR lpDisplayName
,
1932 LPWSTR lpServiceName
, LPDWORD lpcchBuffer
)
1938 TRACE("%p %s %p %p\n", hSCManager
,
1939 debugstr_w(lpDisplayName
), lpServiceName
, lpcchBuffer
);
1943 SetLastError( ERROR_INVALID_HANDLE
);
1947 /* provide a buffer if the caller didn't */
1948 if (!lpServiceName
|| *lpcchBuffer
< 2)
1950 lpServiceName
= buffer
;
1951 /* A size of 1 would be enough, but tests show that Windows returns 2,
1952 * probably because of a WCHAR/bytes mismatch in their code.
1957 /* RPC call takes size excluding nul-terminator, whereas *lpcchBuffer
1958 * includes the nul-terminator on input. */
1959 size
= *lpcchBuffer
- 1;
1963 err
= svcctl_GetServiceKeyNameW(hSCManager
, lpDisplayName
, lpServiceName
,
1966 __EXCEPT(rpc_filter
)
1968 err
= map_exception_code(GetExceptionCode());
1972 /* The value of *lpcchBuffer excludes nul-terminator on output. */
1973 if (err
== ERROR_SUCCESS
|| err
== ERROR_INSUFFICIENT_BUFFER
)
1974 *lpcchBuffer
= size
;
1978 return err
== ERROR_SUCCESS
;
1981 /******************************************************************************
1982 * QueryServiceLockStatusA [ADVAPI32.@]
1984 BOOL WINAPI
QueryServiceLockStatusA( SC_HANDLE hSCManager
,
1985 LPQUERY_SERVICE_LOCK_STATUSA lpLockStatus
,
1986 DWORD cbBufSize
, LPDWORD pcbBytesNeeded
)
1988 FIXME("%p %p %08x %p\n", hSCManager
, lpLockStatus
, cbBufSize
, pcbBytesNeeded
);
1993 /******************************************************************************
1994 * QueryServiceLockStatusW [ADVAPI32.@]
1996 BOOL WINAPI
QueryServiceLockStatusW( SC_HANDLE hSCManager
,
1997 LPQUERY_SERVICE_LOCK_STATUSW lpLockStatus
,
1998 DWORD cbBufSize
, LPDWORD pcbBytesNeeded
)
2000 FIXME("%p %p %08x %p\n", hSCManager
, lpLockStatus
, cbBufSize
, pcbBytesNeeded
);
2005 /******************************************************************************
2006 * GetServiceDisplayNameA [ADVAPI32.@]
2008 BOOL WINAPI
GetServiceDisplayNameA( SC_HANDLE hSCManager
, LPCSTR lpServiceName
,
2009 LPSTR lpDisplayName
, LPDWORD lpcchBuffer
)
2011 LPWSTR lpServiceNameW
, lpDisplayNameW
;
2015 TRACE("%p %s %p %p\n", hSCManager
,
2016 debugstr_a(lpServiceName
), lpDisplayName
, lpcchBuffer
);
2018 lpServiceNameW
= SERV_dup(lpServiceName
);
2020 lpDisplayNameW
= heap_alloc(*lpcchBuffer
* sizeof(WCHAR
));
2022 lpDisplayNameW
= NULL
;
2024 sizeW
= *lpcchBuffer
;
2025 if (!GetServiceDisplayNameW(hSCManager
, lpServiceNameW
, lpDisplayNameW
, &sizeW
))
2027 if (lpDisplayName
&& *lpcchBuffer
)
2028 lpDisplayName
[0] = 0;
2029 *lpcchBuffer
= sizeW
*2; /* we can only provide an upper estimation of string length */
2033 if (!WideCharToMultiByte(CP_ACP
, 0, lpDisplayNameW
, (sizeW
+ 1), lpDisplayName
,
2034 *lpcchBuffer
, NULL
, NULL
))
2036 if (*lpcchBuffer
&& lpDisplayName
)
2037 lpDisplayName
[0] = 0;
2038 *lpcchBuffer
= WideCharToMultiByte(CP_ACP
, 0, lpDisplayNameW
, -1, NULL
, 0, NULL
, NULL
);
2042 /* probably due to a bug GetServiceDisplayNameA doesn't modify lpcchBuffer on success.
2043 * (but if the function succeeded it means that is a good upper estimation of the size) */
2047 heap_free(lpDisplayNameW
);
2048 heap_free(lpServiceNameW
);
2052 /******************************************************************************
2053 * GetServiceDisplayNameW [ADVAPI32.@]
2055 BOOL WINAPI
GetServiceDisplayNameW( SC_HANDLE hSCManager
, LPCWSTR lpServiceName
,
2056 LPWSTR lpDisplayName
, LPDWORD lpcchBuffer
)
2062 TRACE("%p %s %p %p\n", hSCManager
,
2063 debugstr_w(lpServiceName
), lpDisplayName
, lpcchBuffer
);
2067 SetLastError( ERROR_INVALID_HANDLE
);
2071 /* provide a buffer if the caller didn't */
2072 if (!lpDisplayName
|| *lpcchBuffer
< 2)
2074 lpDisplayName
= buffer
;
2075 /* A size of 1 would be enough, but tests show that Windows returns 2,
2076 * probably because of a WCHAR/bytes mismatch in their code.
2081 /* RPC call takes size excluding nul-terminator, whereas *lpcchBuffer
2082 * includes the nul-terminator on input. */
2083 size
= *lpcchBuffer
- 1;
2087 err
= svcctl_GetServiceDisplayNameW(hSCManager
, lpServiceName
, lpDisplayName
,
2090 __EXCEPT(rpc_filter
)
2092 err
= map_exception_code(GetExceptionCode());
2096 /* The value of *lpcchBuffer excludes nul-terminator on output. */
2097 if (err
== ERROR_SUCCESS
|| err
== ERROR_INSUFFICIENT_BUFFER
)
2098 *lpcchBuffer
= size
;
2102 return err
== ERROR_SUCCESS
;
2105 /******************************************************************************
2106 * ChangeServiceConfigW [ADVAPI32.@]
2108 BOOL WINAPI
ChangeServiceConfigW( SC_HANDLE hService
, DWORD dwServiceType
,
2109 DWORD dwStartType
, DWORD dwErrorControl
, LPCWSTR lpBinaryPathName
,
2110 LPCWSTR lpLoadOrderGroup
, LPDWORD lpdwTagId
, LPCWSTR lpDependencies
,
2111 LPCWSTR lpServiceStartName
, LPCWSTR lpPassword
, LPCWSTR lpDisplayName
)
2116 TRACE("%p %d %d %d %s %s %p %p %s %s %s\n",
2117 hService
, dwServiceType
, dwStartType
, dwErrorControl
,
2118 debugstr_w(lpBinaryPathName
), debugstr_w(lpLoadOrderGroup
),
2119 lpdwTagId
, lpDependencies
, debugstr_w(lpServiceStartName
),
2120 debugstr_w(lpPassword
), debugstr_w(lpDisplayName
) );
2122 cb_pwd
= lpPassword
? (strlenW(lpPassword
) + 1)*sizeof(WCHAR
) : 0;
2126 err
= svcctl_ChangeServiceConfigW(hService
, dwServiceType
,
2127 dwStartType
, dwErrorControl
, lpBinaryPathName
, lpLoadOrderGroup
, lpdwTagId
,
2128 (const BYTE
*)lpDependencies
, multisz_cb(lpDependencies
), lpServiceStartName
,
2129 (const BYTE
*)lpPassword
, cb_pwd
, lpDisplayName
);
2131 __EXCEPT(rpc_filter
)
2133 err
= map_exception_code(GetExceptionCode());
2137 if (err
!= ERROR_SUCCESS
)
2140 return err
== ERROR_SUCCESS
;
2143 /******************************************************************************
2144 * ChangeServiceConfigA [ADVAPI32.@]
2146 BOOL WINAPI
ChangeServiceConfigA( SC_HANDLE hService
, DWORD dwServiceType
,
2147 DWORD dwStartType
, DWORD dwErrorControl
, LPCSTR lpBinaryPathName
,
2148 LPCSTR lpLoadOrderGroup
, LPDWORD lpdwTagId
, LPCSTR lpDependencies
,
2149 LPCSTR lpServiceStartName
, LPCSTR lpPassword
, LPCSTR lpDisplayName
)
2151 LPWSTR wBinaryPathName
, wLoadOrderGroup
, wDependencies
;
2152 LPWSTR wServiceStartName
, wPassword
, wDisplayName
;
2155 TRACE("%p %d %d %d %s %s %p %p %s %s %s\n",
2156 hService
, dwServiceType
, dwStartType
, dwErrorControl
,
2157 debugstr_a(lpBinaryPathName
), debugstr_a(lpLoadOrderGroup
),
2158 lpdwTagId
, lpDependencies
, debugstr_a(lpServiceStartName
),
2159 debugstr_a(lpPassword
), debugstr_a(lpDisplayName
) );
2161 wBinaryPathName
= SERV_dup( lpBinaryPathName
);
2162 wLoadOrderGroup
= SERV_dup( lpLoadOrderGroup
);
2163 wDependencies
= SERV_dupmulti( lpDependencies
);
2164 wServiceStartName
= SERV_dup( lpServiceStartName
);
2165 wPassword
= SERV_dup( lpPassword
);
2166 wDisplayName
= SERV_dup( lpDisplayName
);
2168 r
= ChangeServiceConfigW( hService
, dwServiceType
,
2169 dwStartType
, dwErrorControl
, wBinaryPathName
,
2170 wLoadOrderGroup
, lpdwTagId
, wDependencies
,
2171 wServiceStartName
, wPassword
, wDisplayName
);
2173 heap_free( wBinaryPathName
);
2174 heap_free( wLoadOrderGroup
);
2175 heap_free( wDependencies
);
2176 heap_free( wServiceStartName
);
2177 heap_free( wPassword
);
2178 heap_free( wDisplayName
);
2183 /******************************************************************************
2184 * ChangeServiceConfig2A [ADVAPI32.@]
2186 BOOL WINAPI
ChangeServiceConfig2A( SC_HANDLE hService
, DWORD dwInfoLevel
,
2191 TRACE("%p %d %p\n",hService
, dwInfoLevel
, lpInfo
);
2193 if (dwInfoLevel
== SERVICE_CONFIG_DESCRIPTION
)
2195 LPSERVICE_DESCRIPTIONA sd
= lpInfo
;
2196 SERVICE_DESCRIPTIONW sdw
;
2198 sdw
.lpDescription
= SERV_dup( sd
->lpDescription
);
2200 r
= ChangeServiceConfig2W( hService
, dwInfoLevel
, &sdw
);
2202 heap_free( sdw
.lpDescription
);
2204 else if (dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
2206 LPSERVICE_FAILURE_ACTIONSA fa
= lpInfo
;
2207 SERVICE_FAILURE_ACTIONSW faw
;
2209 faw
.dwResetPeriod
= fa
->dwResetPeriod
;
2210 faw
.lpRebootMsg
= SERV_dup( fa
->lpRebootMsg
);
2211 faw
.lpCommand
= SERV_dup( fa
->lpCommand
);
2212 faw
.cActions
= fa
->cActions
;
2213 faw
.lpsaActions
= fa
->lpsaActions
;
2215 r
= ChangeServiceConfig2W( hService
, dwInfoLevel
, &faw
);
2217 heap_free( faw
.lpRebootMsg
);
2218 heap_free( faw
.lpCommand
);
2220 else if (dwInfoLevel
== SERVICE_CONFIG_PRESHUTDOWN_INFO
)
2222 r
= ChangeServiceConfig2W( hService
, dwInfoLevel
, lpInfo
);
2225 SetLastError( ERROR_INVALID_PARAMETER
);
2230 /******************************************************************************
2231 * ChangeServiceConfig2W [ADVAPI32.@]
2233 BOOL WINAPI
ChangeServiceConfig2W( SC_HANDLE hService
, DWORD dwInfoLevel
,
2240 SC_RPC_CONFIG_INFOW info
;
2242 info
.dwInfoLevel
= dwInfoLevel
;
2243 info
.u
.descr
= lpInfo
;
2244 err
= svcctl_ChangeServiceConfig2W( hService
, info
);
2246 __EXCEPT(rpc_filter
)
2248 err
= map_exception_code(GetExceptionCode());
2252 if (err
!= ERROR_SUCCESS
)
2255 return err
== ERROR_SUCCESS
;
2258 NTSTATUS
SERV_QueryServiceObjectSecurity(SC_HANDLE hService
,
2259 SECURITY_INFORMATION dwSecurityInformation
,
2260 PSECURITY_DESCRIPTOR lpSecurityDescriptor
,
2261 DWORD cbBufSize
, LPDWORD pcbBytesNeeded
)
2263 SECURITY_DESCRIPTOR descriptor
;
2268 FIXME("%p %d %p %u %p - semi-stub\n", hService
, dwSecurityInformation
,
2269 lpSecurityDescriptor
, cbBufSize
, pcbBytesNeeded
);
2271 if (dwSecurityInformation
!= DACL_SECURITY_INFORMATION
)
2272 FIXME("information %d not supported\n", dwSecurityInformation
);
2274 InitializeSecurityDescriptor(&descriptor
, SECURITY_DESCRIPTOR_REVISION
);
2276 InitializeAcl(&acl
, sizeof(ACL
), ACL_REVISION
);
2277 SetSecurityDescriptorDacl(&descriptor
, TRUE
, &acl
, TRUE
);
2280 status
= RtlMakeSelfRelativeSD(&descriptor
, lpSecurityDescriptor
, &size
);
2281 *pcbBytesNeeded
= size
;
2285 /******************************************************************************
2286 * QueryServiceObjectSecurity [ADVAPI32.@]
2288 BOOL WINAPI
QueryServiceObjectSecurity(SC_HANDLE hService
,
2289 SECURITY_INFORMATION dwSecurityInformation
,
2290 PSECURITY_DESCRIPTOR lpSecurityDescriptor
,
2291 DWORD cbBufSize
, LPDWORD pcbBytesNeeded
)
2293 NTSTATUS status
= SERV_QueryServiceObjectSecurity(hService
, dwSecurityInformation
, lpSecurityDescriptor
,
2294 cbBufSize
, pcbBytesNeeded
);
2295 if (status
!= STATUS_SUCCESS
)
2297 SetLastError(RtlNtStatusToDosError(status
));
2303 /******************************************************************************
2304 * SetServiceObjectSecurity [ADVAPI32.@]
2307 * - SetSecurityInfo should be updated to call this function once it's implemented.
2309 BOOL WINAPI
SetServiceObjectSecurity(SC_HANDLE hService
,
2310 SECURITY_INFORMATION dwSecurityInformation
,
2311 PSECURITY_DESCRIPTOR lpSecurityDescriptor
)
2313 FIXME("%p %d %p\n", hService
, dwSecurityInformation
, lpSecurityDescriptor
);
2317 /******************************************************************************
2318 * SetServiceBits [ADVAPI32.@]
2320 BOOL WINAPI
SetServiceBits( SERVICE_STATUS_HANDLE hServiceStatus
,
2321 DWORD dwServiceBits
,
2323 BOOL bUpdateImmediately
)
2325 FIXME("%p %08x %x %x\n", hServiceStatus
, dwServiceBits
,
2326 bSetBitsOn
, bUpdateImmediately
);
2330 /* thunk for calling the RegisterServiceCtrlHandler handler function */
2331 static DWORD WINAPI
ctrl_handler_thunk( DWORD control
, DWORD type
, void *data
, void *context
)
2333 LPHANDLER_FUNCTION func
= context
;
2336 return ERROR_SUCCESS
;
2339 /******************************************************************************
2340 * RegisterServiceCtrlHandlerA [ADVAPI32.@]
2342 SERVICE_STATUS_HANDLE WINAPI
RegisterServiceCtrlHandlerA( LPCSTR name
, LPHANDLER_FUNCTION handler
)
2344 return RegisterServiceCtrlHandlerExA( name
, ctrl_handler_thunk
, handler
);
2347 /******************************************************************************
2348 * RegisterServiceCtrlHandlerW [ADVAPI32.@]
2350 SERVICE_STATUS_HANDLE WINAPI
RegisterServiceCtrlHandlerW( LPCWSTR name
, LPHANDLER_FUNCTION handler
)
2352 return RegisterServiceCtrlHandlerExW( name
, ctrl_handler_thunk
, handler
);
2355 /******************************************************************************
2356 * RegisterServiceCtrlHandlerExA [ADVAPI32.@]
2358 SERVICE_STATUS_HANDLE WINAPI
RegisterServiceCtrlHandlerExA( LPCSTR name
, LPHANDLER_FUNCTION_EX handler
, LPVOID context
)
2361 SERVICE_STATUS_HANDLE ret
;
2363 nameW
= SERV_dup(name
);
2364 ret
= RegisterServiceCtrlHandlerExW( nameW
, handler
, context
);
2369 /******************************************************************************
2370 * RegisterServiceCtrlHandlerExW [ADVAPI32.@]
2372 SERVICE_STATUS_HANDLE WINAPI
RegisterServiceCtrlHandlerExW( LPCWSTR lpServiceName
,
2373 LPHANDLER_FUNCTION_EX lpHandlerProc
, LPVOID lpContext
)
2375 service_data
*service
;
2376 SC_HANDLE hService
= 0;
2378 TRACE("%s %p %p\n", debugstr_w(lpServiceName
), lpHandlerProc
, lpContext
);
2380 EnterCriticalSection( &service_cs
);
2381 if ((service
= find_service_by_name( lpServiceName
)))
2383 service
->handler
= lpHandlerProc
;
2384 service
->context
= lpContext
;
2385 hService
= service
->handle
;
2387 LeaveCriticalSection( &service_cs
);
2389 if (!hService
) SetLastError( ERROR_SERVICE_DOES_NOT_EXIST
);
2390 return (SERVICE_STATUS_HANDLE
)hService
;
2393 /******************************************************************************
2394 * EnumDependentServicesA [ADVAPI32.@]
2396 BOOL WINAPI
EnumDependentServicesA( SC_HANDLE hService
, DWORD dwServiceState
,
2397 LPENUM_SERVICE_STATUSA lpServices
, DWORD cbBufSize
,
2398 LPDWORD pcbBytesNeeded
, LPDWORD lpServicesReturned
)
2400 FIXME("%p 0x%08x %p 0x%08x %p %p - stub\n", hService
, dwServiceState
,
2401 lpServices
, cbBufSize
, pcbBytesNeeded
, lpServicesReturned
);
2403 *lpServicesReturned
= 0;
2407 /******************************************************************************
2408 * EnumDependentServicesW [ADVAPI32.@]
2410 BOOL WINAPI
EnumDependentServicesW( SC_HANDLE hService
, DWORD dwServiceState
,
2411 LPENUM_SERVICE_STATUSW lpServices
, DWORD cbBufSize
,
2412 LPDWORD pcbBytesNeeded
, LPDWORD lpServicesReturned
)
2414 FIXME("%p 0x%08x %p 0x%08x %p %p - stub\n", hService
, dwServiceState
,
2415 lpServices
, cbBufSize
, pcbBytesNeeded
, lpServicesReturned
);
2417 *lpServicesReturned
= 0;
2421 /******************************************************************************
2422 * NotifyServiceStatusChangeW [ADVAPI32.@]
2424 DWORD WINAPI
NotifyServiceStatusChangeW(SC_HANDLE hService
, DWORD dwNotifyMask
,
2425 SERVICE_NOTIFYW
*pNotifyBuffer
)
2429 SERVICE_STATUS_PROCESS st
;
2431 FIXME("%p 0x%x %p - semi-stub\n", hService
, dwNotifyMask
, pNotifyBuffer
);
2433 ret
= QueryServiceStatusEx(hService
, SC_STATUS_PROCESS_INFO
, (void*)&st
, sizeof(st
), &dummy
);
2436 /* dwNotifyMask is a set of bitflags in same order as SERVICE_ statuses */
2437 if (dwNotifyMask
& (1 << (st
.dwCurrentState
- SERVICE_STOPPED
)))
2439 pNotifyBuffer
->dwNotificationStatus
= ERROR_SUCCESS
;
2440 memcpy(&pNotifyBuffer
->ServiceStatus
, &st
, sizeof(pNotifyBuffer
->ServiceStatus
));
2441 pNotifyBuffer
->dwNotificationTriggered
= 1 << (st
.dwCurrentState
- SERVICE_STOPPED
);
2442 pNotifyBuffer
->pszServiceNames
= NULL
;
2443 TRACE("Queueing notification: 0x%x\n", pNotifyBuffer
->dwNotificationTriggered
);
2444 QueueUserAPC((PAPCFUNC
)pNotifyBuffer
->pfnNotifyCallback
,
2445 GetCurrentThread(), (ULONG_PTR
)pNotifyBuffer
);
2449 /* TODO: If the service is not currently in a matching state, we should
2450 * tell `services` to monitor it. */
2452 return ERROR_SUCCESS
;