First of all, you need to use _TCHAR and _T() for
everything.
https://docs.microsoft.com/en-us/cpp/c-runtime-library/data-type-mappings?view=msvc-160
These for example.
> char stName[256] = " ";
> if (RegKey.QueryDWORDValue("LogFile", v0) == ERROR_SUCCESS)
> stName = _T("");
At the point you call the function, all you're left with is a pointer.
So this statement just replaces your buffer of 256 spaces with a buffer of only 1.
What's more, that memory will be read-only, which is a guaranteed memory fault if you try to write to it.
Just get rid of this line.
> strcat_s(stName, (strlen(stName) + 5), ".log");
Next, _s functions are only "safe" if you know how to use them.
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/strcat-s-wcscat-s-mbscat-s?view=msvc-160
The 2nd parameter is the size of the buffer, which in this case would still be 256.
Perhaps.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
bool test = false;
_TCHAR stName[256] = _T(""); // initially empty
doLogFile(test, stName, sizeof(stName)/sizeof(*stName));
void doLogFile(bool& test, LPTSTR stName, DWORD dwSize)
{
CRegKey RegKey;
DWORD v0=0;
DWORD dwCount=dwSize;
test = false;
long lres = RegKey.Open(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Poller"), KEY_READ);
if(lres == ERROR_SUCCESS){
if (RegKey.QueryDWORDValue(_T("LogFile"), v0) == ERROR_SUCCESS) {
if ((v0) == 1) {
if (RegKey.QueryStringValue(_T("LogFileName"), stName, &dwCount) == ERROR_SUCCESS) {
// it would be a good idea to check the updated dwCount still allows room for .log to be appended.
strcat_s(stName, dwSize, _T(".log"));
test = true;
}
}
}
RegKey.Close(); // must have been opened here.
}
}
| |