How to output an executable using C++


How to output an executable using C++



I would like to have a program that can write another program to the disk. I guess the way would be to use standard binary write to a file. My problem here is that I do not know how to put the other executable in the source of my "installing" program. I do not want to have any extra files next to my installing program. I would like to use the mfc api for this if there is any need to use something other than the standard C++





put it to resource for example
– RbMm
Jun 30 at 10:24






The standard way to get a single file distribution is to use an installer or a zip file. Having one program secretly create a new file and execute that would likely trigger the virus protection on most systems.
– Bo Persson
Jun 30 at 10:25





@Bo Persson well it is planned to be some project for school, kind of like a program that can install extensions on it's own (like a selfmade installer)
– ConstVoidPtr
Jun 30 at 10:32





MSVC has an installation builder project/solution (you might need to add it from the online library of project templates).
– Richard Critten
Jun 30 at 10:41






2 common ways - (1) Stub program that checks for on-line additions/updates; if found downloads and applies them; then launches the real program. (2) Real program does background downloads to a staging area then as (1) but from the staging instead of on-line. Notes: you need to keep careful check of file versions (file hashes are useful for integrity checks); zip like archives are useful for packaging; what do you do if changes are missed (how to do more than 1 update)? This is really not an SO question; flagging to close as Too Broad.
– Richard Critten
Jun 30 at 12:12





1 Answer
1



Edit *.rc resource file and add the following line:


1 RCDATA "C:\my disk\source.exe"



"source.exe" will be included in your program. You can do this with any file.


"source.exe"



1 is used as the ID in above example. You can use any unique identifier.


1



To extract the data during run time:


bool copy()
{
HINSTANCE hinst = AfxGetInstanceHandle(); //or just NULL
HRSRC hrsrc = FindResource(hinst, MAKEINTRESOURCE(1), RT_RCDATA);
if(hrsrc)
{
auto hglobal = LoadResource(hinst, hrsrc);
auto data = LockResource(hglobal);
auto datasize = SizeofResource(AfxGetInstanceHandle(), hrsrc);

CFile file;
if(file.Open(L"c:\target\output.exe", CFile::modeWrite | CFile::modeCreate))
{
file.Write(data, datasize);
file.Close();
return true;
}
else
{
TRACE("cannot open filen");
}
}
else
{
TRACE("resource not foundn");
}

return false;
}





Interesting. But this sounds like something that anti-virus hackers would exploit.
– Andrew Truckle
Jul 1 at 14:02





@Andrew The final installed program should not use this method. Otherwise this is common for a self-executable installation package. If you run any executable it can cause harm. It will be worse if Admin access is granted, for example it can steal passwords and send it to a server before the installation is complete. If an installed program was going to cause harm it doesn't have much to gain by copying a different program to a different location. The anti-virus program will catch common viruses and trojans, otherwise it misses trojans or raises false flag.
– Barmak Shemirani
Jul 1 at 16:02







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to input without newline? (Python)

C++ thread error: no type named ‘type’ MINGW

Analog for TagView in flutter