So I want to add a string to registry, since the registry strings are to written NULL terminated my string contains a null char in various places.
This is what my string looks like.
char names[550] = "1DFA-3327-*\01DFA-3527-*\001DFA-E527-*\00951-1500-
I convert this to whcar_t
string like so.
wchar_t names_w[1000];
size_t charsConverted = 0;
mbstowcs_s(&charsConverted, names_w, names, SIZE);
RegSetValueEx(*pKeyHandle, valueName, 0, REG_MULTI_SZ, (LPBYTE)names_w, size);
The registry entry should be
1DFA-3327-*
1DFA-3527-*
1DFA-E527-*
0951-1500-*
0951-0004-*
0951-160D-*
But this is the registry entry now,
1DFA-3327-*
<a box here>DFA-3527-*
<a box here>DFA-E527-*
951-1500-*
951-0004-*
951-160D-*
So it eats up the 0
in 0951
also eats up the 1
in 1DFA
What I have tried:
1> I tried changing the string to
char names[550] = "1DFA-3327-*\0\01DFA-3527-*\0\001DFA-E527-*\0\00951-1500-
^ ^ Two nulls
2> I tried different conversion.
for(int i; i < SIZE; i++)
names_w[i] = (wchar_t)names[i];
The problem is in your string literal.
char names[550] = "1DFA-3327-*\01DFA-3527-*\001DFA-E527-*\00951-1500-...\0";
^^ ^^^ ^^
You can provide ASCII characters in octal notation (\ooo) or in hexadecimal notation (\x hh). In your case you provide octal notation and this eats up the next up to three characters. You should change your string to
char names[550] = "1DFA-3327-*\0001DFA-3527-*\0001DFA-E527-*\0000951-1500-...\0";
or
char names[550] = "1DFA-3327-*\0" // put null byte at end of GUID
"1DFA-3527-*" "\0" // add null byte as extra literal
"1DFA-E527-*" "\0"
"0951-1500-...\0";
which also makes it easier to identify the GUIDs in the string.
If you want to use hexadecimal notation then take care that these might eat more than two bytes, so you also need to work with the literal concatenation. (See How to properly add hex escapes into a string-literal?)
char names[550] = "1DFA-3327-*\x00" // put null byte at end of GUID
"1DFA-3527-*" "\x00" // add null byte as extra literal
"1DFA-E527-*" "\x00"
"0951-1500-...\x00";
BTW, is there any reason not to directly store a wide char string?
wchar_t names[550] = L"1DFA-3327-*\x001DFA-3527-*\x001DFA-E527-*\x000951-1500-...\0";