title: How to use the DeleteFile windows API function when path goes into start menu
body: <p>I am attempting to use the DeleteFile() function in windows, but any time I put start menu in the file path the function fails.  This seems to be the case with several other functions but I was able to find alternate ways to code those. </p>
  
  <p>The msdn page for this function is <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa363915(v=vs.85).aspx" rel="nofollow">https://msdn.microsoft.com/en-us/library/windows/desktop/aa363915(v=vs.85).aspx</a>.  </p>
  
  <p>The parameter type is LPCSTR which is a C string.  I'm not very good with C strings so that could be why I have a problem with this.</p>
  
  <p>Any help would be appreciated.  Thanks!</p>
  
  <pre><code># if defined(_MSC_VER)
  # ifndef _CRT_SECURE_NO_DEPRECATE
  # define _CRT_SECURE_NO_DEPRECATE (1)
  # endif
  # pragma warning(disable : 4996)
  # endif
  
  #include&lt;Windows.h&gt;
  #include&lt;string&gt;
  #include&lt;lmcons.h&gt;
  #include&lt;fstream&gt;
  #include &lt;tlhelp32.h&gt;
  #include &lt;strsafe.h&gt;
  
  #include&lt;iostream&gt;
  
  using namespace std;
  
  string get_path();
  void terminate_process();
  void uninstall(string noLock);
  void install(string noLock);
  void ErrorExit(LPTSTR lpszFunction);
  
  
  int main()
  {   
      ShowWindow(GetConsoleWindow(), SW_HIDE);
      string noLock = get_path();
      ifstream file(noLock.c_str());
          if (file)
          {
              if (MessageBox(0, "NoLock is already installed.  Do you wish to delete it?", "Proceed?", MB_YESNO) == 6)
              {
                  terminate_process();
                  uninstall(noLock);
                  if (file)
                      MessageBox(0, "NoLock uninstall encountered an error and will close.", "Uninstall failed", MB_OK);
                  return 0;
              }
          }
          terminate_process();
          install(noLock);
  }
  
  string get_path()
  {
  
      string path;
      char user[UNLEN + 1];
      DWORD username_len = UNLEN + 1;
      GetUserName(user, &amp;username_len);
      string username(user);
      path = "C:\\Users\\" + username + "\\AppData\\Roaming\\Microsoft\\Windows\\start menu\\Programs\\Startup";
      string noLock = path + "\\nolock.exe";
      return noLock;
  }
  
  void terminate_process()
  {
  
      PROCESSENTRY32 entry;
      entry.dwSize = sizeof(PROCESSENTRY32);
      HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
      if (Process32First(snapshot, &amp;entry) == TRUE)
      {
          while (Process32Next(snapshot, &amp;entry) == TRUE)
          {
              if (stricmp(entry.szExeFile, "nolock.exe") == 0)
              {
                  HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID);
                  TerminateProcess(hProcess, 0);
                  CloseHandle(hProcess);
              }
          }
      }
      CloseHandle(snapshot);
      Sleep(1500);
  }
  
  void install(string noLock)
  {
  
      if (!CopyFile("nolock.exe", noLock.c_str(), false))
      {
          MessageBox(NULL, "Installation failed.", NULL, MB_OK);
          return;
      }
      ShellExecute(NULL, "open", noLock.c_str(), NULL, NULL, 0);
      MessageBox(NULL, "Installation complete.", "Success", MB_OK);
  
  }
  
  void uninstall(string noLock)
  {
  
      if (DeleteFile(noLock.c_str()))
          MessageBox(0, "Uninstall has completed successfully.", "Uninstall complete", MB_OK);
  
  }
  </code></pre>