【问题标题】:SendMessage to close hidden windowSendMessage 关闭隐藏窗口
【发布时间】:2018-09-04 04:56:06
【问题描述】:

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

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# 中阻止它?
  • 您是否尝试关闭整个应用程序的窗口?如果其他应用程序也是您的,您为什么不为此使用 uae IPC?
  • SendMessage 是这里的绝对要求吗?
  • @AlexF 我想我记得当应用程序被隐藏时窗口句柄总是为 0,但也许不是这样。
  • 不,我开始了这个过程。 SendMessage 不是绝对要求。是的,我可以使用 IPC,但我试图看到一种“更简单”的方法。

标签: c# sendmessage


【解决方案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);
}  

【讨论】:

  • 我得到了句柄,但不,它不起作用。我正在创建一个 HWND_MESSAGE 窗口
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-30
  • 1970-01-01
  • 2012-11-02
  • 2012-10-14
  • 2014-08-13
  • 1970-01-01
相关资源
最近更新 更多