mshtml: Remove old channel from load group in nsIAsyncVerifyRedirectCallback::AsyncOn...
[wine/multimedia.git] / dlls / advapi32 / service.c
blob0e5001ad17678b89bae97163f46636f4eba67123
1 /*
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
23 #include "config.h"
24 #include "wine/port.h"
26 #include <stdarg.h>
27 #include <string.h>
28 #include <time.h>
29 #include <assert.h>
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winsvc.h"
34 #include "winerror.h"
35 #include "winreg.h"
36 #include "wine/unicode.h"
37 #include "wine/debug.h"
38 #include "winternl.h"
39 #include "lmcons.h"
40 #include "lmserver.h"
42 #include "svcctl.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;
61 LPVOID context;
62 HANDLE thread;
63 SC_HANDLE handle;
64 SC_HANDLE full_access_handle;
65 BOOL unicode : 1;
66 union {
67 LPSERVICE_MAIN_FUNCTIONA a;
68 LPSERVICE_MAIN_FUNCTIONW w;
69 } proc;
70 LPWSTR args;
71 WCHAR name[1];
72 } service_data;
74 static CRITICAL_SECTION service_cs;
75 static CRITICAL_SECTION_DEBUG service_cs_debug =
77 0, 0, &service_cs,
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 )
97 UINT len;
98 LPWSTR wstr;
100 if( !str )
101 return NULL;
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 );
105 return wstr;
108 static inline LPWSTR SERV_dupmulti(LPCSTR str)
110 UINT len = 0, n = 0;
111 LPWSTR wstr;
113 if( !str )
114 return NULL;
115 do {
116 len += MultiByteToWideChar( CP_ACP, 0, &str[n], -1, NULL, 0 );
117 n += (strlen( &str[n] ) + 1);
118 } while (str[n]);
119 len++;
120 n++;
122 wstr = HeapAlloc( GetProcessHeap(), 0, len*sizeof (WCHAR) );
123 MultiByteToWideChar( CP_ACP, 0, str, n, wstr, len );
124 return wstr;
127 static inline DWORD multisz_cb(LPCWSTR wmultisz)
129 const WCHAR *wptr = wmultisz;
131 if (wmultisz == NULL)
132 return 0;
134 while (*wptr)
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;
148 RPC_STATUS status;
149 handle_t rpc_handle;
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);
155 return NULL;
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);
164 return NULL;
167 return rpc_handle;
170 DECLSPEC_HIDDEN void __RPC_USER MACHINE_HANDLEW_unbind(MACHINE_HANDLEW MachineName, handle_t h)
172 RpcBindingFree(&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;
192 default:
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};
208 LPWSTR name;
209 DWORD len;
210 HKEY service_current_key;
211 DWORD service_current;
212 LONG ret;
213 DWORD type;
215 ret = RegOpenKeyExW(HKEY_LOCAL_MACHINE, service_current_key_str, 0,
216 KEY_QUERY_VALUE, &service_current_key);
217 if (ret != ERROR_SUCCESS)
218 return NULL;
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)
224 return NULL;
225 len = sizeof(format)/sizeof(WCHAR) + 10 /* strlenW("4294967295") */;
226 name = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
227 if (!name)
228 return NULL;
229 snprintfW(name, len, format, service_current);
230 return name;
233 static HANDLE service_open_pipe(void)
235 LPWSTR szPipe = service_get_pipe_name();
236 HANDLE handle = INVALID_HANDLE_VALUE;
238 do {
239 handle = CreateFileW(szPipe, GENERIC_READ|GENERIC_WRITE,
240 0, NULL, OPEN_ALWAYS, 0, NULL);
241 if (handle != INVALID_HANDLE_VALUE)
242 break;
243 if (GetLastError() != ERROR_PIPE_BUSY)
244 break;
245 } while (WaitNamedPipeW(szPipe, NMPWAIT_USE_DEFAULT_WAIT));
246 HeapFree(GetProcessHeap(), 0, szPipe);
248 return handle;
251 static service_data *find_service_by_name( const WCHAR *name )
253 unsigned int i;
255 if (nb_services == 1) /* only one service (FIXME: should depend on OWN_PROCESS etc.) */
256 return services[0];
257 for (i = 0; i < nb_services; i++)
258 if (!strcmpiW( name, services[i]->name )) return services[i];
259 return NULL;
262 /******************************************************************************
263 * service_thread
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;
273 TRACE("%p\n", arg);
275 while (str[len])
277 len += strlenW(&str[len]) + 1;
278 argc++;
280 len++;
282 if (info->unicode)
284 LPWSTR *argv, p;
286 argv = HeapAlloc(GetProcessHeap(), 0, (argc+1)*sizeof(LPWSTR));
287 for (argc=0, p=str; *p; p += strlenW(p) + 1)
288 argv[argc++] = p;
289 argv[argc] = NULL;
291 info->proc.w(argc, argv);
292 HeapFree(GetProcessHeap(), 0, argv);
294 else
296 LPSTR strA, *argv, p;
297 DWORD lenA;
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)
305 argv[argc++] = p;
306 argv[argc] = NULL;
308 info->proc.a(argc, argv);
309 HeapFree(GetProcessHeap(), 0, argv);
310 HeapFree(GetProcessHeap(), 0, strA);
312 return 0;
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);
322 if (service->thread)
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,
332 service, 0, NULL );
333 SetEvent( service_event ); /* notify the main loop */
334 return 0;
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);
348 return ret;
351 /******************************************************************************
352 * service_control_dispatcher
354 static DWORD WINAPI service_control_dispatcher(LPVOID arg)
356 SC_HANDLE manager;
357 HANDLE pipe;
359 if (!(manager = OpenSCManagerW( NULL, NULL, SC_MANAGER_CONNECT )))
361 ERR("failed to open service manager error %u\n", GetLastError());
362 return 0;
365 pipe = service_open_pipe();
367 if (pipe==INVALID_HANDLE_VALUE)
369 WARN("failed to create control pipe error = %d\n", GetLastError());
370 return 0;
373 /* dispatcher loop */
374 while (1)
376 service_data *service;
377 service_start_info info;
378 WCHAR *data = NULL;
379 BOOL r;
380 DWORD data_size = 0, count, result;
382 r = ReadFile( pipe, &info, FIELD_OFFSET(service_start_info,data), &count, NULL );
383 if (!r)
385 if (GetLastError() != ERROR_BROKEN_PIPE)
386 ERR( "pipe read failed error %u\n", GetLastError() );
387 break;
389 if (count != FIELD_OFFSET(service_start_info,data))
391 ERR( "partial pipe read %u\n", count );
392 break;
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 );
399 if (!r)
401 if (GetLastError() != ERROR_BROKEN_PIPE)
402 ERR( "pipe read failed error %u\n", GetLastError() );
403 break;
405 if (count != data_size)
407 ERR( "partial pipe read %u/%u\n", count, data_size );
408 break;
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;
418 goto done;
421 TRACE( "got request %u for service %s\n", info.cmd, debugstr_w(data) );
423 /* handle the request */
424 switch (info.cmd)
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 );
435 break;
436 case WINESERV_SENDCONTROL:
437 result = service_handle_control(service, info.control);
438 break;
439 default:
440 ERR("received invalid command %u\n", info.cmd);
441 result = ERROR_INVALID_PARAMETER;
442 break;
445 done:
446 WriteFile(pipe, &result, sizeof(result), &count, NULL);
447 HeapFree( GetProcessHeap(), 0, data );
450 CloseHandle(pipe);
451 CloseServiceHandle( manager );
452 return 1;
455 /******************************************************************************
456 * service_run_main_thread
458 static BOOL service_run_main_thread(void)
460 DWORD i, n, ret;
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 */
475 for (;;)
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 */
489 SERVICE_STATUS st;
490 SERVICE_PRESHUTDOWN_INFO spi;
491 DWORD timeout = 5000;
492 BOOL res;
494 EnterCriticalSection( &service_cs );
495 n = 0;
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);
501 ret = ERROR_SUCCESS;
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 );
506 if (res)
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 );
524 ExitProcess(0);
526 else if (ret == 1)
528 TRACE( "control dispatcher exited, shutting down\n" );
529 /* FIXME: we should maybe send a shutdown control to running services */
530 ExitProcess(0);
532 else if (ret == 2)
534 continue; /* rebuild the list */
536 else if (ret < n)
538 services[wait_services[ret]]->thread = 0;
539 CloseHandle( wait_handles[ret] );
540 if (n == 4) return TRUE; /* it was the last running thread */
542 else return FALSE;
546 /******************************************************************************
547 * StartServiceCtrlDispatcherA [ADVAPI32.@]
549 * See StartServiceCtrlDispatcherW.
551 BOOL WINAPI StartServiceCtrlDispatcherA( const SERVICE_TABLE_ENTRYA *servent )
553 service_data *info;
554 unsigned int i;
555 BOOL ret = TRUE;
557 TRACE("%p\n", servent);
559 if (nb_services)
561 SetLastError( ERROR_SERVICE_ALREADY_RUNNING );
562 return FALSE;
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;
575 services[i] = info;
578 service_run_main_thread();
580 return ret;
583 /******************************************************************************
584 * StartServiceCtrlDispatcherW [ADVAPI32.@]
586 * Connects a process containing one or more services to the service control
587 * manager.
589 * PARAMS
590 * servent [I] A list of the service names and service procedures
592 * RETURNS
593 * Success: TRUE.
594 * Failure: FALSE.
596 BOOL WINAPI StartServiceCtrlDispatcherW( const SERVICE_TABLE_ENTRYW *servent )
598 service_data *info;
599 unsigned int i;
600 BOOL ret = TRUE;
602 TRACE("%p\n", servent);
604 if (nb_services)
606 SetLastError( ERROR_SERVICE_ALREADY_RUNNING );
607 return FALSE;
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;
620 services[i] = info;
623 service_run_main_thread();
625 return ret;
628 /******************************************************************************
629 * LockServiceDatabase [ADVAPI32.@]
631 SC_LOCK WINAPI LockServiceDatabase (SC_HANDLE hSCManager)
633 SC_RPC_LOCK hLock = NULL;
634 DWORD err;
636 TRACE("%p\n",hSCManager);
638 __TRY
640 err = svcctl_LockServiceDatabase(hSCManager, &hLock);
642 __EXCEPT(rpc_filter)
644 err = map_exception_code(GetExceptionCode());
646 __ENDTRY
647 if (err != ERROR_SUCCESS)
649 SetLastError(err);
650 return NULL;
652 return hLock;
655 /******************************************************************************
656 * UnlockServiceDatabase [ADVAPI32.@]
658 BOOL WINAPI UnlockServiceDatabase (SC_LOCK ScLock)
660 DWORD err;
661 SC_RPC_LOCK hRpcLock = ScLock;
663 TRACE("%p\n",ScLock);
665 __TRY
667 err = svcctl_UnlockServiceDatabase(&hRpcLock);
669 __EXCEPT(rpc_filter)
671 err = map_exception_code(GetExceptionCode());
673 __ENDTRY
674 if (err != ERROR_SUCCESS)
676 SetLastError(err);
677 return FALSE;
679 return TRUE;
682 /******************************************************************************
683 * SetServiceStatus [ADVAPI32.@]
685 * PARAMS
686 * hService []
687 * lpStatus []
689 BOOL WINAPI
690 SetServiceStatus( SERVICE_STATUS_HANDLE hService, LPSERVICE_STATUS lpStatus )
692 DWORD err;
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);
700 __TRY
702 err = svcctl_SetServiceStatus( hService, lpStatus );
704 __EXCEPT(rpc_filter)
706 err = map_exception_code(GetExceptionCode());
708 __ENDTRY
709 if (err != ERROR_SUCCESS)
711 SetLastError(err);
712 return FALSE;
715 if (lpStatus->dwCurrentState == SERVICE_STOPPED)
716 CloseServiceHandle((SC_HANDLE)hService);
718 return TRUE;
722 /******************************************************************************
723 * OpenSCManagerA [ADVAPI32.@]
725 * Establish a connection to the service control manager and open its database.
727 * PARAMS
728 * lpMachineName [I] Pointer to machine name string
729 * lpDatabaseName [I] Pointer to database name string
730 * dwDesiredAccess [I] Type of access
732 * RETURNS
733 * Success: A Handle to the service control manager database
734 * Failure: NULL
736 SC_HANDLE WINAPI OpenSCManagerA( LPCSTR lpMachineName, LPCSTR lpDatabaseName,
737 DWORD dwDesiredAccess )
739 LPWSTR lpMachineNameW, lpDatabaseNameW;
740 SC_HANDLE ret;
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);
747 return ret;
750 /******************************************************************************
751 * OpenSCManagerW [ADVAPI32.@]
753 * See OpenSCManagerA.
755 SC_HANDLE WINAPI OpenSCManagerW( LPCWSTR lpMachineName, LPCWSTR lpDatabaseName,
756 DWORD dwDesiredAccess )
758 SC_HANDLE handle = 0;
759 LONG r;
761 TRACE("(%s,%s,0x%08x)\n", debugstr_w(lpMachineName),
762 debugstr_w(lpDatabaseName), dwDesiredAccess);
764 __TRY
766 r = svcctl_OpenSCManagerW(lpMachineName, lpDatabaseName, dwDesiredAccess, (SC_RPC_HANDLE *)&handle);
768 __EXCEPT(rpc_filter)
770 r = map_exception_code(GetExceptionCode());
772 __ENDTRY
774 if (r!=ERROR_SUCCESS)
776 SetLastError( r );
777 handle = 0;
780 TRACE("returning %p\n", handle);
781 return handle;
784 /******************************************************************************
785 * ControlService [ADVAPI32.@]
787 * Send a control code to a service.
789 * PARAMS
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
794 * RETURNS
795 * Success: TRUE.
796 * Failure: FALSE.
798 * BUGS
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 )
805 DWORD err;
807 TRACE("%p %d %p\n", hService, dwControl, lpServiceStatus);
809 __TRY
811 err = svcctl_ControlService(hService, dwControl, lpServiceStatus);
813 __EXCEPT(rpc_filter)
815 err = map_exception_code(GetExceptionCode());
817 __ENDTRY
818 if (err != ERROR_SUCCESS)
820 SetLastError(err);
821 return FALSE;
824 return TRUE;
827 /******************************************************************************
828 * CloseServiceHandle [ADVAPI32.@]
830 * Close a handle to a service or the service control manager database.
832 * PARAMS
833 * hSCObject [I] Handle to service or service control manager database
835 * RETURNS
836 * Success: TRUE
837 * Failure: FALSE
839 BOOL WINAPI
840 CloseServiceHandle( SC_HANDLE hSCObject )
842 DWORD err;
844 TRACE("%p\n", hSCObject);
846 __TRY
848 err = svcctl_CloseServiceHandle((SC_RPC_HANDLE *)&hSCObject);
850 __EXCEPT(rpc_filter)
852 err = map_exception_code(GetExceptionCode());
854 __ENDTRY
856 if (err != ERROR_SUCCESS)
858 SetLastError(err);
859 return FALSE;
861 return TRUE;
865 /******************************************************************************
866 * OpenServiceA [ADVAPI32.@]
868 * Open a handle to a service.
870 * PARAMS
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
875 * RETURNS
876 * Success: Handle to the service
877 * Failure: NULL
879 SC_HANDLE WINAPI OpenServiceA( SC_HANDLE hSCManager, LPCSTR lpServiceName,
880 DWORD dwDesiredAccess )
882 LPWSTR lpServiceNameW;
883 SC_HANDLE ret;
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);
890 return ret;
894 /******************************************************************************
895 * OpenServiceW [ADVAPI32.@]
897 * See OpenServiceA.
899 SC_HANDLE WINAPI OpenServiceW( SC_HANDLE hSCManager, LPCWSTR lpServiceName,
900 DWORD dwDesiredAccess)
902 SC_HANDLE handle = 0;
903 DWORD err;
905 TRACE("%p %s %d\n", hSCManager, debugstr_w(lpServiceName), dwDesiredAccess);
907 if (!hSCManager)
909 SetLastError( ERROR_INVALID_HANDLE );
910 return 0;
913 __TRY
915 err = svcctl_OpenServiceW(hSCManager, lpServiceName, dwDesiredAccess, (SC_RPC_HANDLE *)&handle);
917 __EXCEPT(rpc_filter)
919 err = map_exception_code(GetExceptionCode());
921 __ENDTRY
923 if (err != ERROR_SUCCESS)
925 SetLastError(err);
926 handle = 0;
929 TRACE("returning %p\n",handle);
930 return handle;
933 /******************************************************************************
934 * CreateServiceW [ADVAPI32.@]
936 SC_HANDLE WINAPI
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,
943 LPCWSTR lpPassword )
945 SC_HANDLE handle = 0;
946 DWORD err;
947 SIZE_T passwdlen;
949 TRACE("%p %s %s\n", hSCManager,
950 debugstr_w(lpServiceName), debugstr_w(lpDisplayName));
952 if (!hSCManager)
954 SetLastError( ERROR_INVALID_HANDLE );
955 return 0;
958 if (lpPassword)
959 passwdlen = (strlenW(lpPassword) + 1) * sizeof(WCHAR);
960 else
961 passwdlen = 0;
963 __TRY
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);
971 __EXCEPT(rpc_filter)
973 err = map_exception_code(GetExceptionCode());
975 __ENDTRY
977 if (err != ERROR_SUCCESS)
979 SetLastError(err);
980 handle = 0;
982 return handle;
986 /******************************************************************************
987 * CreateServiceA [ADVAPI32.@]
989 SC_HANDLE WINAPI
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,
996 LPCSTR lpPassword )
998 LPWSTR lpServiceNameW, lpDisplayNameW, lpBinaryPathNameW,
999 lpLoadOrderGroupW, lpDependenciesW, lpServiceStartNameW, lpPasswordW;
1000 SC_HANDLE r;
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 );
1026 return r;
1030 /******************************************************************************
1031 * DeleteService [ADVAPI32.@]
1033 * Delete a service from the service control manager database.
1035 * PARAMS
1036 * hService [I] Handle of the service to delete
1038 * RETURNS
1039 * Success: TRUE
1040 * Failure: FALSE
1042 BOOL WINAPI DeleteService( SC_HANDLE hService )
1044 DWORD err;
1046 __TRY
1048 err = svcctl_DeleteService(hService);
1050 __EXCEPT(rpc_filter)
1052 err = map_exception_code(GetExceptionCode());
1054 __ENDTRY
1055 if (err != 0)
1057 SetLastError(err);
1058 return FALSE;
1061 return TRUE;
1065 /******************************************************************************
1066 * StartServiceA [ADVAPI32.@]
1068 * Start a service
1070 * PARAMS
1071 * hService [I] Handle of service
1072 * dwNumServiceArgs [I] Number of arguments
1073 * lpServiceArgVectors [I] Address of array of argument strings
1075 * NOTES
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.
1084 * RETURNS
1085 * Success: TRUE.
1086 * Failure: FALSE
1088 BOOL WINAPI StartServiceA( SC_HANDLE hService, DWORD dwNumServiceArgs,
1089 LPCSTR *lpServiceArgVectors )
1091 LPWSTR *lpwstr=NULL;
1092 unsigned int i;
1093 BOOL r;
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);
1113 return r;
1117 /******************************************************************************
1118 * StartServiceW [ADVAPI32.@]
1120 * See StartServiceA.
1122 BOOL WINAPI StartServiceW(SC_HANDLE hService, DWORD dwNumServiceArgs,
1123 LPCWSTR *lpServiceArgVectors)
1125 DWORD err;
1127 TRACE("%p %d %p\n", hService, dwNumServiceArgs, lpServiceArgVectors);
1129 __TRY
1131 err = svcctl_StartServiceW(hService, dwNumServiceArgs, lpServiceArgVectors);
1133 __EXCEPT(rpc_filter)
1135 err = map_exception_code(GetExceptionCode());
1137 __ENDTRY
1138 if (err != ERROR_SUCCESS)
1140 SetLastError(err);
1141 return FALSE;
1144 return TRUE;
1147 /******************************************************************************
1148 * QueryServiceStatus [ADVAPI32.@]
1150 * PARAMS
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;
1159 BOOL ret;
1160 DWORD dummy;
1162 TRACE("%p %p\n", hService, lpservicestatus);
1164 if (!hService)
1166 SetLastError(ERROR_INVALID_HANDLE);
1167 return FALSE;
1169 if (!lpservicestatus)
1171 SetLastError(ERROR_INVALID_ADDRESS);
1172 return FALSE;
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)) ;
1178 return ret;
1182 /******************************************************************************
1183 * QueryServiceStatusEx [ADVAPI32.@]
1185 * Get information about a service.
1187 * PARAMS
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
1194 * RETURNS
1195 * Success: TRUE
1196 * FAILURE: FALSE
1198 BOOL WINAPI QueryServiceStatusEx(SC_HANDLE hService, SC_STATUS_TYPE InfoLevel,
1199 LPBYTE lpBuffer, DWORD cbBufSize,
1200 LPDWORD pcbBytesNeeded)
1202 DWORD err;
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;
1215 else
1217 __TRY
1219 err = svcctl_QueryServiceStatusEx(hService, InfoLevel, lpBuffer, cbBufSize, pcbBytesNeeded);
1221 __EXCEPT(rpc_filter)
1223 err = map_exception_code(GetExceptionCode());
1225 __ENDTRY
1227 if (err != ERROR_SUCCESS)
1229 SetLastError(err);
1230 return FALSE;
1232 return TRUE;
1235 /******************************************************************************
1236 * QueryServiceConfigA [ADVAPI32.@]
1238 BOOL WINAPI QueryServiceConfigA( SC_HANDLE hService, LPQUERY_SERVICE_CONFIGA config,
1239 DWORD size, LPDWORD needed )
1241 DWORD n;
1242 LPSTR p, buffer;
1243 BOOL ret;
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 );
1251 return FALSE;
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);
1269 ret = FALSE;
1271 #define MAP_STR(str) \
1272 do { \
1273 if (configW->str) \
1275 DWORD sz = WideCharToMultiByte( CP_ACP, 0, configW->str, -1, p, n, NULL, NULL ); \
1276 if (!sz) goto done; \
1277 config->str = p; \
1278 p += sz; \
1279 n -= sz; \
1281 } while (0)
1283 MAP_STR( lpBinaryPathName );
1284 MAP_STR( lpLoadOrderGroup );
1285 MAP_STR( lpDependencies );
1286 MAP_STR( lpServiceStartName );
1287 MAP_STR( lpDisplayName );
1288 #undef MAP_STR
1290 *needed = p - (LPSTR)config;
1291 ret = TRUE;
1293 done:
1294 HeapFree( GetProcessHeap(), 0, buffer );
1295 return ret;
1298 static DWORD move_string_to_buffer(BYTE **buf, LPWSTR *string_ptr)
1300 DWORD cb;
1302 if (!*string_ptr)
1304 cb = sizeof(WCHAR);
1305 memset(*buf, 0, cb);
1307 else
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;
1315 *buf += cb;
1317 return cb;
1320 static DWORD size_string(LPCWSTR string)
1322 return (string ? (strlenW(string) + 1)*sizeof(WCHAR) : sizeof(WCHAR));
1325 /******************************************************************************
1326 * QueryServiceConfigW [ADVAPI32.@]
1328 BOOL WINAPI
1329 QueryServiceConfigW( SC_HANDLE hService,
1330 LPQUERY_SERVICE_CONFIGW lpServiceConfig,
1331 DWORD cbBufSize, LPDWORD pcbBytesNeeded)
1333 QUERY_SERVICE_CONFIGW config;
1334 DWORD total;
1335 DWORD err;
1336 BYTE *bufpos;
1338 TRACE("%p %p %d %p\n", hService, lpServiceConfig,
1339 cbBufSize, pcbBytesNeeded);
1341 memset(&config, 0, sizeof(config));
1343 __TRY
1345 err = svcctl_QueryServiceConfigW(hService, &config);
1347 __EXCEPT(rpc_filter)
1349 err = map_exception_code(GetExceptionCode());
1351 __ENDTRY
1353 if (err != ERROR_SUCCESS)
1355 TRACE("services.exe: error %u\n", err);
1356 SetLastError(err);
1357 return FALSE;
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);
1379 return FALSE;
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) );
1396 return TRUE;
1399 /******************************************************************************
1400 * QueryServiceConfig2A [ADVAPI32.@]
1402 * Note
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)
1410 BOOL ret;
1411 LPBYTE bufferW = NULL;
1413 if(buffer && size)
1414 bufferW = HeapAlloc( GetProcessHeap(), 0, size);
1416 ret = QueryServiceConfig2W(hService, dwLevel, bufferW, size, needed);
1417 if(!ret) goto cleanup;
1419 switch(dwLevel) {
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))) {
1425 DWORD sz;
1426 configA->lpDescription = (LPSTR)(configA + 1);
1427 sz = WideCharToMultiByte( CP_ACP, 0, configW->lpDescription, -1,
1428 configA->lpDescription, size - sizeof(SERVICE_DESCRIPTIONA), NULL, NULL );
1429 if (!sz) {
1430 FIXME("WideCharToMultiByte failed for configW->lpDescription\n");
1431 ret = FALSE;
1432 configA->lpDescription = NULL;
1435 else configA->lpDescription = NULL;
1437 break;
1438 case SERVICE_CONFIG_PRESHUTDOWN_INFO:
1439 if (buffer && bufferW && *needed<=size)
1440 memcpy(buffer, bufferW, *needed);
1441 break;
1442 default:
1443 FIXME("conversation W->A not implemented for level %d\n", dwLevel);
1444 ret = FALSE;
1445 break;
1448 cleanup:
1449 HeapFree( GetProcessHeap(), 0, bufferW);
1450 return ret;
1453 /******************************************************************************
1454 * QueryServiceConfig2W [ADVAPI32.@]
1456 * See QueryServiceConfig2A.
1458 BOOL WINAPI QueryServiceConfig2W(SC_HANDLE hService, DWORD dwLevel, LPBYTE buffer,
1459 DWORD size, LPDWORD needed)
1461 DWORD err;
1463 if(dwLevel!=SERVICE_CONFIG_DESCRIPTION && dwLevel!=SERVICE_CONFIG_PRESHUTDOWN_INFO) {
1464 FIXME("Level %d not implemented\n", dwLevel);
1465 SetLastError(ERROR_INVALID_LEVEL);
1466 return FALSE;
1469 if(!buffer && size) {
1470 SetLastError(ERROR_INVALID_ADDRESS);
1471 return FALSE;
1474 TRACE("%p 0x%d %p 0x%d %p\n", hService, dwLevel, buffer, size, needed);
1476 __TRY
1478 err = svcctl_QueryServiceConfig2W(hService, dwLevel, buffer, size, needed);
1480 __EXCEPT(rpc_filter)
1482 err = map_exception_code(GetExceptionCode());
1484 __ENDTRY
1486 if (err != ERROR_SUCCESS)
1488 SetLastError( err );
1489 return FALSE;
1492 switch (dwLevel)
1494 case SERVICE_CONFIG_DESCRIPTION:
1495 if (buffer)
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);
1500 break;
1504 return TRUE;
1507 /******************************************************************************
1508 * EnumServicesStatusA [ADVAPI32.@]
1510 BOOL WINAPI
1511 EnumServicesStatusA( SC_HANDLE hmngr, DWORD type, DWORD state, LPENUM_SERVICE_STATUSA
1512 services, DWORD size, LPDWORD needed, LPDWORD returned,
1513 LPDWORD resume_handle )
1515 BOOL ret;
1516 unsigned int i;
1517 ENUM_SERVICE_STATUSW *servicesW = NULL;
1518 DWORD sz, n;
1519 char *p;
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 );
1528 return FALSE;
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);
1536 ret = FALSE;
1537 for (i = 0; i < *returned; i++)
1539 sz = WideCharToMultiByte( CP_ACP, 0, servicesW[i].lpServiceName, -1, p, n, NULL, NULL );
1540 if (!sz) goto done;
1541 services[i].lpServiceName = p;
1542 p += sz;
1543 n -= sz;
1544 if (servicesW[i].lpDisplayName)
1546 sz = WideCharToMultiByte( CP_ACP, 0, servicesW[i].lpDisplayName, -1, p, n, NULL, NULL );
1547 if (!sz) goto done;
1548 services[i].lpDisplayName = p;
1549 p += sz;
1550 n -= sz;
1552 else services[i].lpDisplayName = NULL;
1553 services[i].ServiceStatus = servicesW[i].ServiceStatus;
1556 ret = TRUE;
1558 done:
1559 HeapFree( GetProcessHeap(), 0, servicesW );
1560 return ret;
1563 /******************************************************************************
1564 * EnumServicesStatusW [ADVAPI32.@]
1566 BOOL WINAPI
1567 EnumServicesStatusW( SC_HANDLE hmngr, DWORD type, DWORD state, LPENUM_SERVICE_STATUSW
1568 services, DWORD size, LPDWORD needed, LPDWORD returned,
1569 LPDWORD resume_handle )
1571 DWORD err, i;
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);
1577 if (resume_handle)
1578 FIXME("resume handle not supported\n");
1580 if (!hmngr)
1582 SetLastError( ERROR_INVALID_HANDLE );
1583 return FALSE;
1586 /* make sure we pass a valid pointer */
1587 if (!services || size < sizeof(*services))
1589 services = &dummy_status;
1590 size = sizeof(dummy_status);
1593 __TRY
1595 err = svcctl_EnumServicesStatusW( hmngr, type, state, (BYTE *)services, size, needed, returned );
1597 __EXCEPT(rpc_filter)
1599 err = map_exception_code( GetExceptionCode() );
1601 __ENDTRY
1603 if (err != ERROR_SUCCESS)
1605 SetLastError( err );
1606 return FALSE;
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);
1617 return TRUE;
1620 /******************************************************************************
1621 * EnumServicesStatusExA [ADVAPI32.@]
1623 BOOL WINAPI
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 )
1628 BOOL ret;
1629 unsigned int i;
1630 ENUM_SERVICE_STATUS_PROCESSA *services = (ENUM_SERVICE_STATUS_PROCESSA *)buffer;
1631 ENUM_SERVICE_STATUS_PROCESSW *servicesW = NULL;
1632 WCHAR *groupW = NULL;
1633 DWORD sz, n;
1634 char *p;
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 );
1643 return FALSE;
1645 if (group)
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 );
1652 return FALSE;
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);
1663 ret = FALSE;
1664 for (i = 0; i < *returned; i++)
1666 sz = WideCharToMultiByte( CP_ACP, 0, servicesW[i].lpServiceName, -1, p, n, NULL, NULL );
1667 if (!sz) goto done;
1668 services[i].lpServiceName = p;
1669 p += sz;
1670 n -= sz;
1671 if (servicesW[i].lpDisplayName)
1673 sz = WideCharToMultiByte( CP_ACP, 0, servicesW[i].lpDisplayName, -1, p, n, NULL, NULL );
1674 if (!sz) goto done;
1675 services[i].lpDisplayName = p;
1676 p += sz;
1677 n -= sz;
1679 else services[i].lpDisplayName = NULL;
1680 services[i].ServiceStatusProcess = servicesW[i].ServiceStatusProcess;
1683 ret = TRUE;
1685 done:
1686 HeapFree( GetProcessHeap(), 0, servicesW );
1687 HeapFree( GetProcessHeap(), 0, groupW );
1688 return ret;
1691 /******************************************************************************
1692 * EnumServicesStatusExW [ADVAPI32.@]
1694 BOOL WINAPI
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 )
1699 DWORD err, i;
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));
1706 if (resume_handle)
1707 FIXME("resume handle not supported\n");
1709 if (level != SC_ENUM_PROCESS_INFO)
1711 SetLastError( ERROR_INVALID_LEVEL );
1712 return FALSE;
1714 if (!hmngr)
1716 SetLastError( ERROR_INVALID_HANDLE );
1717 return FALSE;
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);
1727 __TRY
1729 err = svcctl_EnumServicesStatusExW( hmngr, type, state, buffer, size, needed,
1730 returned, group );
1732 __EXCEPT(rpc_filter)
1734 err = map_exception_code( GetExceptionCode() );
1736 __ENDTRY
1738 if (err != ERROR_SUCCESS)
1740 SetLastError( err );
1741 return FALSE;
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);
1752 return TRUE;
1755 /******************************************************************************
1756 * GetServiceKeyNameA [ADVAPI32.@]
1758 BOOL WINAPI GetServiceKeyNameA( SC_HANDLE hSCManager, LPCSTR lpDisplayName,
1759 LPSTR lpServiceName, LPDWORD lpcchBuffer )
1761 LPWSTR lpDisplayNameW, lpServiceNameW;
1762 DWORD sizeW;
1763 BOOL ret = FALSE;
1765 TRACE("%p %s %p %p\n", hSCManager,
1766 debugstr_a(lpDisplayName), lpServiceName, lpcchBuffer);
1768 lpDisplayNameW = SERV_dup(lpDisplayName);
1769 if (lpServiceName)
1770 lpServiceNameW = HeapAlloc(GetProcessHeap(), 0, *lpcchBuffer * sizeof(WCHAR));
1771 else
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 */
1780 goto cleanup;
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);
1789 goto cleanup;
1792 /* lpcchBuffer not updated - same as in GetServiceDisplayNameA */
1793 ret = TRUE;
1795 cleanup:
1796 HeapFree(GetProcessHeap(), 0, lpServiceNameW);
1797 HeapFree(GetProcessHeap(), 0, lpDisplayNameW);
1798 return ret;
1801 /******************************************************************************
1802 * GetServiceKeyNameW [ADVAPI32.@]
1804 BOOL WINAPI GetServiceKeyNameW( SC_HANDLE hSCManager, LPCWSTR lpDisplayName,
1805 LPWSTR lpServiceName, LPDWORD lpcchBuffer )
1807 DWORD err;
1808 WCHAR buffer[2];
1809 DWORD size;
1811 TRACE("%p %s %p %p\n", hSCManager,
1812 debugstr_w(lpDisplayName), lpServiceName, lpcchBuffer);
1814 if (!hSCManager)
1816 SetLastError( ERROR_INVALID_HANDLE );
1817 return 0;
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.
1827 *lpcchBuffer = 2;
1830 /* RPC call takes size excluding nul-terminator, whereas *lpcchBuffer
1831 * includes the nul-terminator on input. */
1832 size = *lpcchBuffer - 1;
1834 __TRY
1836 err = svcctl_GetServiceKeyNameW(hSCManager, lpDisplayName, lpServiceName,
1837 &size);
1839 __EXCEPT(rpc_filter)
1841 err = map_exception_code(GetExceptionCode());
1843 __ENDTRY
1845 /* The value of *lpcchBuffer excludes nul-terminator on output. */
1846 if (err == ERROR_SUCCESS || err == ERROR_INSUFFICIENT_BUFFER)
1847 *lpcchBuffer = size;
1849 if (err)
1850 SetLastError(err);
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);
1863 return FALSE;
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);
1875 return FALSE;
1878 /******************************************************************************
1879 * GetServiceDisplayNameA [ADVAPI32.@]
1881 BOOL WINAPI GetServiceDisplayNameA( SC_HANDLE hSCManager, LPCSTR lpServiceName,
1882 LPSTR lpDisplayName, LPDWORD lpcchBuffer)
1884 LPWSTR lpServiceNameW, lpDisplayNameW;
1885 DWORD sizeW;
1886 BOOL ret = FALSE;
1888 TRACE("%p %s %p %p\n", hSCManager,
1889 debugstr_a(lpServiceName), lpDisplayName, lpcchBuffer);
1891 lpServiceNameW = SERV_dup(lpServiceName);
1892 if (lpDisplayName)
1893 lpDisplayNameW = HeapAlloc(GetProcessHeap(), 0, *lpcchBuffer * sizeof(WCHAR));
1894 else
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 */
1903 goto cleanup;
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);
1912 goto cleanup;
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) */
1917 ret = TRUE;
1919 cleanup:
1920 HeapFree(GetProcessHeap(), 0, lpDisplayNameW);
1921 HeapFree(GetProcessHeap(), 0, lpServiceNameW);
1922 return ret;
1925 /******************************************************************************
1926 * GetServiceDisplayNameW [ADVAPI32.@]
1928 BOOL WINAPI GetServiceDisplayNameW( SC_HANDLE hSCManager, LPCWSTR lpServiceName,
1929 LPWSTR lpDisplayName, LPDWORD lpcchBuffer)
1931 DWORD err;
1932 DWORD size;
1933 WCHAR buffer[2];
1935 TRACE("%p %s %p %p\n", hSCManager,
1936 debugstr_w(lpServiceName), lpDisplayName, lpcchBuffer);
1938 if (!hSCManager)
1940 SetLastError( ERROR_INVALID_HANDLE );
1941 return 0;
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.
1951 *lpcchBuffer = 2;
1954 /* RPC call takes size excluding nul-terminator, whereas *lpcchBuffer
1955 * includes the nul-terminator on input. */
1956 size = *lpcchBuffer - 1;
1958 __TRY
1960 err = svcctl_GetServiceDisplayNameW(hSCManager, lpServiceName, lpDisplayName,
1961 &size);
1963 __EXCEPT(rpc_filter)
1965 err = map_exception_code(GetExceptionCode());
1967 __ENDTRY
1969 /* The value of *lpcchBuffer excludes nul-terminator on output. */
1970 if (err == ERROR_SUCCESS || err == ERROR_INSUFFICIENT_BUFFER)
1971 *lpcchBuffer = size;
1973 if (err)
1974 SetLastError(err);
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)
1986 DWORD cb_pwd;
1987 DWORD err;
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;
1997 __TRY
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());
2008 __ENDTRY
2010 if (err != ERROR_SUCCESS)
2011 SetLastError(err);
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;
2026 BOOL r;
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 );
2053 return r;
2056 /******************************************************************************
2057 * ChangeServiceConfig2A [ADVAPI32.@]
2059 BOOL WINAPI ChangeServiceConfig2A( SC_HANDLE hService, DWORD dwInfoLevel,
2060 LPVOID lpInfo)
2062 BOOL r = FALSE;
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);
2097 else
2098 SetLastError( ERROR_INVALID_PARAMETER );
2100 return r;
2103 /******************************************************************************
2104 * ChangeServiceConfig2W [ADVAPI32.@]
2106 BOOL WINAPI ChangeServiceConfig2W( SC_HANDLE hService, DWORD dwInfoLevel,
2107 LPVOID lpInfo)
2109 DWORD err;
2111 __TRY
2113 err = svcctl_ChangeServiceConfig2W( hService, dwInfoLevel, lpInfo );
2115 __EXCEPT(rpc_filter)
2117 err = map_exception_code(GetExceptionCode());
2119 __ENDTRY
2121 if (err != ERROR_SUCCESS)
2122 SetLastError(err);
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;
2136 DWORD size;
2137 BOOL succ;
2138 ACL acl;
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);
2151 size = cbBufSize;
2152 succ = MakeSelfRelativeSD(&descriptor, lpSecurityDescriptor, &size);
2153 *pcbBytesNeeded = size;
2154 return succ;
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);
2165 return TRUE;
2168 /******************************************************************************
2169 * SetServiceBits [ADVAPI32.@]
2171 BOOL WINAPI SetServiceBits( SERVICE_STATUS_HANDLE hServiceStatus,
2172 DWORD dwServiceBits,
2173 BOOL bSetBitsOn,
2174 BOOL bUpdateImmediately)
2176 FIXME("%p %08x %x %x\n", hServiceStatus, dwServiceBits,
2177 bSetBitsOn, bUpdateImmediately);
2178 return TRUE;
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;
2186 func( control );
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 )
2211 LPWSTR nameW;
2212 SERVICE_STATUS_HANDLE ret;
2214 nameW = SERV_dup(name);
2215 ret = RegisterServiceCtrlHandlerExW( nameW, handler, context );
2216 HeapFree( GetProcessHeap(), 0, nameW );
2217 return ret;
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;
2228 BOOL found = FALSE;
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;
2238 found = TRUE;
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;
2258 return TRUE;
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;
2272 return TRUE;