【问题标题】:How to display a pop-up message box from a driver (kernel mode)?如何从驱动程序(内核模式)显示弹出消息框?
【发布时间】:2012-04-11 11:10:42
【问题描述】:

我正在编写一个驱动程序,它需要立即弹出一个对话框来通知用户事件。
(有点类似于 NTFS 的 “损坏文件” 通知,只是这不是与文件系统相关的驱动程序。)

我知道 ExRaiseHardErrorIoRaiseInformationalHardError 应该能够做到这一点,但它们似乎不起作用——它们“成功”返回而实际上没有做任何事情。

我该怎么做(创建用户模式程序)?


代码的用户模式版本(正常工作)如下。

在内核模式版本中,我调用 ExRaiseHardError 而不是 NtRaiseHardError,但方式完全相同。

#include <windows.h>

#pragma comment(lib, "ntdll.lib")    // Needs ntdll.lib from Windows Driver Kit

typedef enum HardErrorResponseType {
    ResponseTypeAbortRetryIgnore,
    ResponseTypeOK,
    ResponseTypeOKCancel,
    ResponseTypeRetryCancel,
    ResponseTypeYesNo,
    ResponseTypeYesNoCancel,
    ResponseTypeShutdownSystem,
    ResponseTypeTrayNotify,
    ResponseTypeCancelTryAgainContinue
} HardErrorResponseType;

typedef enum HardErrorResponse {
    ResponseReturnToCaller,
    ResponseNotHandled,
    ResponseAbort, ResponseCancel,
    ResponseIgnore,
    ResponseNo,
    ResponseOk,
    ResponseRetry,
    ResponseYes
} HardErrorResponse;

typedef enum HardErrorResponseButton {
    ResponseButtonOK,
    ResponseButtonOKCancel,
    ResponseButtonAbortRetryIgnore,
    ResponseButtonYesNoCancel,
    ResponseButtonYesNo,
    ResponseButtonRetryCancel,
    ResponseButtonCancelTryAgainContinue
} HardErrorResponseButton;

typedef enum HardErrorResponseDefaultButton {
    DefaultButton1 = 0,
    DefaultButton2 = 0x100,
    DefaultButton3 = 0x200
} HardErrorResponseDefaultButton;

typedef enum HardErrorResponseIcon {
    IconAsterisk = 0x40,
    IconError = 0x10,
    IconExclamation = 0x30,
    IconHand = 0x10,
    IconInformation = 0x40,
    IconNone = 0,
    IconQuestion = 0x20,
    IconStop = 0x10,
    IconWarning = 0x30,
    IconUserIcon = 0x80
} HardErrorResponseIcon;

typedef enum HardErrorResponseOptions {
    ResponseOptionNone = 0,
    ResponseOptionDefaultDesktopOnly = 0x20000,
    ResponseOptionHelp = 0x4000,
    ResponseOptionRightAlign = 0x80000,
    ResponseOptionRightToLeftReading = 0x100000,
    ResponseOptionTopMost = 0x40000,
    ResponseOptionServiceNotification = 0x00200000,
    ResponseOptionServiceNotificationNT3X = 0x00040000,
    ResponseOptionSetForeground = 0x10000,
    ResponseOptionSystemModal = 0x1000,
    ResponseOptionTaskModal = 0x2000,
    ResponseOptionNoFocus = 0x00008000
} HardErrorResponseOptions;

typedef LONG NTSTATUS;

typedef struct _UNICODE_STRING {
    USHORT Length;
    USHORT MaximumLength;
    PWSTR Buffer;
} UNICODE_STRING, *PUNICODE_STRING;

EXTERN_C DECLSPEC_IMPORT NTSTATUS NTAPI NtRaiseHardError(
    IN NTSTATUS ErrorStatus, IN ULONG NumberOfParameters,
    IN ULONG UnicodeStringParameterMask, IN PULONG_PTR Parameters,
    IN ULONG ValidResponseOptions,
    OUT HardErrorResponse *Response);

EXTERN_C DECLSPEC_IMPORT VOID NTAPI RtlInitUnicodeString(
    IN OUT PUNICODE_STRING DestinationString, IN PCWSTR SourceString);

#define STATUS_ACCESS_DENIED ((NTSTATUS)0xC0000022L)
#define STATUS_SERVICE_NOTIFICATION ((NTSTATUS)0x50000018L)

int main(void)
{
    HardErrorResponse r;

    // To display a standard NTSTATUS value:
    NtRaiseHardError(STATUS_ACCESS_DENIED, 0, 0, NULL, ResponseTypeOK, &r);

    // To display a custom string:
    UNICODE_STRING wTitle, wText;
    RtlInitUnicodeString(&wTitle, L"Title");
    RtlInitUnicodeString(&wText, L"Text");
    ULONG_PTR params[4] = {
        (ULONG_PTR)&wText,
        (ULONG_PTR)&wTitle,
        (
            (ULONG)ResponseButtonOK   |
            (ULONG)IconInformation    |
            (ULONG)ResponseOptionNone |
            (ULONG)DefaultButton1
        ),
        INFINITE
    };
    NtRaiseHardError(STATUS_SERVICE_NOTIFICATION, 4, 0x3, params, 0, &r);

    return 0;
}

【问题讨论】:

  • 你能说明你是如何使用这个函数的吗?
  • 我不熟悉ExRaiseHardError,但我不希望IoRaiseInformationalHardError 会立即显示一条消息。我认为它只是排队等待管理员下次登录时查看。
  • @Gabe:嗯……那么文件系统驱动程序如何立即显示消息?
  • 我没有尝试,但是...您是否尝试在内核模式下使用 ZwRaiseHardError?
  • @Adriano:据我所知,它在内核模式下不存在。

标签: c windows winapi driver kernel-mode


【解决方案1】:

我不认为如果没有在用户模式下运行的应用程序(或服务),您就无法在内存中加载驱动程序(即使它将成为过滤器驱动程序)。

【讨论】:

    【解决方案2】:

    内核驱动程序不能显示 MessageBox。如果您想这样做,那么您必须通过带有内核驱动程序的用户级应用程序提供通信功能,然后从您的用户级应用程序显示该 MessageBox。

    所有关于[Zw/Nt]RaiseHardError 的讨论都是无关紧要的。如果你反汇编 MessageBox,你会发现这个 API 最终会被调用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-10
      • 2018-05-07
      • 1970-01-01
      • 2012-05-28
      • 2015-11-19
      • 1970-01-01
      • 2012-05-26
      相关资源
      最近更新 更多