[XDK][MSGINA]: Consistency codestyle fixes.

[SHELL32]
- Use explicit unicode functions;
- Use _countof instead of sizeof(foo)/sizeof(foo[0]);
- Enable the shutdown privilege when we need it, and disable as soon as possible (code taken from taskmgr).

svn path=/trunk/; revision=72719
This commit is contained in:
Hermès Bélusca-Maïto 2016-09-17 22:39:54 +00:00
parent b5fc396ab8
commit a1895a9da6
3 changed files with 67 additions and 65 deletions

View file

@ -13,7 +13,7 @@
13 stub -noname ShellStatusHostShuttingDown ; ()
14 stub -noname ShellNotifyThemeUserChange ; (long long)
15 stub -noname ShellSwitchWhenInteractiveReady ; (long long)
16 stdcall -noname ShellDimScreen (ptr ptr)
16 stdcall -noname ShellDimScreen(ptr ptr)
17 stub -noname ShellInstallAccountFilterData ; ()
18 stub -noname ShellStatusHostBegin ; (long)
19 stub -noname ShellIsUserInteractiveLogonAllowed ; (long)

View file

@ -802,15 +802,15 @@ Cleanup:
*/
static BOOL ConfirmDialog(HWND hWndOwner, UINT PromptId, UINT TitleId)
{
WCHAR Prompt[256];
WCHAR Title[256];
WCHAR Prompt[256];
WCHAR Title[256];
LoadStringW(shell32_hInstance, PromptId, Prompt, sizeof(Prompt) / sizeof(WCHAR));
LoadStringW(shell32_hInstance, TitleId, Title, sizeof(Title) / sizeof(WCHAR));
return MessageBoxW(hWndOwner, Prompt, Title, MB_YESNO|MB_ICONQUESTION) == IDYES;
LoadStringW(shell32_hInstance, PromptId, Prompt, _countof(Prompt));
LoadStringW(shell32_hInstance, TitleId, Title, _countof(Title));
return MessageBoxW(hWndOwner, Prompt, Title, MB_YESNO | MB_ICONQUESTION) == IDYES;
}
typedef HRESULT (__stdcall *tShellDimScreen) (IUnknown** Unknown, HWND* hWindow);
typedef HRESULT (WINAPI *tShellDimScreen)(IUnknown** Unknown, HWND* hWindow);
BOOL
CallShellDimScreen(IUnknown** pUnknown, HWND* hWindow)
@ -819,8 +819,8 @@ CallShellDimScreen(IUnknown** pUnknown, HWND* hWindow)
static BOOL Initialized = FALSE;
if (!Initialized)
{
HMODULE mod = LoadLibrary(TEXT("msgina.dll"));
ShellDimScreen = (tShellDimScreen)GetProcAddress(mod, MAKEINTRESOURCEA(16));
HMODULE mod = LoadLibraryW(L"msgina.dll");
ShellDimScreen = (tShellDimScreen)GetProcAddress(mod, (LPCSTR)16);
Initialized = TRUE;
}
@ -831,6 +831,33 @@ CallShellDimScreen(IUnknown** pUnknown, HWND* hWindow)
}
/* Used to get the shutdown privilege */
static BOOL
EnablePrivilege(LPCWSTR lpszPrivilegeName, BOOL bEnablePrivilege)
{
BOOL Success;
HANDLE hToken;
TOKEN_PRIVILEGES tp;
Success = OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES,
&hToken);
if (!Success) return Success;
Success = LookupPrivilegeValueW(NULL,
lpszPrivilegeName,
&tp.Privileges[0].Luid);
if (!Success) goto Quit;
tp.PrivilegeCount = 1;
tp.Privileges[0].Attributes = (bEnablePrivilege ? SE_PRIVILEGE_ENABLED : 0);
Success = AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL);
Quit:
CloseHandle(hToken);
return Success;
}
/*************************************************************************
* RestartDialogEx [SHELL32.730]
@ -842,25 +869,16 @@ int WINAPI RestartDialogEx(HWND hWndOwner, LPCWSTR lpwstrReason, DWORD uFlags, D
CComPtr<IUnknown> fadeHandler;
HWND parent;
if (!CallShellDimScreen(&fadeHandler, &parent))
parent = hWndOwner;
/* FIXME: use lpwstrReason */
if (ConfirmDialog(parent, IDS_RESTART_PROMPT, IDS_RESTART_TITLE))
{
HANDLE hToken;
TOKEN_PRIVILEGES npr;
/* enable the shutdown privilege for the current process */
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
{
LookupPrivilegeValueA(0, "SeShutdownPrivilege", &npr.Privileges[0].Luid);
npr.PrivilegeCount = 1;
npr.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &npr, 0, 0, 0);
CloseHandle(hToken);
}
EnablePrivilege(L"SeShutdownPrivilege", TRUE);
ExitWindowsEx(EWX_REBOOT, uReason);
EnablePrivilege(L"SeShutdownPrivilege", FALSE);
}
return 0;
@ -870,21 +888,20 @@ int WINAPI RestartDialogEx(HWND hWndOwner, LPCWSTR lpwstrReason, DWORD uFlags, D
* LogOffDialogProc
*
* NOTES: Used to make the Log Off dialog work
*
*/
INT_PTR CALLBACK LogOffDialogProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
INT_PTR CALLBACK LogOffDialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(Message)
switch (uMsg)
{
case WM_INITDIALOG:
{
return TRUE;
}
case WM_CLOSE:
EndDialog(hwnd, IDCANCEL);
break;
case WM_COMMAND:
switch(LOWORD(wParam))
switch (LOWORD(wParam))
{
case IDOK:
ExitWindowsEx(EWX_LOGOFF, 0);
@ -894,13 +911,13 @@ INT_PTR CALLBACK LogOffDialogProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM
break;
}
break;
default:
break;
}
return FALSE;
}
/*************************************************************************
* LogoffWindowsDialog [SHELL32.54]
*/
@ -909,14 +926,14 @@ EXTERN_C int WINAPI LogoffWindowsDialog(HWND hWndOwner)
{
CComPtr<IUnknown> fadeHandler;
HWND parent;
if (!CallShellDimScreen(&fadeHandler, &parent))
parent = hWndOwner;
DialogBox(shell32_hInstance, MAKEINTRESOURCE(IDD_LOG_OFF), parent, LogOffDialogProc);
DialogBoxW(shell32_hInstance, MAKEINTRESOURCEW(IDD_LOG_OFF), parent, LogOffDialogProc);
return 0;
}
/*************************************************************************
* RestartDialog [SHELL32.59]
*/
@ -926,27 +943,6 @@ int WINAPI RestartDialog(HWND hWndOwner, LPCWSTR lpstrReason, DWORD uFlags)
return RestartDialogEx(hWndOwner, lpstrReason, uFlags, 0);
}
/*************************************************************************
* Used to get the shutdown privilege
*/
VOID ExitWindows_GetShutdownPrivilege(VOID)
{
HANDLE hToken;
TOKEN_PRIVILEGES npr;
/* enable shut down privilege for current process */
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
{
LookupPrivilegeValueA(0, "SeShutdownPrivilege", &npr.Privileges[0].Luid);
npr.PrivilegeCount = 1;
npr.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &npr, 0, 0, 0);
CloseHandle(hToken);
}
}
/*************************************************************************
* ExitWindowsDialog_backup
*
@ -960,8 +956,9 @@ VOID ExitWindowsDialog_backup(HWND hWndOwner)
if (ConfirmDialog(hWndOwner, IDS_SHUTDOWN_PROMPT, IDS_SHUTDOWN_TITLE))
{
ExitWindows_GetShutdownPrivilege();
EnablePrivilege(L"SeShutdownPrivilege", TRUE);
ExitWindowsEx(EWX_SHUTDOWN, 0);
EnablePrivilege(L"SeShutdownPrivilege", FALSE);
}
}
@ -979,7 +976,7 @@ VOID ExitWindowsDialog_backup(HWND hWndOwner)
void WINAPI ExitWindowsDialog(HWND hWndOwner)
{
typedef DWORD (WINAPI *ShellShFunc)(HWND hParent, WCHAR *Username, BOOL bHideLogoff);
HINSTANCE msginaDll = LoadLibraryA("msgina.dll");
HINSTANCE msginaDll = LoadLibraryW(L"msgina.dll");
TRACE("(%p)\n", hWndOwner);
@ -990,21 +987,21 @@ void WINAPI ExitWindowsDialog(HWND hWndOwner)
/* If the DLL cannot be found for any reason, then it simply uses a
dialog box to ask if the user wants to shut down the computer. */
if(!msginaDll)
if (!msginaDll)
{
TRACE("Unable to load msgina.dll.\n");
ExitWindowsDialog_backup(parent);
return;
}
ShellShFunc pShellShutdownDialog = (ShellShFunc) GetProcAddress(msginaDll, "ShellShutdownDialog");
ShellShFunc pShellShutdownDialog = (ShellShFunc)GetProcAddress(msginaDll, "ShellShutdownDialog");
if(pShellShutdownDialog)
if (pShellShutdownDialog)
{
/* Actually call the function */
DWORD returnValue = pShellShutdownDialog(parent, NULL, FALSE);
switch(returnValue)
switch (returnValue)
{
case 0x01: /* Log off user */
{
@ -1013,8 +1010,9 @@ void WINAPI ExitWindowsDialog(HWND hWndOwner)
}
case 0x02: /* Shut down */
{
ExitWindows_GetShutdownPrivilege();
EnablePrivilege(L"SeShutdownPrivilege", TRUE);
ExitWindowsEx(EWX_SHUTDOWN, 0);
EnablePrivilege(L"SeShutdownPrivilege", FALSE);
break;
}
case 0x03: /* Install Updates/Shutdown (?) */
@ -1023,25 +1021,28 @@ void WINAPI ExitWindowsDialog(HWND hWndOwner)
}
case 0x04: /* Reboot */
{
ExitWindows_GetShutdownPrivilege();
EnablePrivilege(L"SeShutdownPrivilege", TRUE);
ExitWindowsEx(EWX_REBOOT, 0);
EnablePrivilege(L"SeShutdownPrivilege", FALSE);
break;
}
case 0x10: /* Sleep */
{
if(IsPwrSuspendAllowed())
if (IsPwrSuspendAllowed())
{
ExitWindows_GetShutdownPrivilege();
EnablePrivilege(L"SeShutdownPrivilege", TRUE);
SetSuspendState(FALSE, FALSE, FALSE);
EnablePrivilege(L"SeShutdownPrivilege", FALSE);
}
break;
}
case 0x40: /* Hibernate */
{
if(IsPwrHibernateAllowed())
if (IsPwrHibernateAllowed())
{
ExitWindows_GetShutdownPrivilege();
EnablePrivilege(L"SeShutdownPrivilege", TRUE);
SetSuspendState(TRUE, FALSE, TRUE);
EnablePrivilege(L"SeShutdownPrivilege", FALSE);
}
break;
}
@ -1054,7 +1055,8 @@ void WINAPI ExitWindowsDialog(HWND hWndOwner)
{
/* If the function cannot be found, then revert to using the backup solution */
TRACE("Unable to find the 'ShellShutdownDialog' function");
FreeLibrary(msginaDll);
ExitWindowsDialog_backup(parent);
}
FreeLibrary(msginaDll);
}

View file

@ -61,7 +61,7 @@ typedef struct _ACL {
/* Privilege attributes */
#define SE_PRIVILEGE_ENABLED_BY_DEFAULT (0x00000001L)
#define SE_PRIVILEGE_ENABLED (0x00000002L)
#define SE_PRIVILEGE_REMOVED (0X00000004L)
#define SE_PRIVILEGE_REMOVED (0x00000004L)
#define SE_PRIVILEGE_USED_FOR_ACCESS (0x80000000L)
#define SE_PRIVILEGE_VALID_ATTRIBUTES (SE_PRIVILEGE_ENABLED_BY_DEFAULT | \