📄 settings.cpp
字号:
/*static*/HRESULT
Settings_t::RegQueryValueHR(
HKEY RegKey,
__in const WCHAR* pValueName,
__in_opt DWORD* pValueType,
__out_opt BYTE* pData,
__inout_opt DWORD* pDataSize,
__in_opt bool* pDoesNotExist
)
{
ASSERT(pValueName != NULL);
ASSERT(pValueName[0]);
long Result = RegQueryValueEx(
RegKey,
pValueName,
NULL,
pValueType,
pData,
pDataSize
);
if (Result == ERROR_SUCCESS)
{
return S_OK;
}
if (Result == ERROR_FILE_NOT_FOUND)
{
if (pDoesNotExist != NULL)
{
*pDoesNotExist = true;
}
}
return CommonUtilities_t::GetErrorFromWin32();
}
/*------------------------------------------------------------------------------
RegGetValueSize
static function to get the size of a registry value data
Parameters:
RegKey: [in] handle to currently open key
pSubKeyName: [in] the sub key name
DataSize: [in, out] Pointer to a variable that specifies the size, in bytes,
of the buffer pointed to by the lpData parameter
pDoesNotExist: [out, optional] pointer to a bool variable indicates if the value
exists or not
Returns: S_OK indicates success and pDataSize points to a varialbe that specifies data
Otherwise failure.
If the value does not exists, we return failiure. and set the *pDoesNotExist as true
Note: Caller should check *pDoesNotExists only when return value is not S_OK
------------------------------------------------------------------------------*/
/*static*/
inline
HRESULT
Settings_t::RegGetValueSize(
HKEY RegKey,
__in const WCHAR* pValueName,
__out DWORD* pDataSize,
__out_opt bool* pDoesNotExist
)
{
ASSERT(pValueName != NULL);
ASSERT(pValueName[0]);
ASSERT(pDataSize != NULL);
return RegQueryValueHR(
RegKey,
pValueName,
NULL,
NULL,
pDataSize,
pDoesNotExist
);
}
/*------------------------------------------------------------------------------
GetSettingValueFromRegistry
static function to get a setting value from registry
Parameters:
RegKey: [in] handle to currently open key
pSettingName: [in] pointer to a null-terminated setting name string
ppSettingValue: [out] Pointer to a pointer of buffer which holds the setting value
pSettingValueSize: [out] pointer to a DWORD variable indicates the size of the value in bytes
pValueDoesNotExist: [out, opt] pointer to a bool variable indicates whether this value exists or not
Returns: S_OK indicates success, which means the *ppSettingValue holds the value
and *pSettingValueSize is greater than 0
If the value does not exists or the size is 0, we return failiure. and set the *pDoesNotExist as true
Note: Caller should check *pDoesNotExists only when return value is not S_OK
------------------------------------------------------------------------------*/
/*static*/ HRESULT
Settings_t::GetSettingValueFromRegistryByKey(
HKEY RegKey,
__in const WCHAR* pSettingName,
__deref_out_opt BYTE** ppSettingValue,
__out DWORD* pSettingValueSize,
__out_opt bool* pValueDoesNotExist
)
{
ASSERT((pSettingName != NULL) && pSettingName[0]);
ASSERT(ppSettingValue != NULL);
*ppSettingValue = NULL;
*pSettingValueSize = 0;
DWORD RegValueSize;
HRESULT hr = RegGetValueSize(
RegKey,
pSettingName,
&RegValueSize,
pValueDoesNotExist
);
if (FAILED(hr))
{
return hr;
}
if (RegValueSize == 0) //empty string
{
return E_FAIL;
}
BYTE* pRegValue = NULL;
//allocate memory to hold the reg value
pRegValue = reinterpret_cast<BYTE*>(LocalAlloc(LMEM_ZEROINIT, RegValueSize));
if (pRegValue == NULL)
{
return E_OUTOFMEMORY;
}
hr = RegQueryValueHR(
RegKey,
pSettingName,
NULL,
pRegValue,
&RegValueSize
);
if (FAILED(hr))
{
if (pRegValue)
{
LocalFree(pRegValue);
}
return hr;
}
*ppSettingValue = pRegValue;
*pSettingValueSize = RegValueSize;
return S_OK;
}
/*------------------------------------------------------------------------------
GetSettingFileName
static function to get a setting file name
Parameters:
pPrefix: [in] pointer to a null-terminated file prefix string
pSettingName: [in] pointer to a null-terminated setting name string
pBuffer: [out] Pointer to a buffer which holds the setting value
BufferSize: [in] the buffer size
Returns: S_OK indicates success, which means the *pBuffer holds the right
file name, otherwise failure.
------------------------------------------------------------------------------*/
/*static*/ HRESULT
Settings_t::GetSettingFileName(
__in const WCHAR* pPrefix,
__in const WCHAR* pSettingName,
__out_ecount(BufferSize) WCHAR* pBuffer,
size_t BufferSize
)
{
ASSERT(pPrefix != NULL);
ASSERT(pSettingName != NULL);
HRESULT hr = StringCchCopy(
pBuffer,
BufferSize,
pPrefix
);
if (FAILED(hr))
{
return CommonUtilities_t::GetErrorFromWin32();
}
hr = StringCchCat(
pBuffer,
BufferSize,
pSettingName
);
if (FAILED(hr))
{
return CommonUtilities_t::GetErrorFromWin32();
}
return S_OK;
}
/*------------------------------------------------------------------------------
GetSettingValueFromFile
static function to get a setting value from file
Parameters:
pFilePrefix: [in] pointer to a null-terminated file prefix name
pSettingName: [in] pointer to a null-terminated setting name string
ppSettingValue: [out] Pointer to a pointer of buffer which holds the setting value
pSettingValueSize: [out] pointer to a DWORD variable indicates the size of the value in bytes
pValueDoesNotExist: [out, opt] pointer to a bool variable indicates whether this value exists or not
Returns: S_OK indicates success, which means the *ppSettingValue holds the value
and *pSettingValueSize is greater than 0
If the value does not exists or the size is 0, we return failiure. and set the *pDoesNotExist as true
Note: Caller should check *pDoesNotExists only when return value is not S_OK
------------------------------------------------------------------------------*/
/*static*/ HRESULT
Settings_t::GetSettingValueFromFile(
__in const WCHAR* pFilePrefix,
__in const WCHAR* pSettingName,
__deref_out_opt BYTE** ppSettingValue,
__out DWORD* pSettingValueSize,
__out_opt bool* pValueDoesNotExist
)
{
ASSERT((pFilePrefix != NULL) && pFilePrefix[0]);
ASSERT((pSettingName != NULL) && pSettingName[0]);
ASSERT(ppSettingValue != NULL);
ASSERT(pSettingValueSize != NULL);
*ppSettingValue = NULL;
*pSettingValueSize = 0;
WCHAR FileName[MAX_PATH] = L"";
if (pValueDoesNotExist != NULL)
{
*pValueDoesNotExist = false;
}
//come up the file name we are looking for
HRESULT hr = GetSettingFileName(
pFilePrefix,
pSettingName,
FileName,
ARRAYSIZE(FileName)
);
if (FAILED(hr))
{
return hr;
}
return GetSettingValueFromFile(
FileName,
ppSettingValue,
pSettingValueSize,
pValueDoesNotExist
);
}
/*------------------------------------------------------------------------------
GetSettingValueFromFile
static function to get a setting value from file
Parameters:
pFileName: [in] Pointer to the name of the file
ppSettingValue: [out] Pointer to a pointer of buffer which holds the setting value
pSettingValueSize: [out] pointer to a DWORD variable indicates the size of the value in bytes
pValueDoesNotExist: [out, opt] pointer to a bool variable indicates whether this value exists or not
Returns: S_OK indicates success, which means the *ppSettingValue holds the value
and *pSettingValueSize is greater than 0
If the value does not exists or the size is 0, we return failiure. and set the *pDoesNotExist as true
Note: Caller should check *pDoesNotExists only when return value is not S_OK
------------------------------------------------------------------------------*/
/*static*/ HRESULT
Settings_t::GetSettingValueFromFile(
__in const WCHAR* pFileName,
__deref_out_opt BYTE** ppSettingValue,
__out DWORD* pSettingValueSize,
__out_opt bool* pValueDoesNotExist
)
{
ASSERT((pFileName != NULL) && pFileName[0]);
ASSERT(ppSettingValue != NULL);
ASSERT(pSettingValueSize != NULL);
*ppSettingValue = NULL;
*pSettingValueSize = 0;
if (pValueDoesNotExist != NULL)
{
*pValueDoesNotExist = false;
}
HANDLE FileHandle = CreateFile(
pFileName,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
0,
NULL
);
if (FileHandle == INVALID_HANDLE_VALUE)
{
if (pValueDoesNotExist != NULL)
{
*pValueDoesNotExist = true;
}
return E_FAIL;
}
HRESULT hr = S_OK;
DWORD FileSize;
BYTE* pValue = NULL;
DWORD NumberOfBytesRead = 0;
//we assume the setting file size will be less than 0xFFFFFFFF
FileSize = GetFileSize(
FileHandle,
NULL
);
if (FileSize == 0xFFFFFFFF)
{
hr = CommonUtilities_t::GetErrorFromWin32();
goto exit;
}
if (FileSize == 0)
{
hr = E_FAIL;
goto exit;
}
//allocate memory
pValue = reinterpret_cast<BYTE*>(LocalAlloc(LMEM_ZEROINIT, FileSize));
if (pValue == NULL)
{
hr = E_OUTOFMEMORY;
goto exit;
}
if (!ReadFile(
FileHandle,
pValue,
FileSize,
&NumberOfBytesRead,
0))
{
hr = CommonUtilities_t::GetErrorFromWin32();
goto exit;
}
if (NumberOfBytesRead != FileSize)
{
ASSERT(0);
hr = E_FAIL;
goto exit;
}
*ppSettingValue = pValue;
*pSettingValueSize = FileSize;
exit:
if ((FAILED(hr)) && (pValue != NULL))
{
//free the buffer we allocated from heap if some error happens
LocalFree(pValue);
}
if (FileHandle != INVALID_HANDLE_VALUE)
{
CloseHandle(FileHandle);
}
return hr;
}
/*------------------------------------------------------------------------------
CreateProfileFromXML
static function to create RTCProfile object from XML string
Parameters:
bstrXML: [in] the input XML string
pRTCClient: [in] the pointer to IRTCClient object
ppProfile: [out] the pointer to the point of IRTCProfile object
Returns: S_OK indicates success, otherwise failure.
------------------------------------------------------------------------------*/
/*static*/HRESULT
Settings_t::CreateProfileFromXML(
__in BSTR bstrXML,
__in IRTCClient* pRTCClient,
__deref_out_opt IRTCProfile** ppProfile
)
{
//internal function
ASSERT(bstrXML != NULL);
ASSERT(pRTCClient != NULL);
if (ppProfile == NULL)
{
return E_INVALIDARG;
}
*ppProfile = NULL;
//get the RTC provisioning interface
CComPtr<IRTCClientProvisioning> cpProvisioning;
HRESULT hr = pRTCClient->QueryInterface(IID_IRTCClientProvisioning, (void**)&cpProvisioning);
if (FAILED(hr))
{
return hr;
}
//create the profile
return cpProvisioning->CreateProfile(bstrXML, ppProfile);
}
HRESULT
Settings_t::GetSystemRingTone(
WCHAR* pBuffer,
unsigned int BufferSize
)
{
if (pBuffer == NULL || BufferSize <= 0)
{
return E_INVALIDARG;
}
HRESULT hr = RegistryGetString(
SN_VOIP_RINGTONESPATH_ROOT,
SN_VOIP_RINGTONESPATH_PATH,
SN_VOIP_RINGTONESPATH_VALUE,
pBuffer,
BufferSize
);
if (FAILED(hr) ||
pBuffer[0] == L'\0' ||
BufferSize <= 0)
{
//use default ringtone
return StringCchCopy(
pBuffer,
BufferSize,
sc_DefaultRingTone
);
}
return S_OK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -