title: Adding new key to registry doesn't work
body: <p>I wrote two functions <code>install()</code> and <code>del()</code>:</p>
  
  <pre><code>#include &lt;windows.h&gt;
  #include &lt;Lmcons.h&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  void install (char * fileAndPath, char * registryName)
  {
      const unsigned long size = strlen(fileAndPath);
      HKEY software;
      HKEY mykey;
      long yRes = RegCreateKey(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion", &amp;software);
  long nResult = RegCreateKey(software, "Run", &amp;mykey);
  long j = RegSetValueEx(mykey, registryName, 0, REG_SZ, (LPBYTE)fileAndPath, size + 1);
  if (yRes != ERROR_SUCCESS)
          cout &lt;&lt; "Error: Could not create registry key yRes  " &lt;&lt; "\tERROR: " &lt;&lt; yRes &lt;&lt; endl;
      else
          cout &lt;&lt; "Success: Key created" &lt;&lt; endl;
  if (nResult != ERROR_SUCCESS)
          cout &lt;&lt; "Error: Could not create registry key nResult " &lt;&lt; "\tERROR: " &lt;&lt; nResult &lt;&lt; endl;
      else
          cout &lt;&lt; "Success: Key created" &lt;&lt; endl;
  if (j != ERROR_SUCCESS)
          cout &lt;&lt; "Error: Could not create registry key j" &lt;&lt; "\tERROR: " &lt;&lt; j &lt;&lt; endl;
      else
          cout &lt;&lt; "Success: Key created" &lt;&lt; endl;
      RegCloseKey(mykey);
      RegCloseKey(software);
  }
  
  void del(char * registryName)
  {
  HKEY software;
  HKEY mykey;
  RegCreateKey(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion",&amp;software);
  RegCreateKey(software,"Run",&amp;mykey);
  RegDeleteValue(mykey, registryName);
  RegCloseKey(mykey);
  RegCloseKey(software);
  }
  
  int main() {
      install("C:\\Users\\Dannael\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\test.exe", "TestKey");
      }
      system("pause");
      return 0;
  
  }
  </code></pre>
  
  <p>Program crashes after running, i can't find the problem.</p>
  
  <p>EDIT: I've changed buffer size value as Roman R. said, but program still doesn't want to add key to registry..</p>
  
  <p>EDIT2: I've updated code.</p>
  
  <p>Output:</p>
  
  <pre><code>    Success: Key created
      Error: Could not create registry key nResult ERROR: 5
      Error: Could not create registry key j ERROR: 6
  </code></pre>
  
  <p>Partly solved:</p>
  
  <p>Changing values HKEY_LOCAL_MACHINE to HKEY_CURRENT_USER allow to write keys in registry for current user</p>