歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C++調用可執行程序

C++調用可執行程序

日期:2017/3/1 10:32:53   编辑:Linux編程
1.WinExeC

WinExec(strPath.c_str(), SW_SHOW) ;

關閉:

HWND dc = FindWindow(0, "無標題 - 記事本 ");
CloseWindow(dc);
如果不行的話,按ctr+del+alt看記事本的標題是什麼??改了它

2.

HINSTANCE ShellExecute(HWND hwnd, LPCTSTR lpOperation, LPCTSTR lpFile, LPCTSTR lpParameters, LPCTSTR lpDirectory, INT nShowCmd);

下面是影藏打開 www.88181.com,然後關閉

[cpp]
  1. //聲明結構體
  2. SHELLEXECUTEINFO ShExecInfo;
  3. //創建
  4. void CBaseDialogDlg::OnButton1()
  5. {
  6. ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
  7. ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS ;
  8. ShExecInfo.hwnd = NULL;
  9. ShExecInfo.lpVerb = NULL;
  10. ShExecInfo.lpFile = "calc.exe"; //can be a file as well
  11. ShExecInfo.lpParameters = "";
  12. ShExecInfo.lpDirectory = NULL;
  13. ShExecInfo.nShow = SW_SHOW;
  14. ShExecInfo.hInstApp = NULL;
  15. ShellExecuteEx(&ShExecInfo);
  16. }
  17. //關閉
  18. void CBaseDialogDlg::OnButton2()
  19. {
  20. if( ShExecInfo.hProcess != NULL)
  21. {
  22. TerminateProcess(ShExecInfo.hProcess,0);
  23. ShExecInfo.hProcess = NULL;
  24. }
  25. }

3.

CreateProcess

下面是用CreateProcess打開一個進程,然後關閉那個進程

[cpp]
  1. static DWORD dwProcessId=0; //你啟動的另一個程序的進程ID
  2. void __fastcall TForm1::Button1Click(TObject *Sender)
  3. {
  4. STARTUPINFO si;
  5. PROCESS_INFORMATION pi;
  6. memset(&si, 0, sizeof(si));
  7. si.cb = sizeof(STARTUPINFO);
  8. if (CreateProcess(NULL, "notepad.exe ",NULL,
  9. NULL,FALSE,0,NULL,NULL,&si,&pi)) {
  10. dwProcessId = pi.dwProcessId;
  11. CloseHandle(pi.hProcess);
  12. CloseHandle(pi.hThread);
  13. }
  14. }
  15. void __fastcall TForm1::Button2Click(TObject *Sender)
  16. {
  17. HANDLE hProcess;
  18. hProcess = OpenProcess(PROCESS_ALL_ACCESS,
  19. FALSE, dwProcessId);
  20. if (hProcess != NULL) {
  21. TerminateProcess(hProcess, 0);
  22. CloseHandle(hProcess);
  23. }
  24. }
Copyright © Linux教程網 All Rights Reserved