阅读:3103回复:27
应用程序怎么跟驱动程序相联系呀????
听说要用API可是我一点都不懂,不知道应用程序怎么跟驱动程序联系起来,懂的人进来指点指点呀!
|
|
|
沙发#
发布于:2003-05-07 20:45
(我只知道VXD的工作方式)在应用程序中间有个CreatFile和DeviceIoControl函数,首先通过CreatFile加载驱动,再通过DeviceIoControl调用驱动。其中第二个参数就是传到VXD去的一个变量。通过它你就可以分情况使用VXD了。
|
|
板凳#
发布于:2003-05-07 21:16
呵呵,我的是WDM的驱动呀,55555 :o
|
|
|
地板#
发布于:2003-05-08 09:16
我理解和VXD的有很多相似的地方,应用程序把硬件设备看做一个文件,通过GREATFILE来打开该设备,然后在调用DEVICEIOCTL,或者,WRITEFILE,或者READFILE等API函数,相应的驱动中分发例成被执行,完成读写数据的 功能。
最基本的是这样子。 |
|
|
地下室#
发布于:2003-05-08 10:36
WDM和VXD除了分层有点不同外,应该实现原理是差不多的吧
|
|
5楼#
发布于:2003-05-08 14:08
可不可以说的具体点呀!
|
|
|
6楼#
发布于:2003-05-08 20:05
具体的我就不懂了,只是看过写介绍,应该在具体实现上相差不大。你先找找书看吧
|
|
7楼#
发布于:2003-05-08 20:24
1、先获得设备接口,如下所示
///////////////////////////////////////////////////////////////////////////// // GetDeviceViaInterface: Open a handle via a device interface // 获得硬件设备接口 HANDLE GetDeviceViaInterface( GUID* pGuid, DWORD instance) { // Get handle to relevant device information set HDEVINFO info = SetupDiGetClassDevs(pGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE); if(info==INVALID_HANDLE_VALUE) return NULL; // Get interface data for the requested instance SP_INTERFACE_DEVICE_DATA ifdata; ifdata.cbSize = sizeof(ifdata); if(!SetupDiEnumDeviceInterfaces(info, NULL, pGuid, instance, &ifdata)) { SetupDiDestroyDeviceInfoList(info); return NULL; } // Get size of symbolic link name DWORD ReqLen; SetupDiGetDeviceInterfaceDetail(info, &ifdata, NULL, 0, &ReqLen, NULL); PSP_INTERFACE_DEVICE_DETAIL_DATA ifDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc(ReqLen); if( ifDetail==NULL) { SetupDiDestroyDeviceInfoList(info); return NULL; } // Get symbolic link name ifDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA); if( !SetupDiGetDeviceInterfaceDetail(info, &ifdata, ifDetail, ReqLen, NULL, NULL)) { SetupDiDestroyDeviceInfoList(info); //delete ifDetail; free( (PVOID)ifDetail ); return NULL; } // Open file HANDLE rv = CreateFile( ifDetail->DevicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0,//FILE_ATTRIBUTE_NORMAL, NULL); if( rv==INVALID_HANDLE_VALUE) rv = NULL; //delete ifDetail; free( (PVOID)ifDetail ); SetupDiDestroyDeviceInfoList(info); return rv; } |
|
|
8楼#
发布于:2003-05-08 20:26
2、将上面函数的返回值保存,就可以readfile、writefile和DeviceIoControl了
|
|
|
9楼#
发布于:2003-05-08 23:17
我也是从别的地方抄来的,行不行自己调试。
HANDLE GetDeviceViaInterface( GUID* pGuid, DWORD instance); int main(int argc, char* argv[]) { // Open device HANDLE hHiImWdm = GetDeviceViaInterface((LPGUID)&WDM_GUID,0); if( hHiImWdm==NULL) { printf(\"Could not find open HiImWdm device\\n\"); return 1; } printf(\"Opened OK\\n\"); // Read what\'s left in buffer DWORD TxdBytes; ULONG Rvalue = 0; if( !ReadFile( hHiImWdm, &Rvalue, 4, &TxdBytes, NULL)) printf(\"Could not read value\\n\"); else if( TxdBytes==4) printf(\"Read successfully read stored value of 0x%X\\n\",Rvalue); else printf(\"Wrong number of bytes read: %d\\n\",TxdBytes); // Write ULONG Wvalue = 0xabcdef01; if( !WriteFile( hHiImWdm, &Wvalue, 4, &TxdBytes, NULL)) printf(\"Could 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( hHiImWdm, &Rvalue, 4, &TxdBytes, NULL)) printf(\"Could not read value\\n\"); else if( TxdBytes==4) { if( Rvalue==Wvalue) printf(\"Read succeeded\\n\"); else printf(\"Read succeeded, but got wrong value: %X\",Rvalue); } else printf(\"Wrong number of bytes read: %d\\n\",TxdBytes); // Check duff write fails if( !WriteFile( hHiImWdm, &Wvalue, 5, &TxdBytes, NULL)) printf(\"Duff Write correctly failed with error %d\\n\",GetLastError()); else printf(\"Duff Write unexpectedly succeeded\\n\"); // Close device CloseHandle(hHiImWdm); return 0; } HANDLE GetDeviceViaInterface( GUID* pGuid, DWORD instance) { // Get handle to relevant device information set 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; } // Get interface data for the requested instance 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; } // Get size of symbolic link name 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); return NULL; } // Get symbolic link name ifDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA); if( !SetupDiGetDeviceInterfaceDetail(info, &ifdata, ifDetail, ReqLen, NULL, NULL)) { SetupDiDestroyDeviceInfoList(info); delete ifDetail; return NULL; } printf(\"Symbolic link is %s\\n\",ifDetail->DevicePath); // Open file 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; } |
|
10楼#
发布于:2003-05-08 23:27
还有这种方式,可惜我调试还没通过
typedef struct _WDM_DEVICE_EXTENSION { PDEVICE_OBJECT pdo; PDEVICE_OBJECT fdo; PDEVICE_OBJECT NextDevice; UNICODE_STRING ifSymLinkName; #if DBG WMILIB_CONTEXT WmiLibInfo; #endif // DBG } WDM_DEVICE_EXTENSION, *PWDM_DEVICE_EXTENSION; // Generated by DriverWizard version DriverStudio 2.5.0 (Build 201) // Set a default 32-bit tag value to be stored with each heap block // allocated by operator new. Use BoundsChecker to view the memory pool. // This value can be overridden using the global function SetPoolTag(). POOLTAG DefaultPoolTag(\'DPWS\'); // Create the global driver trace object // TODO: Use KDebugOnlyTrace if you want trace messages // to appear only in debug builds. Use KTrace if // you want trace messages to always appear. KTrace t(\"SWPDriver\"); ///////////////////////////////////////////////////////////////////// // Begin INIT section #pragma code_seg(\"INIT\") DECLARE_DRIVER_CLASS(SWPDriver, NULL) ///////////////////////////////////////////////////////////////////// // SWPDriver::DriverEntry // // Routine Description: // This is the first entry point called by the system when the // driver is loaded. // // Parameters: // RegistryPath - String used to find driver parameters in the // registry. To locate SWPDriver look for: // HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\SWPDriver // // Return Value: // NTSTATUS - Return STATUS_SUCCESS if no errors are encountered. // Any other indicates to the system that an error has occured. // // Comments: // NTSTATUS SWPDriver::DriverEntry(PUNICODE_STRING RegistryPath) { t << \"In DriverEntry\\n\"; // Open the \"Parameters\" key under the driver KRegistryKey Params(RegistryPath, L\"Parameters\"); if ( NT_SUCCESS(Params.LastError()) ) { #if DBG ULONG bBreakOnEntry = FALSE; // Read \"BreakOnEntry\" value from registry Params.QueryValue(L\"BreakOnEntry\", &bBreakOnEntry); // If requested, break into debugger if (bBreakOnEntry) DbgBreakPoint(); #endif // Load driver data members from the registry LoadRegistryParameters(Params); } m_Unit = 0; return STATUS_SUCCESS; } ///////////////////////////////////////////////////////////////////// // SWPDriver::LoadRegistryParameters // // Routine Description: // Load driver data members from the registry. // // Parameters: // Params - Open registry key pointing to \"Parameters\" // // Return Value: // None // // Comments: // Member variables are updated with values read from registry. // // The parameters are found as values under the \"Parameters\" key, // HKLM\\SYSTEM\\CurrentControlSet\\Services\\SWPDriver\\Parameters\\... // void SWPDriver::LoadRegistryParameters(KRegistryKey &Params) { m_bBreakOnEntry = FALSE; Params.QueryValue(L\"BreakOnEntry\", &m_bBreakOnEntry); t << \"m_bBreakOnEntry loaded from registry, resulting value: [\" << m_bBreakOnEntry << \"]\\n\"; m_SWPDRegister = 0x8888; Params.QueryValue(L\"SWPDRegister\", &m_SWPDRegister); t << \"m_SWPDRegister loaded from registry, resulting value: [\" << m_SWPDRegister << \"]\\n\"; } // End INIT section ///////////////////////////////////////////////////////////////////// #pragma code_seg() ///////////////////////////////////////////////////////////////////// // SWPDriver::AddDevice // // Routine Description: // Called when the system detects a device for which this // driver is responsible. // // Parameters: // Pdo - Physical Device Object. This is a pointer to a system device // object that represents the physical device. // // Return Value: // NTSTATUS - Success or failure code. // // Comments: // This function creates the Functional Device Object, or FDO. The FDO // enables this driver to handle requests for the physical device. // UNICODE_STRING HiImWdmRegistryPath; #define MofResourceNameText L\"MofResource\" #if DBG NTSTATUS QueryWmiRegInfo( IN PDEVICE_OBJECT fdo, OUT PULONG PRegFlags, OUT PUNICODE_STRING PInstanceName, OUT PUNICODE_STRING *PRegistryPath, OUT PUNICODE_STRING MofResourceName, OUT PDEVICE_OBJECT *Pdo) { PWDM_DEVICE_EXTENSION dx = (PWDM_DEVICE_EXTENSION)fdo->DeviceExtension; *PRegFlags = WMIREG_FLAG_INSTANCE_PDO; *PRegistryPath = &HiImWdmRegistryPath; RtlInitUnicodeString(MofResourceName, MofResourceNameText); *Pdo = dx->pdo; return STATUS_SUCCESS; } #endif //#if DBG /*WMIGUIDREGINFO HiImWdmWmiGuidList[1] = {*/ /* { &SWPDriverDevice_CLASS_GUID/*WMI_GUID*///, 1, 0 }, //}; //#endif // DBG 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; } NTSTATUS SetWmiDataBlock( 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 QueryWmiDataBlock( 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; // Check output buffer size if (OutBufferSize<size) { status = STATUS_BUFFER_TOO_SMALL; break; } // Store uint32 BufferFirstWord *(ULONG *)PBuffer = *(ULONG*)SharedMemory; (UCHAR *)PBuffer += sizeof(ULONG); // Store string SymbolicLinkName as counted Unicode *(USHORT *)PBuffer = (USHORT)SymLinkNameLen; (UCHAR *)PBuffer += sizeof(USHORT); RtlCopyMemory(PBuffer, dx->ifSymLinkName.Buffer, SymLinkNameLen); // Store total size *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 SetWmiDataItem( IN PDEVICE_OBJECT fdo, IN PIRP Irp, IN ULONG GuidIndex, IN ULONG InstanceIndex, IN ULONG DataItemId, IN ULONG BufferSize, IN PUCHAR PBuffer) { return FailWMIRequest(fdo, Irp, GuidIndex); } #if DBG WMIGUIDREGINFO HiImWdmWmiGuidList[1]; /*HiImWdmWmiGuidList[1].Guid=&SWPDriverDevice_CLASS_GUID; HiImWdmWmiGuidList[1].InstanceCount=1; ULONG Flags=1; */ #endif // DBG void RegisterWmi( IN PDEVICE_OBJECT fdo) { #if DBG PWDM_DEVICE_EXTENSION dx=(PWDM_DEVICE_EXTENSION)fdo->DeviceExtension; dx->WmiLibInfo.GuidCount = 1; dx->WmiLibInfo.GuidList = HiImWdmWmiGuidList; dx->WmiLibInfo.QueryWmiRegInfo = QueryWmiRegInfo; dx->WmiLibInfo.QueryWmiDataBlock = QueryWmiDataBlock; dx->WmiLibInfo.SetWmiDataBlock = SetWmiDataBlock; dx->WmiLibInfo.SetWmiDataItem = SetWmiDataItem; dx->WmiLibInfo.ExecuteWmiMethod = NULL; dx->WmiLibInfo.WmiFunctionControl = NULL; IoWMIRegistrationControl(fdo, WMIREG_ACTION_REGISTER); #endif // DBG } NTSTATUS SWPDriver::AddDevice(PDEVICE_OBJECT Pdo) { t << \"AddDevice called\\n\"; // Create the device object. Note that we used a form of \"placement\" new, // that is a member operator of KDevice. This form will use storage // allocated by the system in the device object\'s device to store our // class instance. SWPDriverDevice * pDevice = new ( static_cast<PCWSTR>(KUnitizedName(L\"SWPDriverDevice\", m_Unit)), FILE_DEVICE_UNKNOWN, NULL, 0, DO_DIRECT_IO | DO_POWER_PAGABLE ) SWPDriverDevice(Pdo, m_Unit); PDEVICE_OBJECT fdo; // Remember fdo in our device extension PWDM_DEVICE_EXTENSION dx = (PWDM_DEVICE_EXTENSION)fdo->DeviceExtension; dx->pdo = Pdo; dx->fdo = fdo; // Register and enable our device interface NTSTATUS status = IoRegisterDeviceInterface( Pdo, & , NULL, &dx->ifSymLinkName); if( NT_ERROR(status)) { IoDeleteDevice(fdo); return status; } IoSetDeviceInterfaceState(&dx->ifSymLinkName, TRUE); // Attach to the driver stack below us dx->NextDevice = IoAttachDeviceToDeviceStack(fdo,Pdo); // Set fdo flags appropriately fdo->Flags &= ~DO_DEVICE_INITIALIZING; fdo->Flags |= DO_BUFFERED_IO; RegisterWmi(fdo); if (pDevice == NULL) { t << \"Error creating device SWPDriverDevice\" << (ULONG) m_Unit << EOL; return STATUS_INSUFFICIENT_RESOURCES; } status = pDevice->ConstructorStatus(); if ( !NT_SUCCESS(status) ) { t << \"Error constructing device SWPDriverDevice\" << (ULONG) m_Unit << \" status \" << (ULONG) status << EOL; delete pDevice; } else { m_Unit++; pDevice->ReportNewDevicePowerState(PowerDeviceD0); } return status; } :D |
|
11楼#
发布于:2003-05-09 11:10
请问mailme大哥,
那个guid如何获得? 还有如何得到usb鼠标的guid? [编辑 - 5/9/03 by waterwindsxu] |
|
|
12楼#
发布于:2003-05-09 12:17
谢谢各位给我的参考,我好好研究一下!
|
|
|
13楼#
发布于:2003-05-09 13:49
请问mailme大哥, 用工具产生就可以了,不记得在ddk还是sdk中了 |
|
|
14楼#
发布于:2003-05-09 22:15
我已经调试过了,程序能够取得GUID,可是,GETPATH取不到路径。
|
|
15楼#
发布于:2003-05-15 14:28
我已能够得到句柄和可以读写,只是不知道驱动中的KIRP结构,我想直接赋值,但是总是死机。
|
|
16楼#
发布于:2003-05-16 13:34
呵呵,我的是WDM的驱动呀,55555 :o 对 WDM 驱动 也能 CreatFile和DeviceIoControl ,只要在驱动中创建一个符号链接名,用IoCreateSymbolicLink(...)。 |
|
|
17楼#
发布于:2003-05-16 15:56
同意swf2003
|
|
18楼#
发布于:2003-05-16 18:57
谢谢,各位了
|
|
|
19楼#
发布于:2003-05-16 22:01
问一个问题:应用程序怎么和驱动程序相联系,比如说我做一个视频采集卡的USB驱动,于是驱动程序是和那个硬件是对应的,但我做的应用程序是个播放器,我在播放器中按个按钮要执行播放采集来的图象的操作,那么驱动程序就工作了。这个过程是应用程序调用了驱动程序里的一个函数呢,还是在驱动程序生成过程中通过某个机制已经和应用程序联体了?
|
|
上一页
下一页