Thread: Copy an .exe to a new .exe file using C/C++

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    52

    Copy an .exe to a new .exe file using C/C++

    Let there be an executable file say, t1.exe. If I run t1.exe then it shall copy itself to a new executable file say t2.exe.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    
    void main()
    {
    FILE *fp,*tp;
    unsigned long int t=0;
    tp=fopen("t2.exe","wb");
    //rewind(tp);
    if((fp=fopen("t1.exe","rb"))!=NULL)
    {
    fseek(fp,0L,2);
    unsigned long int pos=ftell(fp);
    rewind(fp);
    do{
    fputc(fgetc(fp),tp);
    t++;
    }while(t!=pos+1);
    }
    
    fcloseall();
    //system("t2.exe");
    printf("End of t1");
    getch();
    }
    t2.exe is created but when I run this it says "t2 has stopped working." I'm using Windows 7(Home Basic) 64bit. The program is compiled with Borland C++ V. 5.02.

    And amazingly t1.exe and t2.exe has exactly same file size(in bytes) but still t1.exe works but t2.exe doesn't......don't understand why...??!!

    Please reply fast!!! It's URGENT....!!!!
    Thnx!

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    t2.exe will, having the same code as t1.exe, try to open t2.exe (itself) for binary writing. That will most likely fail.
    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. #3
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    strangely enough i tried the method of copying each byte over to the new file and the new file was a 16-bit DOS file :S but there's always another way of doing things

    C code
    Code:
    include <windows.h>
    
    int main() {
         char * original_file = "originalfile.exe";
         char * new_file = "newfile.exe";
         ( void ) CopyFile ( ( LPCTSTR )original_file, ( LPCTSTR )new_file, FALSE );
         return 0;
    }

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by codeprada View Post
    strangely enough i tried the method of copying each byte over to the new file and the new file was a 16-bit DOS file :S but there's always another way of doing things
    Probably nothing was copied to the new file and Windows assumed that it was MSDOS. That happened because all 32-bit exe files have the "This program cannot be run in DOS mode" string somewhere at the beginning.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Note that this is a windows programming problem, not a C/C++ problem.

    A windows executable cannot copy itself, as - when it is running - as (32 and 64 bit) windows use a mechanism of memory mapped files to load the executable image into memory. A consequence of this is effectively locking the executable file so it cannot be copied (or deleted).

    The only way for an executable to copy itself is indirect. For example, create a second process to do the copy. The second process must wait for all processes running the executable to exit before copying.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    52
    nvoigt, u r rite!!! Thanks!!!

    grumpy: A windows executable cannot copy itself, as - when it is running - as (32 and 64 bit) windows use a mechanism of memory mapped files to load the executable image into memory. A consequence of this is effectively locking the executable file so it cannot be copied (or deleted).

    I want to know more about this. Either you write or tell me any book or article where this topic has been explained!!!

    And would you please post me the code for the method you have mentioned.....

    Thnx!!!

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You'll be able to find an explanation on MSDN (Microsoft Developer Network). Do a search for something like "executable memory mapped". The description I gave in a preceding post is a short summary of a couple MSDN magazine articles that I read several years ago (2003, if not earlier). In practice, a number of articles available through MSDN describe the WIN32 Portable Executable File Format, and those articles often go into details on how executables are loaded (since the format of a file and methods used to load it into memory are closely related).

    There is no "the code" for the method I mentioned - there are many possible ways of implementing it. And there are also many alternative methods. Look up techniques for creating processes, interprocess communication, etc.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  5. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM