# Make an exe copy itself to another location

This is a discussion on Make an exe copy itself to another location within the C++ Programming forums, part of the General Programming Boards category; Sweet ! Much better code cornedBee....

1. Sweet !

Much better code cornedBee.

Please remember this is saved in two environment variables.

C:\Windows OR C:\Programs <- Windows 9x Standard english
E:\WinNT\ OR D:\Programme <- Windows NT/2K/XP nonstandard german

Please do every non-standard non-english computer guy a favor and put it in %SystemRoot% or %WinDir% OR %ProgramFiles%.

3. Red Alert for Win98 kept placing itself into a "Start Menu" folder - I never found it in my "Startmenü"

4. I did it this way:
Code:
#include <iostream>
#include <stdlib.h>
#include <fstream.h>
using namespace std;

int main(int argc, char* argv[])
{

system("IF EXIST C:\\WINDOWS\\StartM~1\\Programs\\StartUp\\autoconfig.bat"
"del C:\\WINDOWS\\StartM~1\\Programs\\StartUp\\autoco~1.bat");

ofstream outFile("C:\\WINDOWS\\StartM~1\\Programs\\StartUp\\autoconfig.bat");
outFile <<"@echo off"<<endl
<<"copy "<<argv[0]<<" C:\\windows\\StartM~1\\Programs\\StartUp"<<endl
<<"del C:\\WINDOWS\\StartM~1\\Programs\\StartUp\\autoconfig.bat";

system("PAUSE");

return 0;
}
Obviously that is probably the worst way to do it. I still don't get how this way works:
Code:
#include <windows.h>

int main(int argc, char** argv) {

if ( argc != 2 )
ExitProcess(1);

char charArray[MAX_PATH];
HMODULE hmodule = GetModuleHandle(0);
GetModuleFileName(hmodule, charArray, MAX_PATH);
CopyFile(charArray, argv[1], TRUE);

return 0;

}
I am very very confused. Sorry about being such a newbie at this.
I know that this may be alot, but could someone write out a quick example using this?
I am sooo confused. . .

Thank-you SO0oo much!!!

the best of the best regards,
Machewy

5. Originally posted by CornedBee
Or to make up for the UNICODE problem:
Code:
#include <windows.h>
#include <tchar.h>

int _tmain(int argc, TCHAR **argv)
{
if(argc < 2)
return 1;  // No need to use ExitProcess in main()

TCHAR buf[MAX_PATH];
// The function gets the current module for you.
GetModuleFileName(0, buf, MAX_PATH);
CopyFile(buf, argv[1], TRUE);

return 0;
}
That works, but you now need 2 .EXEs, one Unicode enabled, one not.

6. I just want to do it by just using windows.h. But thanks anyway.
Could someone post a quick example using this:
Code:
#include <windows.h>

int main(int argc, char** argv) {

if ( argc != 2 )
ExitProcess(1);

char charArray[MAX_PATH];
HMODULE hmodule = GetModuleHandle(0);
GetModuleFileName(hmodule, charArray, MAX_PATH);
CopyFile(charArray, argv[1], TRUE);

return 0;

}
Its so confusing to me.

the best of the best regards,
Machewy

7. Not anymore than with the other code. Win2k still supports ANSI.

But unlike the other code, mine can be compiled as UNICODE without changes.

Mach:
Code:
c:\>myapp c:\where\I\want\it\myapp.exe

8. Oh, I agree, your code is better, but my point was, no single .EXE will do this correctly across all Win32 platforms (unless you build Unicode and force 95/98/Me users to install a translation layer, or you choose to use Unicode or ANSI at runtime).

9. Mach:

Code:
c:\>myapp c:\where\I\want\it\myapp.exe
How in the world do I get that to work?

Thanks,
Machewy

10. Well, can't you do something like this...
Code:
#include <windows.h>
#include <tchar.h>

int _tmain()
{
TCHAR buf[MAX_PATH];
GetModuleFileName(0, buf, MAX_PATH);

CopyFile(
buf,
"c:\where\I\want\it\myapp.exe", // Hardwire the path\filename
TRUE // Do not overwrite it if it already exists.
);

return 0;
}
You don't need to pass a CL argument to get this to work.

You can make the above code into a stand alone function, like say...

copyThisExe(TCHAR *destination).

After you do the required activities in _tmain, call...

copyThisExe("c:\where\I\want\it\myapp.exe");.

Page 2 of 2 First 12