title: Self-deleting program works fine in C but error in C++ project
body: <p>I need a way to self-delete the executable within my Win32 C++ project, and I found a program that does it in C:</p>
  <p>selfdel.c:</p>
  <pre class="lang-c prettyprint-override"><code>//  http://www.catch22.net/tuts/win32/self-deleting-executables
  //  selfdel.c
  //
  //  Self deleting executable for Win9x/WinNT (works for all versions of windows)
  //
  //  J Brown 1/10/2003
  //
  //  This source file must be compiled with /GZ turned OFF
  //  (basically, disable run-time stack checks)
  //
  //  Under debug build this is always on (MSVC6)
  //
  //
  /**
   * The way this works is:
      * Firstly a child process is created in a suspended state (any process will do - i.e. explorer.exe).
      * Some code is then injected into the address-space of the child process.
      * The injected code waits for the parent-process to exit.
      * The parent-process is then deleted.
      * The injected code then calls ExitProcess, which terminates the child process.
  */
  #include &lt;windows.h&gt;
  #include &lt;tchar.h&gt;
  
  #pragma pack(push, 1)
  
  #define CODESIZE 0x200
  
  //
  //  Structure to inject into remote process. Contains 
  //  function pointers and code to execute.
  //
  typedef struct _SELFDEL
  {
      struct _SELFDEL *Arg0;          // pointer to self
  
      BYTE    opCodes[CODESIZE];      // code 
  
      HANDLE  hParent;                // parent process handle
  
      FARPROC fnWaitForSingleObject;
      FARPROC fnCloseHandle;
      FARPROC fnDeleteFile;
      FARPROC fnSleep;
      FARPROC fnExitProcess;
      FARPROC fnRemoveDirectory;
      FARPROC fnGetLastError;
  
      BOOL    fRemDir;
  
      TCHAR   szFileName[MAX_PATH];   // file to delete
  
  } SELFDEL;
  
  #pragma pack(pop)
  
  #ifdef _DEBUG
  #define FUNC_ADDR(func) (PVOID)(*(DWORD *)((BYTE *)func + 1) + (DWORD)((BYTE *)func + 5))
  #else
  #define FUNC_ADDR(func) func
  #endif
  
  //
  //  Routine to execute in remote process. 
  //
  static void remote_thread(SELFDEL *remote)
  {
      // wait for parent process to terminate
      remote-&gt;fnWaitForSingleObject(remote-&gt;hParent, INFINITE);
      remote-&gt;fnCloseHandle(remote-&gt;hParent);
  
      // try to delete the executable file 
      while(!remote-&gt;fnDeleteFile(remote-&gt;szFileName))
      {
          // failed - try again in one second's time
          remote-&gt;fnSleep(1000);
      }
  
      // finished! exit so that we don't execute garbage code
      remote-&gt;fnExitProcess(0);
  }
  
  //
  //  Delete currently running executable and exit
  //  
  BOOL SelfDelete(BOOL fRemoveDirectory)
  {
      STARTUPINFO         si = { sizeof(si) };
      PROCESS_INFORMATION pi;
  
      CONTEXT             context;
      DWORD               oldProt;
      SELFDEL             local;
      DWORD               entrypoint;
  
      TCHAR               szExe[MAX_PATH] = _T(&quot;explorer.exe&quot;);
  
      //
      //  Create executable suspended
      //
      if(CreateProcess(0, szExe, 0, 0, 0, CREATE_SUSPENDED|IDLE_PRIORITY_CLASS, 0, 0, &amp;si, &amp;pi))
      {
          local.fnWaitForSingleObject     = (FARPROC)WaitForSingleObject;
          local.fnCloseHandle             = (FARPROC)CloseHandle;
          local.fnDeleteFile              = (FARPROC)DeleteFile;
          local.fnSleep                   = (FARPROC)Sleep;
          local.fnExitProcess             = (FARPROC)ExitProcess;
          local.fnRemoveDirectory         = (FARPROC)RemoveDirectory;
          local.fnGetLastError            = (FARPROC)GetLastError;
  
          local.fRemDir                   = fRemoveDirectory;
  
          // Give remote process a copy of our own process handle
          DuplicateHandle(GetCurrentProcess(), GetCurrentProcess(), 
              pi.hProcess, &amp;local.hParent, 0, FALSE, 0);
  
          GetModuleFileName(0, local.szFileName, MAX_PATH);
  
          // copy in binary code
          memcpy(local.opCodes, FUNC_ADDR(remote_thread), CODESIZE);
  
          //
          // Allocate some space on process's stack and place
          // our SELFDEL structure there. Then set the instruction pointer 
          // to this location and let the process resume
          //
          context.ContextFlags = CONTEXT_INTEGER|CONTEXT_CONTROL;
          GetThreadContext(pi.hThread, &amp;context);
  
          // Allocate space on stack (aligned to cache-line boundary)
          entrypoint = (context.Esp - sizeof(SELFDEL)) &amp; ~0x1F;
          
          //
          // Place a pointer to the structure at the bottom-of-stack 
          // this pointer is located in such a way that it becomes 
          // the remote_thread's first argument!!
          //
          local.Arg0 = (SELFDEL *)entrypoint;
  
          context.Esp = entrypoint - 4;   // create dummy return address
          context.Eip = entrypoint + 4;   // offset of opCodes within structure
  
          // copy in our code+data at the exe's entry-point
          VirtualProtectEx(pi.hProcess,   (PVOID)entrypoint, sizeof(local), PAGE_EXECUTE_READWRITE, &amp;oldProt);
          WriteProcessMemory(pi.hProcess, (PVOID)entrypoint, &amp;local, sizeof(local), 0);
  
          FlushInstructionCache(pi.hProcess, (PVOID)entrypoint, sizeof(local));
  
          SetThreadContext(pi.hThread, &amp;context);
  
          // Let the process continue
          ResumeThread(pi.hThread);
          CloseHandle(pi.hThread);
          CloseHandle(pi.hProcess);
  
          return TRUE;
      }
  
      return FALSE;
  }
  </code></pre>
  <p>Compiling this alone using the Visual Studio Developer Command Prompt and <code>cl</code> creates an executable and it runs just as expected. Putting this in my C++ project creates an error within <code>remote_thread</code>:</p>
  <p><a href="https://i.stack.imgur.com/jdyJY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jdyJY.png" alt="Error image in IDE" /></a></p>
  <p>I can't figure out this error. Building in x64 and x86 causes the same error. I have tried putting it in there just by itself, adding <code>extern &quot;C&quot; {}</code> as a wrapper, bringing the file in as a <code>.c</code> file and then linking with a header file, but then the header method broke all the other windows header files (probably because the compiler couldn't differentiate between c headers and cpp headers?) but that's besides the point, what am I missing here?</p>