阅读:1299回复:1
关于指针传递问题,急!
主要的程序流程是这样:进入DevIoControl()函数后,由pIrp得到控制代码uIoControlCode,先执行DevGetBindingList(pDeviceObject, pIrp),然后返回pIrp指针,当控制代码为
IOCTL_PTUSERIO_OPEN_ADAPTER时,执行DevOpenAdapter(pDeviceObject, pIrp),然后在返回pIrp,再当控制代码为IOCTL_PTUSERIO_QUERY_OID或者是IOCTL_PTUSERIO_SET_OID,执行 DevHandleOidRequest(pDeviceObject, pIrp),现在问题就出在这,当进入DevHandleOidRequest(pDeviceObject, pIrp)函数中时,运行到 ”if(pOpenContext == NULL)“那里时出错,调试器打印调试信息:invalid handle1, 也就是说在pOpenContext = (POPEN_CONTEXT)pIrpStack->FileObject->FsContext时,pOpenContext没有得到结构指针。 我看别人的程序也是这样写的,但是却没有这样的问题。 我不知道问题出在哪?很郁闷 源代码如下(代码有点长): // 这是处理IRP_MJ_DEVICE_CONTROL的派遣例程,如果是适配器句柄,我们要减小对打开环境的引用 NTSTATUS DevIoControl(PDEVICE_OBJECT pDeviceObject, PIRP pIrp) { // 假设失败 NTSTATUS status = STATUS_INVALID_DEVICE_REQUEST; // 取得此IRP(pIrp)的I/O堆栈指针 PIO_STACK_LOCATION pIrpStack = IoGetCurrentIrpStackLocation(pIrp); // 取得I/O控制代码 ULONG uIoControlCode = pIrpStack->Parameters.DeviceIoControl.IoControlCode; // 取得I/O缓冲区指针和它的长度 PVOID pIoBuffer = pIrp->AssociatedIrp.SystemBuffer; ULONG uInSize = pIrpStack->Parameters.DeviceIoControl.InputBufferLength; ULONG uOutSize = pIrpStack->Parameters.DeviceIoControl.OutputBufferLength; ULONG uTransLen = 0; DBGPRINT((" DevIoControl... \n")); switch(uIoControlCode) { case IOCTL_PTUSERIO_ENUMERATE: { return DevGetBindingList(pDeviceObject, pIrp); } break; case IOCTL_PTUSERIO_OPEN_ADAPTER: // 打开一个适配器。实际上是为适配器关联一个OPEN_CONTEXT结构 { return DevOpenAdapter(pDeviceObject, pIrp); } break; case IOCTL_PTUSERIO_QUERY_OID: case IOCTL_PTUSERIO_SET_OID: { return DevHandleOidRequest(pDeviceObject, pIrp); } break; default: return FltDevIoControl(pDeviceObject, pIrp); } if(status == STATUS_SUCCESS) pIrp->IoStatus.Information = uTransLen; else pIrp->IoStatus.Information = 0; pIrp->IoStatus.Status = status; IoCompleteRequest(pIrp, IO_NO_INCREMENT); return status; } 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 //打开适配器 NTSTATUS DevOpenAdapter( PDEVICE_OBJECT pDeviceObject, PIRP pIrp ) { PIO_STACK_LOCATION pIrpStack; NTSTATUS status = STATUS_SUCCESS; ULONG BytesReturned = 0; PADAPT pAdapt; POPEN_CONTEXT pOpenContext; PVOID pIoBuffer=NULL; ULONG uInSize; pIrpStack=IoGetCurrentIrpStackLocation(pIrp); DBGPRINT(("==>Pt DevOpenAdapter: FileObject %p\n", pIrpStack->FileObject)); pIoBuffer=pIrp->AssociatedIrp.SystemBuffer; uInSize=pIrpStack->Parameters.DeviceIoControl.InputBufferLength; DBGPRINT(( " Looking For Name : \042%*.*ws\042\n", uInSize/sizeof( wchar_t ), uInSize/sizeof( wchar_t ), pIoBuffer )); // // Lookup Adapter By Name // ---------------------- // If successful the lookup function has added a ref count to the found ADAPT // structure. // pAdapt = LookupAdaptByName((PUCHAR)pIoBuffer, uInSize); if( !pAdapt ) { DBGPRINT(( " Adapter Not Found\n" )); status = STATUS_OBJECT_NAME_NOT_FOUND; goto CompleteTheIRP; } DBGPRINT(( " Found Adapter\n" )); // // Fail Open If Unbind Is In Progress // NdisAcquireSpinLock(&pAdapt->Lock); if( pAdapt->UnbindingInProcess ) { NdisReleaseSpinLock(&pAdapt->Lock); DBGPRINT(( " Unbind In Process\n" )); PtDerefAdapter( pAdapt ); status = STATUS_INVALID_DEVICE_STATE; goto CompleteTheIRP; } NdisReleaseSpinLock(&pAdapt->Lock); if( pAdapt->pOpenContext!=NULL ) { DBGPRINT(( " Handle Already Associated(1)\n" )); PtDerefAdapter( pAdapt ); status = STATUS_DEVICE_BUSY; goto CompleteTheIRP; } pOpenContext = DevAllocateOpenContext( pAdapt ); if( pOpenContext=NULL ) { DBGPRINT(( " Unable To Allocate Open Context\n" )); PtDerefAdapter( pAdapt ); status = STATUS_INSUFFICIENT_RESOURCES; goto CompleteTheIRP; } if ( InterlockedCompareExchangePointer (& (pAdapt->pOpenContext), pOpenContext, NULL) != NULL) { DBGPRINT(( " Handle Already Associated(2)\n" )); PtDerefAdapter( pAdapt ); status = STATUS_DEVICE_BUSY; goto CompleteTheIRP; } // // Associate This Handle With The Open Context // pIrpStack->FileObject->FsContext = pOpenContext; DBGPRINT(("句柄关联成功\n")); status=STATUS_SUCCESS; // // Complete The IRP // CompleteTheIRP: if (status==STATUS_SUCCESS) { pIrp->IoStatus.Information=BytesReturned; } else { pIrp->IoStatus.Information =0; } pIrp->IoStatus.Status = status; IoCompleteRequest(pIrp, IO_NO_INCREMENT); DBGPRINT(("<== Pt DevOpenAdapter\n")); return status; } 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 // 处理用户的OID请求 NTSTATUS DevHandleOidRequest(PDEVICE_OBJECT pDeviceObject, PIRP pIrp) { // 假设失败 NTSTATUS status = STATUS_INVALID_DEVICE_REQUEST; // 取得此IRP(pIrp)的I/O堆栈指针 PIO_STACK_LOCATION pIrpStack = IoGetCurrentIrpStackLocation(pIrp); // 取得I/O控制代码 ULONG uIoControlCode = pIrpStack->Parameters.DeviceIoControl.IoControlCode; // 取得I/O缓冲区指针和它的长度 PPTUSERIO_OID_DATA pOidData = (PPTUSERIO_OID_DATA)pIrp->AssociatedIrp.SystemBuffer; ULONG uInSize = pIrpStack->Parameters.DeviceIoControl.InputBufferLength; ULONG uOutSize = pIrpStack->Parameters.DeviceIoControl.OutputBufferLength; ULONG uTransLen = 0; POPEN_CONTEXT pOpenContext; PADAPT pAdapt; do { // 取得与此句柄关联的OPEN_CONTEXT结构的指针 // 首先检查此句柄是否打开适配器 pOpenContext = (POPEN_CONTEXT)pIrpStack->FileObject->FsContext; if(pOpenContext == NULL) { status = STATUS_INVALID_HANDLE; DBGPRINT(("invalid handle1\n")); break; } pAdapt = pOpenContext->pAdapt; if(pAdapt == NULL) { status = STATUS_INVALID_HANDLE; DBGPRINT(("invalid handle2\n")); break; } 。。。。。。。 } |
|
沙发#
发布于:2008-08-04 16:59
在DevOpenAdapter函数中,if(pOpenContext=NULL)改成if(pOpenContext==NULL)
|
|