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.