C. Modify the string in the executable file to generate a new executable file.

Source: Internet
Author: User

C. Modify the string in the executable file to generate a new executable file.

This technology is often used in remote control software, and we already have a remote control software server. We use the client configuration of the remote control software to generate our own server software to update the port and IP address of the server.


See the source code below


# Include <stdio. h>
# Include <Windows. h>




Int FindStr (char * destStr, char * srcStr, int destStrLen, int srcStrLen );
Void ReplaceStr (char * destStr, char * srcStr, int beginPoint );


Int main ()
{
FILE * pReadFile;
FILE * pOutFile;
Char * pFileBuf;


// Open the source program to be modified
If (pReadFile = fopen ("../custom/TestPE.exe", "rb") = NULL)
{
Printf ("It's failure to open the readable file \ n ");
Return-1;
}


// Source code to be generated
If (pOutFile = fopen ("../custom/TestPE1.exe", "wb") = NULL)
{
Printf ("It's failure to open the writable file \ n ");
Return-1;
}


Fseek (pReadFile, 0L, SEEK_END );
Int fileLen = ftell (pReadFile );


PFileBuf = (char *) malloc (fileLen + 1 );
If (pFileBuf = NULL)
{
Fclose (pReadFile );
Return-1;
}


Fseek (pReadFile, 0L, SEEK_SET );
Fread (pFileBuf, fileLen, sizeof (char), pReadFile );
PFileBuf [fileLen] = '\ 0 ';


// The string to be modified
Char * modifyStr = "bbbbbbbbbbb ";
// String in the source program
Char * findStr = "aaaaaaaaaaaaa ";
Int beginPoint;


// Find the starting position of the string to be searched in the source array.
BeginPoint = FindStr (pFileBuf, findStr, fileLen, 0 );
If (beginPoint =-1)
{
Printf ("It's failure to find the string \ n ");
Return-1;
}


// Replace our string
ReplaceStr (pFileBuf, modifyStr, beginPoint );


// Generate the modified source program
Fwrite (pFileBuf, fileLen, sizeof (char), pOutFile );


Fclose (pReadFile );
Fclose (pOutFile );
If (pFileBuf! = NULL)
{
Free (pFileBuf );
PFileBuf = NULL;
}


Return 0;
}


Int FindStr (char * destStr, char * srcStr, int destStrLen, int srcStrLen)
{
Int I, j, findStrLen;


If (srcStrLen = 0)
{
FindStrLen = strlen (srcStr );
}
Else
{
FindStrLen = srcStrLen;
}


For (I = 0; I <destStrLen; I ++)
{
For (j = 0; j <findStrLen; j ++)
{
If (destStr [I + j]! = SrcStr [j])
{
Break;
}
}


If (j = findStrLen)
{
Return I;
}
}
Return-1;
}


Void ReplaceStr (char * destStr, char * srcStr, int beginPoint)
{
Int srcStrLen, I;
SrcStrLen = strlen (srcStr );


For (I = 0; I <srcStrLen; I ++)
{
DestStr [beginPoint + I] = srcStr [I];
}


DestStr [beginPoint + srcStrLen] = '\ 0 ';
}


How can I generate executable files in C language and send them to other computers?

Are you sure you have an executable file? The extension name of the executable file is .exe, not. c or. cpp. In VC, the C language cannot directly generate the exe file.
 
How to Use vc6 to generate executable files in C Language

After selecting the Win32 Console Application, create an empty project and a new. cpp program, and set the name to ***. c. The code is directly written in the. c file. After compilation and linking, the directory contains the. exe file in the debug folder.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.