2014/01/26

on
● -- ●
.■■■ - c/c++  Microsoft : How to Access a USB Device by Using WinUSB Functions
.■■■ - HID (human interface device) : (wiki) is a type of computer device that interacts directly with, and most often takes input from, humans and may deliver output to humans.
.■■■ - USB Port Insert / Remove detection using WMI (Source Code) ; WM_DEVICECHANGE(MSDN)
.■■■ - USB設備通信開發是在程序中調用一系列Windows API函數(以下簡稱API函數),通過系統自帶的HID驅動程序訪問HID設備。Windows提供了API函數來啟動應用程序與設備驅動程序之間的通信,可以使用任何 能訪問API函數的編程語言, 如VB,C/C++,C#,Delphi等編寫的應用程序,在設備驅動程序的支持下,調用ReadFile、WriteFile等API函數來對USB設 備進行讀寫操作。Windows用來與HID設備通信的API函數,包含在 hid.dll 、 setupapi.dll 、 kernel32.dll  3個dll文檔中,分別起到與HID設備通信、尋找與識別設備、交換數據的作用。
.■■■ - http://www.codeproject.com/KB/system/DriveDetector.aspx
.■■■ - 硬件檢測:安裝、停用、usb設備檢查 和c#實現

.■■■ - 基於Visual C#的USB接口通信程序設計
.■■■ - [C#] 監控USB插拔 : http://kuomingwang.blogspot.tw/2010/09/c-usb.html
// 常數宣告
        //USB
        public const int WM_DEVICECHANGE = 0x219;
        public const int DBT_DEVICEARRIVAL = 0x8000;
        public const int DBT_CONFIGCHANGECANCELED = 0x0019;
        public const int DBT_CONFIGCHANGED = 0x0018;
        public const int DBT_CUSTOMEVENT = 0x8006;
        public const int DBT_DEVICEQUERYREMOVE = 0x8001;
        public const int DBT_DEVICEQUERYREMOVEFAILED = 0x8002;
        public const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
        public const int DBT_DEVICEREMOVEPENDING = 0x8003;
        public const int DBT_DEVICETYPESPECIFIC = 0x8005;
        public const int DBT_DEVNODES_CHANGED = 0x0007;
        public const int DBT_QUERYCHANGECONFIG = 0x0017;
        public const int DBT_USERDEFINED = 0xFFFF;
        //雙擊滑鼠左鍵
        public const int WM_MOUSE_DOUBLE_CHICK = 0x0203;
.■■■ - [C#.NET][WinForm] Windows 視窗訊息接收 - WndProc
.■■■ -
.■■■ -
.■■■ -
.■■■ -
.■■■ -
.■■■ -
.■■■ -
.■■■ -
.■■■ -
.■■■ -
.■■■ -
.■■■ -
.■■■ -
.■■■ -
.■■■ -
.■■■ -
.■■■ -
.■■■ -
.■■■ -
.■■■ -
.■■■ -
.■■■ - //以下是調用windows的API的函數
        //獲得GUID
        [DllImport("hid.dll")]
        public static extern void HidD_GetHidGuid(ref Guid HidGuid);
        Guid guidHID = Guid.Empty;
        //過濾設備,獲取需要的設備
        [DllImport("setupapi.dll", SetLastError = true)]
        public static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, uint Enumerator, IntPtr HwndParent, DIGCF Flags);
        IntPtr hDevInfo;
        //獲取設備,true獲取到
        [DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern Boolean SetupDiEnumDeviceInterfaces(IntPtr hDevInfo, IntPtr devInfo, ref Guid interfaceClassGuid, UInt32 memberIndex, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData);
        public struct SP_DEVICE_INTERFACE_DATA
        {
            public int cbSize ;
            public Guid interfaceClassGuid;
            public int flags;
            public int reserved;
        }

        // 獲取接口的詳細信息 必須調用兩次 第1次返回長度 第2次獲取數據
        [DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr deviceInfoSet, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData, IntPtr deviceInterfaceDetailData,
            int deviceInterfaceDetailDataSize, ref int requiredSize, SP_DEVINFO_DATA deviceInfoData);
        [StructLayout(LayoutKind.Sequential)]
        public class SP_DEVINFO_DATA
        {
            public int cbSize = Marshal.SizeOf(typeof(SP_DEVINFO_DATA));
            public Guid classGuid = Guid.Empty; // temp
            public int devInst = 0; // dumy
            public int reserved = 0;
        }

        [StructLayout(LayoutKind.Sequential, Pack = 2)]
        internal struct SP_DEVICE_INTERFACE_DETAIL_DATA
        {
            internal int cbSize;
            internal short devicePath;
        }

        public enum DIGCF
        {
            DIGCF_DEFAULT = 0x1,
            DIGCF_PRESENT = 0x2,
            DIGCF_ALLCLASSES = 0x4,
            DIGCF_PROFILE = 0x8,
            DIGCF_DEVICEINTERFACE = 0x10
        }

        //獲取設備文件
        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern int CreateFile(
            string lpFileName,                            // file name
            uint dwDesiredAccess,                        // access mode
            uint dwShareMode,                            // share mode
            uint lpSecurityAttributes,                    // SD
            uint dwCreationDisposition,                    // how to create
            uint dwFlagsAndAttributes,                    // file attributes
            uint hTemplateFile                            // handle to template file
            );
        //讀取設備文件
        [DllImport("Kernel32.dll",SetLastError = true)]
        private static extern bool ReadFile
            (
                IntPtr hFile,
                byte[] lpBuffer,
                uint nNumberOfBytesToRead,
                ref uint lpNumberOfBytesRead,
                IntPtr lpOverlapped
            );

        //釋放設備
        [DllImport("hid.dll")]
        static public extern bool HidD_FreePreparsedData(ref IntPtr PreparsedData);
        //關閉訪問設備句柄,結束進程的時候把這個加上保險點
        [DllImport("kernel32.dll")]
        static public extern int CloseHandle(int hObject);

.■■■ -

-
-

0 意見: