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

📄 asyncapi.c

📁 winddk src目录下的WDM源码压缩!
💻 C
📖 第 1 页 / 共 4 页
字号:
/*++

Copyright (c) 1998 Microsoft Corporation

Module Name: 

    asyncapi.c

Abstract


Author:

    Peter Binder (pbinder) 5-13-98

Revision History:
Date     Who       What
-------- --------- ------------------------------------------------------------
5-13-98  pbinder   birth
--*/    

#define _ASYNCAPI_C
#include "pch.h"
#undef _ASYNCAPI_C
#include <stdlib.h>

ULONG
WINAPI
AllocateAddressRange(
    HWND                        hWnd,
    PSTR                        szDeviceName,
    PALLOCATE_ADDRESS_RANGE     allocateAddressRange,
    BOOL                        bAutoAlloc
    )
{
    HANDLE                      hDevice;
    DWORD                       dwRet, dwBytesRet;
    PALLOCATE_ADDRESS_RANGE     pAllocateAddressRange;
    ULONG                       ulBufferSize;

    TRACE(TL_TRACE, (hWnd, "Enter AllocateAddressRange\r\n"));

    TRACE(TL_TRACE, (hWnd, "fulAllocateFlags = 0x%x\r\n", allocateAddressRange->fulAllocateFlags));
    TRACE(TL_TRACE, (hWnd, "fulFlags = 0x%x\r\n", allocateAddressRange->fulFlags));
    TRACE(TL_TRACE, (hWnd, "nLength = 0x%x\r\n", allocateAddressRange->nLength));
    TRACE(TL_TRACE, (hWnd, "MaxSegmentSize = 0x%x\r\n", allocateAddressRange->MaxSegmentSize));
    TRACE(TL_TRACE, (hWnd, "fulAccessType = 0x%x\r\n", allocateAddressRange->fulAccessType));
    TRACE(TL_TRACE, (hWnd, "fulNotificationOptions = 0x%x\r\n", allocateAddressRange->fulNotificationOptions));
    TRACE(TL_TRACE, (hWnd, "Required1394Offset.Off_High = 0x%x\r\n", allocateAddressRange->Required1394Offset.Off_High));
    TRACE(TL_TRACE, (hWnd, "Required1394Offset.Off_low = 0x%x\r\n", allocateAddressRange->Required1394Offset.Off_Low));

    ulBufferSize = sizeof(ALLOCATE_ADDRESS_RANGE) + allocateAddressRange->nLength;

    if (bAutoAlloc) {

        pAllocateAddressRange = (PALLOCATE_ADDRESS_RANGE)LocalAlloc(LPTR, ulBufferSize);
        if (!pAllocateAddressRange) {

            dwRet = GetLastError();
            TRACE(TL_ERROR, (hWnd, "Could not allocate pAllocateAddressRange\r\n"));
            goto Exit_AsyncAllocateAddressRange;
        }

        *pAllocateAddressRange = *allocateAddressRange;
    }
    else
        pAllocateAddressRange = allocateAddressRange;

    hDevice = OpenDevice(hWnd, szDeviceName, FALSE);

    if (hDevice != INVALID_HANDLE_VALUE) {

        dwRet = DeviceIoControl( hDevice,
                                 IOCTL_ALLOCATE_ADDRESS_RANGE,
                                 pAllocateAddressRange,
                                 ulBufferSize, 
                                 pAllocateAddressRange,
                                 ulBufferSize,
                                 &dwBytesRet,
                                 NULL
                                 );

        if (dwRet) {

            TRACE(TL_TRACE, (hWnd, "hAddressRange = %p\r\n", pAllocateAddressRange->hAddressRange));
            TRACE(TL_TRACE, (hWnd, "Required1394Offset.Off_High = 0x%x\r\n", pAllocateAddressRange->Required1394Offset.Off_High));
            TRACE(TL_TRACE, (hWnd, "Required1394Offset.Off_low = 0x%x\r\n", pAllocateAddressRange->Required1394Offset.Off_Low));

            dwRet = ERROR_SUCCESS;
        }
        else {

            dwRet = GetLastError();
            TRACE(TL_ERROR, (hWnd, "Error = 0x%x\r\n", dwRet));
        }
        
        // free up resources
        CloseHandle(hDevice);
    }
    else {

        dwRet = GetLastError();
        TRACE(TL_ERROR, (hWnd, "Error = 0x%x\r\n", dwRet));        
    }

Exit_AsyncAllocateAddressRange:

    // need to free the alloc buffer here...
    if (bAutoAlloc && pAllocateAddressRange) {

        // copy the handle and address offset
        allocateAddressRange->hAddressRange         = pAllocateAddressRange->hAddressRange;
        allocateAddressRange->Required1394Offset    = pAllocateAddressRange->Required1394Offset;
        LocalFree (pAllocateAddressRange);
    }

    TRACE(TL_TRACE, (hWnd, "Exit AllocateAddressRange = %d\r\n", dwRet));
    return(dwRet);
} // AllocateAddressRange

