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