VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/src/Common/Dlgcode.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/Common/Dlgcode.c')
-rw-r--r--src/Common/Dlgcode.c41
1 files changed, 30 insertions, 11 deletions
diff --git a/src/Common/Dlgcode.c b/src/Common/Dlgcode.c
index 4f0c4af6..2e37a6a3 100644
--- a/src/Common/Dlgcode.c
+++ b/src/Common/Dlgcode.c
@@ -13211,15 +13211,32 @@ static DWORD WINAPI SecureDesktopThread(LPVOID lpThreadParameter)
unsigned int monitoringThreadID = 0;
SecureDesktopThreadParam* pParam = (SecureDesktopThreadParam*) lpThreadParameter;
SecureDesktopMonitoringThreadParam monitorParam;
+ HDESK hOriginalDesk = GetThreadDesktop (GetCurrentThreadId ());
+ BOOL bNewDesktopSet = FALSE;
+ int counter = 0;
- SetThreadDesktop (pParam->hDesk);
- SwitchDesktop (pParam->hDesk);
+ // wait for SwitchDesktop to succeed before using it for current thread
+ // we wait a maximum of 5 seconds
+ for (counter = 0; counter < 10; counter++)
+ {
+ if (SwitchDesktop (pParam->hDesk))
+ {
+ bNewDesktopSet = TRUE;
+ break;
+ }
+ Sleep (SECUREDESKTOP_MONOTIR_PERIOD);
+ }
+
+ if (bNewDesktopSet)
+ {
+ SetThreadDesktop (pParam->hDesk);
- // create the thread that will ensure that VeraCrypt secure desktop has always user input
- monitorParam.szVCDesktopName = pParam->szDesktopName;
- monitorParam.hVcDesktop = pParam->hDesk;
- monitorParam.pbStopMonitoring = &bStopMonitoring;
- hMonitoringThread = (HANDLE) _beginthreadex (NULL, 0, SecureDesktopMonitoringThread, (LPVOID) &monitorParam, 0, &monitoringThreadID);
+ // create the thread that will ensure that VeraCrypt secure desktop has always user input
+ monitorParam.szVCDesktopName = pParam->szDesktopName;
+ monitorParam.hVcDesktop = pParam->hDesk;
+ monitorParam.pbStopMonitoring = &bStopMonitoring;
+ hMonitoringThread = (HANDLE) _beginthreadex (NULL, 0, SecureDesktopMonitoringThread, (LPVOID) &monitorParam, 0, &monitoringThreadID);
+ }
pParam->retValue = DialogBoxParamW (pParam->hInstance, pParam->lpTemplateName,
NULL, pParam->lpDialogFunc, pParam->dwInitParam);
@@ -13232,6 +13249,12 @@ static DWORD WINAPI SecureDesktopThread(LPVOID lpThreadParameter)
CloseHandle (hMonitoringThread);
}
+ if (bNewDesktopSet)
+ {
+ SetThreadDesktop (hOriginalDesk);
+ SwitchDesktop (hOriginalDesk);
+ }
+
return 0;
}
@@ -13290,7 +13313,6 @@ INT_PTR SecureDesktopDialogBoxParam(
hSecureDesk = CreateDesktop (szDesktopName, NULL, NULL, 0, desktopAccess, NULL);
if (hSecureDesk)
{
- HDESK hOriginalDesk = GetThreadDesktop (GetCurrentThreadId ());
SecureDesktopThreadParam param;
param.hDesk = hSecureDesk;
@@ -13307,9 +13329,6 @@ INT_PTR SecureDesktopDialogBoxParam(
WaitForSingleObject (hThread, INFINITE);
CloseHandle (hThread);
- SwitchDesktop (hOriginalDesk);
- SetThreadDesktop (hOriginalDesk);
-
retValue = param.retValue;
bSuccess = TRUE;
}