In the process of software development, there are a lot of small functions to implement, although you do not need these things, but if the application pipeline will be yours Program More professional.
1. Set the program to run automatically.
Many monitoring software require that the software can start running without clicking the icon after the system restarts, the method is to write the Registry SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run. For details about the reference program, refer to: (find the execution file of the program directory and add the registry if it exists)
Hkey regkey;
Cstring Spath;
Getmodulefilename (null, Spath. getbuffersetlength (max_path + 1), max_path );
Spath. releasebuffer ();
Int NPOs;
NPOs = Spath. reversefind ('\\');
Spath = Spath. Left (NPOs );
Cstring lpszfile = Spath + "\ *****. EXE"; // Add the name of the execution file to be searched.
Cfilefind ffind;
Bool bsuccess;
Bsuccess = ffind. findfile (lpszfile );
Ffind. Close ();
If (bsuccess)
{
Cstring fullname;
Fullname = lpszfile;
Regkey = NULL;
Regopenkey (HKEY_LOCAL_MACHINE, "SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run", & regkey );
Regsetvalueex (regkey, "*****", 0, REG_SZ, (const unsigned char *) (lpctstr) fullname, fullname. getlength (); // Add the content you need to register in the registry.
This-> updatedata (false );
}
Else
{
Theapp. setmainskin ();
: Afxmessagebox ("failed to find execution program, automatic operation failed ");
Exit (0 );
}
Ii. Automatic Data Source Configuration
Many programs require database-specific operations. Here, access is used as an example, because access is the most commonly used in the development of small and medium VC systems. If the program is transplanted, if you need to configure the data source in the configuration panel for a very basic user, it is a bit difficult to say.
# Include <odbcinst. h>
// Configure the data source. The database is in the application directory. For example, if the database file name is ***. mdb, you can copy the database file to the program directory when running the program.
Cstring Spath;
Getmodulefilename (null, Spath. getbuffersetlength (max_path + 1), max_path );
Spath. releasebuffer ();
Int NPOs;
NPOs = Spath. reversefind ('\\');
Spath = Spath. Left (NPOs );
Cstring lpszfilename = Spath + "\ ***. mdb"; // modify it to the name of your database file.
Cfilefind ffind;
If (! Ffind. findfile (lpszfilename ))
{
: Afxmessagebox ("no database found ");
Exit (0 );
}
Cstring szdesc;
Szdesc. format ("DSN = *****; description = the database for ****; fil = microsoftaccess; defaultdir = % s; DBQ = % s; ", Spath, lpszfilename); // here, the *** number can be added as your description.
// Add a data source
If (! : Sqlconfigdatasource (null, odbc_add_dsn, "Microsoft Access Driver (*. mdb)", (lpcstr) szdesc ))
{
: Afxmessagebox ("32-bit ODBC data source configuration error! ");
Exit (0 );
}
3. Set the display mode:
The runtime environment for porting many programs is changed. It is possible that your original development environment is 1024x768, but when the display is greater than 17 (when the resolution exceeds the resolution of your development), the display of the program may not look good.
Devmode lpdevmode;
Lpdevmode. dmpelsheight = 768; // pixel point in Y direction
Lpdevmode. dmpelswidth = 1024; // pixel point in the X direction
Lpdevmode. dmdisplayfrequency = 85; // screen update rate
Lpdevmode. dmfields = dm_pelswidth | dm_pelsheight | dm_displayfrequency;
Changedisplaysettings (& lpdevmode, 0 );
4. Load other applications in your program:
In addition to calling various modules for collaborative work (DLL), your program may also call applications in different development environments, such as calling Delphi in the VC environment, for the execution program developed by VB, you can use the following method (copy the called application to the program directory ):
Cstring Spath;
Getmodulefilename (null, Spath. getbuffersetlength (max_path + 1), max_path );
Spath. releasebuffer ();
Int NPOs;
NPOs = Spath. reversefind ('\\');
Spath = Spath. Left (NPOs );
Cstring lpszfilename = Spath + "\ ***. EXE"; // modify the name of the file that calls the application.
Cfilefind ffind;
If (! Ffind. findfile (lpszfilename ))
{
: Afxmessagebox ("No called application found! ");
Return false;
}
Else
ShellExecute (null, null, _ T ("***. EXE"), null, Spath, null );
5. Process Termination:
To end other program processes in your program, list the processes and search for them:
# Include "tlhelp32.h"
DWORD processid [50];
Cstring KKK [50];
Handle snapshot = createconlhelp32snapshot (th32cs_snapprocess, 0 );
Processentry32 * info = new processentry32;
Processentry32 processinfo; // declare the process information variable
Processinfo. dwsize = sizeof (processinfo); // you can specify the processinfo size.
// Return the information of the first PROCESS IN THE SYSTEM
Bool status = process32first (snapshot, & processinfo );
Int m_nprocess = 0;
While (Status)
{
Cstring S, str1, str2;
S. Format ("% d", processinfo. cntthreads );
Str1.format ("% s", processinfo. szexefile );
Str1 = processinfo. szexefile;
Kkk [m_nprocess] = processinfo. szexefile;
Processid [m_nprocess] = processinfo. th32processid;
If (str1 = "***. EXE") // ***. EXE is the name of the process to be completed.
{
Handle processhandle;
Processhandle = OpenProcess (process_all_access, false, processid [m_nprocess]);
Terminateprocess (processhandle, 0 );
}
Status = process32next (snapshot, & processinfo );
M_nprocess ++;
}