public void buildtree(ref node root) { UInt32 child1; if (UsbApi.CM_Get_Child(out child1, root.deviceInstance, 0) == 0) { node childNode = new node(child1); root.addChild(childNode); deviceClasses += childNode.deviceClass + " 2 "; this.buildtree(ref childNode); //main.log("child added: " + childNode.deviceClass); UInt32 child; while (UsbApi.CM_Get_Sibling(out child, child1, 0) == 0) { node childSibling = new node(child); root.addChild(childSibling); this.deviceClasses += childSibling.deviceClass + " 1 "; buildtree(ref childSibling); child1 = child; child = 0; //main.log("child added: " + childSibling.deviceClass); } } //free(child1); }
//Checks status and then ejects the provided device instance ID public static bool checkEject(uint devInst) { int CR_SUCCESS = 0x00000000; int DN_REMOVABLE = 0x00004000; UInt32 status; UInt32 problem; if (CR_SUCCESS == UsbApi.CM_Get_DevNode_Status(out status, out problem, devInst, 0) && (DN_REMOVABLE & status) > 0) { UsbApi.PNP_VETO_TYPE pnp_veto_type; System.Text.StringBuilder sb = new System.Text.StringBuilder(255); bool success = (CR_SUCCESS == UsbApi.CM_Request_Device_Eject((IntPtr)devInst, out pnp_veto_type, sb, sb.Capacity, 0)); if (success) { //log("Device Ejected"); } ///else log("Eject Fail"); log("Ejected @ " + DateTime.Now); return(success); //Console.WriteLine("EJECT: " + success.ToString()); } else { //log("Could not Eject"); log("Ejected Failed @ " + DateTime.Now); return(false); //Console.WriteLine("Status False"); } }
public string setDeviceClass() { string classFinal = ""; Microsoft.Win32.RegistryValueKind kind; uint length = 0; UsbApi.CM_Get_DevNode_Registry_Property(deviceInstance, 0x00000001, out kind, IntPtr.Zero, ref length, 0); IntPtr buffer1 = Marshal.AllocHGlobal((int)length); if (UsbApi.CM_Get_DevNode_Registry_Property(deviceInstance, 0x00000001, out kind, buffer1, ref length, 0) == 0) { string driver_detail = Marshal.PtrToStringAnsi(buffer1); //Console.WriteLine("\tDRIVER NAME: {0}", driver_name.ToString()); classFinal += driver_detail + " "; } else { main.log("could not get Class"); //return ""; } UsbApi.CM_Get_DevNode_Registry_Property(deviceInstance, 0x00000008, out kind, IntPtr.Zero, ref length, 0); IntPtr buffer = Marshal.AllocHGlobal((int)length); if (UsbApi.CM_Get_DevNode_Registry_Property(deviceInstance, 0x00000008, out kind, buffer, ref length, 0) == 0) { string driver_detail = Marshal.PtrToStringAnsi(buffer); //Console.WriteLine("\tDRIVER NAME: {0}", driver_name.ToString()); classFinal += driver_detail; //return driver_detail; } else { //main.log("could not get Class"); } return(classFinal); }
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"); }