⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 callmsgring0app.cpp

📁 在Ring0层中调用Ring3层的功能 需要安装DDK
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//
// CallMsgRing0App.cpp  : Defines the entry point for the application.
//
// Generated by C DriverWizard 3.2.0 (Build 2485)
// Requires DDK Only
// File created on 3/12/2005
//
#include "CallMsgRing0App.h"

// Array of possible transfer types
PTCHAR g_TransferTypeArray[] =
{
    _T("IOCTL_CALL_RING3"),
    _T("")
};

HANDLE              g_hDevice = INVALID_HANDLE_VALUE;
HDEVNOTIFY          g_hInterfaceNotification = NULL;
CALLMSGRING0_LIST_ITEM   g_IoList;
CRITICAL_SECTION    g_IoListLock;
HANDLE              g_hIoCompletionThreadTerminationEvent;
HANDLE              g_hIoCompletionThread;

///////////////////////////////////////////////////////////////////////////////////////////////////
//  CallMsgRing0OutputText
//      method to output text in the output window
//
//  Arguments:
//      IN  Format
//              Text format to print to output window
//
//  Return Value:
//      None.
//
VOID CallMsgRing0OutputText(LPCTSTR Format, ...)
{
    TCHAR       str[MAX_STRING_LENGTH];
    va_list     vaList;

    va_start(vaList, Format);

    _vstprintf(str, Format, vaList);

    OutputDebugString(_T("CallMsgRing0: "));
    OutputDebugString(str);
    OutputDebugString(_T("\n"));

    va_end(vaList);

    return;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  CallMsgRing0OutputBuffer
//      method to output text in the output window
//
//  Arguments:
//      IN  Buffer
//              Data Buffer
//
//      IN  Size
//              Size of Data Buffer
//
//  Return Value:
//      None.
//
VOID CallMsgRing0OutputBuffer(PVOID Buffer, ULONG Size)
{
    TCHAR       str[MAX_STRING_LENGTH];
    LONG        length = (LONG)Size;
    PUCHAR      p = (PUCHAR)Buffer;
    TCHAR       data[MAX_STRING_LENGTH];
    TCHAR       rawData[MAX_STRING_LENGTH];
    ULONG       i;
    ULONG       j;

    for (i = 0; i < Size; i += 16)
    {
        ZeroMemory(str, sizeof(str));
        ZeroMemory(rawData, sizeof(rawData));

        _stprintf(str, _T("%04.4X  "), i);

        for (j = 0; j < 16; ++j, ++p)
        {
            if (length > 0)
            {
                _stprintf(data, _T("%02X "), *p);
            }
            else
            {
                _stprintf(data, _T("   "));
            }

            _tcsncat(str, data, 3);

            if (length > 0)
            {
                TCHAR c = (TCHAR)(*p);

                if (_istalnum(_TUCHAR(c)))
                {
                    _stprintf(data, _T("%c"), c);
                }
                else
                {
                    _tcsncat(rawData, _T("?"), 1);
                }

                --length;
            }
        }

        _tcsncat(str, _T("  "), 2);
        _tcsncat(str, rawData, 16);

        OutputDebugString(str);
        OutputDebugString(_T("\n"));
    }

    return;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  CallMsgRing0EnumerateDevices
//      Finds devices associated with the given interface class
//
//  Arguments:
//      IN  hDlg
//              Handle to dialog
//
//  Return Value:
//      status.
//
DWORD CallMsgRing0EnumerateDevices(HWND hDlg)
{
    HWND                                hList;
    DWORD                               lastError;
    HANDLE                              hDev;

    // Get the handle to the device instance list box
    hList = GetDlgItem(hDlg, IDC_DEVICE_INSTANCE_LIST);
    if (hList == NULL)
    {
        lastError = GetLastError();
        CallMsgRing0OutputText(_T("GetDlgItem failed, GetLastError() = %d"), lastError);
        return lastError;
    }

    // Clear the instance list box
    SendMessage(hList, LB_RESETCONTENT, 0, 0);

    // Open handle to device
    hDev = CreateFile(
                 _T("\\\\.\\CallMsgRing0Device"),
                 GENERIC_READ,
                 FILE_SHARE_READ,
                 NULL,
                 OPEN_EXISTING,
                 0,
                 0
                 );

    if (hDev != INVALID_HANDLE_VALUE)
    {
        SendMessage(
            hList,
            LB_ADDSTRING,
            0,
            (LPARAM)_T("\\\\.\\CallMsgRing0Device")
            );

        CloseHandle(hDev);
    }
    else
    {
        CallMsgRing0OutputText(_T("No devices found"));
    }

    return ERROR_SUCCESS;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  CallMsgRing0OpenDevice
//      Opens the nth device found with the given interface class
//
//  Arguments:
//      IN  hDlg
//              Handle to dialog
//
//  Return Value:
//      Handle to device.
//
HANDLE CallMsgRing0OpenDevice(HWND hDlg)
{
    HWND    hList;
    HANDLE  hDev;
    TCHAR   path[MAX_STRING_LENGTH];
    DWORD   itemIndex;

    // Get handle to device instance list box
    hList = GetDlgItem(hDlg, IDC_DEVICE_INSTANCE_LIST);

    // Find the current selection
    itemIndex = (DWORD)SendMessage(hList, LB_GETCURSEL, 0, 0);

    // Get the path string from the list box
    (LPTSTR)SendMessage(
                hList,
                LB_GETTEXT,
                itemIndex,
                (LPARAM)path);

    // Open handle to device
    hDev = CreateFile(
                 path,
                 GENERIC_READ | GENERIC_WRITE,
                 FILE_SHARE_READ | FILE_SHARE_WRITE,
                 NULL,
                 OPEN_EXISTING,
                 FILE_FLAG_OVERLAPPED,
                 0
                 );

    if (hDev == INVALID_HANDLE_VALUE)
    {
        CallMsgRing0OutputText(_T("Error: CreateFile failed for device %s (%d)\n"), path, GetLastError());
        return INVALID_HANDLE_VALUE;
    }

    CallMsgRing0OutputText(_T("Opened device %s"), path);

    // Reflect the selection in the current selection edit box
    SetDlgItemText(hDlg, IDC_SELECTED_DEVICE_EDIT, path);

    return hDev;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  CallMsgRing0MainDlgProc
//      Message loop
//
//  Arguments:
//      IN  hDlg
//              Handle to dialog
//
//      IN  uMsg
//              Message id
//
//      IN  wParam
//              WPARAM
//
//      IN  lParam
//              LPARAM
//
//  Return Value:
//      status.
//
LRESULT CALLBACK CallMsgRing0MainDlgProc(
    HWND    hDlg,
    UINT    uMsg,
    WPARAM  wParam,
    LPARAM  lParam
    )
{
    HWND    hWnd;
    TCHAR   str[MAX_STRING_LENGTH];
    DWORD   itemIndex;
    DWORD   error = ERROR_SUCCESS;
    DWORD   ii;
    BOOL    bInBufferEnable = TRUE;
    BOOL    bOutBufferEnable = TRUE;

    switch (uMsg)
    {
    case WM_INITDIALOG:

        // Initialize our device handle
        g_hDevice = INVALID_HANDLE_VALUE;

        // Populate the list box
        CallMsgRing0EnumerateDevices(hDlg);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -