如下: /****************************** * 创建并起启线程 * ******************************/ BOOL StartThread(LPTHREAD_START_ROUTINE ThreadFunc, THREAD_DATA *td) { ULONG tid; HANDLE hThread; // Create and start thread hThread = CreateThread(NULL, 0, ThreadFunc, td, 0, &tid); if (!hThread) return FALSE; // Wait until thread terminates WaitForSingleObject(hThread, INFINITE); CloseHandle(hThread); return TRUE; } /************************************************ * 创建一个新虚拟桌面,并运行一个新线程 * * (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; } /*************************************** * 在新虚拟桌面启动线程* ***************************************/ DWORD WINAPI MyThread(LPVOID lpParameter) { SetThreadDesktop(((MY_THREAD_DATA *)lpParameter)->hDesk); MessageBox(NULL, ((MY_THREAD_DATA *)lpParameter)->szMsg, "Desktop", MB_OK); return 0; } //调用函数 strcpy(td.szDesktopName, DESKTOPNAME); strcpy(td.szMsg, "Message from new desktop !"); Thread_Desktop(MyThread, (THREAD_DATA *)&td);