LocalFree fails - HELP! (Again)
If I don't execute the LocalFree functions - memory leak.
If I do execute ALL the LocalFree functions - program dies.
Without my "LocalFree" problems - the program works perfectly.
I know I'm doing something stupid. There is some extraneous code in here
that is due to my testing. I left it in. Once again, Thank you in advance!
Here's the code.
CString CChildView::DecryptPOP3Password(BYTE* ppData, DWORD pBytes)
{
// Get the hard drive's serial number - used for encryption purposes.
CHardDriveInfo hardDriveInfo;
hardDriveInfo.InitDriveInfo();
CString strPassword;
DATA_BLOB DataEntropy;
DATA_BLOB DataOut;
DATA_BLOB EncData;
LPWSTR description;
DataEntropy.pbData = (BYTE*)hardDriveInfo.HardDriveSerialNumber;
DataEntropy.cbData = (DWORD)strlen((char*)
hardDriveInfo.HardDriveSerialNumber);
EncData.cbData = pBytes;
EncData.pbData = ppData;
if(CryptUnprotectData(
&EncData,
&description,
&DataEntropy,
NULL,
NULL,
CRYPTPROTECT_LOCAL_MACHINE,
&DataOut))
{
TRACE0("Successful\n");
}
else
{
TRACE0("Failed\n");
}
strPassword = DataOut.pbData;
HLOCAL MyLocalMemory;
DWORD LastError;
MyLocalMemory = LocalFree(description); // This works fine
if (NULL != MyLocalMemory)
LastError = GetLastError();
MyLocalMemory = LocalFree(DataOut.pbData); // This works fine
if (NULL != MyLocalMemory)
LastError = GetLastError();
MyLocalMemory = LocalFree(&hardDriveInfo); // DOESN'T WORK
if (NULL != MyLocalMemory)
{
LastError = GetLastError();
TRACE1("failed with error %d", LastError);
}
MyLocalMemory = LocalFree(EncData.pbData); // THIS FAILS - CORRUPT
// HEAP - STOPS PROGRAM
MyLocalMemory = LocalFree(DataEntropy.pbData);
if (NULL != MyLocalMemory)
{
LastError = GetLastError();
TRACE1("failed with error %d", LastError);
}
return strPassword;
}