_TCHAR pointer to the function.

 void foo(_TCHAR* somestring) 

where somestring is globally declared TCHAR [200], then I call

 RegSetValueEx(..., REG_SZ, (BYTE*)somestring, sizeof(somestring)); 

And only the first character is written.

  • I am not an expert in C, but I know that the lines in C are pointers, on their own to the first element of the data bytes. Accordingly, a simple pointer to a string points to the first character, and in order to output the Nth symbol, we need to add N to the pointer, example char* str = "Helllo"; cout << (*str) += 1 << endl; // e char* str = "Helllo"; cout << (*str) += 1 << endl; // e char* str = "Helllo"; cout << (*str) += 1 << endl; // e - Evgeny Ivanov
  • Written some rubbish. What does "somestring globally declared TCHAR [200]" mean when you clearly see from the question that somestring is just a parameter of the _TCHAR* type foo function ??? - AnT

1 answer 1

If somestring global , then it does not need to be passed to a function - it is visible and so, because it is global :) And then - if it is visible as TCHAR[200] - the sizeof(somestring) expression sizeof(somestring) will be the size of this array.

But when you pass it to the function as void foo(_TCHAR* somestring) , the global variable becomes hidden, and under the name somestring in the body of the function there is already a completely different variable - the local pointer argument. And for it, sizeof(somestring) is not an array size, but a pointer, with all the ensuing consequences.

Pass also the length for writing to the registry to the foo function.

  • With the size already figured out - thanks. But now one more attack, even with nails nailed in size, stopped recording the value, RegSetValueEx returns 0x05 Access is denied. - Dow Jhob
  • This is no longer a language problem, it is most likely that you have rights to where you want, no. Where exactly do you write? What are the first parameters of RegSetValueEx ? Another question (but, judging by the code, it’s not him) is about the use of Unicode, whether everything is true from this point of view. - Harry
  • Before calling the procedure, I put _tcslen to check the length, and in the procedure itself, before recording, also, as soon as I removed these checks, everything took off, I will probably return again to the unit, there is no power for magic .. - Dow Jhob