Thread: Make an exe copy itself to another location

  1. #1
    Registered User Machewy's Avatar
    Join Date
    Apr 2003
    Posts
    42

    Unhappy Make an exe copy itself to another location

    You know when a person downloads a program, and it usually defaults its location to the programs directory( in windows). Right? I was wondering if it was possible to make a C++ script that copies itself to another location. This can be very usefull in many ways. For instance, one may want to make it copy itself to the startup folder in windows, so it runs automatically when windows starts. I know that this can be used for something very pointless too. But this is what I am trying to do with my 'automatic virus scanner' that will check for new possible batch viruses on the computer. Also, if you have ever used batch, you will notice that there is a way to make batch files copy itself.

    using this:
    Code:
    0% copy c:\windows
    Is it possible to do this in C++ like one can do in batch? I have search for a very long time, and I still can't find a simple explanation. Really, I haven't found a explanation at all.

    Thank-you so much,

    best regards,
    Machewy

    "All things come to an end"

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    There are no C++ scripts, but that's probably not what you want to hear...

    I don't know a generic way to find out the current place of the exe (your batch file won't work under certain circumstances), but in Windows you can use GetModuleFileName and CopyFile to do what you want.
    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

  3. #3
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176
    This probably wouldn't work but you could copy the exe file's text and write it to C:\Windows\Start Menu\VirusScan.exe.
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  4. #4
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719
    There are 2 solutions:

    1: Open a handle to it's self, read the data, re-write it.

    2: Use 2 executions, one will communicate with the other. Then the one being copied will shut down, become copied, reinitialized, and the other execution can then shut its self down and then be deleted if necessary by the other execution.

    You could also have the execution delete its original copy once it was copied by simply passing a parameter to an instance of the copy.
    "What are you after - the vague post of the week award?" - Salem
    IPv6 Ready.
    Travel the world, meet interesting people...kill them.
    Trying to fix or change something, only guaruntees and perpetuates its existence.
    I don't know about angels, but it is fear that gives men wings.
    The problem with wanting something is the fear of losing it, or never having it. The thought makes you weak.

    E-Mail Xei

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The copying isn't the problem, finding out the full path and name of the exe is.
    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

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> You know when a person downloads a program

    What is usually downloaded is an installer, not the actual application.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Code:
    #include <iostream>
    
    int main(int argc, char* argv[])
    {
       std::cout << "full path name : " << argv[0] << std::endl;
       return 0;
    }
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>std::cout << "full path name : " << argv[0] << std::endl;
    This isn't guaranteed to give you the full path name.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Hmm... right... relative paths. Didn't think of that.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  11. #11
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    Won't that still work (on windows), as long as your pwd is '.' (relative to the exe)?

    i.e.
    string destination = "wherever\\you\\want"
    string foo = "copy "+ argv[0] + destination;
    system( foo.c_str() )

    (haven't tried it yet)

  12. #12
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Also, recall that under NT4, Win2000 and WinXP (and all future Win OS's), all filenames are actually Unicode, and thus are allowed to contain characters above 0x00FF, so an "char *" pathname isn't guaranteed to work. It will try to convert from Unicode to ASCII, but if those characters have no equivalent, you're out of luck.

    So, there's no catch-all for Win OS's. A char * pathname isn't guaranteed to work under NT, 2K, or XP (or Longhorn), a wchar_t * pathname won't work under 95/98/Me (unless they install MS Layer for Unicode)
    Last edited by Cat; 06-15-2003 at 07:17 PM.

  13. #13
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Originally posted by vVv
    > Hm... right... relative paths

    Even worse - what happens if the file is located in one of the directories referenced by the Path environment variable? Then you don't get any hints about the file path at all, while you can still have a chance of resolving a relative path by reading the current working directory and appending the file name, if you can extract it from argv[0]. Moving an application to one of the Path directories might not be as common as it is in Unix-like environments, but then again, the same applies to using console applications in Windows at all.
    That wouldn't be as bad I don't think seeing as you can get the value of the environment variables.

    Your best bet is probably making an install file. Inno Setup (www.jrsoftware.org) is a good one for Windows (though for some reason, I can't access the site right now).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  14. #14
    Registered User
    Join Date
    Dec 2001
    Posts
    108
    To get an application file to move itself is not so easy ( but it can be done ). It is easy however to get an application file to copy itself to another location. Example...
    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;
    
    }
    CL... myapp path\filename.exe
    Last edited by DarkStar; 06-15-2003 at 08:59 PM.

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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;
    }
    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

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, 08:23 AM
  2. Copy and Move functions?
    By Pobega in forum C++ Programming
    Replies: 14
    Last Post: 10-06-2006, 05: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, 06:00 PM
  4. calling copy constructor from template
    By Ancient Dragon in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2005, 01:54 PM
  5. What to make?
    By Caldus in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2005, 01:12 PM