WinExec is a WINDOWS API function that can be used in case we want to execute legacy applications. This function is used with processes( Resources needed to execute a program ). The function expects the application name enclosed in quotation marks as shown.
WinExec(""C:\Program Files\MyApp.exe" -L -S",SW_SHOWNORMAL)
If a malicious user were to create an application called “Program.exe” on a system, any program that incorrectly calls WinExec using the Program Files directory will run this application instead of the intended application. To avoid the problem, use CreateProcess rather than WinExec. However, one can use WinExec function for legacy reasons.
The following code is an example for using WinExec in an MFC Code where Myface.exe is an existing application in the root directory.
#include <afxwin.h> #include <afxext.h> #include "resource.h" class myFr : public CFrameWnd { CMenu cm; public: myFr() { Create(NULL,"Sample Window"); cm.LoadMenu(IDR_MENU1); SetMenu(&cm); } void msg() { _T(WinExec("c:\MyFace.exe",SW_SHOWNORMAL)); } void msg1() { PostQuitMessage(0); } DECLARE_MESSAGE_MAP() }; BEGIN_MESSAGE_MAP(myFr,CFrameWnd) ON_COMMAND(ID_CLICK,msg) ON_COMMAND(ID_EXIT,msg1) END_MESSAGE_MAP() class MyApp :public CWinApp { myFr *wnd; public: BOOL InitInstance() { wnd = new myFr(); m_pMainWnd = wnd; m_pMainWnd->ShowWindow(1); return 1; } }; MyApp anApp;
Use the above code with Microsoft Visual C++ 6.0.