Save the settings of the EXE program in the EXE file itself

Source: Internet
Author: User

Because it was just a way to come up with it. The old gods laughed before they could use the search engine to learn about the situation.
Generally,ProgramSave some settings and configurations in a specialized file or registry, but in some cases, this is very inconvenient. For example, the program that steals the OICQ number (it has never been used, or it cannot be called), sets the mailbox in advance, runs it on another machine, and obtains the OICQ password in some way, finally, send the password to the specified mailbox. Saving the configured mailbox in a configuration file and putting it on another user's machine is not as convenient as saving it in the execution file itself. This is just an example. In some cases, it is very useful to save the configuration or some information in the executable file itself.
Here is an example to illustrate an implementation method. This method is easy to use.
Two programs: settinginme and setyou. Settinginme reads a string in the program and displays the content of this string in a MessageBox. settinginme is equivalent to the program that reads the settings, and that string is equivalent to our settings. Setyou sets the string in settinginme, which is equivalent to setting or parameters in another program. Run settinginme to view the original settings in the program. Exit settinginme, run setyou in the same folder and enter some characters at will. Then exit setyou, and run settinginme to check whether the settings are changed.
You can download these two executable files first. Settinginme.exe setyou.exe
First, let's take a look at the settinginme source program.

Settinginme
___________________________________________________________________
# Include
# Include
# Define cbmatchstr 55 // strlen ("I'm jiurl, my homepage is http://jiurl.yeah.net. Salute! ")

Char magicstr [cbmatchstr + 256] = "I'm jiurl, my homepage is http://jiurl.yeah.net. Salute! Http://jiurl.yeah.net \ n \ nhttp: // jiurl.nease.net \ n \ njiurl@mail.china.com ";
// 256 is used to store the length of the parameter space, which must not exceed.

Int apientry winmain (hinstance,
Hinstance hprevinstance,
Lpstr lpcmdline,
Int ncmdshow)
{
Char * lptext = magicstr + cbmatchstr;
MessageBox (null, lptext, "Yes! ", Null );
Return 0;
}

After applying for a global variable magicstr, we need to put the setting in it. settinginme will use the content in this global variable as the setting. After the program is compiled, the global variable occupies the same space in the EXE file. You only need to set the setyou program to find the location of the global variable in the file, and then write the corresponding content, we can achieve our goal. Because global variables occupy the same space in the compiled EXE file, they only change the value of global variables and do not destroy executable programs. Here, we use the content of the first 55 bytes of the global variable as a search string to indicate the position of the global variable in the file. The first 256 bytes after the search string, used to store settings. We will discuss more about this later.
Next let's take a look at how setyou finds this global variable and sets its content, the setyou source program.

Setyou
___________________________________________________________________
# Include
# Include
# Include "resource. H"

Int_ptr callback dialogproc (hwnd, uint, wparam, lparam );
Char setstr [256];

# Define cbmatchstr 55 // strlen ("I'm jiurl, my homepage is http://jiurl.yeah.net. Salute! ")

Int apientry winmain (hinstance,
Hinstance hprevinstance,
Lpstr lpcmdline,
Int ncmdshow)
{
Handle hfile;
Char err [256];
Char matchstr [cbmatchstr + 1] = "I'm jiurl, my homepage is http://jiurl.yeah.net. Salute! ";
// String Length 55, string Terminator 1, 56 bytes in total

Hfile = createfile ("settinginme.exe", generic_write | generic_read, 0,
Null, open_existing, file_attribute_normal, null );
// Settinginme.exe and the program must be in the same directory
If (hfile = invalid_handle_value)
{
Sprintf (ERR, "creatfile error: 0x % 08x! ", Getlasterror ());
MessageBox (null, err, "Congratulation! ", Null );
Return 0;
}

Char data;
DWORD nread;
Int retval;
Int I = 0;

// This is not efficient, but it is intuitive,
// If 1024 bytes are read at a time and then compared in memory...
While (1)
{
Retval = readfile (hfile, & Data, 1, & nread, null );
If (retval = 0 | nread = 0)
{
MessageBox (null, "not found place to set", "Congratulation! ", Null );
Break;
}

If (Data = matchstr [I]) I ++;
Else I = 0;

If (I = cbmatchstr)
{
Dialogbox (hinstance, makeintresource (idd_dialog), null, (dlgproc) dialogproc );

Retval = writefile (hfile, setstr, strlen (setstr ),
& Nread, null );
If (retval! = 0)
MessageBox (null, "set OK! "," Yes! ", Null );
Else
{
Sprintf (ERR, "writefile error: 0x % 08x! ", Getlasterror ());
MessageBox (null, err, "Congratulation! ", Null );
}
Break;
}
}

MessageBox (null, "over", "over", null );

Closehandle (hfile );
Return 0;
}

Int_ptr callback dialogproc (hwnd hwnddlg, uint umsg,
Wparam, lparam)
{
Switch (umsg)
{
Case wm_command:
Switch (wparam)
{
Case idok:
Getdlgitemtext (hwnddlg, idc_in, setstr, sizeof (setstr ));
// The length of setstr [] cannot exceed 256.
// In the dialog box, if the input string exceeds 256,
// Getdlgitemtext () intercepts the part in the range.
Enddialog (hwnddlg, true );
Break;
Case idcancel:
Postquitmessage (0 );
Break;
}
Break;
}
Return 0;
}

Setyou first use createfile to open settinginme.exe in the same folder, and then start reading the content of the file in one byte, each reading one byte, compare the byte with the string at the beginning of the global variable used to indicate the location of the global variable. Read a byte and compare whether it is the same as the I (First 0) in the flag string. If it is the same, set I ++; then read and compare (then compare is the second byte in the string), and the same, I ++, and... after some of them are the same, there is a difference. I is reset to 0, then read the file and look back. Or the read content is the same as the entire flag string. We found the global variable to be searched. After finding the global variable, we can use writefile to change the content of the global variable. In the setyou program, a dialog box is displayed to obtain the input string. In setyou writefile (hfile, setstr, strlen (setstr), the last parameter is a strlen, that is, it will only overwrite the front part of the set string. If the parameter is 256 (that is, sizeof (setstr), the entire setting will be overwritten.

in this way, we apply for a global variable statically in a program to reserve some space in the EXE file as the space to save settings, configurations, parameters, and information. Put a flag string at the beginning of the space for searching. In another program, find the flag string, find the space of the global variable, and set the corresponding position. Then, the settings of the EXE program are saved in the EXE file.
it should be noted that after the program is compiled, the content in the EXE file is fixed, so there are other places in the file that are the same as the flag string, otherwise, nothing else is the same as the flag string. If the content of a local and flag string is the same, you only need to change the flag string. As long as the flag string has a certain length, it is easy to be different from other content. Furthermore, after compilation, the content of the EXE file is fixed, and the location of the global variable in the EXE file is fixed. As long as the location can be found, there is no need for a flag string, in this example, we use a flag string for ease of illustration and understanding.
Finally, the program applies for a global space statically and uses the content in the space as a parameter, setting, configuration, or some information. A space of the same size exists in the compiled EXE file. Open the EXE file of this program in another program, and find the location of this space in the EXE file in some way, you can set it.
Alas, this Article is really bad. Let's take a look. I also said that I had won a Nobel Prize for Literature. It seems that I have no chance of playing.

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.