什么消息我收到的时候我杀了我的程序与任务管理器 [英] What message I receive when I kill my program with task manager

查看:123
本文介绍了什么消息我收到的时候我杀了我的程序与任务管理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个C ++ DLL,我是用我的C#应用​​程序,用于监视Windows消息。我想知道,如果WM_CLOSE和WM_QUERYENDSESSION都送,因为我不能看到,从C#应用程序。如果我得到这些消息之一,我想要做一些清理我的文件,但问题是,当我TM杀了它的功能不工作。它接缝,我没有得到消息。我觉得问题是,任务管理器将消息发送到C#应用程序,而不是在C ++ DLL



一些代码:



C ++:

 无效的typedef(* CLOSING_FUNCTION)(); 
CLOSING_FUNCTION myClosingFunction;

无效的typedef(* SHUTDOWN_FUNCTION)();
SHUTDOWN_FUNCTION myShutdownFunction;

LRESULT CALLBACK的WndProc(HWND HWND,UINT的消息,WPARAM wParam参数,LPARAM lParam的)
{
开关(消息)
{

案例WM_CREATE:
返回0;

情况下WM_CLOSE:
myClosingFunction();
返回0;

情况下WM_QUERYENDSESSION:
myShutdownFunction();
返回1;

情况下WM_DESTROY:
myClosingFunction();
PostQuitMessage(0);
返回0;
}

返回DefWindowProc函数(HWND,消息的wParam,lParam的);
}



C#:

  [UnmanagedFunctionPointer(CallingConvention.Cdecl)] 
私人委托无效Close_Function();
私有静态Close_Function myCloseDelegate;

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
私人委托无效Shutdown_Function();
私有静态Shutdown_Function myShutdownDelegate;
静态无效StartMonotoring()
{
myCloseDelegate =关闭;
myShutdownDelegate =关机;

InterceptMessages(myCloseDelegate,myShutdownDelegate);
}

静态无效关闭();
静态无效关机();


解决方案

您进程不会得到,如果它的情况下,任何消息被杀害。开始在 TerminateProcess - 看到我的亮点:




TerminateProcess函数来无条件地使一个进程退出。如果TerminateProcess使用,而不是ExitProcess的全局数据的动态链接库(DLL)中所保存的状态可能会受到影响。



这功能的停止所有线程的执行在过程中并要求所有未决的I / O ...


取消

编辑:在途中任务汉斯帕桑特评论终止 - 使用任务管理器应用程序选项卡时,您只能得到WM_CLOSE。从进程选项卡杀死它是一个粗鲁的终端(TerminateProcess)。


So I have a C++ dll, that I am using in my c# application, for monitoring Windows Messages. I want to know if WM_CLOSE and WM_QUERYENDSESSION are send because I can't see that from a C# application. If I get one of these messages, I want to do some cleanup with my files but the problem is when I kill it with TM the functions don't work. It seams that I don't get the messages. I think the problem is that Task Manager sends a message to the C# app and not to the c++ dll.

Some Code:

c++:

typedef void (*CLOSING_FUNCTION)();
CLOSING_FUNCTION myClosingFunction;

typedef void (*SHUTDOWN_FUNCTION)();
SHUTDOWN_FUNCTION myShutdownFunction;

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {

    case WM_CREATE:
        return 0;

    case WM_CLOSE:
        myClosingFunction();
        return 0;

    case WM_QUERYENDSESSION:
        myShutdownFunction();
        return 1;

    case WM_DESTROY:
        myClosingFunction();
        PostQuitMessage(0);
        return 0;
    }

    return DefWindowProc(hwnd, message, wParam, lParam);
}

c#:

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate void Close_Function();
private static Close_Function myCloseDelegate;

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate void Shutdown_Function();
private static Shutdown_Function myShutdownDelegate;
static void StartMonotoring()
{
       myCloseDelegate = Close;
       myShutdownDelegate = Shutdown;

       InterceptMessages(myCloseDelegate, myShutdownDelegate);
}

static void Close();
static void Shutdown();

解决方案

Your process will not get any messages in case if it is being killed. Start on TerminateProcess - see my highlight:

The TerminateProcess function is used to unconditionally cause a process to exit. The state of global data maintained by dynamic-link libraries (DLLs) may be compromised if TerminateProcess is used rather than ExitProcess.

This function stops execution of all threads within the process and requests cancellation of all pending I/O...

EDIT: Hans Passant comment on way tasks terminated - You only get WM_CLOSE when using the Applications tab in task manager. Killing it from the Processes tab is a rude termination (TerminateProcess).

这篇关于什么消息我收到的时候我杀了我的程序与任务管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