Preface:No system has learned Win32 and is often surprised by the Shell Effect of windows. I have always wanted to achieve this kind of game hall effect, but I cannot go deep. Yesterday, I had the chance to perform a test. After three hours, I finally figured out the truth and listed it as follows to honor the same person.Results:HANDLE handle;//process handleHWND apphwnd;//window handle/*************Global functions for hosting******************///Function to enumerate all windows.int CALLBACK EnumWindowsProc(HWND hwnd, LPARAM param){ DWORD pID; DWORD TpID = GetWindowThreadProcessId(hwnd, &pID);//get process id if (TpID == (DWORD)param) { apphwnd=hwnd;//hwnd is the window handle return false; } return true;}//Functio to start a orocess and return the process handleHANDLE StartProcess(LPCTSTR program, LPCTSTR args){ HANDLE hProcess = NULL; PROCESS_INFORMATION processInfo; STARTUPINFO startupInfo; ::ZeroMemory(&startupInfo, sizeof(startupInfo)); startupInfo.cb = sizeof(startupInfo); startupInfo.dwFlags = STARTF_USESHOWWINDOW; startupInfo.wShowWindow = SW_HIDE; if(::CreateProcess(program, (LPTSTR)args, NULL, // process security NULL, // thread security FALSE, // no inheritance 0, // no startup flags NULL, // no special environment NULL, // default startup directory &startupInfo, &processInfo)) { Sleep(1000); ::EnumWindows(&EnumWindowsProc, processInfo.dwThreadId);//Iterate all windows hProcess = processInfo.hProcess; } return hProcess;}void CXXXXXDlg::OnButton1() { CRect rect; GetClientRect(&rect); handle=StartProcess("C:\\WINDOWS\\system32\\mspaint.exe",""); if(apphwnd!=NULL) { ::SetParent(apphwnd,m_hWnd); SetWindowLong(apphwnd, GWL_STYLE, WS_VISIBLE); ::MoveWindow(apphwnd, rect.left, rect.top,rect.right, rect.bottom, true); } else { MessageBox("Cannot find Window"); }}
Intermediate process:
1. At that time, I had a special liking for ShellExecute. CreateProcess did not test the method of hiding it immediately after creation, so I hoped to use shellexecuteex for window retrieval.
In this way, you must obtain the window from the Process Handle obtained by shellexecuteex. After enumeration and traversal, you can find that the process ID is unique, but the handles opened each time are different.
Two hours later, I had to return to my thoughts and rethink how CreateProcess creates a hidden process. Soon I found that it could work.
2. the painful process is always painful. Only the painful person can gradually accumulate and reduce the pain and turn grief into joy.
Legacy problems:
If you encounter a program without a window handle, this method obviously won't work. What should we do?