SendMessage关闭隐藏窗口

问题描述 投票:1回答:1

我试图通过sendMessage关闭一个隐藏的窗口程序,但它不起作用,因为mainwindowhandlele为零。我尝试了另一个手柄,但它也不起作用

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);
        }
    }
c# sendmessage
1个回答
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);
}  
© www.soinside.com 2019 - 2024. All rights reserved.