<?php
* 修复版:不使用COM扩展读取Windows主题(适配REG_DWORD/REG_SZ解析)
* 解决theme_path解析为0x0的问题,增加调试信息
*/
function getWindowsThemeWithoutCOM() {
$commands = [
'system_mode' => 'chcp 65001 >nul && reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize" /v "SystemUsesLightTheme" 2>nul',
'app_mode' => 'chcp 65001 >nul && reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize" /v "AppsUseLightTheme" 2>nul',
'theme_path' => 'chcp 65001 >nul && reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Themes" /v "CurrentTheme" 2>nul'
];
$result = [];
foreach ($commands as $key => $cmd) {
exec($cmd, $output, $returnCode);
$result[$key . '_raw_output'] = $output;
if ($returnCode !== 0) {
$result[$key] = '未找到键值(Windows版本/权限问题)';
unset($output);
continue;
}
$value = '';
foreach ($output as $line) {
$line = trim($line);
if (empty($line) || strpos($line, 'HKEY_') === 0) {
continue;
}
if (strpos($line, 'REG_DWORD') !== false || strpos($line, 'REG_SZ') !== false) {
$parts = preg_split('/\s{2,}/', $line, -1, PREG_SPLIT_NO_EMPTY);
$value = end($parts);
break;
}
}
$result[$key] = $value;
unset($output);
}
$convertDword = function($dword) {
if (str_starts_with($dword, '0x')) {
return hexdec($dword) === 1 ? '浅色主题' : '深色主题';
}
return '未知';
};
$result['system_mode_text'] = $convertDword($result['system_mode']);
$result['app_mode_text'] = $convertDword($result['app_mode']);
if (!empty($result['theme_path']) && strpos($result['theme_path'], '0x') === false) {
$result['theme_name'] = basename($result['theme_path'], '.theme');
} else {
$result['theme_name'] = '未知(无主题路径/解析失败)';
}
unset($result['system_mode_raw_output'], $result['app_mode_raw_output'], $result['theme_path_raw_output']);
return $result;
}
$themeInfo = getWindowsThemeWithoutCOM();
echo "=== Windows主题信息(修复版) ===\n";
print_r($themeInfo);
?>