📄 config.c
字号:
/******************************************************************//* *//* Winpooch : Windows Watchdog *//* Copyright (C) 2004-2006 Benoit Blanchon *//* *//* This program is free software; you can redistribute it *//* and/or modify it under the terms of the GNU General Public *//* License as published by the Free Software Foundation; either *//* version 2 of the License, or (at your option) any later *//* version. *//* *//* This program is distributed in the hope that it will be *//* useful, but WITHOUT ANY WARRANTY; without even the implied *//* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR *//* PURPOSE. See the GNU General Public License for more *//* details. *//* *//* You should have received a copy of the GNU General Public *//* License along with this program; if not, write to the Free *//* Software Foundation, Inc., *//* 675 Mass Ave, Cambridge, MA 02139, USA. *//* *//******************************************************************//******************************************************************//* Build configuration *//******************************************************************/#define TRACE_LEVEL 2/******************************************************************//* Includes *//******************************************************************/// module's interface#include "Config.h"// project's headers#include "Assert.h"#include "Trace.h"#include "ProjectInfo.h"// standard headers#include <tchar.h>#include <shlobj.h>/******************************************************************//* Internal data types *//******************************************************************/typedef struct { LPCTSTR szName ; LPCTSTR szDefaultValue ; LPTSTR szValue ;} CONFIG_STRING ;typedef struct { LPCTSTR szName ; int iDefaultValue ; int iValue ;} CONFIG_INTEGER ;typedef struct { LPCTSTR szName ; UINT nLength ; UINT nBufferSize ; LPTSTR* pszArray ; BYTE* pBuffer ;} CONFIG_STRING_ARRAY ;/******************************************************************//* Internal constants *//******************************************************************/#define CONFIG_STRING_MAX 256static LPCTSTR szRegKey = TEXT("Software\\" APPLICATION_NAME) ;/******************************************************************//* Internal data *//******************************************************************/static CONFIG_STRING aStrings[_CFGSTR_COUNT] = { { TEXT("Language"), TEXT("English") }, { TEXT("Antivirus"), NULL }, { TEXT("Custom alert sound"), TEXT("") }, { TEXT("Custom ask sound"), TEXT("") }, { TEXT("Custom virus sound"), TEXT("") },} ;static CONFIG_INTEGER aIntegers[_CFGINT_COUNT] = { { TEXT("ProcessGuard detected"), 0 }, { TEXT("RegDefend detected"), 0 }, { TEXT("Splash screen"), 1 }, { TEXT("Max log file size"), 1024*1024 }, { TEXT("Sound"), 1 }, { TEXT("Check for updates"), 1 }, { TEXT("Scan cache length"), 1024 }, { TEXT("Skip first close warning"), 0 }, { TEXT("Tray icon animation"), 1 },} ;static CONFIG_STRING_ARRAY aStringArrays[_CFGSAR_COUNT] = { { TEXT("Scan include pattern") }, { TEXT("Background scan folders") },} ;static HKEY g_hKey = NULL ;/******************************************************************//* Internal macros *//******************************************************************/#define arraysize(a) (sizeof(a)/sizeof((a)[0]))/******************************************************************//* Internal functions *//******************************************************************/VOID _Config_SetDefaultValues () ;/******************************************************************//* Exported function : Init *//******************************************************************/BOOL Config_Init () { LONG lResult ; // try to create config on LOCAL_MACHINE root key lResult = RegCreateKeyEx (HKEY_LOCAL_MACHINE, szRegKey, 0, TEXT(""), REG_OPTION_NON_VOLATILE, KEY_QUERY_VALUE|KEY_SET_VALUE, NULL, &g_hKey, NULL) ; // else try in CURRENT_USER if( lResult!=ERROR_SUCCESS ) lResult = RegCreateKeyEx (HKEY_CURRENT_USER, szRegKey, 0, TEXT(""), REG_OPTION_NON_VOLATILE, KEY_QUERY_VALUE|KEY_SET_VALUE, NULL, &g_hKey, NULL) ; // if both failed the surrender if( lResult!=ERROR_SUCCESS ) return FALSE ; _Config_SetDefaultValues () ; return Config_Load () ;}/******************************************************************//* Exported function : Uninit *//******************************************************************/BOOL Config_Uninit (){ int i; TRACE ; Config_Save () ; RegCloseKey (g_hKey) ; // free strings for( i=0 ; i<_CFGSTR_COUNT ; i++ ) { free (aStrings[i].szValue) ; aStrings[i].szValue = NULL ; } // free array strings for( i=0 ; i<_CFGSTR_COUNT ; i++ ) { aStringArrays[i].nLength = 0 ; aStringArrays[i].nBufferSize = 0 ; free (aStringArrays[i].pszArray) ; aStringArrays[i].pszArray = NULL ; free (aStringArrays[i].pBuffer) ; aStringArrays[i].pBuffer = NULL ; } return TRUE ;}/******************************************************************//* Exported function : Load *//******************************************************************/BOOL Config_Load () { DWORD dwSize ; DWORD dwType ; DWORD dwValue ; LONG lResult ; int i ; // // 1. Strings // // for each config string (begin) for( i=0 ; i<_CFGSTR_COUNT ; i++ ) { TCHAR szBuffer[CONFIG_STRING_MAX] ; // get value dwSize = CONFIG_STRING_MAX ; dwType = REG_SZ ; lResult = RegQueryValueEx (g_hKey, aStrings[i].szName, NULL, &dwType, (BYTE*)szBuffer, &dwSize) ; // if success then store value if( lResult==ERROR_SUCCESS && szBuffer[0]!=0 ) aStrings[i].szValue = _tcsdup (szBuffer) ; } // for each config string (end) // // 2. Integers // // for each config integer (begin) for( i=0 ; i<_CFGINT_COUNT ; i++ ) { // get value dwSize = sizeof(DWORD) ; dwType = REG_DWORD ; lResult = RegQueryValueEx (g_hKey, aIntegers[i].szName, NULL, &dwType, (BYTE*)&dwValue, &dwSize) ; // if success then store value if( lResult==ERROR_SUCCESS ) aIntegers[i].iValue = dwValue ; } // for each config integer (end) // // 3. String arrays // // for each config string array (begin) for( i=0 ; i<_CFGSAR_COUNT ; i++ ) { TRACE_INFO (TEXT("Getting size for array \"%s\"...\n"), aStringArrays[i].szName) ; // get required size dwSize = 0 ; lResult = RegQueryValueEx (g_hKey, aStringArrays[i].szName, NULL, &dwType, NULL, &dwSize) ; if( lResult!=ERROR_SUCCESS || dwType!=REG_MULTI_SZ ) { TRACE_INFO (TEXT("Error while getting size (result=%u, type=%u)\n"), lResult, dwType) ; continue ; } TRACE_INFO (TEXT("Required size is %u bytes\n"), dwSize) ; if( aStringArrays[i].pBuffer!=NULL ) free (aStringArrays[i].pBuffer) ; if( aStringArrays[i].pszArray!=NULL ) free (aStringArrays[i].pszArray) ; if( dwSize == 0 ) { aStringArrays[i].nBufferSize = 0 ; aStringArrays[i].pBuffer = NULL ; aStringArrays[i].nLength = 0 ; aStringArrays[i].pszArray = NULL ; } else { aStringArrays[i].nBufferSize = dwSize ; aStringArrays[i].pBuffer = malloc (dwSize) ; ; TRACE_INFO (TEXT("Reading array \"%s\"...\n"), aStringArrays[i].szName) ; lResult = RegQueryValueEx (g_hKey, aStringArrays[i].szName, NULL, &dwType, aStringArrays[i].pBuffer, &dwSize) ; if( lResult!=ERROR_SUCCESS ) { TRACE_INFO (TEXT("Error while reading\n")) ; free (aStringArrays[i].pBuffer) ; aStringArrays[i].pBuffer = NULL ; aStringArrays[i].nBufferSize = 0 ; continue ; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -