如何編寫Windows CE.net的usb驅動程序(2)
另外一個函數(shù)是USBUninstallDriver()函數(shù),具體代碼如下:
externCBOOL
USBUnInstallDriver()
{
BOOLfRet=FALSE;
HINSTANCEhInst=LoadLibrary(LUSBD.DLL);
if(hInst)
{
LPUN_REGISTER_CLIENT_DRIVER_IDpUnRegisterId=
(LPUN_REGISTER_CLIENT_DRIVER_ID)
GetProcAddress(hInst,gcszUnRegisterClientDriverId);
LPUN_REGISTER_CLIENT_SETTINGSpUnRegisterSettings=
(LPUN_REGISTER_CLIENT_SETTINGS)GetProcAddress(hInst,
gcszUnRegisterClientSettings);
if(pUnRegisterSettings)
{
USB_DRIVER_SETTINGSDriverSettings;
DriverSettings.dwCount=sizeof(DriverSettings);
//必須填入與注冊時相同的信息。
DriverSettings.dwVendorId=USB_NO_INFO;
DriverSettings.dwProductId=USB_NO_INFO;
DriverSettings.dwReleaseNumber=USB_NO_INFO;
DriverSettings.dwDeviceClass=USB_NO_INFO;
DriverSettings.dwDeviceSubClass=USB_NO_INFO;
DriverSettings.dwDeviceProtocol=USB_NO_INFO;
DriverSettings.dwInterfaceClass=0x03;//HID
DriverSettings.dwInterfaceSubClass=0x01;//bootdevice
DriverSettings.dwInterfaceProtocol=0x02;//mouse
fRet=(*pUnRegisterSettings)(gcszMouseDriverId,NULL,
DriverSettings);
}
if(pUnRegisterId)
{
BOOLfRetTemp=(*pUnRegisterId)(gcszMouseDriverId);
fRet=fRet?fRetTemp:fRet;
}
FreeLibrary(hInst);
}
returnfRet;
}
此函數(shù)主要用于刪除USBInstallDriver()時創(chuàng)建的注冊表信息,同樣的它使用自己的函數(shù)接口UnRegisterClientDriverID()和UnRegisterClientSettings()來做相應的處理。
另外一個需要處理的注冊的監(jiān)控通知函數(shù)USBDeviceNotifications():
externCBOOLUSBDeviceNotifications(LPVOIDlpvNotifyParameter,DWORDdwCode,
LPDWORD*dwInfo1,LPDWORD*dwInfo2,LPDWORD*dwInfo3,
LPDWORD*dwInfo4)
{
CMouse*pMouse=(CMouse*)lpvNotifyParameter;
switch(dwCode)
{
caseUSB_CLOSE_DEVICE:
//刪除相關的資源。
deletepMouse;
returnTRUE;
}
returnFALSE;
}
USB鼠標的類的定義如下:
classCMouse
{
public:
CMouse::CMouse(USB_HANDLEhDevice,LPCUSB_FUNCSlpUsbFuncs,
LPCUSB_INTERFACElpInterface);
~CMouse();
BOOLInitialize();
private:
//傳輸完畢調用的回調函數(shù)
staticDWORDCALLBACKMouseTransferCompleteStub(LPVOIDlpvNotifyParameter);
//中斷處理函數(shù)
staticULONGCALLBACKCMouse::MouseThreadStub(PVOIDcontext);
DWORDMouseTransferComplete();
DWORDMouseThread();
BOOLSubmitInterrupt();
BOOLHandleInterrupt();
BOOLm_fClosing;
BOOLm_fReadyForMouseEvents;
HANDLEm_hEvent;
HANDLEm_hThread;
USB_HANDLEm_hDevice;
USB_PIPEm_hInterruptPipe;
USB_TRANSFERm_hInterruptTransfer;
LPCUSB_FUNCSm_lpUsbFuncs;
LPCUSB_INTERFACEm_pInterface;
BOOLm_fPrevButton1;
BOOLm_fPrevButton2;
BOOLm_fPrevButton3;
//數(shù)據(jù)接受緩沖區(qū)。
BYTEm_pbDataBuffer[8];
};
具體實現(xiàn)如下:
//構造函數(shù),初始化時調用
CMouse::CMouse(USB_HANDLEhDevice,LPCUSB_FUNCSlpUsbFuncs,
LPCUSB_INTERFACElpInterface)
{
m_fClosing=FALSE;
m_fReadyForMouseEvents=FALSE;
m_hEvent=NULL;
m_hThread=NULL;
m_hDevice=hDevice;
m_hInterruptPipe=NULL;
m_hInterruptTransfer=NULL;
m_lpUsbFuncs=lpUsbFuncs;
m_pInterface=lpInterface;
m_fPrevButton1=FALSE;
m_fPrevButton2=FALSE;
m_fPrevButton3=FALSE;
memset(m_pbDataBuffer,0,sizeof(m_pbDataBuffer));
}
//析構函數(shù),用于清除申請的資源。
CMouse::~CMouse()
{
//通知系統(tǒng)去關閉相關的函數(shù)接口。
m_fClosing=TRUE;
//Wakeuptheconnectionthreadagainandgiveittimetodie.
if(m_hEvent!=NULL)
{
//通知關閉數(shù)據(jù)接受線程。
SetEvent(m_hEvent);
if(m_hThread!=NULL)
{
DWORDdwWaitReturn;
dwWaitReturn=WaitForSingleObject(m_hThread,1000);
if(dwWaitReturn!=WAIT_OBJECT_0)
{
TerminateThread(m_hThread,DWORD(-1));
}
CloseHandle(m_hThread);
m_hThread=NULL;
}
CloseHandle(m_hEvent);
m_hEvent=NULL;
}
if(m_hInterruptTransfer)
(*m_lpUsbFuncs->lpCloseTransfer)(m_hInterruptTransfer);
if(m_hInterruptPipe)
(*m_lpUsbFuncs->lpClosePipe)(m_hInterruptPipe);
}
//初始化USB鼠標驅動程序
BOOLCMouse::Initialize()
{
LPCUSB_DEVICElpDeviceInfo=(*m_lpUsbFuncs->lpGetDeviceInfo)(m_hDevice);
//檢測配置:USB鼠標應該只有一個中斷管道
if((m_pInterface->lpEndpoints[0].Descriptor.bmAttributesUSB_ENDPOINT_TYPE_MASK)!=USB_ENDPOINT_TYPE_INTERRUPT)
{
RETAILMSG(1,(TEXT(!USBMouse:EP0wrongtype(%u)!rn),
評論