将WM_CLOSE发送到C#应用程序工作意外

时间:2013-02-03 22:14:04

标签: c# c++ winapi sendmessage

使用WM_CLOSE消息关闭C#应用程序时,我遇到了意外行为。在我的场景中,我需要确保在其MSI安装包运行时关闭C#应用程序。 MSI对此没有适当的操作,因此我编写了一个自定义动作DLL,它嵌入到MSI包中并公开了EnsureApplicationClosed函数。以下DLL的相关代码:

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include "TlHelp32.h"

std::vector<DWORD> processesList;

//This method is the main point of interest
BOOL CALLBACK enumWindowsProc(__in HWND hWnd, __in LPARAM lParam)
{
    if(processesList.size()==0) return FALSE;
    DWORD processId;
    GetWindowThreadProcessId(hWnd, &processId);
    int index=0;
    while(index<processesList.size())
    {
        if(processesList.at(index)==processId)
        {
            //Remove process id from the list
            processesList.erase(processesList.begin()+index);
            //Should close main windows of the process found.
            SendMessage(hWnd, WM_CLOSE, (LPARAM)0, (WPARAM)0);
        }
        else
        {
            index++;
        }
    }
    return TRUE;
}
std::vector<DWORD> FindProcessesId(const LPCWSTR processName)
{
    std::vector<DWORD> result;

    PROCESSENTRY32 processInfo;
    processInfo.dwSize = sizeof(processInfo);

    HANDLE processesSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
    if ( processesSnapshot == INVALID_HANDLE_VALUE ) return result;

    Process32First(processesSnapshot, &processInfo);
    if (wcscmp(processName, processInfo.szExeFile)==0)
    {
        result.push_back(processInfo.th32ProcessID);
    }

    while ( Process32Next(processesSnapshot, &processInfo) )
    {
        if ( wcscmp(processName, processInfo.szExeFile)==0 )
        {
            result.push_back(processInfo.th32ProcessID);
        }
    }

    CloseHandle(processesSnapshot);
    return result;
}
void InnerEnsureApplicationClosed(const LPCWSTR processName)
{
    processesList=FindProcessesId(processName);
    BOOL enumeratingWindowsSucceeded = ::EnumWindows( enumWindowsProc, NULL );
}
//Function exported by the DLL
UINT __stdcall EnsureApplicationClosed ( MSIHANDLE hModule )
{
    InnerEnsureApplicationClosed(L"SomeApplication.exe");
    return ERROR_SUCCESS;
}

我正在使用简单的控制台应用来测试这个dll:

#include "stdafx.h"
#include <SomeApplicationCustomActions.h>

int _tmain(int argc, _TCHAR* argv[])
{
    EnsureApplicationClosed(NULL);
    return 0;
}

C#应用程序对于用户大多数时间是不可见的,因为它应该只显示来自第三方应用程序的通知。这就是为什么它有自定义FormClosing处理程序:

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
   Console.Beep();
   if (e.CloseReason != CloseReason.WindowsShutDown && e.CloseReason!= CloseReason.TaskManagerClosing)
   {
       e.Cancel = true;
       this.Hide();
   }
}

我正在使用SendMessage(hWnd,WM_CLOSE,(LPARAM)0,(WPARAM)0)来关闭C#应用程序。检查TaskManagerClosing关闭原因需要在收到此WM_CLOSE消息时关闭,因为我发现SendMessage(hWnd,WM_CLOSE,(LPARAM)0,(WPARAM)0)在C#应用程序中产生TaskManagerClosing关闭原因。 Console.Beep()用于测试以产生声音信号,因此我可以听到输入的处理程序代码。

这个问题只有在我第二次运行DLL测试应用程序时才有效。当我第一次运行测试应用程序时,我没有听到声音信号和C#进程保留在任务管理器中。因此C#应用程序的FormClosing事件不会被触发。我试图用PostMessage替换SendMessage但没有成功。在两种情况下,GetLastError()始终返回18。有没有人遇到同样的问题,知道如何解决它?

提前致谢。

1 个答案:

答案 0 :(得分:2)

在玩了一整天后,我发现了这个问题发生的原因。自定义动作DLL没问题。问题来自.NET ListView控件。如果表单的Visible属性在Shown事件处理程序中设置为false,并且表单上有ListView控件,则它将不受第一次从另一个应用程序发送的WM_CLOSE消息的影响。 WM_CLOSE只是没有出现在WndProc方法中。我已经通过覆盖表单的SetVisibleCore修复了它,但无论如何这个问题看起来很奇怪,并且似乎是.NET中的一个错误。