In VC Program To call the EXE file or batch file:
1. Use the system function.
This function can be used directly to call the EXE program or bat batch processing program.
Example: cstring strcommand ("D: \ test. Bat ");
System (strcommand );
Ii. Use the ShellExecute Function
This function can hide or display the called window..
Hinstance ShellExecute (hwnd,
Lpctstr lpoperation,
Lpctstr lpfile,
Lptstr lpparameters,
Lpctstr lpdirectory,
Int nshowcmd
);
3. Use the CreateProcess Function
STD: String strcommand = Strapp +" \ Perdecodex2ap.exe ";
Process_information PI;
Startupinfo Si;
// Initialize the variable
Memset (& Si, 0, sizeof (SI ));
Si. cb = sizeof (SI );
Si. wshowwindow = sw_hide;
Si. dwflags = startf_useshowwindow;
Char buff [256];
Sprintf (buff, "% s", strcommand. c_str ());
Bool fret = CreateProcess (null,
Buff,
Null,
Null,
False,
Normal_priority_class | create_no_window,
Null,
Null,
& Si,
& PI );
If (! Fret)
{
Lpvoid lpmsgbuf;
Formatmessage (format_message_allocate_buffer |
Format_message_from_system |
Format_message_ignore_inserts,
Null,
Getlasterror (),
Makelangid (lang_neutral, sublang_default ),//Default language
(Lptstr) & lpmsgbuf,
0,
Null );
Afxmessagebox (lpctstr) lpmsgbuf );
Return false;
}
4. Use the following encapsulated functions (self-written) and try various methods to start the executable program.
Commandexecuter (const char * szappname, const char * szcmd)
{
Char szworkpath [256] = {0 };
Char szcommand [256] = {0 };
Int J = 0;
// Obtain parameters from the configuration file
Startupinfo Si = {0 };
Process_information Pi = {0 }; Si. cb = sizeof (SI );
If (szappname! = NULL & strlen (szappname)> 0)
{
Memset (szworkpath, 0,256 ); Getsystemdirectory (szworkpath, 256 );
Szworkpath [3] = '\ 0 ';
Strcat (szworkpath, "Program Files \\");
Strcat (szworkpath, szappname ); If (_ access (szworkpath, strlen (szworkpath) =-1)
{
Strcpy (szworkpath, szappname );
} // If (_ access (szworkpath, strlen (szworkpath) =-1)
Strcat (szworkpath ,"");
Strcat (szworkpath, szcmd ); If (CreateProcess (null,
(Char *) szworkpath,
Null,
Null,
False,
0,
Null,
Null,
& Si,
& PI ))
{
Closehandle (PI. hthread );
Closehandle (PI. hprocess );
Return;
}
If (CreateProcess (szappname,
(Lpstr) szcmd,
Null,
Null,
False,
0,
Null,
Null,
& Si,
& PI ))
{
Closehandle (PI. hthread );
Closehandle (PI. hprocess );
Return;
}
If (INT) ShellExecute (null,
Null,
(Lpcstr) szworkpath,
Null,
Null,
Sw_show)> 32)
{
// Started successfully
Return;
}
If (INT) ShellExecute (null,
Null,
Szappname,
(Lpcstr) szcmd,
Null,
Sw_show)> 32)
{
// Started successfully
Return;
}
} // If (szappname! = NULL & strlen (szappname)> 0)
Else
{
If (CreateProcess (null, (char *) szcmd, null, null, false, 0, null, null, & Si, & PI ))
{
Closehandle (PI. hthread );
Closehandle (PI. hprocess );
Return;
} // If (CreateProcess (null, (char *) szcmd, null, null, False, 0, null, null, & Si, & PI ))
ShellExecute (null, null, (lpcstr) szcmd, null, null, Sw_show );
}
}