ULONG
WINAPI
FreeAddressRange(
    HWND        hWnd,
    PSTR        szDeviceName,
    HANDLE      hAddressRange
    )
{
    HANDLE      hDevice;
    DWORD       dwRet, dwBytesRet;
    
    TRACE(TL_TRACE, (hWnd, "Enter FreeAddressRange\r\n"));

    TRACE(TL_TRACE, (hWnd, "hAddressRange = %p\r\n", hAddressRange));

    hDevice = OpenDevice(hWnd, szDeviceName, FALSE);

    if (hDevice != INVALID_HANDLE_VALUE) {

        dwRet = DeviceIoControl( hDevice,
                                 IOCTL_FREE_ADDRESS_RANGE,
                                 &hAddressRange,
                                 sizeof(HANDLE),
                                 NULL,
                                 0,
                                 &dwBytesRet,
                                 NULL
                                 );

        if (!dwRet) {

            dwRet = GetLastError();
            TRACE(TL_ERROR, (hWnd, "Error = 0x%x\r\n", dwRet));
        }
        else {

            dwRet = ERROR_SUCCESS;
        }

        // free up resources
        CloseHandle(hDevice);
    }
    else {

        dwRet = GetLastError();
        TRACE(TL_ERROR, (hWnd, "Error = 0x%x\r\n", dwRet));        
    }
    
    TRACE(TL_TRACE, (hWnd, "Exit FreeAddressRange = %d\r\n", dwRet));
    return(dwRet);
} // FreeAddressRange

ULONG
WINAPI
GetAddressData (
    HWND                hWnd,
    PSTR                szDeviceName,
    PGET_ADDRESS_DATA   getAddressData
    )
{
    HANDLE      hDevice;
    DWORD       dwRet, dwBytesRet;

    TRACE(TL_TRACE, (hWnd, "Enter GetAddressData\r\n"));

    TRACE(TL_TRACE, (hWnd, "hAddressRange = %p\r\n", getAddressData->hAddressRange));
    TRACE(TL_TRACE, (hWnd, "nLength = 0x%x\r\n", getAddressData->nLength));
    TRACE(TL_TRACE, (hWnd, "ulOffset = 0x%x\r\n", getAddressData->ulOffset));

    hDevice = OpenDevice(hWnd, szDeviceName, FALSE);

    if (hDevice != INVALID_HANDLE_VALUE) {

        dwRet = DeviceIoControl( hDevice,
                                 IOCTL_GET_ADDRESS_DATA,
                                 getAddressData,
                                 sizeof(GET_ADDRESS_DATA)+getAddressData->nLength,
                                 getAddressData,
                                 sizeof(GET_ADDRESS_DATA)+getAddressData->nLength,
                                 &dwBytesRet,
                                 NULL
                                 );

        if (!dwRet) {

            dwRet = GetLastError();
            TRACE(TL_ERROR, (hWnd, "Error = 0x%x\r\n", dwRet));
        }
        else {

            dwRet = ERROR_SUCCESS;
        }

        // free up resources
        CloseHandle(hDevice);
    }
    else {

        dwRet = GetLastError();
        TRACE(TL_ERROR, (hWnd, "Error = 0x%x\r\n", dwRet));        
    }
    

        
    TRACE(TL_TRACE, (hWnd, "Exit GetAddressData = %d\r\n", dwRet));
    return(dwRet);
} // GetAddressData

ULONG
WINAPI
SetAddressData(
    HWND                hWnd,
    PSTR                szDeviceName,
    PSET_ADDRESS_DATA   setAddressData
    )
{
    HANDLE      hDevice;
    DWORD       dwRet, dwBytesRet;
    
    TRACE(TL_TRACE, (hWnd, "Enter SetAddressData\r\n"));

    TRACE(TL_TRACE, (hWnd, "hAddressRange = %p\r\n", setAddressData->hAddressRange));
    TRACE(TL_TRACE, (hWnd, "nLength = 0x%x\r\n", setAddressData->nLength));
    TRACE(TL_TRACE, (hWnd, "ulOffset = 0x%x\r\n", setAddressData->ulOffset));

    hDevice = OpenDevice(hWnd, szDeviceName, FALSE);

    if (hDevice != INVALID_HANDLE_VALUE) {

        dwRet = DeviceIoControl( hDevice,
                                 IOCTL_SET_ADDRESS_DATA,
                                 setAddressData,
                                 sizeof(SET_ADDRESS_DATA)+setAddressData->nLength,
                                 NULL,
                                 0,
                                 &dwBytesRet,
                                 NULL
                                 );

        if (!dwRet) {

            dwRet = GetLastError();
            TRACE(TL_ERROR, (hWnd, "Error = 0x%x\r\n", dwRet));
        }
        else {

            dwRet = ERROR_SUCCESS;
        }

        // free up resources
        CloseHandle(hDevice);
    }
    else {

        dwRet = GetLastError();
        TRACE(TL_ERROR, (hWnd, "Error = 0x%x\r\n", dwRet));        
    }
    
    TRACE(TL_TRACE, (hWnd, "Exit SetAddressData = %d\r\n", dwRet));
    return(dwRet);
} // SetAddressData

