exe嵌入到自己的程序中

来源:互联网 发布:多语言翻译软件 编辑:程序博客网 时间:2024/05/20 06:27

思路:创建进程的时候,通过enumwindos获得句柄,movewindow到自己窗口中。

 

在codeproject的基础和csdn的基础上进行了修改

 

 

static HANDLE g_hEvent=NULL;//event handlestatic HANDLE handle;//process handlestatic HWND 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 idif (TpID == (DWORD)param){apphwnd=hwnd;//hwnd is the window handleSetEvent(g_hEvent);return false;}ResetEvent(g_hEvent);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 securityNULL,  // thread securityFALSE, // no inheritance0,     // no startup flagsNULL,  // no special environmentNULL,  // default startup directory&startupInfo,&processInfo)){ /* success */WaitForInputIdle(processInfo.hProcess,-1);::EnumWindows(&EnumWindowsProc, processInfo.dwThreadId);//Iterate all windowshProcess = processInfo.hProcess;} /* success */return hProcess;}/**********************************************************///handle for host menuvoid CHostMSPaintDlg::OnActionHostmspaint() {CRect rect;DWORD nRet=-1;g_hEvent=CreateEvent(NULL,FALSE,FALSE,NULL);GetClientRect(&rect);//get our dialog size into recthandle=StartProcess(NULL,"C:\\Program Files\\Microsoft Office\\Office12\\WINWORD.EXEd:\1.doc");//Start ms paintnRet=WaitForSingleObject(g_hEvent,5000);//waited the target window for 2000 msCloseHandle(g_hEvent);if(nRet!=WAIT_OBJECT_0){::MessageBox(NULL,_T("启动word,超时!"),_T("错误提示"),MB_ICONEXCLAMATION);SendMessage(WM_CLOSE,0,0);return;}if(apphwnd!=NULL)//check for window handle{::SetParent(apphwnd,m_hWnd);//set parent of ms paint to our dialog.SetWindowLong(apphwnd, GWL_STYLE, WS_VISIBLE);//eraze title of ms paint window.//Positioning ms paint.::MoveWindow(apphwnd, rect.left, rect.top,rect.right, rect.bottom, true);::ShowWindow(apphwnd,SW_SHOW);}else//no window for our processMessageBox("Cannot find Window");}//handle for kill process menu.void CHostMSPaintDlg::OnActionKillprocess() {TerminateProcess(handle,0);//kill the process using handle}