public void getAndLogDetails(UsbApi.DEV_BROADCAST_HDR phdr, Message m) { uint test = 1; int searchFlag = 0; //log(phdr.dbch_DeviceType.ToString()); UsbApi.DEV_BROADCAST_DEVICEINTERFACE pDevInf = (UsbApi.DEV_BROADCAST_DEVICEINTERFACE)Marshal.PtrToStructure(m.LParam, typeof(UsbApi.DEV_BROADCAST_DEVICEINTERFACE)); //log(pDevInf.dbcc_name.ToString()); //string name = GetDeviceName(pDevInf); string pid = ""; string vid = ""; string serianNo = pDevInf.dbcc_name.Split('#')[2]; getDetails(pDevInf.dbcc_name.ToString(), ref pid, ref vid); //log(pid.ToString()); //log(vid.ToString()); Guid DiskGUID = new Guid("A5DCBF10-6530-11D2-901F-00C04FB951ED"); IntPtr h = UsbApi.SetupDiGetClassDevs(ref DiskGUID, new IntPtr(0), IntPtr.Zero, UsbApi.DIGCF_PRESENT | UsbApi.DIGCF_DEVICEINTERFACE); if (h != (IntPtr)UsbApi.INVALID_HANDLE_VALUE) { bool Success = true; uint i = 0; while (Success) { // create a Device Interface Data structure UsbApi.SP_DEVICE_INTERFACE_DATA dia = new UsbApi.SP_DEVICE_INTERFACE_DATA(); dia.cbSize = Marshal.SizeOf(dia); // start the enumeration Success = UsbApi.SetupDiEnumDeviceInterfaces(h, IntPtr.Zero, ref DiskGUID, i, ref dia); if (Success) { // build a DevInfo Data structure //Console.WriteLine("DiskGUID Agian: " + DiskGUID.ToString()); UsbApi.SP_DEVINFO_DATA da = new UsbApi.SP_DEVINFO_DATA(); da.cbSize = 28; // this becomes 32 for 64 bit systems; // build a Device Interface Detail Data structure UsbApi.SP_DEVICE_INTERFACE_DETAIL_DATA didd = new UsbApi.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 (UsbApi.SetupDiGetDeviceInterfaceDetail(h, ref dia, ref didd, nbytes, out n_required_size, ref da)) { // Now we get the InstanceID of the USB level device i++; IntPtr ptrInstanceBuf = Marshal.AllocHGlobal(nBytes); UsbApi.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); //log("Searching: " + pid_temp + "\t" + vid_temp); if (pid == pid_temp && vid == vid_temp) { //-----Build Device Tree and log Details ---- //log("-----Build Device Tree and log Details ----"); test = da.devInst; searchFlag = 1; //----------------------------------------------- Marshal.FreeHGlobal(ptrInstanceBuf); break; } Marshal.FreeHGlobal(ptrInstanceBuf); } } i++; } UsbApi.SetupDiDestroyDeviceInfoList(h); } else { log("Invalid Handle Value"); } if (searchFlag == 1) { node device = new node(test); //log(da.devInst.ToString()); deviceTree devTree = new deviceTree(device, pid, vid, serianNo); devTree.buildtree(ref device); devTree.logDetails(); } else { Console.WriteLine("Device Not Found"); } }
public void getAndLogDetails(UsbApi.DEV_BROADCAST_HDR phdr, Message m) { uint test = 1; int searchFlag = 0; //log(phdr.dbch_DeviceType.ToString()); UsbApi.DEV_BROADCAST_DEVICEINTERFACE pDevInf = (UsbApi.DEV_BROADCAST_DEVICEINTERFACE)Marshal.PtrToStructure(m.LParam, typeof(UsbApi.DEV_BROADCAST_DEVICEINTERFACE)); //log(pDevInf.dbcc_name.ToString()); //string name = GetDeviceName(pDevInf); string pid = ""; string vid = ""; string serianNo = pDevInf.dbcc_name.Split('#')[2]; getDetails(pDevInf.dbcc_name.ToString(), ref pid, ref vid); //log(pid.ToString()); //log(vid.ToString()); Guid DiskGUID = new Guid("A5DCBF10-6530-11D2-901F-00C04FB951ED"); IntPtr h = UsbApi.SetupDiGetClassDevs(ref DiskGUID, new IntPtr(0), IntPtr.Zero, UsbApi.DIGCF_PRESENT | UsbApi.DIGCF_DEVICEINTERFACE); if (h != (IntPtr)UsbApi.INVALID_HANDLE_VALUE) { bool Success = true; uint i = 0; while (Success) { // create a Device Interface Data structure UsbApi.SP_DEVICE_INTERFACE_DATA dia = new UsbApi.SP_DEVICE_INTERFACE_DATA(); dia.cbSize = Marshal.SizeOf(dia); // start the enumeration Success = UsbApi.SetupDiEnumDeviceInterfaces(h, IntPtr.Zero, ref DiskGUID, i, ref dia); if (Success) { // build a DevInfo Data structure //Console.WriteLine("DiskGUID Agian: " + DiskGUID.ToString()); UsbApi.SP_DEVINFO_DATA da = new UsbApi.SP_DEVINFO_DATA(); da.cbSize = 28; // this becomes 32 for 64 bit systems; // build a Device Interface Detail Data structure UsbApi.SP_DEVICE_INTERFACE_DETAIL_DATA didd = new UsbApi.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 (UsbApi.SetupDiGetDeviceInterfaceDetail(h, ref dia, ref didd, nbytes, out n_required_size, ref da)) { // Now we get the InstanceID of the USB level device i++; IntPtr ptrInstanceBuf = Marshal.AllocHGlobal(nBytes); UsbApi.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); //log("Searching: " + pid_temp + "\t" + vid_temp); if (pid == pid_temp && vid == vid_temp) { //-----Build Device Tree and log Details ---- //log("-----Build Device Tree and log Details ----"); test = da.devInst; searchFlag = 1; //----------------------------------------------- Marshal.FreeHGlobal(ptrInstanceBuf); break; } Marshal.FreeHGlobal(ptrInstanceBuf); } } i++; } UsbApi.SetupDiDestroyDeviceInfoList(h); } else log("Invalid Handle Value"); if (searchFlag == 1) { node device = new node(test); //log(da.devInst.ToString()); deviceTree devTree = new deviceTree(device, pid, vid, serianNo); devTree.buildtree(ref device); devTree.logDetails(); } else Console.WriteLine("Device Not Found"); }