title: How to convert a dynamic string to wchar_t to change background
body: <p>I've been looking around the internet for some solutions however they all convert it from a constant string. <a href="http://www.cplusplus.com/forum/general/84768/" rel="nofollow noreferrer">Here's</a> a piece of code I took to convert strings to wchar_t without additional libraries. What I'm trying to do is, I want to change the background of my windows computer with my background. Now I can't assume that the folder that I downloaded is in C:\Downloads because some people change their downloads folder or maybe they moved the whole folder to another location. So in the first code, I'm trying to get the path of the .exe file. </p>
  
  <pre><code>string GetExePath() {
      char buffer[MAX_PATH];
      GetModuleFileNameA(NULL, buffer, MAX_PATH);
      string::size_type pos = string(buffer).find_last_of("\\/");
      return string(buffer).substr(0, pos + 1);//gets the first character in path up to the final backslash
  }
  </code></pre>
  
  <p>Next I'm going to grab the picture that I want to make as my background in the same folder as the .exe file.</p>
  
  <pre><code>//error on the third parameter
  int return_value = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, L(string)(GetExePath() + "\\picture.png"), SPIF_UPDATEINIFILE);
  </code></pre>
  
  <ol>
  <li>Error (active) E0040    expected an identifier      </li>
  <li>Error (active) E0020    identifier "L" is undefined  </li>
  <li>Error (active) E0254 type name is not allowed</li>
  <li>Error (active) E0018    expected a ')'</li>
  </ol>
  
  <hr>
  
  <p>After a while, I replaced the return type of the function and so it would return wchar_t*.</p>
  
  <pre><code>const wchar_t* GetExePath() {
      char buffer[MAX_PATH];
      GetModuleFileNameA(NULL, buffer, MAX_PATH);
      string::size_type pos = string(buffer).find_last_of("\\/");
      string path = string(buffer).substr(0, pos + 1);
      path += "\\HandleCamWallpaperwithgradient.png";
      cout &lt;&lt; path &lt;&lt; endl;
      wstring wide;
      for (int i = 0; i &lt; path.length(); ++i){
          wide += wchar_t(path[i]);
      }
      const wchar_t* result = wide.c_str();
      return result;
  }
  </code></pre>
  
  <p>However, the third parameter is showing an error saying</p>
  
  <ol>
  <li>Error E0167 argument of type "const wchar_t *" is incompatible with parameter of type "PVOID"</li>
  </ol>
  
  <p>So how can I fix it?</p>
  
  <hr>
  
  <p>Edit: Someone thought that this was a duplicate and it isn't. <a href="https://stackoverflow.com/questions/25485268/how-to-convert-string-to-wstring-in-c">How to convert string to wstring in C++</a> is NOT correlated with this question as the one who is asking on that thread is asking help for special characters.</p>