gpt4 book ai didi

c# - 使用任务管理器终止程序时收到的消息

转载 作者:太空狗 更新时间:2023-10-29 23:33:21 24 4
gpt4 key购买 nike

所以我有一个 C++ dll,我在我的 c# 应用程序中使用它来监视 Windows 消息。我想知道是否发送了 WM_CLOSE 和 WM_QUERYENDSESSION,因为我无法从 C# 应用程序中看到它们。如果我收到这些消息之一,我想对我的文件做一些清理,但问题是当我用 TM 杀死它时,功能不起作用。看来我没有收到消息。我认为问题在于任务管理器将消息发送到 C# 应用程序而不是 c++ dll。

一些代码:

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();

最佳答案

如果进程被终止,您的进程将不会收到任何消息。开始 TerminateProcess - 查看我的亮点:

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...

编辑:Hans Passant 对任务终止方式的评论 - 使用任务管理器中的“应用程序”选项卡时,您只会收到 WM_CLOSE。从进程选项卡中终止它是一种粗鲁的终止 (TerminateProcess)。

关于c# - 使用任务管理器终止程序时收到的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10706344/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com