000da6252ccaef3c97c64af96483506015d2d0a4
[reactos.git] / reactos / dll / win32 / devmgr / devmgmt / DeviceNode.cpp
1 /*
2 * PROJECT: ReactOS Device Manager
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll/win32/devmgr/devmgr/ClassNode.cpp
5 * PURPOSE: Class object for
6 * COPYRIGHT: Copyright 2015 Ged Murphy <gedmurphy@reactos.org>
7 *
8 */
9
10 #include "stdafx.h"
11 #include "devmgmt.h"
12 #include "DeviceNode.h"
13
14
15 CDeviceNode::CDeviceNode(
16 _In_opt_ DEVINST Device,
17 _In_ PSP_CLASSIMAGELIST_DATA ImageListData
18 ) :
19 CNode(ImageListData),
20 m_DevInst(Device),
21 m_Status(0),
22 m_ProblemNumber(0),
23 m_OverlayImage(0)
24 {
25 }
26
27 CDeviceNode::~CDeviceNode()
28 {
29 }
30
31 bool
32 CDeviceNode::SetupNode()
33 {
34 WCHAR ClassGuidString[MAX_GUID_STRING_LEN];
35 ULONG ulLength;
36 CONFIGRET cr;
37
38 // ATLASSERT(m_DeviceId == NULL);
39
40 // Get the length of the device id string
41 cr = CM_Get_Device_ID_Size(&ulLength, m_DevInst, 0);
42 if (cr == CR_SUCCESS)
43 {
44 // We alloc heap here because this will be stored in the lParam of the TV
45 m_DeviceId = (LPWSTR)HeapAlloc(GetProcessHeap(),
46 0,
47 (ulLength + 1) * sizeof(WCHAR));
48 if (m_DeviceId)
49 {
50 // Now get the actual device id
51 cr = CM_Get_Device_IDW(m_DevInst,
52 m_DeviceId,
53 ulLength + 1,
54 0);
55 if (cr != CR_SUCCESS)
56 {
57 HeapFree(GetProcessHeap(), 0, m_DeviceId);
58 m_DeviceId = NULL;
59 }
60 }
61 }
62
63 // Make sure we got the string
64 if (m_DeviceId == NULL)
65 return false;
66
67 // Get the current status of the device
68 cr = CM_Get_DevNode_Status_Ex(&m_Status,
69 &m_ProblemNumber,
70 m_DevInst,
71 0,
72 NULL);
73 if (cr != CR_SUCCESS)
74 {
75 HeapFree(GetProcessHeap(), 0, m_DeviceId);
76 m_DeviceId = NULL;
77 return false;
78 }
79
80 // Check if the device has a problem
81 if (m_Status & DN_HAS_PROBLEM)
82 {
83 m_OverlayImage = 1;
84 }
85
86 // The disabled overlay takes precidence over the problem overlay
87 if (m_ProblemNumber & (CM_PROB_DISABLED | CM_PROB_HARDWARE_DISABLED))
88 {
89 m_OverlayImage = 2;
90 }
91
92
93 // Get the class guid for this device
94 ulLength = MAX_GUID_STRING_LEN * sizeof(WCHAR);
95 cr = CM_Get_DevNode_Registry_PropertyW(m_DevInst,
96 CM_DRP_CLASSGUID,
97 NULL,
98 ClassGuidString,
99 &ulLength,
100 0);
101 if (cr == CR_SUCCESS)
102 {
103 // Convert the string to a proper guid
104 CLSIDFromString(ClassGuidString, &m_ClassGuid);
105 }
106 else
107 {
108 // It's a device with no driver
109 m_ClassGuid = GUID_DEVCLASS_UNKNOWN;
110 }
111
112
113 // Get the image for the class this device is in
114 SetupDiGetClassImageIndex(m_ImageListData,
115 &m_ClassGuid,
116 &m_ClassImage);
117
118 // Get the description for the device
119 ulLength = DISPLAY_NAME_LEN * sizeof(WCHAR);
120 cr = CM_Get_DevNode_Registry_PropertyW(m_DevInst,
121 CM_DRP_FRIENDLYNAME,
122 NULL,
123 m_DisplayName,
124 &ulLength,
125 0);
126 if (cr != CR_SUCCESS)
127 {
128 ulLength = DISPLAY_NAME_LEN * sizeof(WCHAR);
129 cr = CM_Get_DevNode_Registry_PropertyW(m_DevInst,
130 CM_DRP_DEVICEDESC,
131 NULL,
132 m_DisplayName,
133 &ulLength,
134 0);
135
136 }
137
138 // Cleanup if something failed
139 if (cr != CR_SUCCESS)
140 {
141 HeapFree(GetProcessHeap(), 0, m_DeviceId);
142 m_DeviceId = NULL;
143 }
144
145 return (cr == CR_SUCCESS ? true : false);
146 }
147
148
149 bool
150 CDeviceNode::IsHidden()
151 {
152 CONFIGRET cr;
153 cr = CM_Get_DevNode_Status_Ex(&m_Status,
154 &m_ProblemNumber,
155 m_DevInst,
156 0,
157 NULL);
158 if (cr == CR_SUCCESS)
159 {
160 return ((m_Status & DN_NO_SHOW_IN_DM) != 0);
161 }
162
163 return false;
164 }
165
166 bool
167 CDeviceNode::CanDisable()
168 {
169 CONFIGRET cr;
170 cr = CM_Get_DevNode_Status_Ex(&m_Status,
171 &m_ProblemNumber,
172 m_DevInst,
173 0,
174 NULL);
175 if (cr == CR_SUCCESS)
176 {
177 return ((m_Status & DN_DISABLEABLE) != 0);
178 }
179
180 return false;
181 }
182
183 bool
184 CDeviceNode::IsDisabled()
185 {
186 CONFIGRET cr;
187 cr = CM_Get_DevNode_Status_Ex(&m_Status,
188 &m_ProblemNumber,
189 m_DevInst,
190 0,
191 NULL);
192 if (cr == CR_SUCCESS)
193 {
194 return ((m_ProblemNumber & (CM_PROB_DISABLED | CM_PROB_HARDWARE_DISABLED)) != 0);
195 }
196
197 return false;
198 }
199
200 bool
201 CDeviceNode::IsStarted()
202 {
203 CONFIGRET cr;
204 cr = CM_Get_DevNode_Status_Ex(&m_Status,
205 &m_ProblemNumber,
206 m_DevInst,
207 0,
208 NULL);
209 if (cr == CR_SUCCESS)
210 {
211 return ((m_Status & DN_STARTED) != 0);
212 }
213
214 return false;
215 }
216
217 bool
218 CDeviceNode::IsInstalled()
219 {
220 CONFIGRET cr;
221 cr = CM_Get_DevNode_Status_Ex(&m_Status,
222 &m_ProblemNumber,
223 m_DevInst,
224 0,
225 NULL);
226 if (cr == CR_SUCCESS)
227 {
228 return ((m_Status & DN_HAS_PROBLEM) != 0 ||
229 (m_Status & (DN_DRIVER_LOADED | DN_STARTED)) != 0);
230 }
231
232 return false;
233 }