0

I am trying to create a registry key with type REG_SZ and with a value longer than 4 chars.

But I can't figure out the right way to pass the data as argument :

#include <windows.h>
#include <stdio.h>


HKEY OpenKey(HKEY hRootKey, wchar_t* strKey)
{
    HKEY hKey;
    LONG nError = RegOpenKeyEx(hRootKey, strKey, 0, KEY_ALL_ACCESS, &hKey);

    if (nError == ERROR_FILE_NOT_FOUND)
    {
        printf("Debug: Creating registry key\n");
        nError = RegCreateKeyEx(hRootKey, strKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, NULL);
    }

    if (nError) {
        printf("Error: Could not find or create\n");
    }

    return hKey;
}

void SetValSZ(HKEY hKey, LPCTSTR lpValue, LPCTSTR data)
{
    LONG nError = RegSetValueEx(hKey, lpValue, 0, REG_SZ, (const BYTE*) data, sizeof(data));

    if (nError)
        printf("Error: Could not set registry value\n");
}

int main()
{

    HKEY hKey = OpenKey(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Policies\\Microsoft\\FVE");
    const wchar_t data[] = L"AAAABBBBCCCC";
    SetValSZ(hKey, L"RecoveryKeyMessage", data);
    RegCloseKey(hKey);

    return 0;
}

When I run it, only the first 4 characters are saved. I must have a type mistake...

Do you have any idea to help me to fix it ?

Thank you.

PS: I hope my english is clear enoth, feel free to ask me more if it isn't.

Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
  • Does this answer your question? [How to find the 'sizeof' (a pointer pointing to an array)?](https://stackoverflow.com/questions/492384/how-to-find-the-sizeof-a-pointer-pointing-to-an-array) – Raymond Chen Sep 17 '21 at 00:25

1 Answers1

1
void SetValSZ(HKEY hKey, LPCTSTR lpValue, LPCTSTR data)
{
    RegSetValueEx(hKey, lpValue, 0, REG_SZ, (const BYTE*) data, sizeof(data));
}

data is being passed as a pointer to a function, so its actual size will be lost. sizeof(data) is translated to the size of a pointer, which is 4 bytes in this case.

You have to either pass in the size as a parameter:

int len = sizeof(data);
SetValSZ(..., data, len);

Or better yet, use wcslen() to calculate the string length, then multiply that by sizeof(wchar_t).

Since you are using those T macros, the function can be written as:

RegSetValueEx(hKey,lpValue,0,REG_SZ,(const BYTE*)data,sizeof(TCHAR)*(lstrlen(data) + 1));

Edit: The size should also include the terminating null character

Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
  • 1
    Note that when using `wcslen()`/`lstrlen()`, you have to `+1` to the string length to include the null-terminator, per the documentation: "*If the data is of type REG_SZ, REG_EXPAND_SZ, or REG_MULTI_SZ, **`cbData` must include the size of the terminating null character or characters***", thus you need to use `sizeof(TCHAR) * (lstrlen(data) + 1)` in this case. – Remy Lebeau Sep 16 '21 at 23:18
  • @RemyLebeau, thanks, fixed it. – Barmak Shemirani Sep 16 '21 at 23:51