TDI 编程--kernel socket 源码---DrvMain.cpp

来源:互联网 发布:学习编程先学什么 编辑:程序博客网 时间:2024/06/05 16:58

#include "KernelHeader.h"
#include "DrvMain.h"
#include "function.h"

 

#include "NetDrv.h"

 

extern "C"
{
VOID TDIFunLib_DriverUnload(PDRIVER_OBJECT  DriverObject);
NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING  pRegistryPath);

}


#pragma alloc_text( INIT, DriverEntry )
#pragma alloc_text( PAGE, TDIFunLib_DriverUnload)

 

/**********************************************************
 *
 * Fun: DriverEntry
 *
 *
 **********************************************************/

NTSTATUS
DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING  pRegistryPath)
{
 NTSTATUS NtStatus = STATUS_SUCCESS; 
 UNICODE_STRING usDriverName = {0};
 UNICODE_STRING usSymbolicLinkName = {0};
 PDEVICE_OBJECT pControlDeviceObject = NULL;
 UINT uiIndex = 0;

 RtlInitUnicodeString( &usDriverName, TDIFunLib_DEVICE_NAME);
 RtlInitUnicodeString( &usSymbolicLinkName, TDIFunLib_DOSDEVICE_NAME);
 

 /*
  * Create Named Control Device Object
  */

 NtStatus = IoCreateDevice( DriverObject, sizeof(TDIFunLib_DEVICE_EXTENSION), &usDriverName, FILE_DEVICE_UNKNOWN, FILE_DEVICE_SECURE_OPEN,FALSE, &pControlDeviceObject);
 if ( NtStatus != STATUS_SUCCESS ) 
 {
  return NtStatus ;
 }

 /*
  * Assign Dispatch Routine
  */
  
 for (uiIndex=0; uiIndex < IRP_MJ_MAXIMUM_FUNCTION; uiIndex++)
 {
  DriverObject->MajorFunction[uiIndex] = TDIFunLib_CommonDispatch;
 }
 
 DriverObject->MajorFunction[ IRP_MJ_CREATE ] = TDIFunLib_CreateDispatch;
 DriverObject->MajorFunction[ IRP_MJ_READ ]  = TDIFunLib_ReadDispatch;
 DriverObject->MajorFunction[ IRP_MJ_WRITE ]  = TDIFunLib_WriteDispatch;
 DriverObject->MajorFunction[ IRP_MJ_CLEANUP ] = TDIFunLib_CleanUpDispatch;
 DriverObject->MajorFunction[ IRP_MJ_CLOSE ]  = TDIFunLib_CloseDispatch;
 DriverObject->MajorFunction[ IRP_MJ_DEVICE_CONTROL ] = TDIFunLib_IoControlDispatch;
 DriverObject->MajorFunction[ IRP_MJ_INTERNAL_DEVICE_CONTROL ] = TDIFunLib_IoControlInternal;
 
 /*
  * If this Fun is missing, Driver can't be dynamically Unloaded.
  */

 DriverObject->DriverUnload = TDIFunLib_DriverUnload;
 

 /*
  * Set Flag On ControlDeviceObject to determin type of IO.  IO Manager Copy Data from Databuffer
  * given by user-mode app to sysBuffer On behalf of driver.
  */

 pControlDeviceObject->Flags |= DO_BUFFERED_IO;
 

 /*
  * Driver  Iniitalizing OK !!
  */

 pControlDeviceObject->Flags &= (~DO_DEVICE_INITIALIZING);
 

 /*
  * Create symbolicLink to ControlDeviceObject
  */
 
 IoCreateSymbolicLink(&usSymbolicLinkName, &usDriverName); 

 

 return NtStatus;
}

 

/**********************************************************
 *
 * Fun: TDIFunLib_DriverUnload
 *
 *
 **********************************************************/

VOID TDIFunLib_DriverUnload(PDRIVER_OBJECT  DriverObject)
{
 UNICODE_STRING usSymbolicLinkName;


 /*
  * Delete SymbolicLinkName
  */

 RtlInitUnicodeString( &usSymbolicLinkName, TDIFunLib_DOSDEVICE_NAME );

 IoDeleteSymbolicLink( &usSymbolicLinkName );


 /*
  * Delete DeviceObject
  */
 
  IoDeleteDevice( DriverObject->DeviceObject );
 
 
  /*
   *
   * Release Other resouces
   *
   */

 

原创粉丝点击