想想這種情況應該是不可能的,浏覽器在windows系統下運作可以不聽操作系統的命令?怎麼可能,只可能我獲得的東西不正確
void CtestDlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
STARTUPINFO suInfo;
PROCESS_INFORMATION procInfo;
memset (&suInfo, 0, sizeof(suInfo));
suInfo.cb = sizeof(suInfo);
suInfo.dwFlags = STARTF_USESHOWWINDOW;
suInfo.wShowWindow = TRUE;
CHAR chCmdLine[] = " http://www.baidu.com/";
HKEY key = NULL;
CHAR szXplorer[MAX_PATH];
DWORD dwBufLen;
DWORD type = REG_SZ;
ZeroMemory(szXplorer,MAX_PATH);
if(ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_CLASSES_ROOT,
"http\\shell\\open\\command",
0,
KEY_QUERY_VALUE,
&key))
{
LONG lRet;
lRet = ::RegQueryValueEx(key,NULL,NULL,&type,(LPBYTE)szXplorer,&dwBufLen);
if(lRet == ERROR_SUCCESS)
strcat(szXplorer, chCmdLine);
RegCloseKey(key);
}
BOOL bRet = CreateProcess(NULL/*"C://Program Files//360Chrome//Chrome//Application//360chrome.exe"*//*"c://program files//internet explorer//iexplore.exe"*/,
szXplorer, NULL, NULL, false,
NORMAL_PRIORITY_CLASS, NULL, NULL, &suInfo, &procInfo);
if(bRet)
{
CloseHandle(procInfo.hThread);
CloseHandle(procInfo.hProcess);
}
m_dwProcessId = procInfo.dwProcessId;
}
void CtestDlg::OnBnClickedButton2()
{
// TODO: Add your control notification handler code here
HANDLE handle = OpenProcess(SYNCHRONIZE | PROCESS_TERMINATE, FALSE, m_dwProcessId);
EnumWindows(EnumWindowsProc, m_dwProcessId);
CloseHandle(handle);
}
BOOL CALLBACK CtestDlg::EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
DWORD wndPid;
CString Title;
// lParam = procInfo.dwProcessId;
// This gets the windows handle and pid of enumerated window.
GetWindowThreadProcessId(hwnd, &wndPid);
// This gets the windows title text
// from the window, using the window handle
CWnd::FromHandle( hwnd )->GetWindowText(Title);
// this makes sure that the PID matches that PID we started, and window
// text exists, before we kill it . I don't think this is really needed,
// I included it because some apps have more than one window.
if ( wndPid == (DWORD)lParam && Title.GetLength() != 0)
{
// Please kindly close this process
::PostMessage(hwnd, WM_CLOSE, 0, 0);
return false;
}
else
{
// Keep enumerating
return true;
}
}
是不是沒取對句柄?用spy++看看。或者試試其他幾個關閉的消息WM_DESTROY ,WM_CLOSE,WM_QUIT。或者發送鼠標點擊消息,參數傳關閉按鈕的坐標。