Memory Access Violation using CRegKey

I have an application that is pretty old that I am tasked to update to run on new hardware. I am using Visual Studio 2019 to accomplish this.

I have a really basic function to grab a filename from the registry but it is throwing a memory access violation. Now this function worked, and has worked since 2002, but in this latest version of Visual studio it now fails.

I get the error when trying to use RegKey.QueryDWORDValue below.

Here is the error:
Exception thrown at 0x771F8D1C (ntdll.dll) in Poller.exe: 0xC0000005: Access violation writing location 0x00863F30.

Here is the code I am using to call the function doLogFile:

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
26
27
bool test = false;
char stName[256] = " ";
doLogFile(test, stName);


void doLogFile(bool& test, LPTSTR stName)
{
    CRegKey RegKey;
    DWORD v0=0;
    DWORD dwCount=256;
//    TCHAR szName[256];
//    ULONG cchBuffer = 256;
    stName = _T("");
    test = false;
    long lres = RegKey.Open(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Poller"), KEY_READ);
    if(lres == ERROR_SUCCESS){
	if (RegKey.QueryDWORDValue("LogFile", v0) == ERROR_SUCCESS) {
		if ((v0) == 1) {
    		    if (RegKey.QueryStringValue(_T("LogFileName"), stName, &dwCount) == ERROR_SUCCESS) {
		strcat_s(stName, (strlen(stName) + 5), ".log");
		test = true;
	        }
            }
        }
    }
    if(RegKey)RegKey.Close();
}


If I activate the TCHAR szName[256] variable and use that, I get the value from registry, but I then am having issues copying, or assigning, it to the returned stName variable.

I do not use C++ very often and I am sure this is something simple, but I am stuck and need some help moving forward.
Last edited on
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.
    }
}



Thank you so much. It is rather hard to not use this language for years and then have to jump back in like this. I truly appreciate the assistance.
Topic archived. No new replies allowed.