1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
11 * This is a tool that will be useful for various tasks if/when we build LO on WSL
13 * It is a Win32 program, not a Linux (WSL) one.
15 * Compile as: cl -MD wsl-lo-helper.cpp advapi32.lib
23 static void print_result(const wchar_t* argv0
, const wchar_t* result
)
26 if (WideCharToMultiByte(CP_UTF8
, WC_ERR_INVALID_CHARS
, result
, -1, output
, sizeof(output
), NULL
,
30 fprintf(stderr
, "%S --8.3: Could not convert result to UTF-8.\n", argv0
);
34 // Intentionally output no newline at end
39 static REGSAM
parse_bitness(const wchar_t* argv0
, const wchar_t* command
, const wchar_t* bitness
)
41 if (wcscmp(bitness
, L
"32") == 0)
42 return KEY_WOW64_32KEY
;
43 else if (wcscmp(bitness
, L
"64") == 0)
44 return KEY_WOW64_64KEY
;
47 fprintf(stderr
, "%S %S: Use 32 or 64.\n", argv0
, command
);
52 static void parse_hive(const wchar_t* argv0
, const wchar_t* path
, HKEY
* hivep
,
53 const wchar_t** hive_namep
)
55 if (wcscmp(path
, L
"HKEY_CLASSES_ROOT") == 0)
57 *hivep
= HKEY_CLASSES_ROOT
;
58 *hive_namep
= L
"HKEY_CLASSES_ROOT";
60 else if (wcscmp(path
, L
"HKEY_CURRENT_CONFIG") == 0)
62 *hivep
= HKEY_CURRENT_CONFIG
;
63 *hive_namep
= L
"HKEY_CURRENT_CONFIG";
65 else if (wcscmp(path
, L
"HKEY_CURRENT_USER") == 0)
67 *hivep
= HKEY_CURRENT_USER
;
68 *hive_namep
= L
"HKEY_CURRENT_USER";
70 else if (wcscmp(path
, L
"HKEY_LOCAL_MACHINE") == 0)
72 *hivep
= HKEY_LOCAL_MACHINE
;
73 *hive_namep
= L
"HKEY_LOCAL_MACHINE";
75 else if (wcscmp(path
, L
"HKEY_USERS") == 0)
78 *hive_namep
= L
"HKEY_USERS";
82 fprintf(stderr
, "%S: Invalid Registry hive %S.\n", argv0
, path
);
87 int wmain(int argc
, wchar_t** argv
)
91 fprintf(stderr
, "%S: Usage:\n", argv
[0]);
92 fprintf(stderr
, "%S --8.3 <windows-pathname>\n", argv
[0]);
93 fprintf(stderr
, " Print the 8.3 form of a Windows pathnam. Fail it not present.\n");
94 fprintf(stderr
, "%S --read-registry [32|64] <path>\n", argv
[0]);
95 fprintf(stderr
, " Read a string value from the Registry and print it.\n");
96 fprintf(stderr
, "%S --list-registry [32|64] <path>\n", argv
[0]);
97 fprintf(stderr
, " List subkeys of a key in the Registry.\n");
101 if (wcscmp(argv
[1], L
"--8.3") == 0)
105 fprintf(stderr
, "%S --8.3: One pathname argument expected\n", argv
[0]);
109 // The argument should be a pathname in Windows format. The
110 // output will be the 8.3 pathname if present. If not present,
113 wchar_t woutput
[1000];
114 if (GetShortPathNameW(argv
[2], woutput
, sizeof(woutput
) / sizeof(woutput
[0])) == 0)
116 fprintf(stderr
, "%S --8.3: Could not get short pathname of %S.\n", argv
[0], argv
[2]);
120 print_result(argv
[0], woutput
);
122 else if (wcscmp(argv
[1], L
"--read-registry") == 0)
126 fprintf(stderr
, "%S --read-registry: Bitness and path arguments expected.\n", argv
[0]);
130 REGSAM sam
= parse_bitness(argv
[0], L
"--read-registry", argv
[2]);
132 wchar_t* path
= wcsdup(argv
[3]);
134 for (wchar_t* p
= path
; *p
!= L
'\0'; p
++)
138 wchar_t* const first_backslash
= wcschr(path
, L
'\\');
139 if (first_backslash
== NULL
)
141 fprintf(stderr
, "%S: Invalid path %S to value in the Registry.\n", argv
[0], path
);
145 *first_backslash
= L
'\0';
146 wchar_t* const key_path
= first_backslash
+ 1;
149 const wchar_t* hive_name
;
150 parse_hive(argv
[0], path
, &hive
, &hive_name
);
153 wchar_t result
[1000];
154 DWORD count
= sizeof(result
);
156 wchar_t* last_backslash
= wcsrchr(key_path
, L
'\\');
157 if (last_backslash
== NULL
)
159 fprintf(stderr
, "%S: Invalid path %S\\%S to value in the Registry.\n", argv
[0],
160 hive_name
, key_path
);
164 *last_backslash
= L
'\0';
165 wchar_t* value
= last_backslash
+ 1;
168 if (RegOpenKeyExW(hive
, key_path
, 0, KEY_QUERY_VALUE
| sam
, &key
) != ERROR_SUCCESS
)
170 fprintf(stderr
, "%S: Opening key %S\\%S in %S-bit Registry failed.\n", argv
[0],
171 hive_name
, key_path
, argv
[2]);
175 if (RegQueryValueExW(key
, value
, NULL
, &type
, (LPBYTE
)result
, &count
) != ERROR_SUCCESS
)
177 fprintf(stderr
, "%S: Reading value %S\\%S\\%S from %S-bit Registry failed.\n", argv
[0],
178 hive_name
, key_path
, value
, argv
[2]);
183 fprintf(stderr
, "%S: Value %S\\%S\\%S is not a string.\n", argv
[0], hive_name
, key_path
,
187 print_result(argv
[0], result
);
189 else if (wcscmp(argv
[1], L
"--list-registry") == 0)
193 fprintf(stderr
, "%S --list-registry: Bitness and path arguments expected.\n", argv
[0]);
197 REGSAM sam
= parse_bitness(argv
[0], L
"--list-registry", argv
[2]);
199 wchar_t* path
= wcsdup(argv
[3]);
201 for (wchar_t* p
= path
; *p
!= L
'\0'; p
++)
205 wchar_t* const first_backslash
= wcschr(path
, L
'\\');
206 if (first_backslash
== NULL
)
208 fprintf(stderr
, "%S: Invalid path %S to key in the Registry.\n", argv
[0], path
);
212 *first_backslash
= L
'\0';
213 wchar_t* const key_path
= first_backslash
+ 1;
216 const wchar_t* hive_name
;
217 parse_hive(argv
[0], path
, &hive
, &hive_name
);
220 if (RegOpenKeyExW(hive
, key_path
, 0, KEY_ENUMERATE_SUB_KEYS
| sam
, &key
) != ERROR_SUCCESS
)
222 fprintf(stderr
, "%S: Opening key %S\\%S in %S-bit Registry failed.\n", argv
[0],
223 hive_name
, key_path
, argv
[2]);
229 DWORD namelen
= sizeof(name
) / sizeof(name
[0]);
230 while (RegEnumKeyExW(key
, index
, name
, &namelen
, NULL
, NULL
, NULL
, NULL
) == ERROR_SUCCESS
)
232 printf("%S\n", name
);
234 namelen
= sizeof(name
) / sizeof(name
[0]);
239 fprintf(stderr
, "%S: Unrecognized sub-command %S.\n", argv
[0], argv
[1]);
246 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */