From: Hermès Bélusca-Maïto Date: Mon, 26 Sep 2016 22:42:56 +0000 (+0000) Subject: [USERINIT] X-Git-Tag: ReactOS-0.4.3~234 X-Git-Url: https://git.reactos.org/?p=reactos.git;a=commitdiff_plain;h=0a8188519278b7c37d3737a0fc3c8bc7f2d0a333 [USERINIT] - Use explicit unicode functions where possible. - Use ARRAYSIZE(foo) instead of sizeof(foo)/sizeof(foo[0]). - Don't hardcode some buffer sizes. - Try reducing level of code indentation in some functions. - Make StartShell and StartInstaller return a boolean (TRUE: success; FALSE: failure). Will be needed in the next commit. - Remove a useless "#pragma warning". svn path=/trunk/; revision=72821 --- diff --git a/reactos/base/system/userinit/livecd.c b/reactos/base/system/userinit/livecd.c index 2ab8870a6e7..5653ac6524a 100644 --- a/reactos/base/system/userinit/livecd.c +++ b/reactos/base/system/userinit/livecd.c @@ -18,16 +18,16 @@ InitImageInfo(PIMGINFO ImgInfo) ZeroMemory(ImgInfo, sizeof(*ImgInfo)); - ImgInfo->hBitmap = LoadImage(hInstance, - MAKEINTRESOURCE(IDB_ROSLOGO), - IMAGE_BITMAP, - 0, - 0, - LR_DEFAULTCOLOR); + ImgInfo->hBitmap = LoadImageW(hInstance, + MAKEINTRESOURCEW(IDB_ROSLOGO), + IMAGE_BITMAP, + 0, + 0, + LR_DEFAULTCOLOR); if (ImgInfo->hBitmap != NULL) { - GetObject(ImgInfo->hBitmap, sizeof(BITMAP), &bitmap); + GetObject(ImgInfo->hBitmap, sizeof(bitmap), &bitmap); ImgInfo->cxSource = bitmap.bmWidth; ImgInfo->cySource = bitmap.bmHeight; @@ -46,11 +46,11 @@ IsLiveCD(VOID) TRACE("IsLiveCD()\n"); - rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, - REGSTR_PATH_CURRENT_CONTROL_SET, - 0, - KEY_QUERY_VALUE, - &ControlKey); + rc = RegOpenKeyExW(HKEY_LOCAL_MACHINE, + REGSTR_PATH_CURRENT_CONTROL_SET, + 0, + KEY_QUERY_VALUE, + &ControlKey); if (rc != ERROR_SUCCESS) { WARN("RegOpenKeyEx() failed with error %lu\n", rc); @@ -110,7 +110,7 @@ LocalesEnumProc(LPTSTR lpLocale) { if (bSpain == FALSE) { - LoadStringW(hInstance, IDS_SPAIN, lang, 255); + LoadStringW(hInstance, IDS_SPAIN, lang, ARRAYSIZE(lang)); bSpain = TRUE; } else @@ -120,7 +120,7 @@ LocalesEnumProc(LPTSTR lpLocale) } else { - GetLocaleInfoW(lcid, LOCALE_SLANGUAGE, lang, sizeof(lang)/sizeof(WCHAR)); + GetLocaleInfoW(lcid, LOCALE_SLANGUAGE, lang, ARRAYSIZE(lang)); } if (bNoShow == FALSE) @@ -151,7 +151,7 @@ CreateLanguagesList(HWND hwnd) /* Select current locale */ /* or should it be System and not user? */ - GetLocaleInfoW(GetUserDefaultLCID(), LOCALE_SLANGUAGE, langSel, sizeof(langSel)/sizeof(WCHAR)); + GetLocaleInfoW(GetUserDefaultLCID(), LOCALE_SLANGUAGE, langSel, ARRAYSIZE(langSel)); SendMessageW(hList, CB_SELECTSTRING, @@ -170,13 +170,13 @@ GetLayoutName( DWORD dwBufLen; WCHAR szBuf[MAX_PATH], szDispName[MAX_PATH], szIndex[MAX_PATH], szPath[MAX_PATH]; HANDLE hLib; - unsigned i, j, k; + UINT i, j, k; wsprintf(szBuf, L"SYSTEM\\CurrentControlSet\\Control\\Keyboard Layouts\\%s", szLCID); if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, (LPCTSTR)szBuf, 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS) { - dwBufLen = sizeof(szBuf); + dwBufLen = sizeof(szDispName); if (RegQueryValueExW(hKey, L"Layout Display Name", NULL, NULL, (LPBYTE)szDispName, &dwBufLen) == ERROR_SUCCESS) { @@ -197,12 +197,12 @@ GetLayoutName( szDispName[i] = szDispName[i + 1]; } - if (ExpandEnvironmentStringsW(szDispName, szPath, MAX_PATH)) + if (ExpandEnvironmentStringsW(szDispName, szPath, ARRAYSIZE(szPath))) { hLib = LoadLibraryW(szPath); if (hLib) { - if (LoadStringW(hLib, _wtoi(szIndex), szPath, sizeof(szPath) / sizeof(WCHAR)) != 0) + if (LoadStringW(hLib, _wtoi(szIndex), szPath, ARRAYSIZE(szPath)) != 0) { wcscpy(szName, szPath); RegCloseKey(hKey); @@ -323,7 +323,7 @@ CreateKeyboardLayoutList( while (TRUE) { - dwSize = sizeof(szLayoutId) / sizeof(WCHAR); + dwSize = ARRAYSIZE(szLayoutId); lError = RegEnumKeyExW(hKey, dwIndex, @@ -458,10 +458,10 @@ InitializeDefaultUserLocale( i = 0; while (LocaleData[i].pValue != NULL) { - if (GetLocaleInfo(lcid, - LocaleData[i].LCType | LOCALE_NOUSEROVERRIDE, - szBuffer, - sizeof(szBuffer) / sizeof(WCHAR))) + if (GetLocaleInfoW(lcid, + LocaleData[i].LCType | LOCALE_NOUSEROVERRIDE, + szBuffer, + ARRAYSIZE(szBuffer))) { RegSetValueExW(hLocaleKey, LocaleData[i].pValue, @@ -515,7 +515,7 @@ OnDrawItem( if (lpDrawItem->CtlID == uCtlID) { - /* position image in centre of dialog */ + /* Position image in centre of dialog */ left = (lpDrawItem->rcItem.right - pState->ImageInfo.cxSource) / 2; hdcMem = CreateCompatibleDC(lpDrawItem->hDC); @@ -549,17 +549,17 @@ LocaleDlgProc( PSTATE pState; /* Retrieve pointer to the state */ - pState = (PSTATE)GetWindowLongPtr (hwndDlg, GWL_USERDATA); + pState = (PSTATE)GetWindowLongPtrW(hwndDlg, GWLP_USERDATA); switch (uMsg) { case WM_INITDIALOG: /* Save pointer to the global state */ pState = (PSTATE)lParam; - SetWindowLongPtr(hwndDlg, GWL_USERDATA, (DWORD_PTR)pState); + SetWindowLongPtrW(hwndDlg, GWLP_USERDATA, (DWORD_PTR)pState); /* Center the dialog window */ - CenterWindow (hwndDlg); + CenterWindow(hwndDlg); /* Fill the language and keyboard layout lists */ CreateLanguagesList(GetDlgItem(hwndDlg, IDC_LANGUAGELIST)); @@ -666,14 +666,14 @@ StartDlgProc( PSTATE pState; /* Retrieve pointer to the state */ - pState = (PSTATE)GetWindowLongPtr (hwndDlg, GWL_USERDATA); + pState = (PSTATE)GetWindowLongPtrW(hwndDlg, GWLP_USERDATA); switch (uMsg) { case WM_INITDIALOG: /* Save pointer to the state */ pState = (PSTATE)lParam; - SetWindowLongPtr(hwndDlg, GWL_USERDATA, (DWORD_PTR)pState); + SetWindowLongPtrW(hwndDlg, GWLP_USERDATA, (DWORD_PTR)pState); /* Center the dialog window */ CenterWindow(hwndDlg); @@ -734,19 +734,19 @@ RunLiveCD( switch (pState->NextPage) { case LOCALEPAGE: - DialogBoxParam(hInstance, - MAKEINTRESOURCE(IDD_LOCALEPAGE), - NULL, - LocaleDlgProc, - (LPARAM)pState); + DialogBoxParamW(hInstance, + MAKEINTRESOURCEW(IDD_LOCALEPAGE), + NULL, + LocaleDlgProc, + (LPARAM)pState); break; case STARTPAGE: - DialogBoxParam(hInstance, - MAKEINTRESOURCE(IDD_STARTPAGE), - NULL, - StartDlgProc, - (LPARAM)pState); + DialogBoxParamW(hInstance, + MAKEINTRESOURCEW(IDD_STARTPAGE), + NULL, + StartDlgProc, + (LPARAM)pState); break; default: diff --git a/reactos/base/system/userinit/userinit.c b/reactos/base/system/userinit/userinit.c index 5aeeede8d6d..3bcfe28a0fa 100644 --- a/reactos/base/system/userinit/userinit.c +++ b/reactos/base/system/userinit/userinit.c @@ -73,14 +73,14 @@ ReadRegSzKey( return rc; } /* NULL-terminate the string */ - Value[cbData / sizeof(WCHAR)] = '\0'; + Value[cbData / sizeof(WCHAR)] = L'\0'; *pValue = Value; return ERROR_SUCCESS; } -static -BOOL IsConsoleShell(VOID) +static BOOL +IsConsoleShell(VOID) { HKEY ControlKey = NULL; LPWSTR SystemStartOptions = NULL; @@ -133,15 +133,14 @@ cleanup: return ret; } -static -BOOL GetShell( +static BOOL +GetShell( OUT WCHAR *CommandLine, /* must be at least MAX_PATH long */ IN HKEY hRootKey) { HKEY hKey; DWORD Type, Size; WCHAR Shell[MAX_PATH]; - BOOL Ret = FALSE; BOOL ConsoleShell = IsConsoleShell(); LONG rc; @@ -149,34 +148,38 @@ BOOL GetShell( rc = RegOpenKeyExW(hRootKey, L"Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon", 0, KEY_QUERY_VALUE, &hKey); - if (rc == ERROR_SUCCESS) + if (rc != ERROR_SUCCESS) { - Size = MAX_PATH * sizeof(WCHAR); - rc = RegQueryValueExW(hKey, - ConsoleShell ? L"ConsoleShell" : L"Shell", - NULL, - &Type, - (LPBYTE)Shell, - &Size); - if (rc == ERROR_SUCCESS) - { - if ((Type == REG_SZ) || (Type == REG_EXPAND_SZ)) - { - TRACE("Found command line %s\n", debugstr_w(Shell)); - wcscpy(CommandLine, Shell); - Ret = TRUE; - } - else - WARN("Wrong type %lu (expected %u or %u)\n", Type, REG_SZ, REG_EXPAND_SZ); - } - else - WARN("RegQueryValueEx() failed with error %lu\n", rc); - RegCloseKey(hKey); - } - else WARN("RegOpenKeyEx() failed with error %lu\n", rc); + return FALSE; + } - return Ret; + Size = sizeof(Shell); + rc = RegQueryValueExW(hKey, + ConsoleShell ? L"ConsoleShell" : L"Shell", + NULL, + &Type, + (LPBYTE)Shell, + &Size); + RegCloseKey(hKey); + + if (rc != ERROR_SUCCESS) + { + WARN("RegQueryValueEx() failed with error %lu\n", rc); + return FALSE; + } + + if ((Type == REG_SZ) || (Type == REG_EXPAND_SZ)) + { + TRACE("Found command line %s\n", debugstr_w(Shell)); + wcscpy(CommandLine, Shell); + return TRUE; + } + else + { + WARN("Wrong type %lu (expected %u or %u)\n", Type, REG_SZ, REG_EXPAND_SZ); + return FALSE; + } } static VOID @@ -212,7 +215,7 @@ StartAutoApplications( { if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && (findData.nFileSizeHigh || findData.nFileSizeLow)) { - ZeroMemory(&ExecInfo, sizeof(SHELLEXECUTEINFOW)); + ZeroMemory(&ExecInfo, sizeof(ExecInfo)); ExecInfo.cbSize = sizeof(ExecInfo); wcscpy(&szPath[len+1], findData.cFileName); ExecInfo.lpVerb = L"open"; @@ -242,18 +245,18 @@ TryToStartShell( si.wShowWindow = SW_SHOWNORMAL; ZeroMemory(&pi, sizeof(pi)); - ExpandEnvironmentStrings(Shell, ExpandedShell, MAX_PATH); - - if (!CreateProcess(NULL, - ExpandedShell, - NULL, - NULL, - FALSE, - NORMAL_PRIORITY_CLASS, - NULL, - NULL, - &si, - &pi)) + ExpandEnvironmentStringsW(Shell, ExpandedShell, ARRAYSIZE(ExpandedShell)); + + if (!CreateProcessW(NULL, + ExpandedShell, + NULL, + NULL, + FALSE, + NORMAL_PRIORITY_CLASS, + NULL, + NULL, + &si, + &pi)) { WARN("CreateProcess() failed with error %lu\n", GetLastError()); return FALSE; @@ -266,8 +269,8 @@ TryToStartShell( return TRUE; } -static -VOID StartShell(VOID) +static BOOL +StartShell(VOID) { WCHAR Shell[MAX_PATH]; TCHAR szMsg[RC_STRING_MAX_SIZE]; @@ -282,38 +285,41 @@ VOID StartShell(VOID) rc = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Control\\SafeBoot\\Option", 0, KEY_QUERY_VALUE, &hKey); - if(rc == ERROR_SUCCESS) + if (rc == ERROR_SUCCESS) { Size = sizeof(Value); rc = RegQueryValueExW(hKey, L"UseAlternateShell", NULL, &Type, (LPBYTE)&Value, &Size); - if(rc == ERROR_SUCCESS) + RegCloseKey(hKey); + + if (rc == ERROR_SUCCESS) { - RegCloseKey(hKey); - if(Type == REG_DWORD) + if (Type == REG_DWORD) { - if(Value) + if (Value) { /* Safe Mode Alternate Shell required */ rc = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Control\\SafeBoot", 0, KEY_READ, &hKey); - if(rc == ERROR_SUCCESS) + if (rc == ERROR_SUCCESS) { - Size = MAX_PATH * sizeof(WCHAR); + Size = sizeof(Shell); rc = RegQueryValueExW(hKey, L"AlternateShell", NULL, &Type, (LPBYTE)Shell, &Size); - if(rc == ERROR_SUCCESS) + RegCloseKey(hKey); + + if (rc == ERROR_SUCCESS) { - RegCloseKey(hKey); if ((Type == REG_SZ) || (Type == REG_EXPAND_SZ)) { TRACE("Key located - %s\n", debugstr_w(Shell)); + /* Try to run alternate shell */ if (TryToStartShell(Shell)) { TRACE("Alternate shell started (Safe Mode)\n"); - return; + return TRUE; } } else @@ -335,41 +341,45 @@ VOID StartShell(VOID) } } } + /* Try to run shell in user key */ if (GetShell(Shell, HKEY_CURRENT_USER) && TryToStartShell(Shell)) { TRACE("Started shell from HKEY_CURRENT_USER\n"); - return; + return TRUE; } /* Try to run shell in local machine key */ if (GetShell(Shell, HKEY_LOCAL_MACHINE) && TryToStartShell(Shell)) { TRACE("Started shell from HKEY_LOCAL_MACHINE\n"); - return; + return TRUE; } /* Try default shell */ if (IsConsoleShell()) { - if (GetSystemDirectory(Shell, MAX_PATH - 8)) + if (GetSystemDirectoryW(Shell, ARRAYSIZE(Shell) - 8)) wcscat(Shell, L"\\cmd.exe"); else wcscpy(Shell, L"cmd.exe"); } else { - if (GetWindowsDirectory(Shell, MAX_PATH - 13)) + if (GetWindowsDirectoryW(Shell, ARRAYSIZE(Shell) - 13)) wcscat(Shell, L"\\explorer.exe"); else wcscpy(Shell, L"explorer.exe"); } + if (!TryToStartShell(Shell)) { WARN("Failed to start default shell %s\n", debugstr_w(Shell)); - LoadString( GetModuleHandle(NULL), IDS_SHELL_FAIL, szMsg, sizeof(szMsg) / sizeof(szMsg[0])); - MessageBox(0, szMsg, NULL, 0); + LoadString( GetModuleHandle(NULL), IDS_SHELL_FAIL, szMsg, ARRAYSIZE(szMsg)); + MessageBox(NULL, szMsg, NULL, MB_OK); + return FALSE; } + return TRUE; } const WCHAR g_RegColorNames[][32] = { @@ -405,10 +415,9 @@ const WCHAR g_RegColorNames[][32] = { L"MenuHilight", /* 29 = COLOR_MENUHILIGHT */ L"MenuBar" /* 30 = COLOR_MENUBAR */ }; -#define NUM_SYSCOLORS (sizeof(g_RegColorNames) / sizeof(g_RegColorNames[0])) -static -COLORREF StrToColorref( +static COLORREF +StrToColorref( IN LPWSTR lpszCol) { BYTE rgb[3]; @@ -421,8 +430,8 @@ COLORREF StrToColorref( return RGB(rgb[0], rgb[1], rgb[2]); } -static -VOID SetUserSysColors(VOID) +static VOID +SetUserSysColors(VOID) { HKEY hKey; INT i; @@ -433,32 +442,36 @@ VOID SetUserSysColors(VOID) TRACE("()\n"); - rc = RegOpenKeyEx(HKEY_CURRENT_USER, REGSTR_PATH_COLORS, - 0, KEY_QUERY_VALUE, &hKey); + rc = RegOpenKeyExW(HKEY_CURRENT_USER, REGSTR_PATH_COLORS, + 0, KEY_QUERY_VALUE, &hKey); if (rc != ERROR_SUCCESS) { WARN("RegOpenKeyEx() failed with error %lu\n", rc); return; } - for(i = 0; i < NUM_SYSCOLORS; i++) + + for (i = 0; i < ARRAYSIZE(g_RegColorNames); i++) { Size = sizeof(szColor); - rc = RegQueryValueEx(hKey, g_RegColorNames[i], NULL, &Type, - (LPBYTE)szColor, &Size); + rc = RegQueryValueExW(hKey, g_RegColorNames[i], NULL, &Type, + (LPBYTE)szColor, &Size); if (rc == ERROR_SUCCESS && Type == REG_SZ) { crColor = StrToColorref(szColor); SetSysColors(1, &i, &crColor); } else + { WARN("RegQueryValueEx(%s) failed with error %lu\n", debugstr_w(g_RegColorNames[i]), rc); + } } + RegCloseKey(hKey); } -static -VOID SetUserWallpaper(VOID) +static VOID +SetUserWallpaper(VOID) { HKEY hKey; DWORD Type, Size; @@ -467,39 +480,41 @@ VOID SetUserWallpaper(VOID) TRACE("()\n"); - rc = RegOpenKeyEx(HKEY_CURRENT_USER, REGSTR_PATH_DESKTOP, - 0, KEY_QUERY_VALUE, &hKey); - if (rc == ERROR_SUCCESS) + rc = RegOpenKeyExW(HKEY_CURRENT_USER, REGSTR_PATH_DESKTOP, + 0, KEY_QUERY_VALUE, &hKey); + if (rc != ERROR_SUCCESS) { - Size = sizeof(szWallpaper); - rc = RegQueryValueEx(hKey, - L"Wallpaper", - NULL, - &Type, - (LPBYTE)szWallpaper, - &Size); - if (rc == ERROR_SUCCESS && Type == REG_SZ) - { - ExpandEnvironmentStrings(szWallpaper, szWallpaper, MAX_PATH); - TRACE("Using wallpaper %s\n", debugstr_w(szWallpaper)); + WARN("RegOpenKeyEx() failed with error %lu\n", rc); + return; + } - /* Load and change the wallpaper */ - SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, szWallpaper, SPIF_SENDCHANGE); - } - else - { - /* remove the wallpaper */ - TRACE("No wallpaper set in registry (error %lu)\n", rc); - SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, NULL, SPIF_SENDCHANGE); - } - RegCloseKey(hKey); + Size = sizeof(szWallpaper); + rc = RegQueryValueExW(hKey, + L"Wallpaper", + NULL, + &Type, + (LPBYTE)szWallpaper, + &Size); + RegCloseKey(hKey); + + if (rc == ERROR_SUCCESS && Type == REG_SZ) + { + ExpandEnvironmentStringsW(szWallpaper, szWallpaper, ARRAYSIZE(szWallpaper)); + TRACE("Using wallpaper %s\n", debugstr_w(szWallpaper)); + + /* Load and change the wallpaper */ + SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, szWallpaper, SPIF_SENDCHANGE); } else - WARN("RegOpenKeyEx() failed with error %lu\n", rc); + { + /* Remove the wallpaper */ + TRACE("No wallpaper set in registry (error %lu)\n", rc); + SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, NULL, SPIF_SENDCHANGE); + } } -static -VOID SetUserSettings(VOID) +static VOID +SetUserSettings(VOID) { TRACE("()\n"); @@ -518,29 +533,29 @@ NotifyLogon(VOID) TRACE("()\n"); - hModule = LoadLibrary(L"setupapi.dll"); - if (hModule) + hModule = LoadLibraryW(L"setupapi.dll"); + if (!hModule) { - CMP_Report_LogOn = (PCMP_REPORT_LOGON)GetProcAddress(hModule, "CMP_Report_LogOn"); - if (CMP_Report_LogOn) - CMP_Report_LogOn(CMP_MAGIC, GetCurrentProcessId()); - else - WARN("GetProcAddress() failed\n"); - - FreeLibrary(hModule); + WARN("LoadLibrary() failed with error %lu\n", GetLastError()); + return; } + + CMP_Report_LogOn = (PCMP_REPORT_LOGON)GetProcAddress(hModule, "CMP_Report_LogOn"); + if (CMP_Report_LogOn) + CMP_Report_LogOn(CMP_MAGIC, GetCurrentProcessId()); else - WARN("LoadLibrary() failed with error %lu\n", GetLastError()); + WARN("GetProcAddress() failed\n"); + + FreeLibrary(hModule); } -static -VOID +static BOOL StartInstaller(VOID) { WCHAR Shell[MAX_PATH]; WCHAR szMsg[RC_STRING_MAX_SIZE]; - if (GetWindowsDirectory(Shell, MAX_PATH - 12)) + if (GetWindowsDirectoryW(Shell, ARRAYSIZE(Shell) - 12)) wcscat(Shell, L"\\reactos.exe"); else wcscpy(Shell, L"reactos.exe"); @@ -548,16 +563,14 @@ StartInstaller(VOID) if (!TryToStartShell(Shell)) { ERR("Failed to start the installer: %s\n", debugstr_w(Shell)); - LoadStringW(GetModuleHandle(NULL), IDS_INSTALLER_FAIL, szMsg, sizeof(szMsg) / sizeof(szMsg[0])); - MessageBoxW(0, szMsg, NULL, 0); + LoadStringW(GetModuleHandle(NULL), IDS_INSTALLER_FAIL, szMsg, ARRAYSIZE(szMsg)); + MessageBoxW(NULL, szMsg, NULL, MB_OK); + return FALSE; } + return TRUE; } -#ifdef _MSC_VER -#pragma warning(disable : 4100) -#endif /* _MSC_VER */ - int WINAPI wWinMain(IN HINSTANCE hInst, IN HINSTANCE hPrevInstance,