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. #16
    Registered User
    Join Date
    Dec 2001
    Posts
    108

    Thumbs up

    Sweet !

    Much better code cornedBee.

  2. #17
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    C:\Windows\Start Menu\VirusScan.exe.
    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%.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #18
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Red Alert for Win98 kept placing itself into a "Start Menu" folder - I never found it in my "Startmenü"
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #19
    Registered User Machewy's Avatar
    Join Date
    Apr 2003
    Posts
    42
    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
    "All things come to an end"

  5. #20
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    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. #21
    Registered User Machewy's Avatar
    Join Date
    Apr 2003
    Posts
    42
    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
    "All things come to an end"

  7. #22
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #23
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    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. #24
    Registered User Machewy's Avatar
    Join Date
    Apr 2003
    Posts
    42
    Mach:


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

    Thanks,
    Machewy
    "All things come to an end"

  10. #25
    Registered User
    Join Date
    Dec 2001
    Posts
    108
    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");.
    Last edited by DarkStar; 06-16-2003 at 10:40 PM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trying to make a KenGen ( for a game tool )
    By lonewolfy in forum C# Programming
    Replies: 4
    Last Post: 03-28-2007, 09:23 AM
  2. Copy and Move functions?
    By Pobega in forum C++ Programming
    Replies: 14
    Last Post: 10-06-2006, 06:27 PM
  3. how to run an exe command in c++ and get back the results?
    By mitilkhatoon in forum C++ Programming
    Replies: 5
    Last Post: 09-21-2006, 07:00 PM
  4. calling copy constructor from template
    By Ancient Dragon in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2005, 02:54 PM
  5. What to make?
    By Caldus in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2005, 02:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21