ULONG
WINAPI
AsyncRead(
    HWND            hWnd,
    PSTR            szDeviceName,
    PASYNC_READ     asyncRead,
    BOOL            bAutoAlloc
    )
{
    HANDLE          hDevice;
    DWORD           dwRet, dwBytesRet;
    PASYNC_READ     pAsyncRead;
    ULONG           ulBufferSize;
    ULONG           i;

    TRACE(TL_TRACE, (hWnd, "Enter AsyncRead\r\n"));

    TRACE(TL_TRACE, (hWnd, "bRawMode = %d\r\n", asyncRead->bRawMode));
    TRACE(TL_TRACE, (hWnd, "DestinationAddress.IA_Destination_ID.NA_Bus_Number = 0x%x\r\n", asyncRead->DestinationAddress.IA_Destination_ID.NA_Bus_Number));
    TRACE(TL_TRACE, (hWnd, "DestinationAddress.IA_Destination_ID.NA_Node_Number = 0x%x\r\n", asyncRead->DestinationAddress.IA_Destination_ID.NA_Node_Number));
    TRACE(TL_TRACE, (hWnd, "DestinationAddress.IA_Destination_Offset.Off_High = 0x%x\r\n", asyncRead->DestinationAddress.IA_Destination_Offset.Off_High));
    TRACE(TL_TRACE, (hWnd, "DestinationAddress.IA_Destination_Offset.Off_Low = 0x%x\r\n", asyncRead->DestinationAddress.IA_Destination_Offset.Off_Low));
    TRACE(TL_TRACE, (hWnd, "nNumberOfBytesToRead = 0x%x\r\n", asyncRead->nNumberOfBytesToRead));
    TRACE(TL_TRACE, (hWnd, "nBlockSize = 0x%x\r\n", asyncRead->nBlockSize));
    TRACE(TL_TRACE, (hWnd, "fulFlags = 0x%x\r\n", asyncRead->fulFlags));
    TRACE(TL_TRACE, (hWnd, "ulGeneration = 0x%x\r\n", asyncRead->ulGeneration));

    ulBufferSize = sizeof(ASYNC_READ) + asyncRead->nNumberOfBytesToRead;

    if (bAutoAlloc) {

        pAsyncRead = (PASYNC_READ)LocalAlloc(LPTR, ulBufferSize);
        if (!pAsyncRead) {

            dwRet = GetLastError();
            TRACE(TL_ERROR, (hWnd, "Could not allocate pAsyncRead\r\n"));
            goto Exit_AsyncRead;
        }

        FillMemory(pAsyncRead, ulBufferSize, 0);

        *pAsyncRead = *asyncRead;
    }
    else
        pAsyncRead = asyncRead;

    hDevice = OpenDevice(hWnd, szDeviceName, FALSE);

    if (hDevice != INVALID_HANDLE_VALUE) {

        dwRet = DeviceIoControl( hDevice,
                                 IOCTL_ASYNC_READ,
                                 pAsyncRead,
                                 ulBufferSize,
                                 pAsyncRead,
                                 ulBufferSize,
                                 &dwBytesRet,
                                 NULL
                                 );

        if (!dwRet) {

           dwRet = GetLastError();
           TRACE(TL_ERROR, (hWnd, "Error = 0x%x\r\n", dwRet));
        }
        else {

            dwRet = ERROR_SUCCESS;

            for (i=0; i<(asyncRead->nNumberOfBytesToRead/sizeof(ULONG)); i++) {

                PULONG ulTemp;

                ulTemp = (PULONG)&pAsyncRead->Data[i*sizeof(ULONG)];
                TRACE(TL_TRACE, (hWnd, "Quadlet[0x%x] = 0x%x\r\n", i, (ULONG)*ulTemp));
            }
        }
                  
        // free up resources
        CloseHandle(hDevice);
    }
    else {

        dwRet = GetLastError();
        TRACE(TL_ERROR, (hWnd, "Error = 0x%x\r\n", dwRet));        
    }

Exit_AsyncRead:
    
    if (pAsyncRead && bAutoAlloc)

⌨️ 快捷键说明

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