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

📄 utils.cpp

📁 RTL8139 网卡驱动源码 for WinCE.net CEPC
💻 CPP
字号:
//****************************************************************************
//                             File UTILS.CPP
//
//                             Stuff methods
//
//                     Copyright (c) APSoft, 1998-2002.
//                          All rights reserved.
//
//****************************************************************************


extern "C"
{
#include <ntddk.h>                          // Main DDK's include
}

#include <utils.h>                          // Header file
#include <pnpdev.h>                         // Static device members


//****************************************************************************
//                      --- AllocateUnicodeString ---
//
// Purpose: Allocate unicode string and init. it
//   Input: PUNICODE_STRING pusTarget - New string
//          PUNICODE_STRING pusSource - Source string
//  Output: none
// Written: by Anton V. Krivenko 4/9/2002
//****************************************************************************
void AllocateUnicodeString(PUNICODE_STRING pusTarget, PUNICODE_STRING pusSource)
{
    pusTarget->Length           = pusSource->Length;
    pusTarget->MaximumLength    = pusSource->MaximumLength;
    pusTarget->Buffer           = (PWSTR)ExAllocatePool(NonPagedPool, pusTarget->MaximumLength);

    RtlCopyMemory(pusTarget->Buffer, pusSource->Buffer, pusTarget->Length);
}

//****************************************************************************
//                      --- AllocateUnicodeString ---
//
// Purpose: Allocate unicode string and init. it
//   Input: PUNICODE_STRING pusTarget  - New string
//          PWSTR           pwszSource - Source string
//  Output: none
// Written: by Anton V. Krivenko 4/9/2002
//****************************************************************************
void AllocateUnicodeString(PUNICODE_STRING pusTarget, PWSTR pwszSource)
{
    USHORT uLength              = (USHORT)wcslen(pwszSource) * sizeof(WCHAR);

    pusTarget->Length           = uLength;
    pusTarget->MaximumLength    = uLength;
    pusTarget->Buffer           = (PWSTR)ExAllocatePool(NonPagedPool, uLength);

    RtlCopyMemory(pusTarget->Buffer, pwszSource, uLength);
}

//****************************************************************************
//                        --- PnpQueryInterface ---
//
// Purpose: Query interface
//   Input: PDEVICE_OBJECT   pPdo     - Target device
//          CONST GUID     * Type     - Type of interface
//          PVOID            pIface   - Address of interface structure
//          USHORT           uSize    - Size of structure
//          USHORT           uVersion - Version of interface
//  Output: NTSTATUS                  - Operation status
// Written: by Anton V. Krivenko 4/10/2002
//****************************************************************************
NTSTATUS PnpQueryInterface(PDEVICE_OBJECT   pPdo,
                           CONST GUID     * Type,
                           PVOID            pIface,
                           USHORT           uSize,
                           USHORT           uVer)
{
    NTSTATUS ntStatus;

//----------------------------- Allocate IRP ---------------------------------

    PIRP pIrp = IoAllocateIrp(pPdo->StackSize, FALSE);
    ASSERTS_PNPDRV(pIrp != NULL);

    if (pIrp != NULL)
    {
        PIO_STACK_LOCATION pNextSp = IoGetNextIrpStackLocation(pIrp);

//------------------------- Initialize Interface -----------------------------

        RtlZeroMemory(pIface, uSize);

//----------------------- Ssetup IRP stack location --------------------------

        pNextSp->MajorFunction                           = IRP_MJ_PNP;
        pNextSp->MinorFunction                           = IRP_MN_QUERY_INTERFACE;
        pNextSp->Parameters.QueryInterface.InterfaceType = Type;
        pNextSp->Parameters.QueryInterface.Size          = uSize;
        pNextSp->Parameters.QueryInterface.Version       = uVer;
        pNextSp->Parameters.QueryInterface.Interface     = (PINTERFACE)pIface;
        pNextSp->Parameters.QueryInterface.InterfaceSpecificData
                                                         = NULL;

//-------------------------- Set default status ------------------------------

        pIrp->IoStatus.Status  = STATUS_NOT_SUPPORTED;

//----------------------------- Send request ---------------------------------

        ntStatus = CPnpDevice::CallDriverSync(pPdo, pIrp);

//---------------------------- Free resources --------------------------------

        IoFreeIrp(pIrp);
    }                                       // if (pIrp != NULL)
    else
        ntStatus = STATUS_INSUFFICIENT_RESOURCES;

    return ntStatus;
}

//****************************************************************************
//                         --- PnpGetDeviceID ---
//
// Purpose: Returns device ID
//   Input: PDEVICE_OBJECT    pPdo - Target device
//          BUS_QUERY_ID_TYPE Type - Type of ID to return
//  Output: PWSTR                  - ID string (MULTI_SZ for HardwareIDs
//                                   and ComptibleIDs). User MUST delete string
//                                   using ExFreePool!
// Written: by Anton V. Krivenko 4/17/2002
//****************************************************************************
PWSTR PnpGetDeviceID(PDEVICE_OBJECT pPdo, BUS_QUERY_ID_TYPE Type)
{
    PWSTR pRet = NULL;

//----------------------------- Allocate IRP ---------------------------------

    PIRP pIrp = IoAllocateIrp(pPdo->StackSize, FALSE);
    ASSERTS_PNPDRV(pIrp != NULL);

    if (pIrp != NULL)
    {
        PIO_STACK_LOCATION pNextSp = IoGetNextIrpStackLocation(pIrp);

//------------------------ Setup IRP stack location --------------------------

        pNextSp->MajorFunction             = IRP_MJ_PNP;
        pNextSp->MinorFunction             = IRP_MN_QUERY_ID;
        pNextSp->Parameters.QueryId.IdType = Type;

//-------------------------- Set default status ------------------------------

        pIrp->IoStatus.Status      = STATUS_NOT_SUPPORTED;
        pIrp->IoStatus.Information = 0;

//----------------------------- Send request ---------------------------------

        NTSTATUS ntStatus = CPnpDevice::CallDriverSync(pPdo, pIrp);

//----------------------------- Check result ---------------------------------

        if (NT_SUCCESS(ntStatus))
        {
            PVOID pID = (PVOID)pIrp->IoStatus.Information;

            if (MmIsAddressValid(pID))
                pRet = (PWSTR)pID;
        }                                   // if (NT_SUCCESS(ntStatus))

//---------------------------- Free resources --------------------------------

        IoFreeIrp(pIrp);
    }                                       // if (pIrp != NULL)

    return pRet;
}

⌨️ 快捷键说明

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