heshw99
驱动牛犊
驱动牛犊
  • 注册日期2003-03-08
  • 最后登录2004-12-01
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
阅读:3217回复:27

应用程序怎么跟驱动程序相联系呀????

楼主#
更多 发布于:2003-05-07 20:24
听说要用API可是我一点都不懂,不知道应用程序怎么跟驱动程序联系起来,懂的人进来指点指点呀!

最新喜欢:

lmhhlmhhlmhhlm... huoyanhuoyan
凉风有性,秋月无边,亏我思娇的情绪好比度日如年,虽然我不是玉树临风,潇洒倜傥,但是我有广阔的胸襟和强健的背弯......
AthlonXP
驱动小牛
驱动小牛
  • 注册日期2003-05-11
  • 最后登录2010-01-20
  • 粉丝0
  • 关注0
  • 积分10分
  • 威望1点
  • 贡献值0点
  • 好评度1点
  • 原创分0分
  • 专家分0分
沙发#
发布于:2003-05-21 11:53
windows wdm设备驱动程序开发指南一书上说的很清楚,也有相应的例子,我试过,可以的,但他只能出现一个32位的dos窗口,我想把他做成win界面下的,可调用hWdm1 = GetDeviceViaInterface((LPGUID)&WDM1_GUID, 0);时编译就不能通过,说是redefine了,哪位做过的前辈来指导一下了.有同样困难的也可以一起探讨.
email:yxp@qixiangc.com.
qq:17171109


你把guid换个值试试 :)
AthlonXP是个好人,他一生没有欺压过同侪,没有背叛过朋友。从小到大,他都是个好孩子,没有出过什么差错,没有让家人师长担过心。虽然用一句时髦的话,他不善于"推销"自己,但是做学问扎扎实实,不会投机取巧;做工作也是一步一个脚印,不会敷衍塞责。他虽然是个大烟枪,偶尔也喝喝酒,但是没有什么见不得人的恶习。他花钱虽然不特别大方,可是也不吝啬。许多不如他的人,或幸或不幸,一般的也都成了家。可是为什么MM不喜欢他呢??
hamsir
驱动小牛
驱动小牛
  • 注册日期2003-03-24
  • 最后登录2003-06-17
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
板凳#
发布于:2003-05-19 11:24
看你的说法,是不是用的windriver?
如果是回个话,我可以告诉你。
一只流泪的鱼
blue_wind
驱动中牛
驱动中牛
  • 注册日期2002-11-24
  • 最后登录2006-11-30
  • 粉丝0
  • 关注0
  • 积分16分
  • 威望3点
  • 贡献值0点
  • 好评度1点
  • 原创分0分
  • 专家分0分
地板#
发布于:2003-05-18 22:24
windows wdm设备驱动程序开发指南一书上说的很清楚,也有相应的例子,我试过,可以的,但他只能出现一个32位的dos窗口,我想把他做成win界面下的,可调用hWdm1 = GetDeviceViaInterface((LPGUID)&WDM1_GUID, 0);时编译就不能通过,说是redefine了,哪位做过的前辈来指导一下了.有同样困难的也可以一起探讨.
email:yxp@qixiangc.com.
qq:17171109
好东西大家共享,没必要做从复的劳动! 冒着微软的炮火前进!前进!前进!进!
najjalover
驱动牛犊
驱动牛犊
  • 注册日期2003-03-25
  • 最后登录2003-05-17
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
地下室#
发布于:2003-05-17 13:22
Sorry,我只做过VXD的,别的不知道了
arthurtu
驱动巨牛
驱动巨牛
  • 注册日期2001-11-08
  • 最后登录2020-12-19
  • 粉丝0
  • 关注0
  • 积分26分
  • 威望161点
  • 贡献值0点
  • 好评度35点
  • 原创分0分
  • 专家分0分
  • 社区居民
5楼#
发布于:2003-05-17 13:18
[quote]谢谢,各位了

是这样一个过程:应用程序中有个DEVICEIOCONTROL函数,当调用它时,相当于在驱动程序中调用ONWIN32DEVICEIOCONTROL函数,在应用程序的DEVICEIOCONTROL函数中有个控制代码可以控制驱动程序的工作 [/quote]
这是vxd的。
najjalover
驱动牛犊
驱动牛犊
  • 注册日期2003-03-25
  • 最后登录2003-05-17
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
6楼#
发布于:2003-05-17 13:11
谢谢,各位了

是这样一个过程:应用程序中有个DEVICEIOCONTROL函数,当调用它时,相当于在驱动程序中调用ONWIN32DEVICEIOCONTROL函数,在应用程序的DEVICEIOCONTROL函数中有个控制代码可以控制驱动程序的工作
sunnyflyfox
驱动牛犊
驱动牛犊
  • 注册日期2003-04-09
  • 最后登录2004-01-09
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
7楼#
发布于:2003-05-17 08:48
还有两个类,有兴趣的同我联系
Email:sunnyflyfox@163.com
sunnyflyfox
驱动牛犊
驱动牛犊
  • 注册日期2003-04-09
  • 最后登录2004-01-09
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
8楼#
发布于:2003-05-17 08:45
我已成功了,我把代码发给各位参考吧:
 

HANDLE GetDeviceViaInterface( GUID* pGuid, DWORD instance);

extern GUID DriverPrint=   { 0xaba1121c, 0xebfb, 0x4796, { 0xb5, 0xbb, 0xd1, 0xb6, 0xc7, 0xf9, 0x8, 0xdc }  };


//extern GUID DriverPrint=  { 0x87472ba0, 0x61bc, 0x11d2, 0xb6, 0x77, 0x0, 0xc0, 0xdf, 0xe4, 0xc1, 0xf3};

/*HANDLE OpenByInterface(
  GUID pClassGuid,
  DWORD instance,
  PDWORD pError
  )
{
HANDLE hDev;
CDeviceInterfaceClass DevClass(&pClassGuid,pError);
// if(*pError!=ERROR_SUCCESS)
// return INVALID_HANDLE_VALUE;
CDeviceInterface DevInterface(&DevClass,instance,pError);

// if(*pError!=ERROR_SUCCESS)
// return INVALID_HANDLE_VALUE;
 
hDev=CreateFile(
DevInterface.DevicePath(),
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
   DWORD d=GetLastError();
   //2 The system cannot find the file specified.  
// Add \"About...\" menu item to system menu.
//3 The system cannot find the path specified.  ERROR_PATH_NOT_FOUND
// if(hDev==INVALID_HANDLE_VALUE)
// *pError=GetLastError();
return hDev;
} */

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;
}

class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestCKPDriverDlg dialog

CTestCKPDriverDlg::CTestCKPDriverDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestCKPDriverDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTestCKPDriverDlg)
m_strList = _T(\"\");
m_lgRange = 0;
m_ulOffset = 0;
m_ulStart  = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTestCKPDriverDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTestCKPDriverDlg)
DDX_Control(pDX, IDC_EDIT_OFFSET, m_conOffset);
DDX_Control(pDX, IDC_RADIO_READ, m_conRead);
DDX_Control(pDX, IDC_RADIO_BASE8, m_cbuttonRadio);
DDX_Control(pDX, IDC_EDIT_START, m_conStart);
DDX_Control(pDX, IDC_EDIT_RANGE, m_conRange);
DDX_Control(pDX, IDC_EDIT_LIST, m_conList);
DDX_Text(pDX, IDC_EDIT_LIST, m_strList);
DDX_Text(pDX, IDC_EDIT_RANGE, m_lgRange);
DDX_Text(pDX, IDC_EDIT_OFFSET, m_ulOffset);
DDX_Text(pDX, IDC_EDIT_START, m_ulStart);
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestCKPDriverDlg, CDialog)
//{{AFX_MSG_MAP(CTestCKPDriverDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_CLEAR, OnButtonClear)
ON_BN_CLICKED(IDC_BUTTON_APPLY, OnButtonApply)
ON_BN_CLICKED(IDC_RADIO_BASE8, OnRadioBase8)
ON_BN_CLICKED(IDC_RADIO_BASE32, OnRadioBase32)
ON_BN_CLICKED(IDC_RADIO_READ, OnRadioRead)
ON_BN_CLICKED(IDC_RADIO_WRITE, OnRadioWrite)
ON_BN_CLICKED(IDC_RADIO_SEARCH, OnRadioSearch)
ON_BN_CLICKED(IDEXIT, OnExit)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestCKPDriverDlg message handlers

