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"
36 #include "wine/unicode.h"
37 #include "wine/debug.h"
44 #include "wine/exception.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(service
);
48 void __RPC_FAR
* __RPC_USER
MIDL_user_allocate(SIZE_T len
)
50 return HeapAlloc(GetProcessHeap(), 0, len
);
53 void __RPC_USER
MIDL_user_free(void __RPC_FAR
* ptr
)
55 HeapFree(GetProcessHeap(), 0, ptr
);
58 typedef struct service_data_t
60 LPHANDLER_FUNCTION_EX handler
;
64 SC_HANDLE full_access_handle
;
67 LPSERVICE_MAIN_FUNCTIONA a
;
68 LPSERVICE_MAIN_FUNCTIONW w
;
74 static CRITICAL_SECTION service_cs
;
75 static CRITICAL_SECTION_DEBUG service_cs_debug
=
78 { &service_cs_debug
.ProcessLocksList
,
79 &service_cs_debug
.ProcessLocksList
},
80 0, 0, { (DWORD_PTR
)(__FILE__
": service_cs") }
82 static CRITICAL_SECTION service_cs
= { &service_cs_debug
, -1, 0, 0, 0, 0 };
84 static service_data
**services
;
85 static unsigned int nb_services
;
86 static HANDLE service_event
;
88 extern HANDLE CDECL
__wine_make_process_system(void);
90 /******************************************************************************
91 * String management functions (same behaviour as strdup)
92 * NOTE: the caller of those functions is responsible for calling HeapFree
93 * in order to release the memory allocated by those functions.
95 static inline LPWSTR
SERV_dup( LPCSTR str
)
102 len
= MultiByteToWideChar( CP_ACP
, 0, str
, -1, NULL
, 0 );
103 wstr
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof (WCHAR
) );
104 MultiByteToWideChar( CP_ACP
, 0, str
, -1, wstr
, len
);
108 static inline LPWSTR
SERV_dupmulti(LPCSTR str
)
116 len
+= MultiByteToWideChar( CP_ACP
, 0, &str
[n
], -1, NULL
, 0 );
117 n
+= (strlen( &str
[n
] ) + 1);
122 wstr
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof (WCHAR
) );
123 MultiByteToWideChar( CP_ACP
, 0, str
, n
, wstr
, len
);
127 static inline DWORD
multisz_cb(LPCWSTR wmultisz
)
129 const WCHAR
*wptr
= wmultisz
;
131 if (wmultisz
== NULL
)
135 wptr
+= lstrlenW(wptr
)+1;
136 return (wptr
- wmultisz
+ 1)*sizeof(WCHAR
);
139 /******************************************************************************
140 * RPC connection with services.exe
143 DECLSPEC_HIDDEN handle_t __RPC_USER
MACHINE_HANDLEW_bind(MACHINE_HANDLEW MachineName
)
145 WCHAR transport
[] = SVCCTL_TRANSPORT
;
146 WCHAR endpoint
[] = SVCCTL_ENDPOINT
;
147 RPC_WSTR binding_str
;
151 status
= RpcStringBindingComposeW(NULL
, transport
, (RPC_WSTR
)MachineName
, endpoint
, NULL
, &binding_str
);
152 if (status
!= RPC_S_OK
)
154 ERR("RpcStringBindingComposeW failed (%d)\n", (DWORD
)status
);
158 status
= RpcBindingFromStringBindingW(binding_str
, &rpc_handle
);
159 RpcStringFreeW(&binding_str
);
161 if (status
!= RPC_S_OK
)
163 ERR("Couldn't connect to services.exe: error code %u\n", (DWORD
)status
);
170 DECLSPEC_HIDDEN
void __RPC_USER
MACHINE_HANDLEW_unbind(MACHINE_HANDLEW MachineName
, handle_t h
)
175 static LONG WINAPI
rpc_filter(EXCEPTION_POINTERS
*eptr
)
177 return I_RpcExceptionFilter(eptr
->ExceptionRecord
->ExceptionCode
);
180 static DWORD
map_exception_code(DWORD exception_code
)
182 switch (exception_code
)
184 case RPC_X_NULL_REF_POINTER
:
185 return ERROR_INVALID_ADDRESS
;
186 case RPC_X_ENUM_VALUE_OUT_OF_RANGE
:
187 case RPC_X_BYTE_COUNT_TOO_SMALL
:
188 return ERROR_INVALID_PARAMETER
;
189 case RPC_S_INVALID_BINDING
:
190 case RPC_X_SS_IN_NULL_CONTEXT
:
191 return ERROR_INVALID_HANDLE
;
193 return exception_code
;
197 /******************************************************************************
198 * Service IPC functions
200 static LPWSTR
service_get_pipe_name(void)
202 static const WCHAR format
[] = { '\\','\\','.','\\','p','i','p','e','\\',
203 'n','e','t','\\','N','t','C','o','n','t','r','o','l','P','i','p','e','%','u',0};
204 static const WCHAR service_current_key_str
[] = { 'S','Y','S','T','E','M','\\',
205 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
206 'C','o','n','t','r','o','l','\\',
207 'S','e','r','v','i','c','e','C','u','r','r','e','n','t',0};
210 HKEY service_current_key
;
211 DWORD service_current
;
215 ret
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, service_current_key_str
, 0,
216 KEY_QUERY_VALUE
, &service_current_key
);
217 if (ret
!= ERROR_SUCCESS
)
219 len
= sizeof(service_current
);
220 ret
= RegQueryValueExW(service_current_key
, NULL
, NULL
, &type
,
221 (BYTE
*)&service_current
, &len
);
222 RegCloseKey(service_current_key
);
223 if (ret
!= ERROR_SUCCESS
|| type
!= REG_DWORD
)
225 len
= sizeof(format
)/sizeof(WCHAR
) + 10 /* strlenW("4294967295") */;
226 name
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
229 snprintfW(name
, len
, format
, service_current
);
233 static HANDLE
service_open_pipe(void)
235 LPWSTR szPipe
= service_get_pipe_name();
236 HANDLE handle
= INVALID_HANDLE_VALUE
;
239 handle
= CreateFileW(szPipe
, GENERIC_READ
|GENERIC_WRITE
,
240 0, NULL
, OPEN_ALWAYS
, 0, NULL
);
241 if (handle
!= INVALID_HANDLE_VALUE
)
243 if (GetLastError() != ERROR_PIPE_BUSY
)
245 } while (WaitNamedPipeW(szPipe
, NMPWAIT_USE_DEFAULT_WAIT
));
246 HeapFree(GetProcessHeap(), 0, szPipe
);
251 static service_data
*find_service_by_name( const WCHAR
*name
)
255 if (nb_services
== 1) /* only one service (FIXME: should depend on OWN_PROCESS etc.) */
257 for (i
= 0; i
< nb_services
; i
++)
258 if (!strcmpiW( name
, services
[i
]->name
)) return services
[i
];
262 /******************************************************************************
265 * Call into the main service routine provided by StartServiceCtrlDispatcher.
267 static DWORD WINAPI
service_thread(LPVOID arg
)
269 service_data
*info
= arg
;
270 LPWSTR str
= info
->args
;
271 DWORD argc
= 0, len
= 0;
277 len
+= strlenW(&str
[len
]) + 1;
286 argv
= HeapAlloc(GetProcessHeap(), 0, (argc
+1)*sizeof(LPWSTR
));
287 for (argc
=0, p
=str
; *p
; p
+= strlenW(p
) + 1)
291 info
->proc
.w(argc
, argv
);
292 HeapFree(GetProcessHeap(), 0, argv
);
296 LPSTR strA
, *argv
, p
;
299 lenA
= WideCharToMultiByte(CP_ACP
,0, str
, len
, NULL
, 0, NULL
, NULL
);
300 strA
= HeapAlloc(GetProcessHeap(), 0, lenA
);
301 WideCharToMultiByte(CP_ACP
,0, str
, len
, strA
, lenA
, NULL
, NULL
);
303 argv
= HeapAlloc(GetProcessHeap(), 0, (argc
+1)*sizeof(LPSTR
));
304 for (argc
=0, p
=strA
; *p
; p
+= strlen(p
) + 1)
308 info
->proc
.a(argc
, argv
);
309 HeapFree(GetProcessHeap(), 0, argv
);
310 HeapFree(GetProcessHeap(), 0, strA
);
315 /******************************************************************************
316 * service_handle_start
318 static DWORD
service_handle_start(service_data
*service
, const WCHAR
*data
, DWORD count
)
320 TRACE("%s argsize %u\n", debugstr_w(service
->name
), count
);
324 WARN("service is not stopped\n");
325 return ERROR_SERVICE_ALREADY_RUNNING
;
328 HeapFree(GetProcessHeap(), 0, service
->args
);
329 service
->args
= HeapAlloc(GetProcessHeap(), 0, count
* sizeof(WCHAR
));
330 memcpy( service
->args
, data
, count
* sizeof(WCHAR
) );
331 service
->thread
= CreateThread( NULL
, 0, service_thread
,
333 SetEvent( service_event
); /* notify the main loop */
337 /******************************************************************************
338 * service_handle_control
340 static DWORD
service_handle_control(const service_data
*service
, DWORD dwControl
)
342 DWORD ret
= ERROR_INVALID_SERVICE_CONTROL
;
344 TRACE("%s control %u\n", debugstr_w(service
->name
), dwControl
);
346 if (service
->handler
)
347 ret
= service
->handler(dwControl
, 0, NULL
, service
->context
);
351 /******************************************************************************
352 * service_control_dispatcher
354 static DWORD WINAPI
service_control_dispatcher(LPVOID arg
)
359 if (!(manager
= OpenSCManagerW( NULL
, NULL
, SC_MANAGER_CONNECT
)))
361 ERR("failed to open service manager error %u\n", GetLastError());
365 pipe
= service_open_pipe();
367 if (pipe
==INVALID_HANDLE_VALUE
)
369 WARN("failed to create control pipe error = %d\n", GetLastError());
373 /* dispatcher loop */
376 service_data
*service
;
377 service_start_info info
;
380 DWORD data_size
= 0, count
, result
;
382 r
= ReadFile( pipe
, &info
, FIELD_OFFSET(service_start_info
,data
), &count
, NULL
);
385 if (GetLastError() != ERROR_BROKEN_PIPE
)
386 ERR( "pipe read failed error %u\n", GetLastError() );
389 if (count
!= FIELD_OFFSET(service_start_info
,data
))
391 ERR( "partial pipe read %u\n", count
);
394 if (count
< info
.total_size
)
396 data_size
= info
.total_size
- FIELD_OFFSET(service_start_info
,data
);
397 data
= HeapAlloc( GetProcessHeap(), 0, data_size
);
398 r
= ReadFile( pipe
, data
, data_size
, &count
, NULL
);
401 if (GetLastError() != ERROR_BROKEN_PIPE
)
402 ERR( "pipe read failed error %u\n", GetLastError() );
405 if (count
!= data_size
)
407 ERR( "partial pipe read %u/%u\n", count
, data_size
);
412 /* find the service */
414 if (!(service
= find_service_by_name( data
)))
416 FIXME( "got request %u for unknown service %s\n", info
.cmd
, debugstr_w(data
));
417 result
= ERROR_INVALID_PARAMETER
;
421 TRACE( "got request %u for service %s\n", info
.cmd
, debugstr_w(data
) );
423 /* handle the request */
426 case WINESERV_STARTINFO
:
427 if (!service
->handle
)
429 if (!(service
->handle
= OpenServiceW( manager
, data
, SERVICE_SET_STATUS
)) ||
430 !(service
->full_access_handle
= OpenServiceW( manager
, data
, GENERIC_READ
|GENERIC_WRITE
)))
431 FIXME( "failed to open service %s\n", debugstr_w(data
) );
433 result
= service_handle_start(service
, data
+ info
.name_size
,
434 data_size
/ sizeof(WCHAR
) - info
.name_size
);
436 case WINESERV_SENDCONTROL
:
437 result
= service_handle_control(service
, info
.control
);
440 ERR("received invalid command %u\n", info
.cmd
);
441 result
= ERROR_INVALID_PARAMETER
;
446 WriteFile(pipe
, &result
, sizeof(result
), &count
, NULL
);
447 HeapFree( GetProcessHeap(), 0, data
);
451 CloseServiceHandle( manager
);
455 /******************************************************************************
456 * service_run_main_thread
458 static BOOL
service_run_main_thread(void)
461 HANDLE wait_handles
[MAXIMUM_WAIT_OBJECTS
];
462 UINT wait_services
[MAXIMUM_WAIT_OBJECTS
];
464 service_event
= CreateEventW( NULL
, FALSE
, FALSE
, NULL
);
466 /* FIXME: service_control_dispatcher should be merged into the main thread */
467 wait_handles
[0] = __wine_make_process_system();
468 wait_handles
[1] = CreateThread( NULL
, 0, service_control_dispatcher
, NULL
, 0, NULL
);
469 wait_handles
[2] = service_event
;
471 TRACE("Starting %d services running as process %d\n",
472 nb_services
, GetCurrentProcessId());
474 /* wait for all the threads to pack up and exit */
477 EnterCriticalSection( &service_cs
);
478 for (i
= 0, n
= 3; i
< nb_services
&& n
< MAXIMUM_WAIT_OBJECTS
; i
++)
480 if (!services
[i
]->thread
) continue;
481 wait_services
[n
] = i
;
482 wait_handles
[n
++] = services
[i
]->thread
;
484 LeaveCriticalSection( &service_cs
);
486 ret
= WaitForMultipleObjects( n
, wait_handles
, FALSE
, INFINITE
);
487 if (!ret
) /* system process event */
490 SERVICE_PRESHUTDOWN_INFO spi
;
491 DWORD timeout
= 5000;
494 EnterCriticalSection( &service_cs
);
496 for (i
= 0; i
< nb_services
&& n
< MAXIMUM_WAIT_OBJECTS
; i
++)
498 if (!services
[i
]->thread
) continue;
500 res
= QueryServiceStatus(services
[i
]->full_access_handle
, &st
);
502 if (res
&& (st
.dwControlsAccepted
& SERVICE_ACCEPT_PRESHUTDOWN
))
504 res
= QueryServiceConfig2W( services
[i
]->full_access_handle
, SERVICE_CONFIG_PRESHUTDOWN_INFO
,
505 (LPBYTE
)&spi
, sizeof(spi
), &i
);
508 FIXME("service should be able to delay shutdown\n");
509 timeout
+= spi
.dwPreshutdownTimeout
;
510 ret
= service_handle_control( services
[i
], SERVICE_CONTROL_PRESHUTDOWN
);
511 wait_handles
[n
++] = services
[i
]->thread
;
514 else if (res
&& (st
.dwControlsAccepted
& SERVICE_ACCEPT_SHUTDOWN
))
516 ret
= service_handle_control( services
[i
], SERVICE_CONTROL_SHUTDOWN
);
517 wait_handles
[n
++] = services
[i
]->thread
;
520 LeaveCriticalSection( &service_cs
);
522 TRACE("last user process exited, shutting down (timeout: %d)\n", timeout
);
523 WaitForMultipleObjects( n
, wait_handles
, TRUE
, timeout
);
528 TRACE( "control dispatcher exited, shutting down\n" );
529 /* FIXME: we should maybe send a shutdown control to running services */
534 continue; /* rebuild the list */
538 services
[wait_services
[ret
]]->thread
= 0;
539 CloseHandle( wait_handles
[ret
] );
540 if (n
== 4) return TRUE
; /* it was the last running thread */
546 /******************************************************************************
547 * StartServiceCtrlDispatcherA [ADVAPI32.@]
549 * See StartServiceCtrlDispatcherW.
551 BOOL WINAPI
StartServiceCtrlDispatcherA( const SERVICE_TABLE_ENTRYA
*servent
)
557 TRACE("%p\n", servent
);
561 SetLastError( ERROR_SERVICE_ALREADY_RUNNING
);
564 while (servent
[nb_services
].lpServiceName
) nb_services
++;
565 services
= HeapAlloc( GetProcessHeap(), 0, nb_services
* sizeof(*services
) );
567 for (i
= 0; i
< nb_services
; i
++)
569 DWORD len
= MultiByteToWideChar(CP_ACP
, 0, servent
[i
].lpServiceName
, -1, NULL
, 0);
570 DWORD sz
= FIELD_OFFSET( service_data
, name
[len
] );
571 info
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sz
);
572 MultiByteToWideChar(CP_ACP
, 0, servent
[i
].lpServiceName
, -1, info
->name
, len
);
573 info
->proc
.a
= servent
[i
].lpServiceProc
;
574 info
->unicode
= FALSE
;
578 service_run_main_thread();
583 /******************************************************************************
584 * StartServiceCtrlDispatcherW [ADVAPI32.@]
586 * Connects a process containing one or more services to the service control
590 * servent [I] A list of the service names and service procedures
596 BOOL WINAPI
StartServiceCtrlDispatcherW( const SERVICE_TABLE_ENTRYW
*servent
)
602 TRACE("%p\n", servent
);
606 SetLastError( ERROR_SERVICE_ALREADY_RUNNING
);
609 while (servent
[nb_services
].lpServiceName
) nb_services
++;
610 services
= HeapAlloc( GetProcessHeap(), 0, nb_services
* sizeof(*services
) );
612 for (i
= 0; i
< nb_services
; i
++)
614 DWORD len
= strlenW(servent
[i
].lpServiceName
) + 1;
615 DWORD sz
= FIELD_OFFSET( service_data
, name
[len
] );
616 info
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sz
);
617 strcpyW(info
->name
, servent
[i
].lpServiceName
);
618 info
->proc
.w
= servent
[i
].lpServiceProc
;
619 info
->unicode
= TRUE
;
623 service_run_main_thread();
628 /******************************************************************************
629 * LockServiceDatabase [ADVAPI32.@]
631 SC_LOCK WINAPI
LockServiceDatabase (SC_HANDLE hSCManager
)
633 SC_RPC_LOCK hLock
= NULL
;
636 TRACE("%p\n",hSCManager
);
640 err
= svcctl_LockServiceDatabase(hSCManager
, &hLock
);
644 err
= map_exception_code(GetExceptionCode());
647 if (err
!= ERROR_SUCCESS
)
655 /******************************************************************************
656 * UnlockServiceDatabase [ADVAPI32.@]
658 BOOL WINAPI
UnlockServiceDatabase (SC_LOCK ScLock
)
661 SC_RPC_LOCK hRpcLock
= ScLock
;
663 TRACE("%p\n",ScLock
);
667 err
= svcctl_UnlockServiceDatabase(&hRpcLock
);
671 err
= map_exception_code(GetExceptionCode());
674 if (err
!= ERROR_SUCCESS
)
682 /******************************************************************************
683 * SetServiceStatus [ADVAPI32.@]
690 SetServiceStatus( SERVICE_STATUS_HANDLE hService
, LPSERVICE_STATUS lpStatus
)
694 TRACE("%p %x %x %x %x %x %x %x\n", hService
,
695 lpStatus
->dwServiceType
, lpStatus
->dwCurrentState
,
696 lpStatus
->dwControlsAccepted
, lpStatus
->dwWin32ExitCode
,
697 lpStatus
->dwServiceSpecificExitCode
, lpStatus
->dwCheckPoint
,
698 lpStatus
->dwWaitHint
);
702 err
= svcctl_SetServiceStatus( hService
, lpStatus
);
706 err
= map_exception_code(GetExceptionCode());
709 if (err
!= ERROR_SUCCESS
)
715 if (lpStatus
->dwCurrentState
== SERVICE_STOPPED
)
716 CloseServiceHandle((SC_HANDLE
)hService
);
722 /******************************************************************************
723 * OpenSCManagerA [ADVAPI32.@]
725 * Establish a connection to the service control manager and open its database.
728 * lpMachineName [I] Pointer to machine name string
729 * lpDatabaseName [I] Pointer to database name string
730 * dwDesiredAccess [I] Type of access
733 * Success: A Handle to the service control manager database
736 SC_HANDLE WINAPI
OpenSCManagerA( LPCSTR lpMachineName
, LPCSTR lpDatabaseName
,
737 DWORD dwDesiredAccess
)
739 LPWSTR lpMachineNameW
, lpDatabaseNameW
;
742 lpMachineNameW
= SERV_dup(lpMachineName
);
743 lpDatabaseNameW
= SERV_dup(lpDatabaseName
);
744 ret
= OpenSCManagerW(lpMachineNameW
, lpDatabaseNameW
, dwDesiredAccess
);
745 HeapFree(GetProcessHeap(), 0, lpDatabaseNameW
);
746 HeapFree(GetProcessHeap(), 0, lpMachineNameW
);
750 /******************************************************************************
751 * OpenSCManagerW [ADVAPI32.@]
753 * See OpenSCManagerA.
755 SC_HANDLE WINAPI
OpenSCManagerW( LPCWSTR lpMachineName
, LPCWSTR lpDatabaseName
,
756 DWORD dwDesiredAccess
)
758 SC_HANDLE handle
= 0;
761 TRACE("(%s,%s,0x%08x)\n", debugstr_w(lpMachineName
),
762 debugstr_w(lpDatabaseName
), dwDesiredAccess
);
766 r
= svcctl_OpenSCManagerW(lpMachineName
, lpDatabaseName
, dwDesiredAccess
, (SC_RPC_HANDLE
*)&handle
);
770 r
= map_exception_code(GetExceptionCode());
774 if (r
!=ERROR_SUCCESS
)
780 TRACE("returning %p\n", handle
);
784 /******************************************************************************
785 * ControlService [ADVAPI32.@]
787 * Send a control code to a service.
790 * hService [I] Handle of the service control manager database
791 * dwControl [I] Control code to send (SERVICE_CONTROL_* flags from "winsvc.h")
792 * lpServiceStatus [O] Destination for the status of the service, if available
799 * Unlike M$' implementation, control requests are not serialized and may be
800 * processed asynchronously.
802 BOOL WINAPI
ControlService( SC_HANDLE hService
, DWORD dwControl
,
803 LPSERVICE_STATUS lpServiceStatus
)
807 TRACE("%p %d %p\n", hService
, dwControl
, lpServiceStatus
);
811 err
= svcctl_ControlService(hService
, dwControl
, lpServiceStatus
);
815 err
= map_exception_code(GetExceptionCode());
818 if (err
!= ERROR_SUCCESS
)
827 /******************************************************************************
828 * CloseServiceHandle [ADVAPI32.@]
830 * Close a handle to a service or the service control manager database.
833 * hSCObject [I] Handle to service or service control manager database
840 CloseServiceHandle( SC_HANDLE hSCObject
)
844 TRACE("%p\n", hSCObject
);
848 err
= svcctl_CloseServiceHandle((SC_RPC_HANDLE
*)&hSCObject
);
852 err
= map_exception_code(GetExceptionCode());
856 if (err
!= ERROR_SUCCESS
)
865 /******************************************************************************
866 * OpenServiceA [ADVAPI32.@]
868 * Open a handle to a service.
871 * hSCManager [I] Handle of the service control manager database
872 * lpServiceName [I] Name of the service to open
873 * dwDesiredAccess [I] Access required to the service
876 * Success: Handle to the service
879 SC_HANDLE WINAPI
OpenServiceA( SC_HANDLE hSCManager
, LPCSTR lpServiceName
,
880 DWORD dwDesiredAccess
)
882 LPWSTR lpServiceNameW
;
885 TRACE("%p %s %d\n", hSCManager
, debugstr_a(lpServiceName
), dwDesiredAccess
);
887 lpServiceNameW
= SERV_dup(lpServiceName
);
888 ret
= OpenServiceW( hSCManager
, lpServiceNameW
, dwDesiredAccess
);
889 HeapFree(GetProcessHeap(), 0, lpServiceNameW
);
894 /******************************************************************************
895 * OpenServiceW [ADVAPI32.@]
899 SC_HANDLE WINAPI
OpenServiceW( SC_HANDLE hSCManager
, LPCWSTR lpServiceName
,
900 DWORD dwDesiredAccess
)
902 SC_HANDLE handle
= 0;
905 TRACE("%p %s %d\n", hSCManager
, debugstr_w(lpServiceName
), dwDesiredAccess
);
909 SetLastError( ERROR_INVALID_HANDLE
);
915 err
= svcctl_OpenServiceW(hSCManager
, lpServiceName
, dwDesiredAccess
, (SC_RPC_HANDLE
*)&handle
);
919 err
= map_exception_code(GetExceptionCode());
923 if (err
!= ERROR_SUCCESS
)
929 TRACE("returning %p\n",handle
);
933 /******************************************************************************
934 * CreateServiceW [ADVAPI32.@]
937 CreateServiceW( SC_HANDLE hSCManager
, LPCWSTR lpServiceName
,
938 LPCWSTR lpDisplayName
, DWORD dwDesiredAccess
,
939 DWORD dwServiceType
, DWORD dwStartType
,
940 DWORD dwErrorControl
, LPCWSTR lpBinaryPathName
,
941 LPCWSTR lpLoadOrderGroup
, LPDWORD lpdwTagId
,
942 LPCWSTR lpDependencies
, LPCWSTR lpServiceStartName
,
945 SC_HANDLE handle
= 0;
949 TRACE("%p %s %s\n", hSCManager
,
950 debugstr_w(lpServiceName
), debugstr_w(lpDisplayName
));
954 SetLastError( ERROR_INVALID_HANDLE
);
959 passwdlen
= (strlenW(lpPassword
) + 1) * sizeof(WCHAR
);
965 err
= svcctl_CreateServiceW(hSCManager
, lpServiceName
,
966 lpDisplayName
, dwDesiredAccess
, dwServiceType
, dwStartType
, dwErrorControl
,
967 lpBinaryPathName
, lpLoadOrderGroup
, lpdwTagId
, (const BYTE
*)lpDependencies
,
968 multisz_cb(lpDependencies
), lpServiceStartName
, (const BYTE
*)lpPassword
, passwdlen
,
969 (SC_RPC_HANDLE
*)&handle
);
973 err
= map_exception_code(GetExceptionCode());
977 if (err
!= ERROR_SUCCESS
)
986 /******************************************************************************
987 * CreateServiceA [ADVAPI32.@]
990 CreateServiceA( SC_HANDLE hSCManager
, LPCSTR lpServiceName
,
991 LPCSTR lpDisplayName
, DWORD dwDesiredAccess
,
992 DWORD dwServiceType
, DWORD dwStartType
,
993 DWORD dwErrorControl
, LPCSTR lpBinaryPathName
,
994 LPCSTR lpLoadOrderGroup
, LPDWORD lpdwTagId
,
995 LPCSTR lpDependencies
, LPCSTR lpServiceStartName
,
998 LPWSTR lpServiceNameW
, lpDisplayNameW
, lpBinaryPathNameW
,
999 lpLoadOrderGroupW
, lpDependenciesW
, lpServiceStartNameW
, lpPasswordW
;
1002 TRACE("%p %s %s\n", hSCManager
,
1003 debugstr_a(lpServiceName
), debugstr_a(lpDisplayName
));
1005 lpServiceNameW
= SERV_dup( lpServiceName
);
1006 lpDisplayNameW
= SERV_dup( lpDisplayName
);
1007 lpBinaryPathNameW
= SERV_dup( lpBinaryPathName
);
1008 lpLoadOrderGroupW
= SERV_dup( lpLoadOrderGroup
);
1009 lpDependenciesW
= SERV_dupmulti( lpDependencies
);
1010 lpServiceStartNameW
= SERV_dup( lpServiceStartName
);
1011 lpPasswordW
= SERV_dup( lpPassword
);
1013 r
= CreateServiceW( hSCManager
, lpServiceNameW
, lpDisplayNameW
,
1014 dwDesiredAccess
, dwServiceType
, dwStartType
, dwErrorControl
,
1015 lpBinaryPathNameW
, lpLoadOrderGroupW
, lpdwTagId
,
1016 lpDependenciesW
, lpServiceStartNameW
, lpPasswordW
);
1018 HeapFree( GetProcessHeap(), 0, lpServiceNameW
);
1019 HeapFree( GetProcessHeap(), 0, lpDisplayNameW
);
1020 HeapFree( GetProcessHeap(), 0, lpBinaryPathNameW
);
1021 HeapFree( GetProcessHeap(), 0, lpLoadOrderGroupW
);
1022 HeapFree( GetProcessHeap(), 0, lpDependenciesW
);
1023 HeapFree( GetProcessHeap(), 0, lpServiceStartNameW
);
1024 HeapFree( GetProcessHeap(), 0, lpPasswordW
);
1030 /******************************************************************************
1031 * DeleteService [ADVAPI32.@]
1033 * Delete a service from the service control manager database.
1036 * hService [I] Handle of the service to delete
1042 BOOL WINAPI
DeleteService( SC_HANDLE hService
)
1048 err
= svcctl_DeleteService(hService
);
1050 __EXCEPT(rpc_filter
)
1052 err
= map_exception_code(GetExceptionCode());
1065 /******************************************************************************
1066 * StartServiceA [ADVAPI32.@]
1071 * hService [I] Handle of service
1072 * dwNumServiceArgs [I] Number of arguments
1073 * lpServiceArgVectors [I] Address of array of argument strings
1076 * - NT implements this function using an obscure RPC call.
1077 * - You might need to do a "setenv SystemRoot \\WINNT" in your .cshrc
1078 * to get things like "%SystemRoot%\\System32\\service.exe" to load.
1079 * - This will only work for shared address space. How should the service
1080 * args be transferred when address spaces are separated?
1081 * - Can only start one service at a time.
1082 * - Has no concept of privilege.
1088 BOOL WINAPI
StartServiceA( SC_HANDLE hService
, DWORD dwNumServiceArgs
,
1089 LPCSTR
*lpServiceArgVectors
)
1091 LPWSTR
*lpwstr
=NULL
;
1095 TRACE("(%p,%d,%p)\n",hService
,dwNumServiceArgs
,lpServiceArgVectors
);
1097 if (dwNumServiceArgs
)
1098 lpwstr
= HeapAlloc( GetProcessHeap(), 0,
1099 dwNumServiceArgs
*sizeof(LPWSTR
) );
1101 for(i
=0; i
<dwNumServiceArgs
; i
++)
1102 lpwstr
[i
]=SERV_dup(lpServiceArgVectors
[i
]);
1104 r
= StartServiceW(hService
, dwNumServiceArgs
, (LPCWSTR
*)lpwstr
);
1106 if (dwNumServiceArgs
)
1108 for(i
=0; i
<dwNumServiceArgs
; i
++)
1109 HeapFree(GetProcessHeap(), 0, lpwstr
[i
]);
1110 HeapFree(GetProcessHeap(), 0, lpwstr
);
1117 /******************************************************************************
1118 * StartServiceW [ADVAPI32.@]
1120 * See StartServiceA.
1122 BOOL WINAPI
StartServiceW(SC_HANDLE hService
, DWORD dwNumServiceArgs
,
1123 LPCWSTR
*lpServiceArgVectors
)
1127 TRACE("%p %d %p\n", hService
, dwNumServiceArgs
, lpServiceArgVectors
);
1131 err
= svcctl_StartServiceW(hService
, dwNumServiceArgs
, lpServiceArgVectors
);
1133 __EXCEPT(rpc_filter
)
1135 err
= map_exception_code(GetExceptionCode());
1138 if (err
!= ERROR_SUCCESS
)
1147 /******************************************************************************
1148 * QueryServiceStatus [ADVAPI32.@]
1151 * hService [I] Handle to service to get information about
1152 * lpservicestatus [O] buffer to receive the status information for the service
1155 BOOL WINAPI
QueryServiceStatus(SC_HANDLE hService
,
1156 LPSERVICE_STATUS lpservicestatus
)
1158 SERVICE_STATUS_PROCESS SvcStatusData
;
1162 TRACE("%p %p\n", hService
, lpservicestatus
);
1166 SetLastError(ERROR_INVALID_HANDLE
);
1169 if (!lpservicestatus
)
1171 SetLastError(ERROR_INVALID_ADDRESS
);
1175 ret
= QueryServiceStatusEx(hService
, SC_STATUS_PROCESS_INFO
, (LPBYTE
)&SvcStatusData
,
1176 sizeof(SERVICE_STATUS_PROCESS
), &dummy
);
1177 if (ret
) memcpy(lpservicestatus
, &SvcStatusData
, sizeof(SERVICE_STATUS
)) ;
1182 /******************************************************************************
1183 * QueryServiceStatusEx [ADVAPI32.@]
1185 * Get information about a service.
1188 * hService [I] Handle to service to get information about
1189 * InfoLevel [I] Level of information to get
1190 * lpBuffer [O] Destination for requested information
1191 * cbBufSize [I] Size of lpBuffer in bytes
1192 * pcbBytesNeeded [O] Destination for number of bytes needed, if cbBufSize is too small
1198 BOOL WINAPI
QueryServiceStatusEx(SC_HANDLE hService
, SC_STATUS_TYPE InfoLevel
,
1199 LPBYTE lpBuffer
, DWORD cbBufSize
,
1200 LPDWORD pcbBytesNeeded
)
1204 TRACE("%p %d %p %d %p\n", hService
, InfoLevel
, lpBuffer
, cbBufSize
, pcbBytesNeeded
);
1206 if (InfoLevel
!= SC_STATUS_PROCESS_INFO
)
1208 err
= ERROR_INVALID_LEVEL
;
1210 else if (cbBufSize
< sizeof(SERVICE_STATUS_PROCESS
))
1212 *pcbBytesNeeded
= sizeof(SERVICE_STATUS_PROCESS
);
1213 err
= ERROR_INSUFFICIENT_BUFFER
;
1219 err
= svcctl_QueryServiceStatusEx(hService
, InfoLevel
, lpBuffer
, cbBufSize
, pcbBytesNeeded
);
1221 __EXCEPT(rpc_filter
)
1223 err
= map_exception_code(GetExceptionCode());
1227 if (err
!= ERROR_SUCCESS
)
1235 /******************************************************************************
1236 * QueryServiceConfigA [ADVAPI32.@]
1238 BOOL WINAPI
QueryServiceConfigA( SC_HANDLE hService
, LPQUERY_SERVICE_CONFIGA config
,
1239 DWORD size
, LPDWORD needed
)
1244 QUERY_SERVICE_CONFIGW
*configW
;
1246 TRACE("%p %p %d %p\n", hService
, config
, size
, needed
);
1248 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, 2 * size
)))
1250 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1253 configW
= (QUERY_SERVICE_CONFIGW
*)buffer
;
1254 ret
= QueryServiceConfigW( hService
, configW
, 2 * size
, needed
);
1255 if (!ret
) goto done
;
1257 config
->dwServiceType
= configW
->dwServiceType
;
1258 config
->dwStartType
= configW
->dwStartType
;
1259 config
->dwErrorControl
= configW
->dwErrorControl
;
1260 config
->lpBinaryPathName
= NULL
;
1261 config
->lpLoadOrderGroup
= NULL
;
1262 config
->dwTagId
= configW
->dwTagId
;
1263 config
->lpDependencies
= NULL
;
1264 config
->lpServiceStartName
= NULL
;
1265 config
->lpDisplayName
= NULL
;
1267 p
= (LPSTR
)(config
+ 1);
1268 n
= size
- sizeof(*config
);
1271 #define MAP_STR(str) \
1275 DWORD sz = WideCharToMultiByte( CP_ACP, 0, configW->str, -1, p, n, NULL, NULL ); \
1276 if (!sz) goto done; \
1283 MAP_STR( lpBinaryPathName
);
1284 MAP_STR( lpLoadOrderGroup
);
1285 MAP_STR( lpDependencies
);
1286 MAP_STR( lpServiceStartName
);
1287 MAP_STR( lpDisplayName
);
1290 *needed
= p
- (LPSTR
)config
;
1294 HeapFree( GetProcessHeap(), 0, buffer
);
1298 static DWORD
move_string_to_buffer(BYTE
**buf
, LPWSTR
*string_ptr
)
1305 memset(*buf
, 0, cb
);
1309 cb
= (strlenW(*string_ptr
) + 1)*sizeof(WCHAR
);
1310 memcpy(*buf
, *string_ptr
, cb
);
1311 MIDL_user_free(*string_ptr
);
1314 *string_ptr
= (LPWSTR
)*buf
;
1320 static DWORD
size_string(LPCWSTR string
)
1322 return (string
? (strlenW(string
) + 1)*sizeof(WCHAR
) : sizeof(WCHAR
));
1325 /******************************************************************************
1326 * QueryServiceConfigW [ADVAPI32.@]
1329 QueryServiceConfigW( SC_HANDLE hService
,
1330 LPQUERY_SERVICE_CONFIGW lpServiceConfig
,
1331 DWORD cbBufSize
, LPDWORD pcbBytesNeeded
)
1333 QUERY_SERVICE_CONFIGW config
;
1338 TRACE("%p %p %d %p\n", hService
, lpServiceConfig
,
1339 cbBufSize
, pcbBytesNeeded
);
1341 memset(&config
, 0, sizeof(config
));
1345 err
= svcctl_QueryServiceConfigW(hService
, &config
);
1347 __EXCEPT(rpc_filter
)
1349 err
= map_exception_code(GetExceptionCode());
1353 if (err
!= ERROR_SUCCESS
)
1355 TRACE("services.exe: error %u\n", err
);
1360 /* calculate the size required first */
1361 total
= sizeof (QUERY_SERVICE_CONFIGW
);
1362 total
+= size_string(config
.lpBinaryPathName
);
1363 total
+= size_string(config
.lpLoadOrderGroup
);
1364 total
+= size_string(config
.lpDependencies
);
1365 total
+= size_string(config
.lpServiceStartName
);
1366 total
+= size_string(config
.lpDisplayName
);
1368 *pcbBytesNeeded
= total
;
1370 /* if there's not enough memory, return an error */
1371 if( total
> cbBufSize
)
1373 SetLastError( ERROR_INSUFFICIENT_BUFFER
);
1374 MIDL_user_free(config
.lpBinaryPathName
);
1375 MIDL_user_free(config
.lpLoadOrderGroup
);
1376 MIDL_user_free(config
.lpDependencies
);
1377 MIDL_user_free(config
.lpServiceStartName
);
1378 MIDL_user_free(config
.lpDisplayName
);
1382 *lpServiceConfig
= config
;
1383 bufpos
= ((BYTE
*)lpServiceConfig
) + sizeof(QUERY_SERVICE_CONFIGW
);
1384 move_string_to_buffer(&bufpos
, &lpServiceConfig
->lpBinaryPathName
);
1385 move_string_to_buffer(&bufpos
, &lpServiceConfig
->lpLoadOrderGroup
);
1386 move_string_to_buffer(&bufpos
, &lpServiceConfig
->lpDependencies
);
1387 move_string_to_buffer(&bufpos
, &lpServiceConfig
->lpServiceStartName
);
1388 move_string_to_buffer(&bufpos
, &lpServiceConfig
->lpDisplayName
);
1390 TRACE("Image path = %s\n", debugstr_w(lpServiceConfig
->lpBinaryPathName
) );
1391 TRACE("Group = %s\n", debugstr_w(lpServiceConfig
->lpLoadOrderGroup
) );
1392 TRACE("Dependencies = %s\n", debugstr_w(lpServiceConfig
->lpDependencies
) );
1393 TRACE("Service account name = %s\n", debugstr_w(lpServiceConfig
->lpServiceStartName
) );
1394 TRACE("Display name = %s\n", debugstr_w(lpServiceConfig
->lpDisplayName
) );
1399 /******************************************************************************
1400 * QueryServiceConfig2A [ADVAPI32.@]
1403 * observed under win2k:
1404 * The functions QueryServiceConfig2A and QueryServiceConfig2W return the same
1405 * required buffer size (in byte) at least for dwLevel SERVICE_CONFIG_DESCRIPTION
1407 BOOL WINAPI
QueryServiceConfig2A(SC_HANDLE hService
, DWORD dwLevel
, LPBYTE buffer
,
1408 DWORD size
, LPDWORD needed
)
1411 LPBYTE bufferW
= NULL
;
1414 bufferW
= HeapAlloc( GetProcessHeap(), 0, size
);
1416 ret
= QueryServiceConfig2W(hService
, dwLevel
, bufferW
, size
, needed
);
1417 if(!ret
) goto cleanup
;
1420 case SERVICE_CONFIG_DESCRIPTION
:
1421 if (buffer
&& bufferW
) {
1422 LPSERVICE_DESCRIPTIONA configA
= (LPSERVICE_DESCRIPTIONA
) buffer
;
1423 LPSERVICE_DESCRIPTIONW configW
= (LPSERVICE_DESCRIPTIONW
) bufferW
;
1424 if (configW
->lpDescription
&& (size
> sizeof(SERVICE_DESCRIPTIONA
))) {
1426 configA
->lpDescription
= (LPSTR
)(configA
+ 1);
1427 sz
= WideCharToMultiByte( CP_ACP
, 0, configW
->lpDescription
, -1,
1428 configA
->lpDescription
, size
- sizeof(SERVICE_DESCRIPTIONA
), NULL
, NULL
);
1430 FIXME("WideCharToMultiByte failed for configW->lpDescription\n");
1432 configA
->lpDescription
= NULL
;
1435 else configA
->lpDescription
= NULL
;
1438 case SERVICE_CONFIG_PRESHUTDOWN_INFO
:
1439 if (buffer
&& bufferW
&& *needed
<=size
)
1440 memcpy(buffer
, bufferW
, *needed
);
1443 FIXME("conversation W->A not implemented for level %d\n", dwLevel
);
1449 HeapFree( GetProcessHeap(), 0, bufferW
);
1453 /******************************************************************************
1454 * QueryServiceConfig2W [ADVAPI32.@]
1456 * See QueryServiceConfig2A.
1458 BOOL WINAPI
QueryServiceConfig2W(SC_HANDLE hService
, DWORD dwLevel
, LPBYTE buffer
,
1459 DWORD size
, LPDWORD needed
)
1463 if(dwLevel
!=SERVICE_CONFIG_DESCRIPTION
&& dwLevel
!=SERVICE_CONFIG_PRESHUTDOWN_INFO
) {
1464 FIXME("Level %d not implemented\n", dwLevel
);
1465 SetLastError(ERROR_INVALID_LEVEL
);
1469 if(!buffer
&& size
) {
1470 SetLastError(ERROR_INVALID_ADDRESS
);
1474 TRACE("%p 0x%d %p 0x%d %p\n", hService
, dwLevel
, buffer
, size
, needed
);
1478 err
= svcctl_QueryServiceConfig2W(hService
, dwLevel
, buffer
, size
, needed
);
1480 __EXCEPT(rpc_filter
)
1482 err
= map_exception_code(GetExceptionCode());
1486 if (err
!= ERROR_SUCCESS
)
1488 SetLastError( err
);
1494 case SERVICE_CONFIG_DESCRIPTION
:
1497 SERVICE_DESCRIPTIONW
*descr
= (SERVICE_DESCRIPTIONW
*)buffer
;
1498 if (descr
->lpDescription
) /* make it an absolute pointer */
1499 descr
->lpDescription
= (WCHAR
*)(buffer
+ (ULONG_PTR
)descr
->lpDescription
);
1507 /******************************************************************************
1508 * EnumServicesStatusA [ADVAPI32.@]
1511 EnumServicesStatusA( SC_HANDLE hmngr
, DWORD type
, DWORD state
, LPENUM_SERVICE_STATUSA
1512 services
, DWORD size
, LPDWORD needed
, LPDWORD returned
,
1513 LPDWORD resume_handle
)
1517 ENUM_SERVICE_STATUSW
*servicesW
= NULL
;
1521 TRACE("%p 0x%x 0x%x %p %u %p %p %p\n", hmngr
, type
, state
, services
, size
, needed
,
1522 returned
, resume_handle
);
1524 sz
= max( 2 * size
, sizeof(*servicesW
) );
1525 if (!(servicesW
= HeapAlloc( GetProcessHeap(), 0, sz
)))
1527 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1531 ret
= EnumServicesStatusW( hmngr
, type
, state
, servicesW
, sz
, needed
, returned
, resume_handle
);
1532 if (!ret
) goto done
;
1534 p
= (char *)services
+ *returned
* sizeof(ENUM_SERVICE_STATUSA
);
1535 n
= size
- (p
- (char *)services
);
1537 for (i
= 0; i
< *returned
; i
++)
1539 sz
= WideCharToMultiByte( CP_ACP
, 0, servicesW
[i
].lpServiceName
, -1, p
, n
, NULL
, NULL
);
1541 services
[i
].lpServiceName
= p
;
1544 if (servicesW
[i
].lpDisplayName
)
1546 sz
= WideCharToMultiByte( CP_ACP
, 0, servicesW
[i
].lpDisplayName
, -1, p
, n
, NULL
, NULL
);
1548 services
[i
].lpDisplayName
= p
;
1552 else services
[i
].lpDisplayName
= NULL
;
1553 services
[i
].ServiceStatus
= servicesW
[i
].ServiceStatus
;
1559 HeapFree( GetProcessHeap(), 0, servicesW
);
1563 /******************************************************************************
1564 * EnumServicesStatusW [ADVAPI32.@]
1567 EnumServicesStatusW( SC_HANDLE hmngr
, DWORD type
, DWORD state
, LPENUM_SERVICE_STATUSW
1568 services
, DWORD size
, LPDWORD needed
, LPDWORD returned
,
1569 LPDWORD resume_handle
)
1572 ENUM_SERVICE_STATUSW dummy_status
;
1574 TRACE("%p 0x%x 0x%x %p %u %p %p %p\n", hmngr
, type
, state
, services
, size
, needed
,
1575 returned
, resume_handle
);
1578 FIXME("resume handle not supported\n");
1582 SetLastError( ERROR_INVALID_HANDLE
);
1586 /* make sure we pass a valid pointer */
1587 if (!services
|| size
< sizeof(*services
))
1589 services
= &dummy_status
;
1590 size
= sizeof(dummy_status
);
1595 err
= svcctl_EnumServicesStatusW( hmngr
, type
, state
, (BYTE
*)services
, size
, needed
, returned
);
1597 __EXCEPT(rpc_filter
)
1599 err
= map_exception_code( GetExceptionCode() );
1603 if (err
!= ERROR_SUCCESS
)
1605 SetLastError( err
);
1609 for (i
= 0; i
< *returned
; i
++)
1611 /* convert buffer offsets into pointers */
1612 services
[i
].lpServiceName
= (WCHAR
*)((char *)services
+ (DWORD_PTR
)services
[i
].lpServiceName
);
1613 if (services
[i
].lpDisplayName
)
1614 services
[i
].lpDisplayName
= (WCHAR
*)((char *)services
+ (DWORD_PTR
)services
[i
].lpDisplayName
);
1620 /******************************************************************************
1621 * EnumServicesStatusExA [ADVAPI32.@]
1624 EnumServicesStatusExA( SC_HANDLE hmngr
, SC_ENUM_TYPE level
, DWORD type
, DWORD state
,
1625 LPBYTE buffer
, DWORD size
, LPDWORD needed
, LPDWORD returned
,
1626 LPDWORD resume_handle
, LPCSTR group
)
1630 ENUM_SERVICE_STATUS_PROCESSA
*services
= (ENUM_SERVICE_STATUS_PROCESSA
*)buffer
;
1631 ENUM_SERVICE_STATUS_PROCESSW
*servicesW
= NULL
;
1632 WCHAR
*groupW
= NULL
;
1636 TRACE("%p %u 0x%x 0x%x %p %u %p %p %p %s\n", hmngr
, level
, type
, state
, buffer
,
1637 size
, needed
, returned
, resume_handle
, debugstr_a(group
));
1639 sz
= max( 2 * size
, sizeof(*servicesW
) );
1640 if (!(servicesW
= HeapAlloc( GetProcessHeap(), 0, sz
)))
1642 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1647 int len
= MultiByteToWideChar( CP_ACP
, 0, group
, -1, NULL
, 0 );
1648 if (!(groupW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
1650 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1651 HeapFree( GetProcessHeap(), 0, servicesW
);
1654 MultiByteToWideChar( CP_ACP
, 0, group
, -1, groupW
, len
* sizeof(WCHAR
) );
1657 ret
= EnumServicesStatusExW( hmngr
, level
, type
, state
, (BYTE
*)servicesW
, sz
,
1658 needed
, returned
, resume_handle
, groupW
);
1659 if (!ret
) goto done
;
1661 p
= (char *)services
+ *returned
* sizeof(ENUM_SERVICE_STATUS_PROCESSA
);
1662 n
= size
- (p
- (char *)services
);
1664 for (i
= 0; i
< *returned
; i
++)
1666 sz
= WideCharToMultiByte( CP_ACP
, 0, servicesW
[i
].lpServiceName
, -1, p
, n
, NULL
, NULL
);
1668 services
[i
].lpServiceName
= p
;
1671 if (servicesW
[i
].lpDisplayName
)
1673 sz
= WideCharToMultiByte( CP_ACP
, 0, servicesW
[i
].lpDisplayName
, -1, p
, n
, NULL
, NULL
);
1675 services
[i
].lpDisplayName
= p
;
1679 else services
[i
].lpDisplayName
= NULL
;
1680 services
[i
].ServiceStatusProcess
= servicesW
[i
].ServiceStatusProcess
;
1686 HeapFree( GetProcessHeap(), 0, servicesW
);
1687 HeapFree( GetProcessHeap(), 0, groupW
);
1691 /******************************************************************************
1692 * EnumServicesStatusExW [ADVAPI32.@]
1695 EnumServicesStatusExW( SC_HANDLE hmngr
, SC_ENUM_TYPE level
, DWORD type
, DWORD state
,
1696 LPBYTE buffer
, DWORD size
, LPDWORD needed
, LPDWORD returned
,
1697 LPDWORD resume_handle
, LPCWSTR group
)
1700 ENUM_SERVICE_STATUS_PROCESSW dummy_status
;
1701 ENUM_SERVICE_STATUS_PROCESSW
*services
= (ENUM_SERVICE_STATUS_PROCESSW
*)buffer
;
1703 TRACE("%p %u 0x%x 0x%x %p %u %p %p %p %s\n", hmngr
, level
, type
, state
, buffer
,
1704 size
, needed
, returned
, resume_handle
, debugstr_w(group
));
1707 FIXME("resume handle not supported\n");
1709 if (level
!= SC_ENUM_PROCESS_INFO
)
1711 SetLastError( ERROR_INVALID_LEVEL
);
1716 SetLastError( ERROR_INVALID_HANDLE
);
1720 /* make sure we pass a valid buffer pointer */
1721 if (!services
|| size
< sizeof(*services
))
1723 buffer
= (BYTE
*)&dummy_status
;
1724 size
= sizeof(dummy_status
);
1729 err
= svcctl_EnumServicesStatusExW( hmngr
, type
, state
, buffer
, size
, needed
,
1732 __EXCEPT(rpc_filter
)
1734 err
= map_exception_code( GetExceptionCode() );
1738 if (err
!= ERROR_SUCCESS
)
1740 SetLastError( err
);
1744 for (i
= 0; i
< *returned
; i
++)
1746 /* convert buffer offsets into pointers */
1747 services
[i
].lpServiceName
= (WCHAR
*)((char *)services
+ (DWORD_PTR
)services
[i
].lpServiceName
);
1748 if (services
[i
].lpDisplayName
)
1749 services
[i
].lpDisplayName
= (WCHAR
*)((char *)services
+ (DWORD_PTR
)services
[i
].lpDisplayName
);
1755 /******************************************************************************
1756 * GetServiceKeyNameA [ADVAPI32.@]
1758 BOOL WINAPI
GetServiceKeyNameA( SC_HANDLE hSCManager
, LPCSTR lpDisplayName
,
1759 LPSTR lpServiceName
, LPDWORD lpcchBuffer
)
1761 LPWSTR lpDisplayNameW
, lpServiceNameW
;
1765 TRACE("%p %s %p %p\n", hSCManager
,
1766 debugstr_a(lpDisplayName
), lpServiceName
, lpcchBuffer
);
1768 lpDisplayNameW
= SERV_dup(lpDisplayName
);
1770 lpServiceNameW
= HeapAlloc(GetProcessHeap(), 0, *lpcchBuffer
* sizeof(WCHAR
));
1772 lpServiceNameW
= NULL
;
1774 sizeW
= *lpcchBuffer
;
1775 if (!GetServiceKeyNameW(hSCManager
, lpDisplayNameW
, lpServiceNameW
, &sizeW
))
1777 if (lpServiceName
&& *lpcchBuffer
)
1778 lpServiceName
[0] = 0;
1779 *lpcchBuffer
= sizeW
*2; /* we can only provide an upper estimation of string length */
1783 if (!WideCharToMultiByte(CP_ACP
, 0, lpServiceNameW
, (sizeW
+ 1), lpServiceName
,
1784 *lpcchBuffer
, NULL
, NULL
))
1786 if (*lpcchBuffer
&& lpServiceName
)
1787 lpServiceName
[0] = 0;
1788 *lpcchBuffer
= WideCharToMultiByte(CP_ACP
, 0, lpServiceNameW
, -1, NULL
, 0, NULL
, NULL
);
1792 /* lpcchBuffer not updated - same as in GetServiceDisplayNameA */
1796 HeapFree(GetProcessHeap(), 0, lpServiceNameW
);
1797 HeapFree(GetProcessHeap(), 0, lpDisplayNameW
);
1801 /******************************************************************************
1802 * GetServiceKeyNameW [ADVAPI32.@]
1804 BOOL WINAPI
GetServiceKeyNameW( SC_HANDLE hSCManager
, LPCWSTR lpDisplayName
,
1805 LPWSTR lpServiceName
, LPDWORD lpcchBuffer
)
1811 TRACE("%p %s %p %p\n", hSCManager
,
1812 debugstr_w(lpDisplayName
), lpServiceName
, lpcchBuffer
);
1816 SetLastError( ERROR_INVALID_HANDLE
);
1820 /* provide a buffer if the caller didn't */
1821 if (!lpServiceName
|| *lpcchBuffer
< 2)
1823 lpServiceName
= buffer
;
1824 /* A size of 1 would be enough, but tests show that Windows returns 2,
1825 * probably because of a WCHAR/bytes mismatch in their code.
1830 /* RPC call takes size excluding nul-terminator, whereas *lpcchBuffer
1831 * includes the nul-terminator on input. */
1832 size
= *lpcchBuffer
- 1;
1836 err
= svcctl_GetServiceKeyNameW(hSCManager
, lpDisplayName
, lpServiceName
,
1839 __EXCEPT(rpc_filter
)
1841 err
= map_exception_code(GetExceptionCode());
1845 /* The value of *lpcchBuffer excludes nul-terminator on output. */
1846 if (err
== ERROR_SUCCESS
|| err
== ERROR_INSUFFICIENT_BUFFER
)
1847 *lpcchBuffer
= size
;
1851 return err
== ERROR_SUCCESS
;
1854 /******************************************************************************
1855 * QueryServiceLockStatusA [ADVAPI32.@]
1857 BOOL WINAPI
QueryServiceLockStatusA( SC_HANDLE hSCManager
,
1858 LPQUERY_SERVICE_LOCK_STATUSA lpLockStatus
,
1859 DWORD cbBufSize
, LPDWORD pcbBytesNeeded
)
1861 FIXME("%p %p %08x %p\n", hSCManager
, lpLockStatus
, cbBufSize
, pcbBytesNeeded
);
1866 /******************************************************************************
1867 * QueryServiceLockStatusW [ADVAPI32.@]
1869 BOOL WINAPI
QueryServiceLockStatusW( SC_HANDLE hSCManager
,
1870 LPQUERY_SERVICE_LOCK_STATUSW lpLockStatus
,
1871 DWORD cbBufSize
, LPDWORD pcbBytesNeeded
)
1873 FIXME("%p %p %08x %p\n", hSCManager
, lpLockStatus
, cbBufSize
, pcbBytesNeeded
);
1878 /******************************************************************************
1879 * GetServiceDisplayNameA [ADVAPI32.@]
1881 BOOL WINAPI
GetServiceDisplayNameA( SC_HANDLE hSCManager
, LPCSTR lpServiceName
,
1882 LPSTR lpDisplayName
, LPDWORD lpcchBuffer
)
1884 LPWSTR lpServiceNameW
, lpDisplayNameW
;
1888 TRACE("%p %s %p %p\n", hSCManager
,
1889 debugstr_a(lpServiceName
), lpDisplayName
, lpcchBuffer
);
1891 lpServiceNameW
= SERV_dup(lpServiceName
);
1893 lpDisplayNameW
= HeapAlloc(GetProcessHeap(), 0, *lpcchBuffer
* sizeof(WCHAR
));
1895 lpDisplayNameW
= NULL
;
1897 sizeW
= *lpcchBuffer
;
1898 if (!GetServiceDisplayNameW(hSCManager
, lpServiceNameW
, lpDisplayNameW
, &sizeW
))
1900 if (lpDisplayName
&& *lpcchBuffer
)
1901 lpDisplayName
[0] = 0;
1902 *lpcchBuffer
= sizeW
*2; /* we can only provide an upper estimation of string length */
1906 if (!WideCharToMultiByte(CP_ACP
, 0, lpDisplayNameW
, (sizeW
+ 1), lpDisplayName
,
1907 *lpcchBuffer
, NULL
, NULL
))
1909 if (*lpcchBuffer
&& lpDisplayName
)
1910 lpDisplayName
[0] = 0;
1911 *lpcchBuffer
= WideCharToMultiByte(CP_ACP
, 0, lpDisplayNameW
, -1, NULL
, 0, NULL
, NULL
);
1915 /* probably due to a bug GetServiceDisplayNameA doesn't modify lpcchBuffer on success.
1916 * (but if the function succeeded it means that is a good upper estimation of the size) */
1920 HeapFree(GetProcessHeap(), 0, lpDisplayNameW
);
1921 HeapFree(GetProcessHeap(), 0, lpServiceNameW
);
1925 /******************************************************************************
1926 * GetServiceDisplayNameW [ADVAPI32.@]
1928 BOOL WINAPI
GetServiceDisplayNameW( SC_HANDLE hSCManager
, LPCWSTR lpServiceName
,
1929 LPWSTR lpDisplayName
, LPDWORD lpcchBuffer
)
1935 TRACE("%p %s %p %p\n", hSCManager
,
1936 debugstr_w(lpServiceName
), lpDisplayName
, lpcchBuffer
);
1940 SetLastError( ERROR_INVALID_HANDLE
);
1944 /* provide a buffer if the caller didn't */
1945 if (!lpDisplayName
|| *lpcchBuffer
< 2)
1947 lpDisplayName
= buffer
;
1948 /* A size of 1 would be enough, but tests show that Windows returns 2,
1949 * probably because of a WCHAR/bytes mismatch in their code.
1954 /* RPC call takes size excluding nul-terminator, whereas *lpcchBuffer
1955 * includes the nul-terminator on input. */
1956 size
= *lpcchBuffer
- 1;
1960 err
= svcctl_GetServiceDisplayNameW(hSCManager
, lpServiceName
, lpDisplayName
,
1963 __EXCEPT(rpc_filter
)
1965 err
= map_exception_code(GetExceptionCode());
1969 /* The value of *lpcchBuffer excludes nul-terminator on output. */
1970 if (err
== ERROR_SUCCESS
|| err
== ERROR_INSUFFICIENT_BUFFER
)
1971 *lpcchBuffer
= size
;
1975 return err
== ERROR_SUCCESS
;
1978 /******************************************************************************
1979 * ChangeServiceConfigW [ADVAPI32.@]
1981 BOOL WINAPI
ChangeServiceConfigW( SC_HANDLE hService
, DWORD dwServiceType
,
1982 DWORD dwStartType
, DWORD dwErrorControl
, LPCWSTR lpBinaryPathName
,
1983 LPCWSTR lpLoadOrderGroup
, LPDWORD lpdwTagId
, LPCWSTR lpDependencies
,
1984 LPCWSTR lpServiceStartName
, LPCWSTR lpPassword
, LPCWSTR lpDisplayName
)
1989 TRACE("%p %d %d %d %s %s %p %p %s %s %s\n",
1990 hService
, dwServiceType
, dwStartType
, dwErrorControl
,
1991 debugstr_w(lpBinaryPathName
), debugstr_w(lpLoadOrderGroup
),
1992 lpdwTagId
, lpDependencies
, debugstr_w(lpServiceStartName
),
1993 debugstr_w(lpPassword
), debugstr_w(lpDisplayName
) );
1995 cb_pwd
= lpPassword
? (strlenW(lpPassword
) + 1)*sizeof(WCHAR
) : 0;
1999 err
= svcctl_ChangeServiceConfigW(hService
, dwServiceType
,
2000 dwStartType
, dwErrorControl
, lpBinaryPathName
, lpLoadOrderGroup
, lpdwTagId
,
2001 (const BYTE
*)lpDependencies
, multisz_cb(lpDependencies
), lpServiceStartName
,
2002 (const BYTE
*)lpPassword
, cb_pwd
, lpDisplayName
);
2004 __EXCEPT(rpc_filter
)
2006 err
= map_exception_code(GetExceptionCode());
2010 if (err
!= ERROR_SUCCESS
)
2013 return err
== ERROR_SUCCESS
;
2016 /******************************************************************************
2017 * ChangeServiceConfigA [ADVAPI32.@]
2019 BOOL WINAPI
ChangeServiceConfigA( SC_HANDLE hService
, DWORD dwServiceType
,
2020 DWORD dwStartType
, DWORD dwErrorControl
, LPCSTR lpBinaryPathName
,
2021 LPCSTR lpLoadOrderGroup
, LPDWORD lpdwTagId
, LPCSTR lpDependencies
,
2022 LPCSTR lpServiceStartName
, LPCSTR lpPassword
, LPCSTR lpDisplayName
)
2024 LPWSTR wBinaryPathName
, wLoadOrderGroup
, wDependencies
;
2025 LPWSTR wServiceStartName
, wPassword
, wDisplayName
;
2028 TRACE("%p %d %d %d %s %s %p %p %s %s %s\n",
2029 hService
, dwServiceType
, dwStartType
, dwErrorControl
,
2030 debugstr_a(lpBinaryPathName
), debugstr_a(lpLoadOrderGroup
),
2031 lpdwTagId
, lpDependencies
, debugstr_a(lpServiceStartName
),
2032 debugstr_a(lpPassword
), debugstr_a(lpDisplayName
) );
2034 wBinaryPathName
= SERV_dup( lpBinaryPathName
);
2035 wLoadOrderGroup
= SERV_dup( lpLoadOrderGroup
);
2036 wDependencies
= SERV_dupmulti( lpDependencies
);
2037 wServiceStartName
= SERV_dup( lpServiceStartName
);
2038 wPassword
= SERV_dup( lpPassword
);
2039 wDisplayName
= SERV_dup( lpDisplayName
);
2041 r
= ChangeServiceConfigW( hService
, dwServiceType
,
2042 dwStartType
, dwErrorControl
, wBinaryPathName
,
2043 wLoadOrderGroup
, lpdwTagId
, wDependencies
,
2044 wServiceStartName
, wPassword
, wDisplayName
);
2046 HeapFree( GetProcessHeap(), 0, wBinaryPathName
);
2047 HeapFree( GetProcessHeap(), 0, wLoadOrderGroup
);
2048 HeapFree( GetProcessHeap(), 0, wDependencies
);
2049 HeapFree( GetProcessHeap(), 0, wServiceStartName
);
2050 HeapFree( GetProcessHeap(), 0, wPassword
);
2051 HeapFree( GetProcessHeap(), 0, wDisplayName
);
2056 /******************************************************************************
2057 * ChangeServiceConfig2A [ADVAPI32.@]
2059 BOOL WINAPI
ChangeServiceConfig2A( SC_HANDLE hService
, DWORD dwInfoLevel
,
2064 TRACE("%p %d %p\n",hService
, dwInfoLevel
, lpInfo
);
2066 if (dwInfoLevel
== SERVICE_CONFIG_DESCRIPTION
)
2068 LPSERVICE_DESCRIPTIONA sd
= lpInfo
;
2069 SERVICE_DESCRIPTIONW sdw
;
2071 sdw
.lpDescription
= SERV_dup( sd
->lpDescription
);
2073 r
= ChangeServiceConfig2W( hService
, dwInfoLevel
, &sdw
);
2075 HeapFree( GetProcessHeap(), 0, sdw
.lpDescription
);
2077 else if (dwInfoLevel
== SERVICE_CONFIG_FAILURE_ACTIONS
)
2079 LPSERVICE_FAILURE_ACTIONSA fa
= lpInfo
;
2080 SERVICE_FAILURE_ACTIONSW faw
;
2082 faw
.dwResetPeriod
= fa
->dwResetPeriod
;
2083 faw
.lpRebootMsg
= SERV_dup( fa
->lpRebootMsg
);
2084 faw
.lpCommand
= SERV_dup( fa
->lpCommand
);
2085 faw
.cActions
= fa
->cActions
;
2086 faw
.lpsaActions
= fa
->lpsaActions
;
2088 r
= ChangeServiceConfig2W( hService
, dwInfoLevel
, &faw
);
2090 HeapFree( GetProcessHeap(), 0, faw
.lpRebootMsg
);
2091 HeapFree( GetProcessHeap(), 0, faw
.lpCommand
);
2093 else if (dwInfoLevel
== SERVICE_CONFIG_PRESHUTDOWN_INFO
)
2095 r
= ChangeServiceConfig2W( hService
, dwInfoLevel
, lpInfo
);
2098 SetLastError( ERROR_INVALID_PARAMETER
);
2103 /******************************************************************************
2104 * ChangeServiceConfig2W [ADVAPI32.@]
2106 BOOL WINAPI
ChangeServiceConfig2W( SC_HANDLE hService
, DWORD dwInfoLevel
,
2113 err
= svcctl_ChangeServiceConfig2W( hService
, dwInfoLevel
, lpInfo
);
2115 __EXCEPT(rpc_filter
)
2117 err
= map_exception_code(GetExceptionCode());
2121 if (err
!= ERROR_SUCCESS
)
2124 return err
== ERROR_SUCCESS
;
2127 /******************************************************************************
2128 * QueryServiceObjectSecurity [ADVAPI32.@]
2130 BOOL WINAPI
QueryServiceObjectSecurity(SC_HANDLE hService
,
2131 SECURITY_INFORMATION dwSecurityInformation
,
2132 PSECURITY_DESCRIPTOR lpSecurityDescriptor
,
2133 DWORD cbBufSize
, LPDWORD pcbBytesNeeded
)
2135 SECURITY_DESCRIPTOR descriptor
;
2140 FIXME("%p %d %p %u %p - semi-stub\n", hService
, dwSecurityInformation
,
2141 lpSecurityDescriptor
, cbBufSize
, pcbBytesNeeded
);
2143 if (dwSecurityInformation
!= DACL_SECURITY_INFORMATION
)
2144 FIXME("information %d not supported\n", dwSecurityInformation
);
2146 InitializeSecurityDescriptor(&descriptor
, SECURITY_DESCRIPTOR_REVISION
);
2148 InitializeAcl(&acl
, sizeof(ACL
), ACL_REVISION
);
2149 SetSecurityDescriptorDacl(&descriptor
, TRUE
, &acl
, TRUE
);
2152 succ
= MakeSelfRelativeSD(&descriptor
, lpSecurityDescriptor
, &size
);
2153 *pcbBytesNeeded
= size
;
2157 /******************************************************************************
2158 * SetServiceObjectSecurity [ADVAPI32.@]
2160 BOOL WINAPI
SetServiceObjectSecurity(SC_HANDLE hService
,
2161 SECURITY_INFORMATION dwSecurityInformation
,
2162 PSECURITY_DESCRIPTOR lpSecurityDescriptor
)
2164 FIXME("%p %d %p\n", hService
, dwSecurityInformation
, lpSecurityDescriptor
);
2168 /******************************************************************************
2169 * SetServiceBits [ADVAPI32.@]
2171 BOOL WINAPI
SetServiceBits( SERVICE_STATUS_HANDLE hServiceStatus
,
2172 DWORD dwServiceBits
,
2174 BOOL bUpdateImmediately
)
2176 FIXME("%p %08x %x %x\n", hServiceStatus
, dwServiceBits
,
2177 bSetBitsOn
, bUpdateImmediately
);
2181 /* thunk for calling the RegisterServiceCtrlHandler handler function */
2182 static DWORD WINAPI
ctrl_handler_thunk( DWORD control
, DWORD type
, void *data
, void *context
)
2184 LPHANDLER_FUNCTION func
= context
;
2187 return ERROR_SUCCESS
;
2190 /******************************************************************************
2191 * RegisterServiceCtrlHandlerA [ADVAPI32.@]
2193 SERVICE_STATUS_HANDLE WINAPI
RegisterServiceCtrlHandlerA( LPCSTR name
, LPHANDLER_FUNCTION handler
)
2195 return RegisterServiceCtrlHandlerExA( name
, ctrl_handler_thunk
, handler
);
2198 /******************************************************************************
2199 * RegisterServiceCtrlHandlerW [ADVAPI32.@]
2201 SERVICE_STATUS_HANDLE WINAPI
RegisterServiceCtrlHandlerW( LPCWSTR name
, LPHANDLER_FUNCTION handler
)
2203 return RegisterServiceCtrlHandlerExW( name
, ctrl_handler_thunk
, handler
);
2206 /******************************************************************************
2207 * RegisterServiceCtrlHandlerExA [ADVAPI32.@]
2209 SERVICE_STATUS_HANDLE WINAPI
RegisterServiceCtrlHandlerExA( LPCSTR name
, LPHANDLER_FUNCTION_EX handler
, LPVOID context
)
2212 SERVICE_STATUS_HANDLE ret
;
2214 nameW
= SERV_dup(name
);
2215 ret
= RegisterServiceCtrlHandlerExW( nameW
, handler
, context
);
2216 HeapFree( GetProcessHeap(), 0, nameW
);
2220 /******************************************************************************
2221 * RegisterServiceCtrlHandlerExW [ADVAPI32.@]
2223 SERVICE_STATUS_HANDLE WINAPI
RegisterServiceCtrlHandlerExW( LPCWSTR lpServiceName
,
2224 LPHANDLER_FUNCTION_EX lpHandlerProc
, LPVOID lpContext
)
2226 service_data
*service
;
2227 SC_HANDLE hService
= 0;
2230 TRACE("%s %p %p\n", debugstr_w(lpServiceName
), lpHandlerProc
, lpContext
);
2232 EnterCriticalSection( &service_cs
);
2233 if ((service
= find_service_by_name( lpServiceName
)))
2235 service
->handler
= lpHandlerProc
;
2236 service
->context
= lpContext
;
2237 hService
= service
->handle
;
2240 LeaveCriticalSection( &service_cs
);
2242 if (!found
) SetLastError(ERROR_SERVICE_DOES_NOT_EXIST
);
2244 return (SERVICE_STATUS_HANDLE
)hService
;
2247 /******************************************************************************
2248 * EnumDependentServicesA [ADVAPI32.@]
2250 BOOL WINAPI
EnumDependentServicesA( SC_HANDLE hService
, DWORD dwServiceState
,
2251 LPENUM_SERVICE_STATUSA lpServices
, DWORD cbBufSize
,
2252 LPDWORD pcbBytesNeeded
, LPDWORD lpServicesReturned
)
2254 FIXME("%p 0x%08x %p 0x%08x %p %p - stub\n", hService
, dwServiceState
,
2255 lpServices
, cbBufSize
, pcbBytesNeeded
, lpServicesReturned
);
2257 *lpServicesReturned
= 0;
2261 /******************************************************************************
2262 * EnumDependentServicesW [ADVAPI32.@]
2264 BOOL WINAPI
EnumDependentServicesW( SC_HANDLE hService
, DWORD dwServiceState
,
2265 LPENUM_SERVICE_STATUSW lpServices
, DWORD cbBufSize
,
2266 LPDWORD pcbBytesNeeded
, LPDWORD lpServicesReturned
)
2268 FIXME("%p 0x%08x %p 0x%08x %p %p - stub\n", hService
, dwServiceState
,
2269 lpServices
, cbBufSize
, pcbBytesNeeded
, lpServicesReturned
);
2271 *lpServicesReturned
= 0;