1 /*
2 * PROJECT: ReactOS
3 * LICENSE: GPL - See COPYING in the top level directory
4 * PURPOSE: Audio Service Plug and Play
5 * COPYRIGHT: Copyright 2009 Johannes Anderwald
6 */
7
8 #include "audiosrv.h"
9
10 #define NDEBUG
11 #include <debug.h>
12
13 BOOL
WaitForService(SC_HANDLE hService,ULONG RetryCount)14 WaitForService(
15 SC_HANDLE hService,
16 ULONG RetryCount)
17 {
18 ULONG Index = 0;
19 DWORD dwSize;
20 SERVICE_STATUS_PROCESS Info;
21
22 do
23 {
24 if (!QueryServiceStatusEx(hService, SC_STATUS_PROCESS_INFO, (LPBYTE)&Info, sizeof(SERVICE_STATUS_PROCESS), &dwSize))
25 {
26 DPRINT("QueryServiceStatusEx failed %x\n", GetLastError());
27 break;
28 }
29
30 if (Info.dwCurrentState == SERVICE_RUNNING)
31 return TRUE;
32
33 Sleep(1000);
34
35 } while (Index++ < RetryCount);
36
37 DPRINT("Timeout while waiting for service to become ready %p\n", hService);
38
39 return FALSE;
40 }
41
42 BOOL
StartAudioService(SC_HANDLE hSCManager,LPWSTR ServiceName,ULONG RetryCount)43 StartAudioService(
44 SC_HANDLE hSCManager,
45 LPWSTR ServiceName,
46 ULONG RetryCount)
47 {
48 SC_HANDLE hService;
49 BOOL ret;
50
51 hService = OpenService(hSCManager, ServiceName, SERVICE_ALL_ACCESS);
52 if (!hService)
53 {
54 DPRINT("Failed to open service %S %x\n", ServiceName, GetLastError());
55 return FALSE;
56 }
57
58 if (!StartService(hService, 0, NULL))
59 {
60 DPRINT("Failed to start service %S %x\n", ServiceName, GetLastError());
61 CloseServiceHandle(hService);
62 return FALSE;
63 }
64
65 ret = WaitForService(hService, RetryCount);
66
67 CloseServiceHandle(hService);
68 return ret;
69 }
70
71 BOOL
StartSystemAudioServices(VOID)72 StartSystemAudioServices(VOID)
73 {
74 SC_HANDLE hSCManager;
75
76 DPRINT("Starting system audio services\n");
77
78 hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
79 if (!hSCManager)
80 {
81 DPRINT("Failed to open service manager %x\n", GetLastError());
82 return FALSE;
83 }
84
85 DPRINT("Starting sysaudio service\n");
86 StartAudioService(hSCManager, L"sysaudio", 20);
87 DPRINT("Starting wdmaud service\n");
88 StartAudioService(hSCManager, L"wdmaud", 20);
89
90 CloseServiceHandle(hSCManager);
91 return TRUE;
92 }
93