BOOL CTestCKPDriverDlg::OnInitDialog()
{
CDialog::OnInitDialog();
//初始化单选按钮
  m_uiCommand=1;
    m_uiMemory=1;
m_ulStart=0;
    m_cbuttonRadio.SetCheck(1);
    m_conRead.SetCheck(1);
 
DWORD instance=1;
  PDWORD pError=NULL;
 
/* hCom=CreateFile(
\"\\\\\\.\\\\SWPrintDevice0\",
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);*/
     GUID* pGuid=(GUID*)&DriverPrint;
DWORD instance1=0;
    hCom=GetDeviceViaInterface( pGuid,instance1);
    DWORD d=GetLastError();
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// Set the icon for this dialog.  The framework does this automatically
//  when the application\'s main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE);// Set small icon
return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTestCKPDriverDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CTestCKPDriverDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTestCKPDriverDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

void CTestCKPDriverDlg::OnButtonClear()
{
   m_strList=\" \";
   UpdateData(false);

}

void CTestCKPDriverDlg::OnOK()
{
// TODO: Add extra validation here

CDialog::OnOK();
}


void CTestCKPDriverDlg::OnButtonApply()
{
  UpdateData(true);
  DWORD lpNumberOfBytesInPut=0;
  seBuffer[0]=m_uiCommand*16+m_uiMemory;
  seBuffer[1]=m_ulStart;
  seBuffer[2]=m_ulOffset;
  seBuffer[3]=m_lgRange;
 
  BOOL status=WriteFile(hCom ,&seBuffer,4,&lpNumberOfBytesInPut,NULL);
  if(!status)
     return;
   DWORD lpNumberOfBytesRead ;
   BOOL staus=ReadFile(hCom, &reBuffer,4,&lpNumberOfBytesRead,NULL);
 
  switch(m_uiCommand)
  {
  case 1:
 {
    //  LPDWORD lpNumberOfBytesRead=NULL; // pointer to number of bytes read
 //   BOOL staus=ReadFile(hCom, &reBuffer[0],4,lpNumberOfBytesRead,NULL);
   //dEorror=GetLastError();
 readData();
 break;
 }
  case 2:
 {
      LPDWORD lpNumberOfBytesWrite=NULL; // pointer to number of bytes read
 writeData();
//  BOOL staus=WriteFile(hCom,seBuffer,256,lpNumberOfBytesWrite,NULL);
 break;
 }
  case 3:
 ;break;

  }
  UpdateData(false);
}

void CTestCKPDriverDlg::readData()
{
    m_strList.Empty( );
    m_strList=(UCHAR)reBuffer;
    m_strList.Format(\"%c\");
    m_strList+=\"reciev  data) !\";
    UpdateData(false);
}

void CTestCKPDriverDlg::writeData()
{
  m_strList.Empty( );
  UpdateData(true);
  m_strList.Format(\"%c\");
  m_strList+=\"Sent data!\";
 // for(int i=0;i<m_strList.GetLength();i++)
//  seBuffer=m_strList.GetAt(i);
 
}

void CTestCKPDriverDlg::OnRadioBase8()
{
 
    m_uiMemory=1;
}

void CTestCKPDriverDlg::OnRadioBase32()
{
 
    m_uiMemory=2;

}

void CTestCKPDriverDlg::OnRadioRead()
{
  m_uiCommand=1;
 
}

void CTestCKPDriverDlg::OnRadioWrite()
{
  m_uiCommand=2;

}

void CTestCKPDriverDlg::OnRadioSearch()
{
  m_uiCommand=3;

}



void CTestCKPDriverDlg::OnExit()
{
CDialog::OnCancel();
}

lele1980
驱动牛犊
驱动牛犊
  • 注册日期2003-04-30
  • 最后登录2003-06-06
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
9楼#
发布于:2003-05-16 22:01
问一个问题:应用程序怎么和驱动程序相联系,比如说我做一个视频采集卡的USB驱动,于是驱动程序是和那个硬件是对应的,但我做的应用程序是个播放器,我在播放器中按个按钮要执行播放采集来的图象的操作,那么驱动程序就工作了。这个过程是应用程序调用了驱动程序里的一个函数呢,还是在驱动程序生成过程中通过某个机制已经和应用程序联体了?
heshw99
驱动牛犊
驱动牛犊
  • 注册日期2003-03-08
  • 最后登录2004-12-01
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
10楼#
发布于:2003-05-16 18:57
谢谢,各位了
凉风有性,秋月无边,亏我思娇的情绪好比度日如年,虽然我不是玉树临风,潇洒倜傥,但是我有广阔的胸襟和强健的背弯......
scsi
驱动中牛
驱动中牛
  • 注册日期2002-03-22
  • 最后登录2011-03-09
  • 粉丝0
  • 关注0
  • 积分1000分
  • 威望116点
  • 贡献值0点
  • 好评度106点
  • 原创分0分
  • 专家分0分
11楼#
发布于:2003-05-16 15:56
同意swf2003
swf2003
驱动中牛
驱动中牛
  • 注册日期2003-02-13
  • 最后登录2011-10-28
  • 粉丝1
  • 关注0
  • 积分4分
  • 威望22点
  • 贡献值0点
  • 好评度20点
  • 原创分0分
  • 专家分0分
12楼#
发布于:2003-05-16 13:34
呵呵,我的是WDM的驱动呀,55555 :o


对 WDM 驱动 也能 CreatFile和DeviceIoControl ,只要在驱动中创建一个符号链接名,用IoCreateSymbolicLink(...)。
你的认可是对我最大的鼓励!
sunnyflyfox
驱动牛犊
驱动牛犊
  • 注册日期2003-04-09
  • 最后登录2004-01-09
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
13楼#
发布于:2003-05-15 14:28
我已能够得到句柄和可以读写,只是不知道驱动中的KIRP结构,我想直接赋值,但是总是死机。
sunnyflyfox
驱动牛犊
驱动牛犊
  • 注册日期2003-04-09
  • 最后登录2004-01-09
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
14楼#
发布于:2003-05-09 22:15
我已经调试过了,程序能够取得GUID,可是,GETPATH取不到路径。
mailme
驱动老牛
驱动老牛
  • 注册日期2001-05-21
  • 最后登录2010-02-25
  • 粉丝0
  • 关注0
  • 积分26分
  • 威望3点
  • 贡献值0点
  • 好评度2点
  • 原创分0分
  • 专家分0分
15楼#
发布于:2003-05-09 13:49
请问mailme大哥,
那个guid如何获得?
还有如何得到usb鼠标的guid?

[编辑 -  5/9/03 by  waterwindsxu]


用工具产生就可以了,不记得在ddk还是sdk中了
早起早睡 精神百倍
heshw99
驱动牛犊
驱动牛犊
  • 注册日期2003-03-08
  • 最后登录2004-12-01
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
16楼#
发布于:2003-05-09 12:17
谢谢各位给我的参考,我好好研究一下!
凉风有性,秋月无边,亏我思娇的情绪好比度日如年,虽然我不是玉树临风,潇洒倜傥,但是我有广阔的胸襟和强健的背弯......
waterwindsxu
驱动小牛
驱动小牛
  • 注册日期2003-04-28
  • 最后登录2018-03-04
  • 粉丝0
  • 关注0
  • 积分58分
  • 威望165点
  • 贡献值0点
  • 好评度4点
  • 原创分0分
  • 专家分0分
  • 社区居民
17楼#
发布于:2003-05-09 11:10
请问mailme大哥,
那个guid如何获得?
还有如何得到usb鼠标的guid?

[编辑 -  5/9/03 by  waterwindsxu]
上善若水! 凯风永至!
sunnyflyfox
驱动牛犊
驱动牛犊
  • 注册日期2003-04-09
  • 最后登录2004-01-09
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
18楼#
发布于: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
sunnyflyfox
驱动牛犊
驱动牛犊
  • 注册日期2003-04-09
  • 最后登录2004-01-09
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
19楼#
发布于: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;
}
上一页
游客

返回顶部