How to integrate support for securing a property in my C++ application?
The following article uses options that are available starting
with the Professional edition and project
type.
Step 1 - Loading and initializing the SecureProp library
// "GetRevealedTextLength" function signature
typedef size_t (__stdcall * GetTextLengthFcn)(LPCWSTR);
// "RevealText" function signature
typedef void (__stdcall * RevealTextFcn)(LPCWSTR, LPWSTR, size_t);
HMODULE hModule = NULL;
void InitSecurePropSupport(GetTextLengthFcn & aGetTextLengthFcn, RevealTextFcn & aRevealTextFcn)
{
// The name of the dll file to load
const wstring kDllName = L"SecureProp.dll";
// Try to load the dll file
hModule = ::LoadLibrary(kDllName.c_str());
if (!hModule)
{
// Couldn't load the dll file, so just output an error message
wcerr << L"Couldn't load " << kDllName << endl;
return;
}
// Match the calling convention of the "GetRevealedTextLength" exported function
aGetTextLengthFcn = reinterpret_cast<GetTextLengthFcn>(::GetProcAddress(hModule, "GetRevealedTextLength"));
if (aGetTextLengthFcn == NULL)
{
// Function signature mismatch
wcerr << L"Couldn't match the function signature of GetRevealedTextLength" << endl;
return;
}
// Match the calling convention of the "RevealText" exported function
aRevealTextFcn = reinterpret_cast<RevealTextFcn<(::GetProcAddress(hModule, "RevealText"));
if (aRevealTextFcn == NULL)
{
// Function signature mismatch
wcerr << L"Couldn't match the function signature of RevealText" << endl;
return;
}
}
SecureProp library exported functions
- GetRevealedTextLength - the DLL exported function that reveals the secured text and returns its length
- RevealText - the DLL exported function that reveals the secured text and stores the result in the provided buffer
Step 2 - Using the SecureProp library
void SecureProps()
{
//------------------------------------------------------------------------------------------
// Load the SecureProp library and initialize the exported functions
//------------------------------------------------------------------------------------------
GetTextLengthFcn GetRevealedTextLength = NULL;
RevealTextFcn RevealText = NULL;
InitSecurePropSupport(GetRevealedTextLength, RevealText);
if (GetRevealedTextLength == NULL || RevealText == NULL)
return;
// Load the properties from the ini file
vector<Property> properties;
LoadPropertiesFromIni(properties);
if (properties.empty())
return;
//------------------------------------------------------------------------------------------
// Use the SecureProp library
//------------------------------------------------------------------------------------------
for (size_t i = 0; i < properties.size(); i++)
{
LPCWSTR propValue = properties[i].mValue.c_str();
//------------------------------------------------------------------------------------------
// Exported function call
// Reveal the secured text and return its length
//------------------------------------------------------------------------------------------
size_t textLength = GetRevealedTextLength(propValue);
// Allocate a buffer of wide characters with the length: textLength + 1
// We need to add 1 in order to provide space for the null terminator
wchar_t * textBuffer = new wchar_t[++textLength];
//------------------------------------------------------------------------------------------
// Exported function call
// Reveal the secured text and store the result in the provided buffer
//------------------------------------------------------------------------------------------
RevealText(propValue, textBuffer, textLength);
// Output the revealed text
wcout << properties[i].mName << L" = " << textBuffer << endl;
// Delete the text buffer
delete[] textBuffer;
textBuffer = NULL;
}
// Free the loaded dll file
::FreeLibrary(hModule);
}
Sample Project
Download the C++ sample showcasing how the secure property functionality works.