Sync with trunk head
[reactos.git] / base / system / winlogon / winlogon.c
index bc70d65..7953566 100644 (file)
@@ -23,17 +23,104 @@ PWLSESSION WLSession = NULL;
 
 /* FUNCTIONS *****************************************************************/
 
+DWORD
+WINAPI
+PlayLogonSoundThread(
+       IN LPVOID lpParameter)
+{
+       HKEY hKey;
+       WCHAR szBuffer[MAX_PATH] = {0};
+       WCHAR szDest[MAX_PATH];
+       DWORD dwSize = sizeof(szBuffer);
+       HMODULE hLibrary;
+       SERVICE_STATUS_PROCESS Info;
+       typedef BOOL (WINAPI *PLAYSOUNDW)(LPCWSTR,HMODULE,DWORD);
+       PLAYSOUNDW Play;
+       ULONG Index = 0;
+
+       if (RegOpenKeyExW(HKEY_CURRENT_USER, L"AppEvents\\Schemes\\Apps\\.Default\\WindowsLogon\\.Current", 0, KEY_READ, &hKey) != ERROR_SUCCESS)
+       {
+               ExitThread(0);
+       }
+
+       if (RegQueryValueExW(hKey, NULL, NULL, NULL, (LPBYTE)szBuffer, &dwSize) != ERROR_SUCCESS)
+       {
+               RegCloseKey(hKey);
+               ExitThread(0);
+       }
+
+
+       RegCloseKey(hKey);
+
+       if (!szBuffer[0])
+               ExitThread(0);
+
+
+       szBuffer[MAX_PATH-1] = L'\0';
+       if (ExpandEnvironmentStringsW(szBuffer, szDest, MAX_PATH))
+       {
+               SC_HANDLE hSCManager, hService;
+
+               hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
+               if (!hSCManager)
+                       ExitThread(0);;
+
+               hService = OpenServiceW(hSCManager, L"wdmaud", GENERIC_READ);
+               if (!hService)
+               {
+                       CloseServiceHandle(hSCManager);
+                       TRACE("WL: failed to open sysaudio Status %x", GetLastError());
+                       ExitThread(0);
+               }
+
+               do
+               {
+                       if (!QueryServiceStatusEx(hService, SC_STATUS_PROCESS_INFO, (LPBYTE)&Info, sizeof(SERVICE_STATUS_PROCESS), &dwSize))
+                       {
+                               TRACE("WL: QueryServiceStatusEx failed %x\n", GetLastError());
+                               break;
+                       }
+
+                       if (Info.dwCurrentState == SERVICE_RUNNING)
+                               break;
+
+                       Sleep(1000);
+
+               }while(Index++ < 20);
+
+               CloseServiceHandle(hService);
+               CloseServiceHandle(hSCManager);
+
+               if (Info.dwCurrentState != SERVICE_RUNNING)
+                       ExitThread(0);
+
+
+               hLibrary = LoadLibraryW(L"winmm.dll");
+               if (hLibrary)
+               {
+                       Play = (PLAYSOUNDW)GetProcAddress(hLibrary, "PlaySoundW");
+                       if (Play)
+                       {
+                               Play(szDest, NULL, SND_FILENAME);
+                       }
+                       FreeLibrary(hLibrary);
+               }
+       }
+       ExitThread(0);
+}
+
+
+
 static BOOL
 StartServicesManager(VOID)
 {
-       HANDLE ServicesInitEvent = NULL;
        STARTUPINFOW StartupInfo;
        PROCESS_INFORMATION ProcessInformation;
-       DWORD Count;
        LPCWSTR ServiceString = L"services.exe";
        BOOL res;
 
        /* Start the service control manager (services.exe) */
+       ZeroMemory(&StartupInfo, sizeof(STARTUPINFOW));
        StartupInfo.cb = sizeof(StartupInfo);
        StartupInfo.lpReserved = NULL;
        StartupInfo.lpDesktop = NULL;
@@ -61,98 +148,55 @@ StartServicesManager(VOID)
                return FALSE;
        }
 
-       /* Wait for event creation (by SCM) for max. 20 seconds */
-       for (Count = 0; Count < 20; Count++)
-       {
-               Sleep(1000);
-
-               TRACE("WL: Attempting to open event \"SvcctrlStartEvent_A3752DX\"\n");
-               ServicesInitEvent = OpenEventW(
-                       SYNCHRONIZE,
-                       FALSE,
-                       L"SvcctrlStartEvent_A3752DX");
-               if (ServicesInitEvent)
-                       break;
-       }
+       TRACE("WL: Created new process - %S\n", ServiceString);
 
-       if (!ServicesInitEvent)
-       {
-               ERR("WL: Failed to open event \"SvcctrlStartEvent_A3752DX\"\n");
-               return FALSE;
-       }
+       CloseHandle(ProcessInformation.hThread);
+       CloseHandle(ProcessInformation.hProcess);
 
-       /* Wait for event signalization */
-       WaitForSingleObject(ServicesInitEvent, INFINITE);
-       CloseHandle(ServicesInitEvent);
        TRACE("WL: StartServicesManager() done.\n");
 
        return TRUE;
 }
 
-static BOOL
-StartCustomService(
-       IN LPCWSTR ServiceName)
-{
-       SC_HANDLE hSCManager = NULL;
-       SC_HANDLE hService = NULL;
-       BOOL ret = FALSE;
-
-       hSCManager = OpenSCManager(NULL, NULL, 0);
-       if (!hSCManager)
-       {
-               ERR("WL: Failed to OpenSCManager\n");
-               goto cleanup;
-       }
-
-       hService = OpenServiceW(hSCManager, ServiceName, SERVICE_START);
-       if (!hService)
-       {
-               ERR("WL: Failed to open the service\n");
-               goto cleanup;
-       }
-       if (!StartServiceW(hService, 0, NULL))
-       {
-               ERR("WL: Failed to start the service\n");
-               goto cleanup;
-       }
-
-       ret = TRUE;
-
-cleanup:
-       if (hService)
-               CloseServiceHandle(hService);
-       if (hSCManager)
-               CloseServiceHandle(hSCManager);
-       return ret;
-}
 
 static BOOL
 StartLsass(VOID)
 {
-       HANDLE LsassInitEvent;
+       STARTUPINFOW StartupInfo;
+       PROCESS_INFORMATION ProcessInformation;
+       LPCWSTR ServiceString = L"lsass.exe";
+       BOOL res;
+
+       /* Start the service control manager (services.exe) */
+       ZeroMemory(&StartupInfo, sizeof(STARTUPINFOW));
+       StartupInfo.cb = sizeof(StartupInfo);
+       StartupInfo.lpReserved = NULL;
+       StartupInfo.lpDesktop = NULL;
+       StartupInfo.lpTitle = NULL;
+       StartupInfo.dwFlags = 0;
+       StartupInfo.cbReserved2 = 0;
+       StartupInfo.lpReserved2 = 0;
+
+       TRACE("WL: Creating new process - %S\n", ServiceString);
 
-       LsassInitEvent = CreateEventW(
+       res = CreateProcessW(
+               ServiceString,
+               NULL,
+               NULL,
                NULL,
-               TRUE,
                FALSE,
-               L"Global\\SECURITY_SERVICES_STARTED");
-       if (!LsassInitEvent)
-       {
-               ERR("WL: Failed to create lsass notification event (error %lu)\n", GetLastError());
-               return FALSE;
-       }
+               DETACHED_PROCESS,
+               NULL,
+               NULL,
+               &StartupInfo,
+               &ProcessInformation);
 
-       /* Start the local security authority subsystem (Netlogon service) */
-       if (!StartCustomService(L"Netlogon"))
-       {
-               ERR("WL: Failed to start NetLogon service (error %lu)\n", GetLastError());
-               return FALSE;
-       }
+       TRACE("WL: Created new process - %S\n", ServiceString);
 
-       WaitForSingleObject(LsassInitEvent, INFINITE);
-       CloseHandle(LsassInitEvent);
+       CloseHandle(ProcessInformation.hThread);
+       CloseHandle(ProcessInformation.hProcess);
 
-       return TRUE;
+       return res;
 }
 
 BOOL
@@ -185,7 +229,7 @@ RemoveStatusMessage(
        return Session->Gina.Functions.WlxRemoveStatusMessage(Session->Gina.Context);
 }
 
-static BOOL CALLBACK
+static INT_PTR CALLBACK
 GinaLoadFailedWindowProc(
        IN HWND hwndDlg,
        IN UINT uMsg,
@@ -245,6 +289,7 @@ WinMain(
 #endif
        ULONG HardErrorResponse;
        MSG Msg;
+       HANDLE hThread;
 
        UNREFERENCED_PARAMETER(hPrevInstance);
        UNREFERENCED_PARAMETER(lpCmdLine);
@@ -361,6 +406,13 @@ WinMain(
        else
                PostMessageW(WLSession->SASWindow, WLX_WM_SAS, WLX_SAS_TYPE_TIMEOUT, 0);
 
+       /* Play logon sound */
+       hThread = CreateThread(NULL, 0, PlayLogonSoundThread, NULL, 0, NULL);
+       if (hThread)
+       {
+               CloseHandle(hThread);
+       }
+
        /* Tell kernel that CurrentControlSet is good (needed
         * to support Last good known configuration boot) */
        NtInitializeRegistry(CM_BOOT_FLAG_ACCEPTED);