我正在尝试通过sendMessage关闭一个隐藏的窗口程序,但是由于mainwindowhandle为零,所以它不起作用。我尝试了其他手柄,但也无法正常工作
Process p = new Process ();
p.StartupInfo.FileName = "HiddenWindowWithMessagePump.exe"
p.Start ();
[DllImport ("user32.dll")]
static extern IntPtr SendMessage (IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
// I tried both WM_CLOSE and WM_QUIT
const int WM_CLOSE = 0x0010;
const int WM_QUIT = 0x12;
// p.MainWindowHandle is 0
SendMessage (p.MainWindowHandle, WM_QUIT, IntPtr.Zero, IntPtr.Zero);
// does not work
SendMessage (p.Handle, WM_QUIT, IntPtr.Zero, IntPtr.Zero);
// this is different from p.Handle, but does not work too
IntPtr p2 = System.Diagnostics.Process.GetProcessById (p.Id).Handle;
隐藏的窗口程序具有消息泵
CreateWindowEx (NULL,L"X", // name of the window class
L"X", // title of the window
0,
300, // x-position of the window
300, // y-position of the window
500, // width of the window
400, // height of the window
HWND_MESSAGE,
NULL, // we aren't using menus, NULL
hInstance, // application handle
NULL); // used with multiple windows, NULL
while (true)
{
if (PeekMessage (&msg, 0, 0, 0, PM_REMOVE))
{
if (WM_QUIT == msg.message)
break;
TranslateMessage (&msg);
DispatchMessage (&msg);
}
}
答案 0 :(得分:1)
如果没有手柄,可以尝试找到窗户。尝试使用下面的代码
[DllImport("user32.dll")]
public static extern int FindWindow(string lpClassName,string lpWindowName);
int iHandle = FindWindow("Notepad", "Untitled - Notepad");
if (iHandle > 0)
{
SendMessage(iHandle, WM_SYSCOMMAND, SC_CLOSE, 0);
}