Thread: Embedding a file within another file

  1. #1
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80

    Embedding a file within another file

    I need to keep users from directly accessing an exe file (call it my.exe). To do so, I am told to embed this file within a new file, which is to be built using C++ (lets call it new.exe). This new file does the following upon execution:

    1) reads the IP add of the computer. if the IP does not match 1.2.3.4 then terminates
    2) asks the user for username/password
    3) connects to a PostgreSQL database to authenticate the user
    4) if successful, then it would execute the embedded file my.exe
    5) when the user is done with my.exe and terminates it, a window with a text box and a SEND button pops up and asks the user to fill it out
    6) the content of the box is sent to the SQL database when SEND is pressed
    7) new.exe terminates

    I could also ask my questions in the following way:
    - how to embed my.exe within new.exe
    - how to do step (1)
    - how to connect to SQL databases in C++ (I think I need API to do so, right?)
    - how to do step (5)

    I am very short on time and need to manage the above as soon as possible. By the way, I would consider my level of knowledge of C++ low.

    Any help is greatly appreciated. Have a happy new year.

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    In Windows environment:

    Embedding an executable (the executable is embedded in a resource file):

    Create a resource file with the following:
    Code:
    #define IDR_MYNEW 		1000
    
    IDR_MYNEW             MYNEW_EXE DISCARDABLE     "C:\\temp\\new.exe"
    Compile the resource file: RC myRC.RC

    Then: cl my.cpp myRC.res ws2_32.lib

    In your my.exe execute the following functions to extract the embedded excecutable:

    FindResource (find embedded executable in resource file)
    LoadResource
    LockResource
    //Now copy embedded executable to hard drive
    CreateFile
    WriteFile

    Code for IP address:
    Code:
    #include <stdio.h>
    #include <WinSock.h>
    
    int main(void)
    {
        WORD wVersionRequested;
        WSADATA wsaData;
        char hostname[255] = {0};
        int iCount = 0;
        PHOSTENT hostinfo;
        wVersionRequested = MAKEWORD( 1, 1 );
        char *ipaddress = NULL;
        if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
            if( gethostname ( hostname, sizeof(hostname)) == 0)
            {
                printf("hostname: %s\n", hostname);
                if((hostinfo = gethostbyname(hostname)) != NULL)
                {
                    while(hostinfo->h_addr_list[iCount])
                    {
                        ipaddress = inet_ntoa(*(struct in_addr *)hostinfo->h_addr_list[iCount]);
                        printf("ipaddress #%d: %s\n", ++iCount, ipaddress);
                    }
                }
            }
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80
    Thanks Bob for your help. For the following command:
    cl my.cpp myRC.res ws2_32.lib
    what is my.cpp supposed to be? When I run it I get an error saying that my.cpp does not exist.

    Also for the following:
    In your my.exe execute the following functions to extract the embedded excecutable:

    FindResource (find embedded executable in resource file)
    LoadResource
    LockResource
    //Now copy embedded executable to hard drive
    CreateFile
    WriteFile
    Can you give some more in-depth info? Or an example demonstrating the usage of those commands? Moreover by "my.exe" did you mean "new.exe"? Because my.exe is supposed to be the original file and new.exe is the resource file.

  4. #4
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    my.cpp is your C Plus Plus file (your Source Code file) Good luck with your program, sounds really cool!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM