Using WinExec function

CoderSource.net
Using WinExec function - Article by radsarma
Rating:

Date: 11/10/2004 12:00:00 AM

WinExec Function

   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.


You Can Rate this Article, if you are Logged In      
 

More Links from CoderSource.net:

 
Refer to a Friend:

Your Details:

Name:     e-mail:

Friend Details:

Name:    e-mail:    


MENU
Home
MFC 
C++
.Net
WIN32
Programming
Forum
My Articles
Add to Google
Add to My Yahoo!
Welcome to Codersource.Net Login | Register | Faq  

SEARCH
Google
 

NOTES:


Thanks for visiting our CoderSource.net. This site will be improved with more articles. Interested visitors can also submit their articles through the Submit Article link.Your article will also be published after due consideration by the editor. 

© Copyright 2003. All rights on content reserved by CoderSource.net. Contact    About Us