Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does RegSetValueEx work even when I break the rule about accounting for NUL termination in the length?
    primarykey
    data
    text
    <p>I've got a simple program that adds calc.exe to startup:</p> <pre><code>#include &lt;windows.h&gt; #include &lt;tchar.h&gt; int main(){ _tprintf(TEXT("Adding calc.exe to SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run...\n")); HKEY hRegRunKey; LPCTSTR lpKeyName = TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"); LPCTSTR lpKeyValue = TEXT("Calculator"); LPCTSTR lpProgram = TEXT("C:\\WINDOWS\\system32\\calc.exe"); DWORD cchProgram = _tcslen(lpProgram); _tprintf(TEXT("Path: %s. \n"), lpProgram); _tprintf(TEXT("Length: %d. \n"), cchProgram); if(RegOpenKeyEx( HKEY_LOCAL_MACHINE, lpKeyName, 0, KEY_SET_VALUE, &amp;hRegRunKey) == ERROR_SUCCESS){ if(RegSetValueEx(hRegRunKey, lpKeyValue, 0, REG_SZ, (const BYTE *)lpProgram, cchProgram * sizeof(TCHAR)) != ERROR_SUCCESS){ _tprintf(TEXT("ERROR: Can't set key value.\n")); exit(1); }else{ _tprintf(TEXT("Key has been added sucessfully.\n")); } } Sleep(5000); RegCloseKey(hRegRunKey); } </code></pre> <p>For me the world of c/c++/WIN32API is still full of misteries... so I have few questions. </p> <p><strong>1. When I define string is it automatically null terminated?</strong></p> <pre><code>LPCTSTR lpProgram = TEXT("C:\\WINDOWS\\system32\\calc.exe"); </code></pre> <p>or should it be done:</p> <pre><code>LPCTSTR lpProgram = TEXT("C:\\WINDOWS\\system32\\calc.exe\0"); </code></pre> <p><strong>2. In my code is final argument to RegSetValueEx set to correct value?</strong></p> <p>From <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms724923%28v=vs.85%29.aspx" rel="nofollow">MSDN - RegSetValueEx function</a> page:</p> <blockquote> <p>cbData [in] The size of the information pointed to by the lpData parameter, in bytes. 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.</p> </blockquote> <p><strong>cchProgram</strong> is set to 28 characters <strong>without</strong> null termination. On my system(because of UNICODE I think?) <strong>cchProgram * sizeof(TCHAR)</strong> = 56. </p> <p>Shouldn't I set it to 58 to add null termination?</p> <hr> <p>When I run this program, as it is above, without any modifications and I'll check Calculator value in registry via <strong>Modify binary date</strong> I get:</p> <pre><code>43 00 3A 00 5C 00 57 00 C.:.\.W. 49 00 4E 00 44 00 4F 00 I.N.D.O. 57 00 53 00 5C 00 73 00 W.S.\.s. 79 00 73 00 74 00 65 00 y.s.t.e. 6D 00 33 00 32 00 5C 00 m.3.2.\. 63 00 61 00 6C 00 63 00 c.a.l.c. 2E 00 65 00 78 00 65 00 ..e.x.e. 00 00 .. </code></pre> <p>Its 58 bytes including null termination. I'am confuse:/</p> <p><strong>UPDATE</strong></p> <p>Accounting for a NULL character by adding 1 to string length when calculating <strong>cbData</strong> yields exactly same result as without adding it.</p> <blockquote> <p><strong>cchProgram * sizeof(TCHAR)</strong> produces same data entry as <strong>(cchProgram + 1) * sizeof(TCHAR)</strong></p> </blockquote> <p>Providing value smaller then a string length doesn't add NULL byte and copies given number of bytes.</p> <blockquote> <p><strong>27 * sizeof(TCHAR)</strong> as cbData produces:</p> </blockquote> <pre><code>43 00 3A 00 5C 00 57 00 C.:.\.W. 49 00 4E 00 44 00 4F 00 I.N.D.O. 57 00 53 00 5C 00 73 00 W.S.\.s. 79 00 73 00 74 00 65 00 y.s.t.e. 6D 00 33 00 32 00 5C 00 m.3.2.\. 63 00 61 00 6C 00 63 00 c.a.l.c. 2E 00 65 00 78 00 ..e.x. </code></pre> <p>I am on some old XP, service pack god knows what, I don't know how other version of windows would handle it. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload