forked from ClusterM/hakchi2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWaitingForm.cs
135 lines (120 loc) · 4.5 KB
/
WaitingForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using com.clusterrr.FelLib;
using com.clusterrr.hakchi_gui.Properties;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Management;
using System.Windows.Forms;
namespace com.clusterrr.hakchi_gui
{
public partial class WaitingForm : Form
{
readonly UInt16 vid, pid;
public WaitingForm(UInt16 vid, UInt16 pid)
{
InitializeComponent();
this.vid = vid;
this.pid = pid;
timer.Enabled = true;
}
public static bool WaitForDevice(UInt16 vid, UInt16 pid)
{
if (Fel.DeviceExists(vid, pid)) return true;
var form = new WaitingForm(vid, pid);
form.ShowDialog();
return form.DialogResult == DialogResult.OK;
}
static bool DeviceExists(UInt16 vid, UInt16 pid)
{
var devices = GetUSBDevices();
var id = string.Format("VID_{0:X4}&PID_{1:X4}", vid, pid);
foreach (var device in devices)
{
if (device.DeviceID.Contains(id))
return true;
}
return false;
}
private void timer_Tick(object sender, EventArgs e)
{
if (Fel.DeviceExists(vid, pid))
{
DialogResult = DialogResult.OK;
timer.Enabled = false;
}
}
private void WaitingForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (!Fel.DeviceExists(vid, pid))
{
if (MessageBox.Show(this, Resources.DoYouWantCancel, Resources.AreYouSure, MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
== System.Windows.Forms.DialogResult.No)
e.Cancel = true;
else
{
DialogResult = DialogResult.Abort;
}
}
}
private void buttonDriver_Click(object sender, EventArgs e)
{
try
{
// XP?
if (System.Environment.OSVersion.Version.Major == 5 && System.Environment.OSVersion.Version.Minor <= 1)
{
MessageBox.Show(this, Resources.XpZadig, "Windows XP/2000", MessageBoxButtons.OK, MessageBoxIcon.Information);
var process = new Process();
var fileName = "http://zadig.akeo.ie/";
process.StartInfo.FileName = fileName;
process.Start();
}
else
{
var process = new Process();
var fileName = Path.Combine(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "driver"), "nesmini_driver.exe");
process.StartInfo.FileName = fileName;
process.Start();
}
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void WaitingForm_FormClosed(object sender, FormClosedEventArgs e)
{
timer.Enabled = false;
}
static List<USBDeviceInfo> GetUSBDevices()
{
List<USBDeviceInfo> devices = new List<USBDeviceInfo>();
ManagementObjectCollection collection;
using (var searcher = new ManagementObjectSearcher(@"SELECT * FROM Win32_PnPEntity where DeviceID Like ""USB%"""))
collection = searcher.Get();
foreach (var device in collection)
{
devices.Add(new USBDeviceInfo(
(string)device.GetPropertyValue("DeviceID"),
(string)device.GetPropertyValue("PNPDeviceID"),
(string)device.GetPropertyValue("Description")
));
}
collection.Dispose();
return devices;
}
}
class USBDeviceInfo
{
public USBDeviceInfo(string deviceID, string pnpDeviceID, string description)
{
this.DeviceID = deviceID;
this.PnpDeviceID = pnpDeviceID;
this.Description = description;
}
public string DeviceID { get; private set; }
public string PnpDeviceID { get; private set; }
public string Description { get; private set; }
}
}