243 lines
12 KiB
C++
243 lines
12 KiB
C++
// utility for registery operation
|
|
//
|
|
// Date: 2019-10-30
|
|
//
|
|
#pragma once
|
|
|
|
#ifndef _INCLUDED_REF_
|
|
#define _INCLUDED_REF_
|
|
#include "../ref/ref.h"
|
|
#endif
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
// export interface for other modules on 2017-08-23 by GB
|
|
namespace sys_util
|
|
{
|
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
|
// registry utilities ...
|
|
enum _wow_type
|
|
{
|
|
WOW_TYPE_DEFAULT = 0, // both given path and the Wow6432Node path in 32-bits app
|
|
WOW_TYPE_32_BITS, // the same as WOW_TYPE_DEFAULT in 32-bits app
|
|
WOW_TYPE_64_BITS, // no affect on registry in 32-bits app
|
|
};
|
|
|
|
// if parameter 'temporary' is true, then the key you created would lost after the system restart !!!
|
|
PORT_API(long) reg_create_key(const wchar_t* path, HKEY* ret = NULL, HKEY root = HKEY_LOCAL_MACHINE, _wow_type type = WOW_TYPE_DEFAULT, bool temporary = false);
|
|
PORT_API(long) reg_close_key(HKEY key);
|
|
PORT_API(long) reg_remove_key(const wchar_t* path, HKEY root = HKEY_LOCAL_MACHINE, _wow_type type = WOW_TYPE_DEFAULT);
|
|
PORT_API(long) reg_remove_value(const wchar_t* path, const wchar_t* val_name, HKEY root = HKEY_LOCAL_MACHINE, _wow_type type = WOW_TYPE_DEFAULT);
|
|
|
|
// parameter of cb
|
|
// data: (const wchar_t*)
|
|
// len: bytes of data
|
|
// total: unused
|
|
// flag: inter_module_data::DATA_FLAG_FINAL
|
|
// param: the same as the parameter 'param'
|
|
//
|
|
// return: error code
|
|
PORT_API(long) reg_get_string(HKEY root, const wchar_t* path, const wchar_t* name, inter_module_data::set_data cb, void* param, _wow_type type = WOW_TYPE_DEFAULT);
|
|
PORT_API(long) reg_get_binary(HKEY root, const wchar_t* path, const wchar_t* name, inter_module_data::set_data cb, void* param, _wow_type type = WOW_TYPE_DEFAULT);
|
|
PORT_API(long) reg_get_integer(HKEY root, const wchar_t* path, const wchar_t* name, int* val, _wow_type type = WOW_TYPE_DEFAULT);
|
|
PORT_API(long) reg_get_integer(HKEY root, const wchar_t* path, const wchar_t* name, unsigned long long* val, _wow_type type = WOW_TYPE_DEFAULT);
|
|
|
|
// parameter of cb
|
|
// data: (const wchar_t*)key_name
|
|
// len: (const wchar_t*)path
|
|
// total: unused
|
|
// flag: inter_module_data::DATA_FLAG_FINAL
|
|
// param: the same as the parameter 'param'
|
|
//
|
|
// return: error code
|
|
PORT_API(long) reg_enum_key(HKEY root, const wchar_t* path, inter_module_data::set_data cb, void* param, _wow_type type = WOW_TYPE_DEFAULT);
|
|
|
|
// parameter of cb
|
|
// data: (const wchar_t*) reg_value name
|
|
// len: (HIDWORD): type - REG_SZ, (wchar_t*)total; REG_DWORD/REG_QWORD, (DWORD/UINT64)total; REG_BINARY, (unsigned char*)total
|
|
// (LODWORD): bytes of data
|
|
// total: value
|
|
// flag: inter_module_data::DATA_FLAG_FINAL
|
|
// param: the same as the parameter 'param'
|
|
//
|
|
// return: error code
|
|
PORT_API(long) reg_enum_value(HKEY root, const wchar_t* path, inter_module_data::set_data cb, void* param, _wow_type type = WOW_TYPE_DEFAULT);
|
|
|
|
// function: set string value
|
|
// val: can be multi-string fromat such as "123\0456\0789\0\0", in this circumstance, caller must provides val_bytes
|
|
// val_bytes: length of val, in bytes. will consider as single string if it was 0
|
|
//
|
|
// NOTE: this api would not support REG_EXPAND_SZ that with unexpanded string such as %systemroot%
|
|
PORT_API(long) reg_set_string(HKEY root, const wchar_t* path, const wchar_t* name, const wchar_t* val, int val_bytes = 0, _wow_type type = WOW_TYPE_DEFAULT);
|
|
PORT_API(long) reg_set_integer(HKEY root, const wchar_t* path, const wchar_t* name, unsigned val, _wow_type type = WOW_TYPE_DEFAULT);
|
|
PORT_API(long) reg_set_integer(HKEY root, const wchar_t* path, const wchar_t* name, unsigned long long val, _wow_type type = WOW_TYPE_DEFAULT);
|
|
PORT_API(long) reg_set_binary(HKEY root, const wchar_t* path, const wchar_t* name, unsigned char* val, int val_bytes, _wow_type type = WOW_TYPE_DEFAULT);
|
|
|
|
// function: to find the default opener of given file type
|
|
// file_ext: the extension name of the file, such as ".txt", lead with '.'
|
|
// cb: data - (const wchar_t*)path_opener
|
|
PORT_API(long) reg_get_file_opener(const wchar_t* file_ext/*.txt*/, inter_module_data::set_data cb, void* param);
|
|
|
|
// function: enumerate installed service
|
|
// cb: data - (const wchar_t*) reg_service name
|
|
// len - (const wchar_t*) reg_service path file
|
|
// total - (const wchar_t*) reg_service description
|
|
// flag - inter_module_data::DATA_FLAG_FINAL
|
|
PORT_API(long) reg_enum_service(inter_module_data::set_data cb, void* param);
|
|
|
|
// function: enumerate the auto starting program when system power on
|
|
// cb: data - (const wchar_t*) reg_program name
|
|
// len - (const wchar_t*) reg_program path file
|
|
// total - (const wchar_t*) reg_startup parameter
|
|
// flag - inter_module_data::DATA_FLAG_FINAL
|
|
PORT_API(long) reg_enum_program_on_system_poweron(inter_module_data::set_data cb, void* param);
|
|
PORT_API(long) reg_remove_program_on_system_poweron(const wchar_t* name); // the name should be the same as in enum_program_on_system_poweron
|
|
|
|
// function: enumerate installed service
|
|
// cb: data - (const wchar_t*) reg_software display name
|
|
// len - (const wchar_t*) reg_uninstaller path file
|
|
// total - (const wchar_t*) reg_publisher
|
|
// flag - inter_module_data::DATA_FLAG_FINAL
|
|
PORT_API(long) reg_enum_uninstaller(inter_module_data::set_data cb, void* param);
|
|
PORT_API(long) reg_reg_uninstaller(const wchar_t* app_name, const wchar_t* display_name, const wchar_t* uninstall_pe, const wchar_t* uninstall_param = NULL
|
|
, const wchar_t* publisher = NULL, const wchar_t* version = NULL, unsigned size_in_bytes = 0, _wow_type type = WOW_TYPE_DEFAULT);
|
|
PORT_API(long) reg_del_uninstaller(const wchar_t* app_name, _wow_type type = WOW_TYPE_DEFAULT);
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
|
// wmi utilities ...
|
|
// initialize a WMI object, use wmi_free to free the returned object
|
|
PORT_API(void*) wmi_initialize(void);
|
|
|
|
// because WMI is query system database, so consider it as registry and provides here ...
|
|
// function: query system configuration through by WMI
|
|
// win32_cls: the class name like Win32_xxxx, e.g. "Win32_NetworkAdapterConfiguration"
|
|
// attr_name: the attribute name in the win32_cls, e.g. "IPAddress" of "Win32_NetworkAdapterConfiguration"
|
|
// cb: data - value depends on len, see comments of parameter 'len'
|
|
// len - value type: VT_BSTR(const wchar_t*), VT_I1~8(char*, short*, int*, longlong*), VT_R4/8(float*, double*), VT_BOOL(bool*)
|
|
// total - unused
|
|
// flag - inter_module_data::DATA_FLAG_INSTAR in enumeration, and inter_module_data::DATA_FLAG_FINAL at over, then the data is invalid
|
|
// param - the same as parameter 'param'
|
|
PORT_API(HRESULT) wmi_query(const wchar_t* win32_cls, const wchar_t* attr_name, inter_module_data::set_data cb, void* param, void* wmi = NULL/*returned by wmi_initialize*/);
|
|
PORT_API(void) wmi_free(void* wmi/*returned by wmi_initialize*/);
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
|
// service utilities ...
|
|
enum _svc_state
|
|
{
|
|
SVC_STATE_UNKNOWN = 0,
|
|
SVC_STATE_STOPPED,
|
|
SVC_STATE_STARTING,
|
|
SVC_STATE_STOPPING,
|
|
SVC_STATE_RUNNING,
|
|
SVC_STATE_RESUMING,
|
|
SVC_STATE_PAUSING,
|
|
SVC_STATE_PAUSED,
|
|
};
|
|
PORT_API(long) svc_install(const wchar_t* svc_file, const wchar_t* svc_name, const wchar_t* svc_desc, const wchar_t* display_name = NULL);
|
|
PORT_API(long) svc_uninstall(const wchar_t* svc_name);
|
|
PORT_API(long) svc_start(const wchar_t* svc_name, DWORD wait_running_timeout = 0/*milliseconds*/);
|
|
PORT_API(long) svc_pause(const wchar_t* svc_name);
|
|
PORT_API(long) svc_resume(const wchar_t* svc_name);
|
|
PORT_API(long) svc_enable(const wchar_t* svc_name, bool enable);
|
|
PORT_API(long) svc_stop(const wchar_t* svc_name, DWORD wait_stopped_timeout = 0/*milliseconds*/);
|
|
PORT_API(long) svc_change_pe_file(const wchar_t* svc_name, const wchar_t* to_pe_file);
|
|
PORT_API(long) svc_change_description(const wchar_t* svc_name, const wchar_t* description);
|
|
PORT_API(bool) svc_is_running(const wchar_t* svc_name);
|
|
PORT_API(long) svc_pe_file(const wchar_t* svc_name, inter_module_data::set_data cb, void* param); // NOTE: DLL service will return "%system%\svchost.exe -k name" !!!
|
|
|
|
// function: handle the service common command, supporting following commands:
|
|
// --install: install self, or other service if with "-pe given service file" parameter
|
|
// --uninstall: uninstall self
|
|
// --restart: restart self
|
|
// --start: start self
|
|
// --stop: stop self
|
|
// --pause: pause self
|
|
// --resume: resume self
|
|
//
|
|
// return: whether handled the command
|
|
//
|
|
// NOTE: this API's codes must be in the service module; if with parameter "-pe given service file" then all operation are both on the given service
|
|
PORT_API(bool) svc_handle_common_command(const wchar_t* svc_name, const wchar_t* svc_desc, long* err);
|
|
|
|
// function: service enter, call this in main() for exe service.... support --install, --uninstall and --restart startup parameter
|
|
// parameter of cb: see the comment of DATA_FALG_SVC_xxx
|
|
PORT_API(long) start_as_service(const wchar_t* svc_name, const wchar_t* svc_desc, inter_module_data::set_data cb, void* param, DWORD timer_elapse = 0/*millisecond*/);
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
|
// shortcut utilities ...
|
|
//
|
|
// lnk_file: the file which with '.lnk' extension name
|
|
// target_file: the final file linked by lnk_file
|
|
//
|
|
PORT_API(HRESULT) link_create(const wchar_t* lnk_file, const wchar_t* target_file, const wchar_t* start_arguments
|
|
, const wchar_t* icon_path_file = NULL, int icon_index = 0, const wchar_t* lnk_desc = NULL);
|
|
|
|
// icon_index: change the icon in target file of icon index; or not change the icon if it was -1, you can call link_change_icon to change it later
|
|
PORT_API(HRESULT) link_change_target_file(const wchar_t* lnk_file, const wchar_t* target_file, int icon_index = 0);
|
|
PORT_API(HRESULT) link_change_icon(const wchar_t* lnk_file, const wchar_t* file_ico, int index = 0);
|
|
PORT_API(HRESULT) link_change_startup_arguments(const wchar_t* lnk_file, const wchar_t* start_arguments);
|
|
PORT_API(HRESULT) link_change_description(const wchar_t* lnk_file, const wchar_t* description);
|
|
PORT_API(long) link_pin_to_taskbar(const wchar_t* lnk_file, bool pin = true);
|
|
|
|
// flag: DATA_FLAG_INSTAR for startup arguments, DATA_FLAG_FINAL for the target pe file
|
|
PORT_API(HRESULT) link_get_target_file(const wchar_t* lnk_file, inter_module_data::set_data cb, void* param);
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////
|
|
// hardware info
|
|
enum cpu_cache_type
|
|
{
|
|
CPU_CACHE_NULL = 0, // 0 = Null - No more caches.
|
|
CPU_CACHE_DATA, // 1 = Data Cache.
|
|
CPU_CACHE_INSTRUCTION, // 2 = Instruction Cache.
|
|
CPU_CACHE_UNIFIED, // 3 = Unified Cache.
|
|
CPU_CACHE_COUNT,
|
|
};
|
|
typedef struct _cpu_cache
|
|
{
|
|
cpu_cache_type type;
|
|
int level;
|
|
UINT64 bytes;
|
|
}CPUCACHE, *LPCPUCACHE;
|
|
typedef struct _cpu_info
|
|
{
|
|
int max_cpuid;
|
|
int cores;
|
|
int speed_base; // in MHz, 0 - not got; < 0 is unreliable
|
|
int speed_max; // in MHz, 0 - not got; < 0 is unreliable
|
|
char *manufactor;
|
|
char *product;
|
|
int cache_num;
|
|
CPUCACHE caches[4];
|
|
}CPUINFO, *LPCPUINFO;
|
|
typedef struct _bios_info
|
|
{
|
|
char *base_board_manufactor;
|
|
char *base_board_product_name;
|
|
char *base_board_version;
|
|
char *bios_vendor;
|
|
char *bios_version;
|
|
char *bios_release_date;
|
|
char bios_major_release;
|
|
char bios_minor_release;
|
|
char controller_major_release;
|
|
char controller_minor_release;
|
|
}BIOSINFO, *LPBIOSINFO;
|
|
|
|
// result: data - LPCPUINFO
|
|
// len - error code, 0 is success
|
|
// total - unused, always be ZERO
|
|
// flag - DATA_FLAG_FINAL
|
|
// param - same as the parameter 'param'
|
|
PORT_API(bool) get_cpu_info(inter_module_data::set_data result, void* param);
|
|
|
|
// result: data - LPBIOSINFO
|
|
// len - error code, 0 is success
|
|
// total - unused, always be ZERO
|
|
// flag - DATA_FLAG_FINAL
|
|
// param - same as the parameter 'param'
|
|
PORT_API(bool) get_bios_info(inter_module_data::set_data result, void* param);
|
|
};
|
|
|
|
|