Sync up to trunk head.
[reactos.git] / dll / win32 / advapi32 / misc / hwprofiles.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: dll/win32/advapi32/misc/hwprofiles.c
5 * PURPOSE: advapi32.dll Hardware Functions
6 * PROGRAMMER: Steven Edwards
7 * Eric Kohl
8 */
9
10 #include <advapi32.h>
11 #include <rpc.h>
12 #include <wine/debug.h>
13 WINE_DEFAULT_DEBUG_CHANNEL(advapi);
14
15 /******************************************************************************
16 * GetCurrentHwProfileA [ADVAPI32.@]
17 *
18 * Get the current hardware profile.
19 *
20 * PARAMS
21 * lpHwProfileInfo [O] Destination for hardware profile information.
22 *
23 * RETURNS
24 * Success: TRUE. lpHwProfileInfo is updated with the hardware profile details.
25 * Failure: FALSE.
26 *
27 * @implemented
28 */
29 BOOL WINAPI
30 GetCurrentHwProfileA(LPHW_PROFILE_INFOA lpHwProfileInfo)
31 {
32 HW_PROFILE_INFOW ProfileInfo;
33 UNICODE_STRING StringU;
34 ANSI_STRING StringA;
35 BOOL bResult;
36 NTSTATUS Status;
37
38 TRACE("GetCurrentHwProfileA() called\n");
39
40 bResult = GetCurrentHwProfileW(&ProfileInfo);
41 if (bResult == FALSE)
42 return FALSE;
43
44 lpHwProfileInfo->dwDockInfo = ProfileInfo.dwDockInfo;
45
46 /* Convert the profile GUID to ANSI */
47 StringU.Buffer = (PWCHAR)ProfileInfo.szHwProfileGuid;
48 StringU.Length = wcslen(ProfileInfo.szHwProfileGuid) * sizeof(WCHAR);
49 StringU.MaximumLength = HW_PROFILE_GUIDLEN * sizeof(WCHAR);
50 StringA.Buffer = (PCHAR)&lpHwProfileInfo->szHwProfileGuid;
51 StringA.Length = 0;
52 StringA.MaximumLength = HW_PROFILE_GUIDLEN;
53 Status = RtlUnicodeStringToAnsiString(&StringA,
54 &StringU,
55 FALSE);
56 if (!NT_SUCCESS(Status))
57 {
58 SetLastError(RtlNtStatusToDosError(Status));
59 return FALSE;
60 }
61
62 /* Convert the profile name to ANSI */
63 StringU.Buffer = (PWCHAR)ProfileInfo.szHwProfileName;
64 StringU.Length = wcslen(ProfileInfo.szHwProfileName) * sizeof(WCHAR);
65 StringU.MaximumLength = MAX_PROFILE_LEN * sizeof(WCHAR);
66 StringA.Buffer = (PCHAR)&lpHwProfileInfo->szHwProfileName;
67 StringA.Length = 0;
68 StringA.MaximumLength = MAX_PROFILE_LEN;
69 Status = RtlUnicodeStringToAnsiString(&StringA,
70 &StringU,
71 FALSE);
72 if (!NT_SUCCESS(Status))
73 {
74 SetLastError(RtlNtStatusToDosError(Status));
75 return FALSE;
76 }
77
78 return TRUE;
79 }
80
81
82 /*
83 * @implemented
84 */
85 BOOL WINAPI
86 GetCurrentHwProfileW(LPHW_PROFILE_INFOW lpHwProfileInfo)
87 {
88 WCHAR szKeyName[256];
89 HKEY hDbKey;
90 HKEY hProfileKey;
91 DWORD dwLength;
92 DWORD dwConfigId;
93 UUID uuid;
94
95 TRACE("GetCurrentHwProfileW() called\n");
96
97 if (lpHwProfileInfo == NULL)
98 {
99 SetLastError(ERROR_INVALID_PARAMETER);
100 return FALSE;
101 }
102
103 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
104 L"System\\CurrentControlSet\\Control\\IDConfigDB",
105 0,
106 KEY_QUERY_VALUE,
107 &hDbKey))
108 {
109 SetLastError(ERROR_REGISTRY_CORRUPT);
110 return FALSE;
111 }
112
113 dwLength = sizeof(DWORD);
114 if (RegQueryValueExW(hDbKey,
115 L"CurrentConfig",
116 0,
117 NULL,
118 (LPBYTE)&dwConfigId,
119 &dwLength))
120 {
121 RegCloseKey(hDbKey);
122 SetLastError(ERROR_REGISTRY_CORRUPT);
123 return FALSE;
124 }
125
126 swprintf(szKeyName,
127 L"Hardware Profile\\%04lu",
128 dwConfigId);
129
130 if (RegOpenKeyExW(hDbKey,
131 szKeyName,
132 0,
133 KEY_QUERY_VALUE | KEY_SET_VALUE,
134 &hProfileKey))
135 {
136 RegCloseKey(hDbKey);
137 SetLastError(ERROR_REGISTRY_CORRUPT);
138 return FALSE;
139 }
140
141 dwLength = sizeof(DWORD);
142 if (RegQueryValueExW(hProfileKey,
143 L"DockState",
144 0,
145 NULL,
146 (LPBYTE)&lpHwProfileInfo->dwDockInfo,
147 &dwLength))
148 {
149 lpHwProfileInfo->dwDockInfo =
150 DOCKINFO_DOCKED | DOCKINFO_UNDOCKED | DOCKINFO_USER_SUPPLIED;
151 }
152
153 dwLength = HW_PROFILE_GUIDLEN * sizeof(WCHAR);
154 if (RegQueryValueExW(hProfileKey,
155 L"HwProfileGuid",
156 0,
157 NULL,
158 (LPBYTE)&lpHwProfileInfo->szHwProfileGuid,
159 &dwLength))
160 {
161 /* Create a new GUID */
162 UuidCreate(&uuid);
163 swprintf(
164 lpHwProfileInfo->szHwProfileGuid,
165 L"{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
166 uuid.Data1,
167 uuid.Data2,
168 uuid.Data3,
169 uuid.Data4[0], uuid.Data4[1],
170 uuid.Data4[2], uuid.Data4[3], uuid.Data4[4], uuid.Data4[5],
171 uuid.Data4[6], uuid.Data4[7]);
172
173 dwLength = (wcslen(lpHwProfileInfo->szHwProfileGuid) + 1) * sizeof(WCHAR);
174 RegSetValueExW(hProfileKey,
175 L"HwProfileGuid",
176 0,
177 REG_SZ,
178 (LPBYTE)lpHwProfileInfo->szHwProfileGuid,
179 dwLength);
180 }
181
182 dwLength = MAX_PROFILE_LEN * sizeof(WCHAR);
183 if (RegQueryValueExW(hProfileKey,
184 L"FriendlyName",
185 0,
186 NULL,
187 (LPBYTE)&lpHwProfileInfo->szHwProfileName,
188 &dwLength))
189 {
190 wcscpy(lpHwProfileInfo->szHwProfileName,
191 L"Noname Hardware Profile");
192 dwLength = (wcslen(lpHwProfileInfo->szHwProfileName) + 1) * sizeof(WCHAR);
193 RegSetValueExW(hProfileKey,
194 L"FriendlyName",
195 0,
196 REG_SZ,
197 (LPBYTE)lpHwProfileInfo->szHwProfileName,
198 dwLength);
199 }
200
201 RegCloseKey(hProfileKey);
202 RegCloseKey(hDbKey);
203
204 return TRUE;
205 }
206
207 /* EOF */