Skip to content

Commit

Permalink
refactor: windows_product_name() does char conversion internally
Browse files Browse the repository at this point in the history
  • Loading branch information
abelcheung committed Nov 25, 2023
1 parent f2c1518 commit f01e0f8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
22 changes: 17 additions & 5 deletions src/utils-win.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,15 @@ enumerate_drive_bins (void)
/*
* Get Windows product name via registry
*/
gunichar2 *
char *
windows_product_name (void)
{
LSTATUS status;
DWORD str_size;
gunichar2 *product_name;
gunichar2 *buf;
gunichar2 *subkey = L"software\\microsoft\\windows nt\\currentversion";
gunichar2 *keyvalue = L"ProductName";
char *result;

status = RegGetValueW(
HKEY_LOCAL_MACHINE,
Expand All @@ -231,21 +232,32 @@ windows_product_name (void)
&str_size
);

g_debug ("1st RegGetValueW(ProductName): status = %li, str_size = %lu", status, str_size);

if ((status != ERROR_SUCCESS) || (str_size > G_MAXSIZE))
return NULL;

product_name = g_malloc ((gsize) str_size);
buf = g_malloc ((gsize) str_size);
status = RegGetValueW(
HKEY_LOCAL_MACHINE,
subkey,
keyvalue,
RRF_RT_REG_SZ,
NULL,
product_name,
buf,
&str_size
);

return (status == ERROR_SUCCESS) ? product_name : NULL;
g_debug ("2nd RegGetValueW(ProductName): status = %li", status);

if (status != ERROR_SUCCESS) {
g_free (buf);
return NULL;
}

result = g_utf16_to_utf8(buf, -1, NULL, NULL, NULL);
g_free (buf);
return result;
}

/*!
Expand Down
2 changes: 1 addition & 1 deletion src/utils-win.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
void gui_message (const char *message );
char * get_win_timezone_name (void);
GSList * enumerate_drive_bins (void);
gunichar2 *windows_product_name (void);
char * windows_product_name (void);
gboolean can_list_win32_folder (const char *path,
GError **error);
gboolean init_wincon_handle (gboolean is_stdout);
Expand Down
9 changes: 1 addition & 8 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -1160,14 +1160,7 @@ _print_csv_header (metarecord meta)
#ifdef G_OS_WIN32
if (live_mode)
{
gunichar2 *buf = windows_product_name();
char *product_name = NULL;

if (buf) {
product_name = g_utf16_to_utf8(
buf, -1, NULL, NULL, NULL);
g_free (buf);
}
char *product_name = windows_product_name();

if (product_name) {
g_print (_("OS: %s"), product_name);
Expand Down

0 comments on commit f01e0f8

Please sign in to comment.