title: Get the user's Desktop folder using Windows API?
body: <p>I am trying to get the user's Desktop folder in a C++ application (via a DLL) by using <code>SHGetSpecialFolderPath</code>:</p>
  
  <pre><code>#define _WIN32_WINNT    0x0500
  #define _WIN32_IE       0x0500
  #define CSIDL_MYMUSIC   0x000D
  #define CSIDL_MYVIDEO   0x000E
  
  #include "dll.h"
  #include &lt;windows.h&gt;
  #include &lt;shlobj.h&gt;
  #include &lt;stdio.h&gt;
  
  TCHAR path[MAX_PATH];
  
  export LPSTR desktop_directory()
  {
  
      if (SHGetSpecialFolderPath(HWND_DESKTOP, path, CSIDL_DESKTOP, FALSE)) { 
          return path;
      }
  
  }
  </code></pre>
  
  <p>First I want to return an else case. I return "ERROR" but the compiler warns me that is trying to convert a <code>CHAR</code> to a <code>LPSTR</code>. With that if there, it looks that the DLL might crash if it cannot get the directory for some reason.</p>
  
  <p>Also from the MSDN documentation, it says "[SHGetSpecialFolderPath is not supported. Instead, use ShGetFolderPath.]", then I navigate to that page and it says "ShGetFolderPath: Deprecated. Gets the path of a folder identified by a CSIDL value." What am I supposed to use instead?</p>
  
  <p>So:</p>
  
  <ol>
  <li>I want to add an else case where I return a string saying "ERROR"</li>
  <li>I want to know if I am using the correct non-deprecated API function that will work to the modern Windows OS as far back as Windows XP.</li>
  </ol>
  
  <p><strong>EDIT</strong></p>
  
  <p>Here is the updated code as requested,</p>
  
  <pre><code>#ifndef UNICODE
  #define UNICODE
  #endif
  
  #ifndef _UNICODE
  #define _UNICODE
  #endif
  
  #define _WIN32_WINNT 0x0500
  #define _WIN32_IE 0x0500
  #define CSIDL_MYMUSIC 0x000D
  #define CSIDL_MYVIDEO 0x000E
  
  #include "dll.h"
  #include &lt;windows.h&gt;
  #include &lt;shlobj.h&gt;
  #include &lt;stdio.h&gt;
  
  export LPCWSTR desktop_directory()
  {
  
      static wchar_t path[MAX_PATH+1];
  
      if (SHGetFolderPath(NULL, CSIDL_DESKTOP, NULL, 0, path)) {
          MessageBox(NULL, path, L"TEST", MB_OK); //test
          return path;
      } else {
          return L"ERROR";
      }
  
  }
  </code></pre>
  
  <p>Compiling with MinGW using: <code>g++ "src\dll\main.cpp" -D UNICODE -D _UNICODE -O3 -DNDEBUG -s -shared -o "output\main.dll"</code></p>
  
  <p>I need to pass the string from the DLL as UTF-8 using <code>WideCharToMultiByte(CP_UTF8, ...)</code>, but I am not sure how to do that.</p>