title: C++: creating shortcut in Windows - real working example using MinGW/g++
body: <p>After some researches I found the solution how to create shortcut in Windows using C++/MinGW/g++:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;windows.h&gt;
  #include &lt;Shlobj.h&gt;
  #include &lt;objidl.h&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  HRESULT CreateLink(
          LPCWSTR FilePath,
          LPCWSTR LnkPath,
          LPCWSTR LnkDesc,
          LPCWSTR WorkDir) {
  
  
      CoInitialize(NULL);
      IShellLinkW* psl;
  
      HRESULT hres = CoCreateInstance(
              CLSID_ShellLink,
              NULL,
              CLSCTX_INPROC_SERVER,
              IID_IShellLink,
              (LPVOID *) &amp; psl);
  
      if (SUCCEEDED(hres)) {
          IPersistFile* ppf;
  
          wstring file(L"C:\\jarlauncher.exe");
          psl-&gt;SetPath((LPWSTR)file.c_str());//Not working - only first letter is visible in created shortcut properties
  
          psl-&gt;SetWorkingDirectory(WorkDir);
          psl-&gt;SetDescription(LnkDesc);
  
          hres = psl-&gt;QueryInterface(IID_IPersistFile, (LPVOID*) &amp; ppf);
  
          if (SUCCEEDED(hres)) {
              hres = ppf-&gt;Save(LnkPath, TRUE);
              ppf-&gt;Release();
          }
  
          psl-&gt;Release();
      }
  
      CoUninitialize();
      return hres;
  }
  </code></pre>
  
  <p>Still testing. The problem is that <code>psl-&gt;SetDescription(LnkDesc);</code> - only one symbol is visible in created shortcut. The same with other fields. What could be the problem here?</p>
  
  <p>But I can create shortcut with Unicode name (added some Japanese characters):</p>
  
  <pre><code>...
     hres = psl-&gt;QueryInterface(IID_IPersistFile, (LPVOID*) &amp;ppf);
  
          wstring shortCutName(L"C:\\猫が好き?.lnk");
  
          if (SUCCEEDED(hres)) {
              hres = ppf-&gt;Save((LPWSTR)shortCutName.c_str(), TRUE);
              ppf-&gt;Release();
          }
  ...
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Now working example:</p>
  
  <pre><code>#include &lt;Shlobj.h&gt;
  #include &lt;tchar.h&gt;
  
  using namespace std;
  
  HRESULT CreateLink(
          LPCWSTR filePath,
          LPCWSTR workDir,
          LPCWSTR desc,
          LPCWSTR iconPath,
          LPCWSTR linkPath) {
  
      CoInitialize(NULL);
      IShellLinkW* psl;
  
      HRESULT hres = CoCreateInstance(
              CLSID_ShellLink,
              NULL,
              CLSCTX_INPROC_SERVER,
              IID_IShellLinkW,
              (LPVOID*) &amp;psl);
  
      if (SUCCEEDED(hres)) {
  
          IPersistFile* ppf;
  
          psl-&gt;SetPath(filePath);
          psl-&gt;SetWorkingDirectory(workDir);
          psl-&gt;SetDescription(desc);
          psl-&gt;SetIconLocation(iconPath, 0);
  
          hres = psl-&gt;QueryInterface(IID_IPersistFile, (LPVOID*) &amp;ppf);
  
          if (SUCCEEDED(hres)) {
              hres = ppf-&gt;Save(linkPath, TRUE);
              ppf-&gt;Release();
          }
  
          psl-&gt;Release();
      }
  
      CoUninitialize();
      return hres;
  }
  
  int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) {
  
      CreateLink(
              L"C:\\Program Files\\Appl\\Appl.exe",
              L"C:\\Program Files\\Appl",
              L"Location: C:\\Program Files\\Appl",
              L"C:\\Program Files\\Appl\\someIcon.ico",
              L"C:\\Appl.lnk");//Shortcut will be created at C:/
  
      return 0;
  }
  </code></pre>
  
  <p>You also need to add <code>Ole32.lib</code> and <code>Uuid.lib</code> library files in project properties. If you don't need terminal window - just add <code>-mwindows</code> to C++ compiler option.  That's all - no more "secret" information. </p>
  
  <p>This file is tested on Windows + NetBeans IDE 8.0 with C/C++ plugin, plus MinGW and msys installed at C:\ and properly configured in NetBeans IDE current project properties.</p>