usbui.c
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 8k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. #include "stdafx.h"
  2. #include "systray.h"
  3. #include <stdio.h>
  4. #include <initguid.h>
  5. #include <usbioctl.h>
  6. #include <wmium.h>
  7. #include <tchar.h>
  8. #define USBUIMENU               100
  9. #define NUM_HCS_TO_CHECK 10
  10. typedef int (CALLBACK *USBERRORMESSAGESCALLBACK) 
  11.     (PUSB_CONNECTION_NOTIFICATION,LPTSTR);
  12. extern HINSTANCE g_hInstance;
  13. static BOOL    g_bUSBUIEnabled = FALSE;
  14. static BOOL    g_bUSBUIIconShown = FALSE;
  15. static HICON   g_hUSBUIIcon = NULL;
  16. static HINSTANCE g_hUsbWatch = NULL;
  17. static USBERRORMESSAGESCALLBACK g_UsbHandler = NULL;
  18. int _cdecl main(){
  19.     return 0;
  20. }
  21.               
  22. #define USBUI_OffsetToPtr(Base, Offset) ((PBYTE)((PBYTE)Base + Offset))
  23. LPTSTR USBUI_CountedStringToSz(LPTSTR lpString)
  24. {
  25.    SHORT    usNameLength;
  26.    LPTSTR  lpStringPlusNull;
  27.    usNameLength = * (USHORT *) lpString;
  28.    lpStringPlusNull = (LPTSTR) LocalAlloc(LMEM_ZEROINIT,
  29.                                           sizeof(TCHAR) * (usNameLength+1));
  30.    if (lpStringPlusNull != NULL) {
  31.       lpString = (LPTSTR) USBUI_OffsetToPtr(lpString, sizeof(USHORT));
  32.       wcsncpy( lpStringPlusNull, lpString, usNameLength );
  33.       lpStringPlusNull[usNameLength] = TEXT('0');
  34.       // _tcscpy( lpStringPlusNull + usNameLength, _TEXT("") );
  35.    }
  36.    return lpStringPlusNull;
  37. }
  38. void USBUI_EventCallbackRoutine(PWNODE_HEADER WnodeHeader, ULONG Context)
  39. {
  40.     PWNODE_SINGLE_INSTANCE          wNode = (PWNODE_SINGLE_INSTANCE)WnodeHeader;
  41.     PUSB_CONNECTION_NOTIFICATION    usbConnectionNotification;
  42.     LPGUID                          eventGuid = &WnodeHeader->Guid;
  43.     LPTSTR                          strInstanceName;
  44.     if (memcmp(&GUID_USB_WMI_STD_DATA, eventGuid, sizeof(GUID)) == 0) {
  45.         usbConnectionNotification = (PUSB_CONNECTION_NOTIFICATION)
  46.                                     USBUI_OffsetToPtr(wNode, 
  47.                                                       wNode->DataBlockOffset);
  48.         //
  49.         // Get the instance name
  50.         //
  51.         strInstanceName = 
  52.             USBUI_CountedStringToSz((LPTSTR) 
  53.                                     USBUI_OffsetToPtr(wNode,
  54.                                                       wNode->OffsetInstanceName));
  55.         if (strInstanceName) {
  56.             if (g_hUsbWatch && g_UsbHandler) {
  57. USBUIEngageHandler:                    
  58.                 g_UsbHandler(usbConnectionNotification, strInstanceName);
  59.             } else {
  60.                 g_hUsbWatch = LoadLibrary(TEXT("usbui.dll"));
  61.                 g_UsbHandler = (USBERRORMESSAGESCALLBACK) 
  62.                     GetProcAddress(g_hUsbWatch, "USBErrorHandler");
  63.                 goto USBUIEngageHandler;
  64.             }
  65.             LocalFree(strInstanceName);
  66.         }
  67.     }
  68. }
  69. int USBUI_ErrorMessagesEnable(BOOL fEnable)
  70. {
  71.     ULONG status;
  72.     status = WmiNotificationRegistration((LPGUID) &GUID_USB_WMI_STD_DATA,
  73.                                      (BOOLEAN)fEnable,
  74.                                          USBUI_EventCallbackRoutine,
  75.                      0,
  76.                                          NOTIFICATION_CALLBACK_DIRECT);
  77.     return status;
  78. }
  79. void USBUI_Notify(HWND hwnd, WPARAM wParam, LPARAM lParam)
  80. {
  81.     switch (lParam)
  82.     {
  83.     case WM_RBUTTONUP:
  84.         {
  85.         USBUI_Menu(hwnd, 1, TPM_RIGHTBUTTON);
  86.         }
  87.     break;
  88.     case WM_LBUTTONDOWN:
  89.     {
  90.             SetTimer(hwnd, USBUI_TIMER_ID, GetDoubleClickTime()+100, NULL);
  91.     }
  92.         break;
  93.     case WM_LBUTTONDBLCLK:
  94.     {
  95.             KillTimer(hwnd, USBUI_TIMER_ID);
  96.             USBUI_Toggle();
  97.     }
  98.         break;
  99.     }
  100. }
  101. void USBUI_Toggle()
  102. {
  103.     USBUI_SetState(!g_bUSBUIEnabled);
  104. }
  105. void USBUI_Timer(HWND hwnd)
  106. {
  107.     KillTimer(hwnd, USBUI_TIMER_ID);
  108.     USBUI_Menu(hwnd, 0, TPM_LEFTBUTTON);
  109. }
  110. /*
  111. HMENU USBUI_CreateMenu()
  112. {
  113.     HMENU hmenu;
  114.     LPSTR lpszMenu1;
  115.     
  116.     hmenu = CreatePopupMenu();
  117.     if (!hmenu)
  118.     {
  119.         return NULL;
  120.     }
  121.     lpszMenu1 = LoadDynamicString(g_bUSBUIEnabled?IDS_USBUIDISABLE:IDS_USBUIENABLE);
  122.     
  123.     // AppendMenu(hmenu,MF_STRING,USBUIMENU,lpszMenu1);
  124. SysTray_AppendMenuString (hmenu,USBUIMENU,lpszMenu1);
  125.     SetMenuDefaultItem(hmenu,USBUIMENU,FALSE);
  126.     DeleteDynamicString(lpszMenu1);
  127.         
  128.     return hmenu;
  129. }
  130.   */
  131. void USBUI_Menu(HWND hwnd, UINT uMenuNum, UINT uButton)
  132. {
  133.     POINT   pt;
  134.     UINT    iCmd;
  135.     HMENU   hmenu = 0;
  136.     
  137.     GetCursorPos(&pt);
  138. //    hmenu = USBUI_CreateMenu();
  139.     if (!hmenu)
  140.     {
  141.         return;
  142.     }
  143.     
  144.     SetForegroundWindow(hwnd);
  145.     iCmd = TrackPopupMenu(hmenu, uButton | TPM_RETURNCMD | TPM_NONOTIFY, pt.x, pt.y, 0, hwnd, NULL);
  146.     
  147.     DestroyMenu(hmenu);
  148.     switch (iCmd)
  149.     {
  150.         case USBUIMENU:
  151.         {
  152.             USBUI_Toggle();
  153.         }
  154.         break;
  155.     }
  156. }
  157. BOOL USBUI_SetState(BOOL On)
  158. {
  159.     int retValue;
  160.     if (g_bUSBUIEnabled != On) {
  161.         //
  162.         // Only enable it if not already enabled
  163.         //
  164.         retValue = (int) USBUI_ErrorMessagesEnable (On);
  165.         g_bUSBUIEnabled = retValue ? g_bUSBUIEnabled : On;
  166.     }
  167.     return g_bUSBUIEnabled;
  168. }
  169. BOOL
  170. IsErrorCheckingEnabled()
  171. {
  172.     DWORD ErrorCheckingEnabled, type = REG_DWORD, size = sizeof(DWORD);
  173.     HKEY hKey;
  174.     //
  175.     // Check the registry value ErrorCheckingEnabled to make sure that we should
  176.     // be enabling this.
  177.     //
  178.     if (ERROR_SUCCESS != 
  179.         RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  180.                         TEXT("SOFTWARE\Microsoft\Windows\CurrentVersion\Usb"),
  181.                         0,
  182.                         KEY_READ,
  183.                         &hKey)) {
  184.         return TRUE;
  185.     }
  186.     if (ERROR_SUCCESS != RegQueryValueEx(hKey, 
  187.                                          TEXT("ErrorCheckingEnabled"),
  188.                                          0,
  189.                                          &type,
  190.                                          (LPBYTE) &ErrorCheckingEnabled,
  191.                                          &size)) {
  192.         return TRUE;
  193.     }
  194.     return (BOOL) ErrorCheckingEnabled;
  195. }
  196. BOOL USBUI_Init(HWND hWnd)
  197. {
  198. TCHAR       HCName[16];
  199.     BOOL        ControllerFound = FALSE;
  200. int         HCNum;
  201.     HANDLE      hHCDev;
  202.     //
  203.     // Check the registry to make sure that it is turned on
  204.     //
  205.     if (!IsErrorCheckingEnabled()) {
  206.         return FALSE;
  207.     }
  208.     //
  209.     // Check for the existence of a USB controller.
  210.     // If there is one, load and initialize USBUI.dll which will check for
  211.     // usb error messages.  If we can't open a controller, than we shouldn't
  212.     // load a USB watch dll.
  213.     //
  214. for (HCNum = 0; HCNum < NUM_HCS_TO_CHECK; HCNum++)
  215. {
  216.         wsprintf(HCName, TEXT("\\.\HCD%d"), HCNum);
  217.         hHCDev = CreateFile(HCName,
  218.                             GENERIC_READ,
  219.                             FILE_SHARE_READ,
  220.                             NULL,
  221.                             OPEN_EXISTING,
  222.                             0,
  223.                             NULL);
  224.         //
  225.         // If the handle is valid, then we've successfully opened a Host
  226.         // Controller.  
  227.         //
  228.         if (hHCDev != INVALID_HANDLE_VALUE) {
  229.             CloseHandle(hHCDev);
  230.             return TRUE;
  231.         }
  232.     }
  233.     return FALSE;
  234. }
  235. //
  236. //  Called at init time and whenever services are enabled/disabled.
  237. //
  238. BOOL USBUI_CheckEnable(HWND hWnd, BOOL bSvcEnabled)
  239. {
  240.     BOOL bEnable = bSvcEnabled && USBUI_Init(hWnd);
  241.     
  242.     if (bEnable != g_bUSBUIEnabled)
  243.     {
  244.         //
  245.         // state change
  246.         //
  247.     USBUI_SetState(bEnable);
  248.     }
  249.     return(bEnable);
  250. }