1

My objective is to wrap a .exe file in another .exe file. Whenever the wrapper .exe is launched, the embedded .exe should be launched. I have added the binary file as a resource. Now I want to load the file as a tmpfile and use the system() function or some alternative to execute the binary file. However, I am not able to get the filename of the tmpfile. I cannot use tmpnam because it will only return a filename but not create an actual temporary file.

My code so far :

#include <iostream>
#include <windows.h>
#include <stdio.h>

using namespace std;

int main()
{
    HRSRC hRes = FindResource(0, MAKEINTRESOURCE(1), RT_RCDATA);
    HGLOBAL hMem = LoadResource(0, hRes);
    void* pMem = LockResource(hMem);
    DWORD size = SizeofResource(0, hRes);

    FILE * f = tmpfile();
    fwrite(pMem, size, 1, f);
    fclose(f);

    return 0;
}

Please do help me. Thank you.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Pratanu Mandal
  • 597
  • 8
  • 23
  • Why do you need this program embedded as a resource? Can't you simply install it besides your main program? – πάντα ῥεῖ Jul 26 '14 at 10:25
  • You are using WinAPI, so you can use GetTempFileName() + GetTempPath() for generating temporary file name. Then create file with fopen() with that generated file name for binary writing. An then use your fwrite() + fclose(). And then ShellExecute() for running app – grisha Jul 26 '14 at 10:38

1 Answers1

2

According to this:

http://man7.org/linux/man-pages/man3/tmpfile.3.html

the file gets deleted after closing, so use fflush instead of fclose (run the fclose after the embedded exe has finished). Then find some information about the filename here:

Is there a way to get the filename from a `FILE*`?

Community
  • 1
  • 1
Arusekk
  • 827
  • 4
  • 22