bool DeviceNameMatch(System.Windows.Forms.Message m, String mydevicePathName)
            {
            // The LParam parameter of Message is a pointer to a DEV_BROADCAST_HDR structure,
            // which is really a longer structure underneath
            //
            DEV_BROADCAST_HDR devBroadcastHeader = new DEV_BROADCAST_HDR();
            Marshal.PtrToStructure(m.LParam, devBroadcastHeader);
            //
            if ((devBroadcastHeader.dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE))
                {
                DEV_BROADCAST_DEVICEINTERFACE devBroadcastDeviceInterface = new DEV_BROADCAST_DEVICEINTERFACE();
                Marshal.PtrToStructure(m.LParam, devBroadcastDeviceInterface);

                return String.Compare(devBroadcastDeviceInterface.dbcc_name, mydevicePathName, true) == 0;
                }

            return false;
            }
示例#2
0
        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            try
            {
                if (m.Msg == WM_DEVICECHANGE)
                {
                    Thread.Sleep(500);
                    DEV_BROADCAST_HDR pHdr = new DEV_BROADCAST_HDR();
                    pHdr = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_HDR));
                    switch ((int)m.WParam)
                    {

                        case UsbNotification.DbtDeviceremovecomplete:

                            Console.WriteLine("Usb_DeviceRemoved");
                            break;

                        case UsbNotification.DbtDevicearrival:
                            Console.WriteLine(" Usb_DeviceAdded"); // this is where you do your magic

                            //Console.WriteLine("Hello: {0}", pHdr.dbch_DeviceType);
                            if (pHdr.dbch_DeviceType == 5)
                            {
                                //Console.WriteLine("Human Interface Device");
                                DEV_BROADCAST_DEVICEINTERFACE pDevInf = (DEV_BROADCAST_DEVICEINTERFACE)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_DEVICEINTERFACE));
                                string name = GetDeviceName(pDevInf);
                                //Console.WriteLine("Name: {0}", name);
                                //GetDeviceNameNew(pDevInf);
                                //Console.WriteLine("Size: {0}\nDevice Type: {1}\nreserved: {2}", pDevInf.dbcc_size, pDevInf.dbcc_devicetype, pDevInf.dbcc_reserved);
                                //Console.WriteLine("Details: {0}", pDevInf.dbcc_name);
                                //Console.WriteLine("Class GUID: {0}", pDevInf.dbcc_classguid);
                                string pid = "";
                                string vid = "";
                                getDetails(pDevInf.dbcc_name.ToString(), ref pid, ref vid);
                                Guid DiskGUID = new Guid("A5DCBF10-6530-11D2-901F-00C04FB951ED");
                                IntPtr h = SetupDiGetClassDevs(ref DiskGUID, new IntPtr(0), IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
                                //Console.WriteLine("Class GUID: {0}", DiskGUID);
                                if (h != (IntPtr)INVALID_HANDLE_VALUE)
                                {

                                    bool Success = true;

                                    uint i = 0;
                                    while (Success)
                                    {
                                        // create a Device Interface Data structure
                                        SP_DEVICE_INTERFACE_DATA dia = new SP_DEVICE_INTERFACE_DATA();
                                        dia.cbSize = Marshal.SizeOf(dia);

                                        // start the enumeration
                                        Success = SetupDiEnumDeviceInterfaces(h, IntPtr.Zero, ref DiskGUID, i, ref dia);

                                        if (Success)
                                        {
                                            // build a DevInfo Data structure
                                            //Console.WriteLine("DiskGUID Agian: " + DiskGUID.ToString());
                                            SP_DEVINFO_DATA da = new SP_DEVINFO_DATA();
                                            da.cbSize = 28; // this becomes 32 for 64 bit systems;

                                            // build a Device Interface Detail Data structure
                                            SP_DEVICE_INTERFACE_DETAIL_DATA didd = new SP_DEVICE_INTERFACE_DETAIL_DATA();
                                            didd.cbSize = 4 + Marshal.SystemDefaultCharSize; // trust me :)

                                            int nRequiredSize = 0;
                                            int nBytes = 256; //Buffer Size = 256
                                            uint n_required_size = (uint)(int)nRequiredSize;
                                            uint nbytes = (uint)(int)nBytes;
                                            if (SetupDiGetDeviceInterfaceDetail(h, ref dia, ref didd, nbytes, out n_required_size, ref da))
                                            {

                                                // Now we get the InstanceID of the USB level device
                                                IntPtr ptrInstanceBuf = Marshal.AllocHGlobal(nBytes);
                                                CM_Get_Device_ID(da.devInst, ptrInstanceBuf, nBytes, 0);
                                                string InstanceID = Marshal.PtrToStringAuto(ptrInstanceBuf);
                                                //Console.WriteLine("Instance ID: " + da.devInst);

                                                //Console.WriteLine("PID: {0}\nVID: {1}", pid, vid);
                                                string pid_temp = "", vid_temp = "";
                                                int storageFlag = 0;
                                                getDetails(InstanceID.ToString(), ref pid_temp, ref vid_temp);

                                                if(pid == pid_temp && vid == vid_temp)
                                                {
                                                    additionalFunctions sa = new additionalFunctions();
                                                    tree.node root = new tree.node(da.devInst);
                                                    //sa.buildTree(root);
                                                    //Console.WriteLine("Device Added: {0} \nGetting Details................{1}....{2}....", InstanceID, pid, vid);
                                                    var test = da.devInst;
                                                    Thread th = new Thread(() =>
                                                    {

                                                        string serialNo = sd.getSerial(InstanceID);
                                                        //Console.WriteLine("Serial Number: {0}", serialNo);
                                                        sd.search(pid, vid, ref storageFlag,     serialNo, test); //Search details of the Given PID VID From WMI
                                                    });
                                                    th.Start();
                                                    th.Join();
                                                    Marshal.FreeHGlobal(ptrInstanceBuf);
                                                    SetupDiDestroyDeviceInfoList(h);
                                                    break;
                                                    /*
                                                    if (storageFlag == 1)
                                                    {
                                                        Console.WriteLine("TRYING TO EJECT!!-------------------" + da.devInst);
                                                        additionalFunctions.checkEject(da.devInst); //Ejects the USB by calling the CM_Request_Device_Eject function
                                                        Console.WriteLine("------------------------------------");
                                                    }
                                                    */
                                                    /*const int BUFFER_SIZE = 256;
                                                    UInt32 RequiredSize;
                                                    UInt32 RegType = 0;
                                                    byte[] ptrBuffer = new byte[BUFFER_SIZE];
                                                    IntPtr test = new IntPtr(BUFFER_SIZE);
                                                    tree.node root1 = new tree.node(da.devInst);
                                                    //additionalFunctions.buildTree(root1);

                                                    if (SetupDiGetDeviceRegistryProperty(h, ref da,(uint) SetupDiGetDeviceRegistryPropertyEnum.SPDRP_DEVICEDESC, out RegType, ptrBuffer, BUFFER_SIZE, out RequiredSize))
                                                    {
                                                        //string ControllerDeviceDesc = Marshal.PtrToStringAuto(ptrBuf);
                                                        //Console.WriteLine("Controller Device Name: {0}", ControllerDeviceDesc);
                                                        SetupDiGetDeviceRegistryProperty(h, ref da, (uint)SetupDiGetDeviceRegistryPropertyEnum.SPDRP_DEVICEDESC, out RegType, ptrBuffer, BUFFER_SIZE, out RequiredSize);
                                                        byte[] data = ptrBuffer;
                                                        IntPtr ptr = Marshal.AllocHGlobal(data.Length);
                                                        try
                                                        {

                                                            //Console.WriteLine("Sibling Status: " + CM_Get_Child(out parentDevInt, da.devInst, 0));
                                                            UInt32 current = da.devInst;
                                                            Microsoft.Win32.RegistryValueKind kind;
                                                            uint length = 0;

                                                            Program.CM_Get_DevNode_Registry_Property(da.devInst, 0x0000001A, out kind, IntPtr.Zero, ref length, 0);
                                                            IntPtr buffer = Marshal.AllocHGlobal((int)length);
                                                            Program.CM_Get_DevNode_Registry_Property(da.devInst, 0x0000001A, out kind, buffer, ref length, 0);
                                                            byte[] test1 = new byte[BUFFER_SIZE];

                                                            Marshal.Copy(buffer,test1, 0, (int)length);

                                                            //Console.WriteLine("dadas" + test1 + "DASdasd" + buffer);
                                                            //Console.WriteLine("\t\tDevice PATH: " + "\t" + System.Text.Encoding.UTF8.GetString(test1).ToString());

                                                            //Console.WriteLine("The New Function------------------\t" + da.devInst);

                                                            //tree.node root = new tree.node(da.devInst);
                                                            //additionalFunctions.buildTree(root);

                                                            String ControllerDesc = System.Text.Encoding.UTF8.GetString(ptrBuffer);
                                                            //Console.WriteLine("Details: {0}", ControllerDesc);
                                                            //Console.WriteLine("Sec Details: {0}", ptrBuffer);

                                                        }
                                                        finally
                                                        {
                                                            Marshal.FreeHGlobal(ptr);
                                                        }

                                                        //Console.WriteLine("Controller: " + RequiredSize);
                                                    }
                                                    else
                                                    {
                                                        if (GetLastError() == 13)
                                                        {
                                                            Console.WriteLine("The Property doesnot exist for the device");
                                                        }
                                                        else
                                                        {
                                                            Console.WriteLine("SetupDiGetDeviceRegistryProperty Error: " + GetLastError().ToString());
                                                        }
                                                    }*/

                                                    //Console.WriteLine("Pid: {0}\nVid:{1}", pid, vid);

                                                }

                                                Marshal.FreeHGlobal(ptrInstanceBuf);

                                            }

                                        }
                                        i++;

                                    }
                                }
                                SetupDiDestroyDeviceInfoList(h);

                            }

                            break;
                    }
                }
                base.WndProc(ref m);

            }
            catch
            {

            }
        }