阅读:4069回复:28
救命呀 。。。为啥我的用户程序老找不到驱动程序呀?
我编了个wdm驱动,用softice调试也可以,安装也成功了,但是用户程序测试时,老出现找不到驱动程序的错误。驱动程序也很简单,就是读写内存。
我把驱动程序和用户测试源程序还有inf文件上传为附件,。 请大哥大姐们抽点时间帮我瞧瞧。 我找了两天错误,找得郁闷呀。 小弟我刚学驱动,觉得原理弄明白了,没想到编起来不顺呀。请高手帮忙呀!!!!!! |
|
|
沙发#
发布于:2005-03-10 20:47
aiwadgj!!!!!!!!!!!!!!!!!!!!!!
姐姐,能努能帮我看看呀 还是上回那个程序,驱动是做好拉,可是测试程序又出现问题了:(((((((( |
|
板凳#
发布于:2005-03-11 08:45
用户测试程序: //////wdmtest.cpp///// # include<windows.h> # include <stdio.h> # include \"setupapi.h\" # include <initguid.h> # include \"guid.h\" HANDLE GetDeviceViaInterface(GUID *pGuid ,DWORD instance); int main(int argc ,char *argv[]) { // HANDLE hdevice = CreateFile(\"\\\\\\\\.\\\\WdmDriver\", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); // if (hdevice == INVALID_HANDLE_VALUE) // { // printf(\"Unable to openyyyy device - error %d\\n\", GetLastError()); // return 1; // } HANDLE hWdmDevice=GetDeviceViaInterface((LPGUID)&WDM_GUID,0); if(hWdmDevice==NULL) { printf(\"can not find the wdm device\\n\"); return 1; } printf(\"open ok\\n\"); DWORD TxdBytes; ULONG Rvalue=0; if(!ReadFile(hWdmDevice,&Rvalue,4,&TxdBytes,NULL)) printf(\"can not read buffer!\\n\"); else if(TxdBytes==4) printf(\"read successfuklly read stored value of 0x%X\\n\",Rvalue); else printf(\"wrong number of bytes read :%d\\n\",TxdBytes); //write ULONG Wvalue=0xabcdef01; if(!WriteFile(hWdmDevice,&Wvalue,4,&TxdBytes,NULL)) printf(\"can not write%x\\n\",Wvalue); else if(TxdBytes==4) printf(\"write succeeded\\n\"); else printf(\"wrong number of bytes written :%d\\n\",TxdBytes); //read Rvalue=0; if(!ReadFile(hWdmDevice,&Rvalue,4,&TxdBytes,NULL)) printf(\"can not read value!\\n\"); else if(TxdBytes==4) { if(Rvalue==Wvalue) printf(\"read successfully!\\n\"); else printf(\"read successfully ,but got wrong value:%X\",Rvalue); } else printf(\"wrong number of bytes read:%d\\n),TxdBytes\"); //check duff write fails if(!WriteFile(hWdmDevice,&Wvalue,5,&TxdBytes,NULL)) printf(\"duff write correctly failed with error %d\\n\",GetLastError()); else printf(\"duff write unecpectedly successed\\n\"); CloseHandle(hWdmDevice); return 0; } HANDLE GetDeviceViaInterface(GUID *pGuid ,DWORD instance) { HDEVINFO info=SetupDiGetClassDevs(pGuid,NULL,NULL,DIGCF_PRESENT|DIGCF_INTERFACEDEVICE); if(info==INVALID_HANDLE_VALUE) { printf(\"no hdevinfo available for this guid\\n\"); return NULL; } SP_INTERFACE_DEVICE_DATA ifdata; ifdata.cbSize=sizeof(ifdata); if(!SetupDiEnumDeviceInterfaces(info,NULL,pGuid,instance,&ifdata)) { printf(\"no SP_INTERFACE_DEVICE_DATA available for this guid instance\\n\"); SetupDiDestroyDeviceInfoList(info); return NULL; } DWORD ReqLen; SetupDiGetDeviceInterfaceDetail(info,&ifdata,NULL,0,&ReqLen,NULL); PSP_INTERFACE_DEVICE_DETAIL_DATA ifdetail= (PSP_INTERFACE_DEVICE_DETAIL_DATA)(new char[ReqLen]); if(ifdetail==NULL) { SetupDiDestroyDeviceInfoList(info); printf(\"the error can appere1!!\\n\"); return NULL; } ifdetail->cbSize=sizeof(PSP_INTERFACE_DEVICE_DETAIL_DATA); if(!SetupDiGetDeviceInterfaceDetail(info,&ifdata,ifdetail,ReqLen,NULL,NULL)) { SetupDiDestroyDeviceInfoList(info); printf(\"the error can appere2!!\\n\"); delete ifdetail; return NULL; } printf(\"symbolic link is %s\\n\",ifdetail->DevicePath); HANDLE rv=CreateFile(ifdetail->DevicePath,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); delete ifdetail; SetupDiDestroyDeviceInfoList(info); return rv; } ///////guid.h/////////////////////////// // guids.h -- Device interface GUID for wdmdriver driver // Copyright (C) 1999. 2000 by Walter Oney // All rights reservedWDM_GUID #ifndef GUIDS_H #define GUIDS_H //59521A18-56CC-4E32-B166-684596F852BE // {3CC63815-E213-495E-BF2E-9D98B8006B7C} //0xc4eaef0bL, 0x048e, 0x4534, 0xae, 0x9c, 0x0d, 0xe3, 0xe5, 0x7b, 0x50, 0x04) //DEFINE_GUID(WDM_GUID, 0xc4eaef0bL, 0x048e, 0x4534, 0xae, 0x9c, 0x0d, 0xe3, 0xe5, 0x7b, 0x50, 0x04); //DEFINE_GUID(WDM_GUID, 0x3cc63815L, 0xe213, 0x495e, 0xbf, 0x2e, 0x9d, 0x98, 0xb8, 0x00, 0x6b, 0x7c); //DEFINE_GUID(WMI_GUID, 0 // {9DBF3661-B89C-4228-8753-508ADFFB7618} DEFINE_GUID(WDM_GUID, 0x9dbf3661, 0xb89c, 0x4228, 0x87, 0x53, 0x50, 0x8a, 0xdf, 0xfb, 0x76, 0x18); // {7D774CA2-C9A2-417f-A7F5-F7175535539C} DEFINE_GUID(WMI_GUID, 0x7d774ca2, 0xc9a2, 0x417f, 0xa7, 0xf5, 0xf7, 0x17, 0x55, 0x35, 0x53, 0x9c); #endif |
|
地板#
发布于:2005-03-11 08:48
驱动程序的头文件:
//////driver.h/////// // Declarations for powtrace driver // Copyright (C) 1999, 2000 by Walter Oney // All rights reserved //#pragma once #define DRIVERNAME \"WDMDRIVER\" // for use in messages #define LDRIVERNAME L\"WDMDRIVER\" // for use in UNICODE string constants /////////////////////////////////////////////////////////////////////////////// // Device extension structure //extern UNICODE_STRING servkey; typedef struct _WDM_DEVICE_EXTENSION { PDEVICE_OBJECT pdo; PDEVICE_OBJECT fdo; PDEVICE_OBJECT NextDevice; UNICODE_STRING ifSymLinkName; WMILIB_CONTEXT WmiLibInfo; }DEVICE_EXTENSION , *PWDM_DEVICE_EXTENSION; //init.cpp//// extern \"C\" NTSTATUS DriverEntry( IN PDRIVER_OBJECT DriverObject ,IN PUNICODE_STRING RegistryPath); VOID DriverUnload(IN PDRIVER_OBJECT DriverObject); //pnp.cpp///////// NTSTATUS AddDevice(IN PDRIVER_OBJECT DriverObject, IN PDEVICE_OBJECT pdo); NTSTATUS DispatchPnp(IN PDEVICE_OBJECT fdo ,IN PIRP Irp); NTSTATUS DispatchPower(IN PDEVICE_OBJECT fdo ,IN PIRP Irp); /////Wmi.cpp////////// VOID RegisterWmi(IN PDEVICE_OBJECT fdo); VOID DeregisterWmi(IN PDEVICE_OBJECT fdo); NTSTATUS DispatchWmi(PDEVICE_OBJECT fdo ,IN PIRP Irp); NTSTATUS WdmSystemControl(IN PWMILIB_CONTEXT WmiLibInfo, PDEVICE_OBJECT fdo ,IN PIRP Irp,OUT PSYSCTL_IRP_DISPOSITION IrpDisposition); ///////dispatch.cpp////////////////// NTSTATUS DispatchClose(PDEVICE_OBJECT fdo, PIRP Irp); NTSTATUS DispatchCreate(PDEVICE_OBJECT fdo, PIRP Irp); NTSTATUS DispatchDeviceControl(PDEVICE_OBJECT fdo, PIRP Irp); NTSTATUS DispatchWrite(PDEVICE_OBJECT fdo, PIRP Irp); NTSTATUS DispatchRead(PDEVICE_OBJECT fdo, PIRP Irp); ////////////////////stddcls.h///////////////// // stddcls.h -- Precompiled headers for WDM drivers // Copyright (C) 1999, 2000 by Walter Oney // All rights reserved #pragma comment(lib,\"wmilib.lib\") #ifdef __cplusplus extern \"C\" { #endif #include <wdm.h> #include <stdio.h> #include <wmilib.h> #include <wmistr.h> //#pragma warning(disable:4201) // nameless struct/union //#define DEPRECATE_DDK_FUNCTIONS 1 //} #ifdef __cplusplus } #endif #define PAGEDCODE code_seg(\"PAGE\") #define LOCKEDCODE code_seg() #define INITCODE code_seg(\"INIT\") #define PAGEDDATA data_seg(\"PAGEDATA\") #define LOCKEDDATA data_seg() #define INITDATA data_seg(\"INITDATA\") #define arraysize(p) (sizeof(p)/sizeof((p)[0])) // Override DDK definition of ASSERT so that debugger halts in the // affected code and halts even in the unchecked OS |
|
地下室#
发布于:2005-03-11 08:59
驱动实现程序:
//////dispatch.cpp//////////////// #ifdef __cplusplus extern \"C\" { #endif #include \"stddcls.h\" #include \"driver.h\" #ifdef __cplusplus } #endif const ULONG SHARED_MEMORY_SIZE =4 ; UCHAR SharedMemory[SHARED_MEMORY_SIZE]; NTSTATUS DispatchRead(PDEVICE_OBJECT fdo, PIRP Irp) { PWDM_DEVICE_EXTENSION dx=(PWDM_DEVICE_EXTENSION )fdo->DeviceExtension; PIO_STACK_LOCATION pIrpstack=IoGetCurrentIrpStackLocation(Irp); NTSTATUS status= STATUS_SUCCESS; ULONG BytesTxd=0; ULONG ReadLen=pIrpstack->Parameters.Read.Length; if(ReadLen>SHARED_MEMORY_SIZE) status=STATUS_INVALID_PARAMETER; else if(ReadLen>0) { RtlMoveMemory(Irp->AssociatedIrp.SystemBuffer, SharedMemory, ReadLen); BytesTxd=ReadLen; } Irp->IoStatus.Status=status; Irp->IoStatus.Information=BytesTxd; IoCompleteRequest(Irp,IO_NO_INCREMENT); return status; } NTSTATUS DispatchClose(PDEVICE_OBJECT fdo, PIRP Irp) { Irp->IoStatus.Status=STATUS_SUCCESS; Irp->IoStatus.Information=0; IoCompleteRequest(Irp,IO_NO_INCREMENT); return STATUS_SUCCESS; } NTSTATUS DispatchCreate(PDEVICE_OBJECT fdo, PIRP Irp) { Irp->IoStatus.Status=STATUS_SUCCESS; Irp->IoStatus.Information=0; IoCompleteRequest(Irp,IO_NO_INCREMENT); return STATUS_SUCCESS; } NTSTATUS DispatchWrite(PDEVICE_OBJECT fdo, PIRP Irp) { PWDM_DEVICE_EXTENSION dx=(PWDM_DEVICE_EXTENSION )fdo->DeviceExtension; PIO_STACK_LOCATION pIrpstack=IoGetCurrentIrpStackLocation(Irp); NTSTATUS status= STATUS_SUCCESS; ULONG BytesTxd=0; ULONG WriteLen=pIrpstack->Parameters.Write.Length; if(WriteLen>SHARED_MEMORY_SIZE) status=STATUS_INVALID_PARAMETER; else if(WriteLen>0) { RtlMoveMemory(SharedMemory, Irp->AssociatedIrp.SystemBuffer, WriteLen); BytesTxd=WriteLen; } Irp->IoStatus.Status=status; Irp->IoStatus.Information=BytesTxd; IoCompleteRequest(Irp,IO_NO_INCREMENT); return status; } NTSTATUS DispatchDeviceControl(PDEVICE_OBJECT fdo, PIRP Irp) { Irp->IoStatus.Status=STATUS_SUCCESS; Irp->IoStatus.Information=0; IoCompleteRequest(Irp,IO_NO_INCREMENT); return STATUS_SUCCESS; } ////////////driverentry.cpp////////////////// // Main program for powtrace driver // Copyright (C) 2002 by Walter Oney // All rights reserved // POWTRACE.SYS is intended as a lower filter that will show all the power-related irps flowing // downward from a function driver. The resulting debug trace can be useful in diagnosing power // management problems with drivers. #ifdef __cplusplus extern \"C\" { #endif #include \"stddcls.h\" #include \"driver.h\" #ifdef __cplusplus } #endif UNICODE_STRING servkey; extern \"C\" NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath) { DriverObject->DriverUnload = DriverUnload; DriverObject->DriverExtension->AddDevice = AddDevice; DriverObject->MajorFunction[IRP_MJ_READ] = DispatchRead; DriverObject->MajorFunction[IRP_MJ_CREATE] = DispatchCreate; DriverObject->MajorFunction[IRP_MJ_CLOSE] = DispatchClose; DriverObject->MajorFunction[IRP_MJ_WRITE] = DispatchWrite; DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DispatchDeviceControl; DriverObject->MajorFunction[IRP_MJ_PNP] = DispatchPnp; DriverObject->MajorFunction[IRP_MJ_POWER] = DispatchPower; // servkey.Buffer = (PWSTR)ExAllocatePool(PagedPool, RegistryPath->Length + sizeof(WCHAR)); DriverObject->MajorFunction[IRP_MJ_SYSTEM_CONTROL] = DispatchWmi; servkey.Buffer=(PWSTR)ExAllocatePool(NonPagedPool,RegistryPath->Length +sizeof(WCHAR)); if(!servkey.Buffer) return STATUS_INSUFFICIENT_RESOURCES; servkey.MaximumLength=RegistryPath->Length +sizeof(WCHAR); RtlCopyUnicodeString(&servkey,RegistryPath); return STATUS_SUCCESS; } VOID DriverUnload(IN PDRIVER_OBJECT DriverObject) { ExFreePool(servkey.Buffer); } //////////////pnp.cpp///////////////////////// #ifdef __cplusplus extern \"C\" { #endif #include \"stddcls.h\" #include <initguid.h> #include \"driver.h\" #include \"guid.h\" #ifdef __cplusplus } #endif //GUID WDMGUID=WdmGuid; NTSTATUS AddDevice(IN PDRIVER_OBJECT DriverObject, IN PDEVICE_OBJECT pdo) { NTSTATUS status; PDEVICE_OBJECT fdo; status=IoCreateDevice( DriverObject, sizeof(DEVICE_EXTENSION), NULL, FILE_DEVICE_UNKNOWN, FILE_DEVICE_SECURE_OPEN, FALSE, &fdo); if(NT_ERROR(status)) return status; PWDM_DEVICE_EXTENSION dx=(PWDM_DEVICE_EXTENSION )fdo->DeviceExtension; dx->pdo=pdo; dx->fdo=fdo; status=IoRegisterDeviceInterface(pdo, &WDM_GUID, NULL, &dx->ifSymLinkName); if(NT_ERROR(status)) { // unable to register interface KdPrint((DRIVERNAME \" - IoRegisterDeviceInterface failed - %8.8lX\\n\", status)); IoDeleteDevice(fdo); return status; } IoSetDeviceInterfaceState(&dx->ifSymLinkName,true); dx->NextDevice=IoAttachDeviceToDeviceStack(fdo,pdo); fdo->Flags&=~DO_DEVICE_INITIALIZING; fdo->Flags|=DO_BUFFERED_IO; RegisterWmi(fdo); return STATUS_SUCCESS; } NTSTATUS DispatchPower(IN PDEVICE_OBJECT fdo ,IN PIRP Irp) { PWDM_DEVICE_EXTENSION dx=(PWDM_DEVICE_EXTENSION )fdo->DeviceExtension; PoStartNextPowerIrp(Irp); IoSkipCurrentIrpStackLocation(Irp); return PoCallDriver(dx->NextDevice,Irp); } NTSTATUS DispatchPnp(IN PDEVICE_OBJECT fdo ,IN PIRP Irp) { PWDM_DEVICE_EXTENSION dx=(PWDM_DEVICE_EXTENSION )fdo->DeviceExtension; PIO_STACK_LOCATION stack=IoGetCurrentIrpStackLocation(Irp); ULONG MinorFunction=stack->MinorFunction; IoSkipCurrentIrpStackLocation(Irp); NTSTATUS status=IoCallDriver(dx->NextDevice,Irp); if(MinorFunction==IRP_MN_REMOVE_DEVICE) { IoSetDeviceInterfaceState(&dx->ifSymLinkName,false); RtlFreeUnicodeString(&dx->ifSymLinkName); DeregisterWmi(fdo); if(dx->NextDevice) IoDetachDevice(dx->NextDevice); IoDeleteDevice(fdo); } return status; } ///////////////////wmi.cpp////////////////////// #ifdef __cplusplus extern \"C\" { #endif //# include <wmilib.h> //# include <wmistr.h> #include \"stddcls.h\" #include \"driver.h\" # include \"guid.h\" //# include <wmilib.h> #include <initguid.h> #include <wdmguid.h> #ifdef __cplusplus } #endif //WMI_GUID WMIGUIDREGINFO GuidList[1]= { {& WMI_GUID ,1,0}, }; extern UNICODE_STRING servkey; extern UCHAR SharedMemory[]; NTSTATUS SetDataItem( IN PDEVICE_OBJECT fdo, IN PIRP Irp, IN ULONG GuidIndex, IN ULONG InstanceIndex, IN ULONG DataItemId, IN ULONG BufferSize, OUT PUCHAR PBuffer); NTSTATUS QueryRegInfo( IN PDEVICE_OBJECT fdo, OUT PULONG PRegFlags, OUT PUNICODE_STRING PInstanceName, OUT PUNICODE_STRING *PRegisterPath, OUT PUNICODE_STRING MofResourceName, IN PDEVICE_OBJECT *pdo); NTSTATUS QueryDataBlock( IN PDEVICE_OBJECT fdo, IN PIRP Irp, IN ULONG GuidIndex, IN ULONG InstanceIndex, IN ULONG InstanceCount, IN OUT PULONG InstanceLengthArray, IN ULONG OutBufferSize, OUT PUCHAR PBuffer); NTSTATUS FailWmiRequest( IN PDEVICE_OBJECT fdo, IN PIRP Irp, IN ULONG GuidIndex); NTSTATUS SetDataBlock( IN PDEVICE_OBJECT fdo, IN PIRP Irp, IN ULONG GuidIndex, IN ULONG InstanceIndex, IN ULONG BufferSize, IN PUCHAR PBuffer); NTSTATUS DispatchWmi(PDEVICE_OBJECT fdo, PIRP Irp) { PWDM_DEVICE_EXTENSION dx=(PWDM_DEVICE_EXTENSION )fdo->DeviceExtension; SYSCTL_IRP_DISPOSITION disposition; NTSTATUS status = WmiSystemControl(&dx->WmiLibInfo,fdo,Irp,&disposition); switch(disposition) { case IrpProcessed: break; case IrpNotCompleted: IoCompleteRequest(Irp,IO_NO_INCREMENT); break; default: case IrpForward: case IrpNotWmi: IoSkipCurrentIrpStackLocation(Irp); status =IoCallDriver(dx->NextDevice,Irp); break; } return status; } NTSTATUS WdmSystemControl(PDEVICE_OBJECT fdo, PIRP Irp) { PWDM_DEVICE_EXTENSION dx=(PWDM_DEVICE_EXTENSION )fdo->DeviceExtension; SYSCTL_IRP_DISPOSITION disposition; NTSTATUS status = WmiSystemControl(&dx->WmiLibInfo,fdo,Irp,&disposition); switch(disposition) { case IrpProcessed: break; case IrpNotCompleted: IoCompleteRequest(Irp,IO_NO_INCREMENT); break; default: case IrpForward: case IrpNotWmi: IoSkipCurrentIrpStackLocation(Irp); status =IoCallDriver(dx->NextDevice,Irp); break; } return status; } VOID RegisterWmi(IN PDEVICE_OBJECT fdo) { PWDM_DEVICE_EXTENSION dx=(PWDM_DEVICE_EXTENSION )fdo->DeviceExtension; dx->WmiLibInfo.GuidCount=1; dx->WmiLibInfo.GuidList=GuidList; dx->WmiLibInfo.QueryWmiRegInfo = QueryRegInfo; dx->WmiLibInfo.QueryWmiDataBlock = QueryDataBlock; dx->WmiLibInfo.SetWmiDataBlock = SetDataBlock; dx->WmiLibInfo.SetWmiDataItem = SetDataItem; dx->WmiLibInfo.ExecuteWmiMethod = NULL; dx->WmiLibInfo.WmiFunctionControl = NULL; IoWMIRegistrationControl(fdo,WMIREG_ACTION_REGISTER); } VOID DeregisterWmi(IN PDEVICE_OBJECT fdo) { IoWMIRegistrationControl(fdo,WMIREG_ACTION_DEREGISTER); } NTSTATUS QueryRegInfo( IN PDEVICE_OBJECT fdo, OUT PULONG PRegFlags, OUT PUNICODE_STRING PInstanceName, OUT PUNICODE_STRING *PRegisterPath, OUT PUNICODE_STRING MofResourceName, IN PDEVICE_OBJECT *pdo) { PWDM_DEVICE_EXTENSION dx=(PWDM_DEVICE_EXTENSION )fdo->DeviceExtension; *PRegFlags=WMIREG_FLAG_INSTANCE_PDO; *PRegisterPath=&servkey; RtlInitUnicodeString(MofResourceName,L\"MofResource\"); *pdo=dx->pdo; return STATUS_SUCCESS; } NTSTATUS QueryDataBlock( IN PDEVICE_OBJECT fdo, IN PIRP Irp, IN ULONG GuidIndex, IN ULONG InstanceIndex, IN ULONG InstanceCount, IN OUT PULONG InstanceLengthArray, IN ULONG OutBufferSize, OUT PUCHAR PBuffer) { PWDM_DEVICE_EXTENSION dx=(PWDM_DEVICE_EXTENSION )fdo->DeviceExtension; NTSTATUS status ; ULONG size=0; switch(GuidIndex) { case 0: { ULONG SymLinkNameLen=dx->ifSymLinkName.Length; size=sizeof(ULONG) +sizeof(USHORT)+SymLinkNameLen; if(OutBufferSize<size) { status=STATUS_BUFFER_TOO_SMALL; break; } *(ULONG*)PBuffer=*(ULONG*)SharedMemory; *(UCHAR*)PBuffer+=sizeof(ULONG); *(USHORT*)PBuffer=(USHORT)SymLinkNameLen; *(UCHAR*)PBuffer+=sizeof(USHORT); RtlCopyMemory(PBuffer,dx->ifSymLinkName.Buffer,SymLinkNameLen); * InstanceLengthArray=size; status=STATUS_SUCCESS; break; } default: status=STATUS_WMI_GUID_NOT_FOUND; break; } status=WmiCompleteRequest(fdo,Irp,status,size,IO_NO_INCREMENT); return status; } NTSTATUS SetDataBlock( IN PDEVICE_OBJECT fdo, IN PIRP Irp, IN ULONG GuidIndex, IN ULONG InstanceIndex, IN ULONG BufferSize, IN PUCHAR PBuffer) { return FailWmiRequest( fdo, Irp, GuidIndex); } NTSTATUS SetDataItem( IN PDEVICE_OBJECT fdo, IN PIRP Irp, IN ULONG GuidIndex, IN ULONG InstanceIndex, IN ULONG DataItemId, IN ULONG BufferSize, OUT PUCHAR PBuffer) { return FailWmiRequest( fdo, Irp, GuidIndex); } NTSTATUS FailWmiRequest( IN PDEVICE_OBJECT fdo, IN PIRP Irp, IN ULONG GuidIndex ) { PWDM_DEVICE_EXTENSION dx=(PWDM_DEVICE_EXTENSION )fdo->DeviceExtension; NTSTATUS status ; if(GuidIndex==0) status=STATUS_INVALID_DEVICE_REQUEST; else status=STATUS_WMI_GUID_NOT_FOUND; status=WmiCompleteRequest(fdo,Irp,status,0,IO_NO_INCREMENT); return status; } |
|
5楼#
发布于:2005-03-11 09:01
我从toaster例子中摘录相关代码并解释
if (SetupDiEnumDeviceInterfaces (hardwareDeviceInfo, 0, // No care about specific PDOs (LPGUID)&GUID_DEVINTERFACE_BUSENUM_TOASTER, 0, // &deviceInterfaceData)) //根据你的GUID枚举设备接口数据 { // // Allocate a function class device data structure to receive the // information about this particular device. // //得到设备信息数据 //由于无法预先得到缓冲区大小,所以采用先失败在重试的方法 //requiredLength返回需要的缓冲区大小 SetupDiGetDeviceInterfaceDetail ( HardwareDeviceInfo, DeviceInterfaceData, NULL, // probing so no output buffer yet 0, // probing so output buffer length of zero &requiredLength, NULL); // not interested in the specific dev-node predictedLength = requiredLength; deviceInterfaceDetailData = malloc (predictedLength); if(deviceInterfaceDetailData) { deviceInterfaceDetailData->cbSize = sizeof (SP_DEVICE_INTERFACE_DETAIL_DATA); } else { printf(\"Couldn\'t allocate %d bytes for device interface details.\\n\", predictedLength); return FALSE; } if (! SetupDiGetDeviceInterfaceDetail ( HardwareDeviceInfo, DeviceInterfaceData, deviceInterfaceDetailData, predictedLength, &requiredLength, NULL)) { printf(\"Error in SetupDiGetDeviceInterfaceDetail\\n\"); free (deviceInterfaceDetailData); return FALSE; } printf(\"Opening %s\\n\", deviceInterfaceDetailData->DevicePath); //得到CreateFile设备需要的设备路径 file = CreateFile ( deviceInterfaceDetailData->DevicePath, GENERIC_READ | GENERIC_WRITE, 0, // FILE_SHARE_READ | FILE_SHARE_WRITE NULL, // no SECURITY_ATTRIBUTES structure OPEN_EXISTING, // No special create flags 0, // No special attributes NULL); // No template file |
|
|
6楼#
发布于:2005-03-11 09:03
///////////////source/////////////////
TARGETNAME=WdmDriver TARGETTYPE=DRIVER DRIVERTYPE=WDM TARGETPATH=obj TARGETLIBS=$(DDK_LIB_PATH)\\stream.lib \\ $(DDK_LIB_PATH)\\wdm.lib \\ $(DDK_LIB_PATH)\\ntoskrnl.lib \\ $(DDK_LIB_PATH)\\wmilib.lib INCLUDES=$(BASEDIR)\\INC;$(BASEDIR)\\INC\\DDK SOURCES= WdmDriver.MOF\\ DriverEntry.cpp \\ dispatch.cpp\\ pnp.cpp\\ wmi.cpp\\ ////////////////wdmdriver.inf////////////////// ; INF file for installing POWTRACE service ; Copyright (C) 2002 by elitesoftstuff ; All rights reserved ; Install this INF file using the Install choice from the context menu. Then hand ; edit the registry to add POWTRACE as a LowerFilter for whatever device you want ; to filter. [Version] Signature=$CHICAGO$ Class =Unknown DriverVer=mm/dd/yyyy ; Copyright 2002 by Proseware, Inc. Provider=%MFGNAME% [Manufacturer] %MFGNAME%=WdmDriver [WdmDriver] %WdmDriver%=WdmDriver.Install,*WdmDriver [DestinationDirs] WdmDriver.Files.Driver.NT=10,system32\\Drivers [SourceDisksFiles] WdmDriver.sys=1 [SourceDisksNames] 1=\"WdmDriver directory\",,,objchk\\i386\\ ;------------------------------------------------------------------------------ ; Windows 2000 Sections ;------------------------------------------------------------------------------ [WdmDriver.Install.NT] CopyFiles=WdmDriver.Files.Driver.NT [WdmDriver.Files.Driver.NT] WdmDriver.sys,,,0x60 ; replace newer, suppress dialog [WdmDriver.Install.NT.services] AddService=WdmDriver,2,WdmDriverService [WdmDriverService] DisplayName=\"wdmDriver\" ServiceType=1 StartType=3 ErrorControl=1 ServiceBinary=%10%\\system32\\drivers\\WdmDriver.sys ;------------------------------------------------------------------------------ ; Windows 98 Sections ;------------------------------------------------------------------------------ ;[DefaultInstall] ;CopyFiles=DriverCopyFiles ;------------------------------------------------------------------------------ ; String Definitions ;------------------------------------------------------------------------------ [Strings] MFGNAME=\"伊莱特工作室软件部\" WdmDriver=\"伊莱特Wdm驱动\" /////////////wdmdriver.mof/////////////// [Dynamic, Provider(\"WMIProv\"), WMI, Description(\"Wdm Driver information\"), guid(\"59521A18-56CC-4E32-B166-684596F852BE\"), locale(\"MS\\\\0x409\")] class WdmInformation { [key, read] string InstanceName; [read] boolean Active; [WmiDataId(1), read, Description(\"first ulong of shared buffer!\") ] uint32 BufferFirstWord; [WmiDataId(2), read, Description(\"symbolic link name!\") ] string SymbolicLinkName; }; |
|
7楼#
发布于:2005-03-11 09:04
谁能帮我瞧瞧
是adddevice有问题吗????????????????? |
|
8楼#
发布于:2005-03-11 09:17
你没有call
IoCreateSymbolicLink() 没有symbolic link 当然找不到 !! |
|
9楼#
发布于:2005-03-11 09:27
你没有call 或者用我上面提的的方法(这种方法通用),或者用KMK的方法(这种方法简单常用)都可以 |
|
|
10楼#
发布于:2005-03-11 10:07
或者用我上面提的的方法(这种方法通用) 没注意到他用了 IoRegisterDeviceInterface() !! ifSymLinkName is NULL !!?? [编辑 - 3/11/05 by KMK] |
|
11楼#
发布于:2005-03-11 10:29
我用了
status=IoRegisterDeviceInterface(pdo, &WDM_GUID, NULL, &dx->ifSymLinkName); 这个函数,已注册symbolic link WDM_GUID也是我用guigen生成的,应该没问题呀:(((((((((((((((((( IoSetDeviceInterfaceState(&dx->ifSymLinkName,true); |
|
12楼#
发布于:2005-03-11 10:31
[quote] 或者用我上面提的的方法(这种方法通用) 没注意到他用了 IoRegisterDeviceInterface() !! ifSymLinkName is NULL !!?? [编辑 - 3/11/05 by KMK] [/quote] 我下载了他的代码RAR文件 在PnP.cpp中的AddDevice中看他调用了IoRegisterDeviceInterface |
|
|
13楼#
发布于:2005-03-11 10:38
我刚用softice调试过了:
//GUID WDMGUID=WdmGuid; NTSTATUS AddDevice(IN PDRIVER_OBJECT DriverObject, IN PDEVICE_OBJECT pdo) { NTSTATUS status; PDEVICE_OBJECT fdo; UNICODE_STRING devname; RtlInitUnicodeString(&devname, L\"\\\\Device\\\\WdmDriver0\"); //IoCreateDevice(DriverObject, sizeof(DEVICE_EXTENSION), &devname, ...); status=IoCreateDevice( DriverObject, sizeof(DEVICE_EXTENSION), &devname, FILE_DEVICE_UNKNOWN, FILE_DEVICE_SECURE_OPEN, FALSE, &fdo); if(NT_ERROR(status)) return status; PWDM_DEVICE_EXTENSION dx=(PWDM_DEVICE_EXTENSION )fdo->DeviceExtension; dx->pdo=pdo; dx->fdo=fdo; status=IoRegisterDeviceInterface(pdo, &WDM_GUID, NULL, &dx->ifSymLinkName); if(NT_ERROR(status)) { // unable to register interface KdPrint((DRIVERNAME \" - IoRegisterDeviceInterface failed - %8.8lX\\n\", status)); IoDeleteDevice(fdo); return status; } IoSetDeviceInterfaceState(&dx->ifSymLinkName,true); dx->NextDevice=IoAttachDeviceToDeviceStack(fdo,pdo); fdo->Flags&=~DO_DEVICE_INITIALIZING; fdo->Flags|=DO_BUFFERED_IO; RegisterWmi(fdo); return STATUS_SUCCESS; } softice可以抓到 fdo->Flags&=~DO_DEVICE_INITIALIZING; fdo->Flags|=DO_BUFFERED_IO; 这两个断点,说明符号连接名已注册 就是说status=IoRegisterDeviceInterface(pdo, &WDM_GUID, NULL, &dx->ifSymLinkName);是正确的。 驱动程序很简单,没用到其他的东西呀:(((( |
|
14楼#
发布于:2005-03-11 10:42
难道和wmi。cpp有关,可是我用户层没用到它的接口函数
和inf文件也该没关吧 真是万事开头难呀:(((((((((( |
|
15楼#
发布于:2005-03-11 10:43
RtlCopyMemory(PBuffer,dx->ifSymLinkName.Buffer,SymLinkNameLen);
Buffer在这是空的 ! [编辑 - 3/11/05 by KMK] |
|
16楼#
发布于:2005-03-11 10:57
可我用softice
status=IoRegisterDeviceInterface(pdo, &WDM_GUID, NULL, &dx->ifSymLinkName); if(NT_ERROR(status)) { // unable to register interface KdPrint((DRIVERNAME \" - IoRegisterDeviceInterface failed - %8.8lX\\n\", status)); ////////////////////////这句softice没跟踪进去说明、 //IoRegisterDeviceInterface,注册是成功的呀 IoDeleteDevice(fdo); return status; } RtlCopyMemory(PBuffer,dx->ifSymLinkName.Buffer,SymLinkNameLen);这是在wmi力的查询符号链接名的。 |
|
17楼#
发布于:2005-03-11 11:03
注册设备接口与注册符号联接是两码子回事
|
|
|
18楼#
发布于:2005-03-11 11:10
我的adddevice是标准的编法,
实在查不出adddevice哪里有错:(((((( |
|
19楼#
发布于:2005-03-11 11:49
设备接口
用旧的命名方法命名设备对象,并创建一个应用程序能够使用的符号连接,存在着两个主要问题。命名设备对象所带来的潜在安全问题我们已经讨论过。此外,访问设备的应用程序需要先知道设备采用的命名方案。如果你的硬件仅由你的应用程序访问,那么不会有什么问题。但是,如果有其它公司想为你的硬件写应用程序,并且有许多硬件公司想制作相似的设备,那么设计一个合适的命名方案是困难的。最后,许多命名方案将依赖于程序员所说的自然语言,这不是一个好的选择。 为了解决这些问题,WDM引入了一个新的设备命名方案,该方案是语言中立的、易于扩展的、可用于许多硬件和软件厂商,并且易于文档化。该方案依靠一个设备接口(device interface)的概念,它基本上是软件如何访问硬件的一个说明。一个设备接口被一个128位的GUID唯一标识。你可以用平台SDK中的UUIDGEN工具或者GUIDGEN工具生成GUID,这两个工具输出同一种数,但格式不同。这个想法就象某些工业组织联合起来共同制定某种硬件的标准访问方法一样。在标准制作过程中,产生了一些GUID,这些GUID将永远关联到某些接口上。 注册设备接口 调用IoRegisterDeviceInterface函数,功能驱动程序的AddDevice函数可以注册一个或多个设备接口: #include <initguid.h> <--1 #include \"guids.h\" <--2 ... NTSTATUS AddDevice(...) { ... IoRegisterDeviceInterface(pdo, &GUID_SIMPLE, NULL, &pdx->ifname); <--3 ... } 我们包含了GUIDS.H头文件,那里定义了DEFINE_GUID宏。DEFINE_GUID通常声明一个外部变量。在驱动程序的某些地方,我们不得不为将要引用的每个GUID保留初始化的存储空间。系统头文件INITGUID.H利用某些预编译指令使DEFINE_GUID宏在已经定义的情况下仍能保留该存储空间。 我使用单独的头文件来保存我要引用的GUID定义。这是一个好的想法,因为用户模式的代码也需要包含这些定义,但它们不需要那些仅与内核模式驱动程序有关的声明。 IoRegisterDeviceInterface的第一个参数必须是设备PDO的地址。第二个参数指出与接口关联的GUID,第三个参数指出额外的接口细分类名。只有Microsoft的代码才使用名称细分类方案。第四个参数是一个UNICODE_STRING串的地址,该串用于接收设备对象的符号连接名。 IoRegisterDeviceInterface的返回值是一个Unicode串,这样在不知道驱动程序编码的情况下,应用程序能用该串确定并打开设备句柄。顺便说一下,这个名字比较丑陋;后面例子是我在Windows 98中为Sample设备生成的名字:\\DosDevices\\0000000000000007#{CAF53C68-A94C-11d2-BB4A-00C04FA330A6}。 注册过程实际就是先创建一个符号连接名,然后再把它存入注册表。之后,当响应PnP请求IRP_MN_START_DEVICE时,驱动程序将调用IoSetDeviceInterfaceState函数“使能”该接口: IoSetDeviceInterfaceState(&pdx->ifname, TRUE); 在响应这个调用过程中,I/O管理器将创建一个指向设备PDO的符号连接对象。以后,驱动程序会执行一个功能相反的调用禁止该接口(用FALSE做参数调用IoSetDeviceInterfaceState)。最后,I/O管理器删除符号连接对象,但它保留了注册表项,即这个名字将总与设备的这个实例关联;但符号连接对象与硬件一同到来或消失。 因为接口名最终指向PDO,所以PDO的安全描述符将最终控制设备的访问权限。这样比较好,因为只有管理员才可以通过控制台控制PDO的安全属性 |
|
上一页
下一页