In the C language for DOS, there is a system () function that executes a command-line program, WINAPI also provides us with similar functions in Windows programming, which are winexec () and ShellExecute (), Here is a discussion of the use of these two functions.
1) winexec () function prototype:
UINT winexec (LPCSTR lpcmdline,uint ucmdshow);
Parameter description: (activation means to accept the focus, that is, the title bar becomes blue)
lpCmdLine: String ending with 0, command-line argument.
Ucmdshow: How the new application runs. The values are as follows:
Sw_hide Hidden
Sw_maximize maximized
Sw_minimize is minimized and the z order is activated in the window after this window (i.e. the next layer of the window)
Sw_restore activates the window and reverts to the initialization size sw_show activates the window with the current size and state
Sw_showdefault Run as Default
Sw_showmaximized activates the window and maximizes
Sw_showminimized activates the window and minimizes
Sw_showminnoactive minimizes but does not change the currently active window
Sw_showna Displays the window in its current state without changing the currently active window
Sw_shownoactivate display windows with initialization size without changing the currently activated window
Sw_shownormal activates and displays the window, and if it is maximum (small), the window will be restored. This value should be used when running the program for the first time
For example, I want to use Notepad to open the C:HDC. TXT "To run in the normal way:
winexec ("Notepad c:\hdc.txt", SW_SHOWNORMAL);
If the call succeeds, the function returns a value that is not less than 31, otherwise the call fails with the meaning of the return value as follows:
0 system memory or insufficient resources
Error_bad_format. EXE file format is invalid (e.g. not 32-bit application)
Error_file_not_found The specified file is provided with the found
Error_path_not_found the specified path was not found
This function is very similar to system () and can only be run. EXE file, so that it is not satisfactory in windows, such as the method can not be used to open the file through the associated method, such as WinExec ("1.html", Sw_showna), you cannot open this document.
2) ShellExecute ()
Function Prototypes:
HINSTANCE ShellExecute (HWND hwnd, LPCTSTR lpoperation, LPCTSTR lpfile, LPCTSTR lpparameters, LPCTSTR Lpdirectory, INT nShowCmd);
Parameter description:
Handle to the HWND window
Lpoperation actions, such as "open", "print", "explore" corresponding to "on", "Print", "Tour", or Empty (""), this time represents the default action.
Lpfile the file to be manipulated.
Lpparameters If lpfile specifies an executable file, the parameter
Lpdirectory operation of the directory nShowCmd program running mode, the value is shown in the example above.
If the function call succeeds, the handle of the instance is returned, if unsuccessful, the return value contains the error information, because the type is more, here is not listed, see WINAPI Help.
Thus, the above example can be changed to (assuming that the window handle is handle)
ShellExecute (Handle, "open", "notepad", "C:\hdc.txt", "", SW_SHOWNORMAL);
It doesn't have to be that complicated, because. The TXT affiliate program is Notepad, so just do it.
ShellExecute (Handle, "", "C:\hdc.txt", "", "", SW_SHOWNORMAL);
This function is similar to command-line command "Start" in Win9x, which can not only manipulate files, but also manipulate HTTP, mailto, and so on. So we can design a hyperlink-style program. For example, in VB, we first declare this function (can be obtained by VB's own API browser):
Public Declare Function ShellExecute Lib "Shell32.dll" Alias "Shellexecutea" (ByVal hwnd as Long, ByVal lpoperation As String, ByVal lpfile as String, ByVal lpparameters as String, ByVal Lpdirec Tory as String, ByVal nShowCmd as long) as long
Then place two labels and set their properties to:
Name lbhomepage lbemail Caption http://bcbtop.126.com mailto:[email protected]
and change the font to blue with an underscore, and then add the following code to the click event of the two label, respectively:
ShellExecute (Me.hwnd, "", Lbhomepage.caption, "", "", SW_SHOWNORMAL);
ShellExecute (Me.hwnd, "", Lbemail.caption, "", "", SW_SHOWNORMAL);
As can be seen from the above, the ShellExecute function is much stronger, and can completely replace the winexec (), in fact, WinExec () is also to be compatible with the previous version of the reservation, so we should try to use ShellExecute ().
How to invoke external programs in C++builder