📄 filestore.cpp
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
#include "FileStore.hpp"
#include "CommonFunctions.hpp"
/*------------------------------------------------------------------------------
GetFileName
Helper function to get the file name
Parameters:
pPrefix: [IN] file prefix
pSettingName: [IN] setting name
pBuffer: [OUT] out buffer holds the file name
BufferSize: [IN] buffer size
Returns (HRESULT): indicates success or failure
------------------------------------------------------------------------------*/
HRESULT
GetFileName(
__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 hr;
}
hr = StringCchCat(
pBuffer,
BufferSize,
pSettingName
);
if (FAILED(hr))
{
return hr;
}
return S_OK;
}
const WCHAR FileStore_t::sc_FileDirectory[] = L"\\Windows\\VoIP\\";
const WCHAR* FileStore_t::sc_FilePrefixes[] =
{
L"\\windows\\VoIP\\Prov0",
L"\\windows\\VoIP\\Prov1",
};
/*------------------------------------------------------------------------------
FileStore_t::FileStore_t
constructor
Parameters:
c_pFileName: [IN] string represents the file name
------------------------------------------------------------------------------*/
FileStore_t::FileStore_t(
const WCHAR* c_pFileName
)
{
if (c_pFileName == NULL ||
c_pFileName[0] == NULL)
{
ASSERT(FALSE);
}
if (!m_Name.assign(c_pFileName))
{
ASSERT(FALSE);
}
}
/*------------------------------------------------------------------------------
FileStore_t::~FileStore_t
destructor
------------------------------------------------------------------------------*/
FileStore_t::~FileStore_t(
void
)
{
;
}
/*------------------------------------------------------------------------------
FileStore_t::SetValue
Set value to file store
Parameters:
OperationCategory: [IN] indicates what category we should operate on
c_pValue: [IN] value to be saved
ValueSize: [IN] value size
Returns (HRESULT): indicates success or failure
------------------------------------------------------------------------------*/
HRESULT
FileStore_t::SetValue(
SettingCategory_e OperationCategory,
const BYTE* c_pValue,
DWORD ValueSize
)
{
if (c_pValue == NULL ||
ValueSize <= 0)
{
return E_INVALIDARG;
}
//get the file name by combining the prefix name and setting name
WCHAR FileName[MAX_PATH] = L"";
HRESULT hr = GetFileName(
sc_FilePrefixes[OperationCategory],
m_Name,
FileName,
ARRAYSIZE(FileName)
);
if (FAILED(hr))
{
return hr;
}
//create the file handle
ce::auto_hfile FileHandle = CreateFile(
FileName,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM,
NULL
);
if (!FileHandle.valid())
{
return E_FAIL;
}
//write to the file
DWORD NumberOfBytesWritten;
if (!WriteFile(
FileHandle,
c_pValue,
ValueSize,
&NumberOfBytesWritten,
NULL
))
{
return E_FAIL;
}
if (ValueSize != NumberOfBytesWritten)
{
return E_FAIL;
}
return S_OK;
}
/*------------------------------------------------------------------------------
FileStore_t::GetValue
Get the value from file store
Parameters:
OperationCategory: [IN] indicates what cateogry we operate on
ppValue: [OUT] out buffer holds the value
pValueSize: [OUT] pointer to value size
Returns (HRESULT): indicates success or failure. If there is no such value exists
in file store, we return S_OK with *ppValue = NULL and *pValueSize = 0.
------------------------------------------------------------------------------*/
HRESULT
FileStore_t::GetValue(
SettingCategory_e OperationCategory,
BYTE** ppValue,
DWORD* pValueSize
)
{
if (ppValue == NULL ||
pValueSize == NULL)
{
return E_INVALIDARG;
}
*ppValue = NULL;
*pValueSize = 0;
//get file name by combining prefix and setting name
WCHAR FileName[MAX_PATH] = L"";
HRESULT hr = GetFileName(
sc_FilePrefixes[OperationCategory],
m_Name,
FileName,
ARRAYSIZE(FileName)
);
if (FAILED(hr))
{
return hr;
}
//check whether the file exists
if (GetFileAttributes(FileName) == (DWORD)(-1))
{
return S_OK;
}
//get file handler
ce::auto_hfile FileHandle = CreateFile(
FileName,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
0,
NULL
);
if (!FileHandle.valid())
{
return E_FAIL;
}
//get file size, we assume the setting file size will be less than 0xFFFFFFFF
DWORD FileSize;
FileSize = GetFileSize(
FileHandle,
NULL
);
if (FileSize == 0xFFFFFFFF || FileSize == 0)
{
return E_FAIL;
}
//allocate memory
BYTE* pValue = NULL;
pValue = reinterpret_cast<BYTE*>(LocalAlloc(LMEM_ZEROINIT, FileSize));
if (pValue == NULL)
{
return E_OUTOFMEMORY;
}
//read data from file
DWORD NumberOfBytesRead = 0;
if (!ReadFile(
FileHandle,
pValue,
FileSize,
&NumberOfBytesRead,
0))
{
hr = E_FAIL;
goto exit;
}
if (NumberOfBytesRead != FileSize)
{
hr = E_FAIL;
goto exit;
}
*ppValue = pValue;
*pValueSize = FileSize;
exit:
if ((FAILED(hr)) && (pValue != NULL))
{
//free the buffer we allocated from heap if some error happens
LocalFree(pValue);
}
return hr;
}
/*------------------------------------------------------------------------------
FileStore_t::Delete
Delete setting from file store
Parameters:
OperationCategory: [IN] indicates which category we are operating on
Returns (HRESULT): indicates success or failure. If the setting doesn't exist
in file store, we return S_OK.
------------------------------------------------------------------------------*/
HRESULT
FileStore_t::Delete(
SettingCategory_e OperationCategory,
bool* pNotExist
)
{
if (pNotExist != NULL)
{
*pNotExist = false;
}
//get file name by combining prefix and setting name
WCHAR FileName[MAX_PATH] = L"";
HRESULT hr = GetFileName(
sc_FilePrefixes[OperationCategory],
m_Name,
FileName,
ARRAYSIZE(FileName)
);
if (FAILED(hr))
{
return hr;
}
//check whether the file exists
if (GetFileAttributes(FileName) == (DWORD)(-1))
{
if (pNotExist != NULL)
{
*pNotExist = true;
}
return S_OK;
}
//delete file
if (!DeleteFile(FileName))
{
return E_FAIL;
}
return S_OK;
}
HRESULT
FileStore_t::CheckOldValueExists(
SettingCategory_e CheckCategory,
bool* pNotExist
)
{
if (pNotExist == NULL)
{
return E_INVALIDARG;
}
//get file name by combining prefix and setting name
WCHAR FileName[MAX_PATH] = L"";
HRESULT hr = GetFileName(
sc_FilePrefixes[CheckCategory],
m_Name,
FileName,
ARRAYSIZE(FileName)
);
if (FAILED(hr))
{
return hr;
}
//check whether the file exists
*pNotExist = (GetFileAttributes(FileName) == (DWORD)(-1));
return S_OK;
}
HRESULT
FileStore_t::DoPreparation(
void
)
{
if (GetFileAttributes(sc_FileDirectory) != (DWORD)(-1))
{
return S_OK;
}
if (CreateDirectory(
sc_FileDirectory,
NULL
) == 0)
{
return E_FAIL;
}
return S_OK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -