• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1245
  • Last Modified:

How to call external application?

I am a Visual Basic programmer but know very little of Visual C++.  I want to know how to call an external application and pass a parameter to it.  The external application is in the same directory as my aplication.  In Visual Basic I do this:
Shell "C:\Program Files\Webconference.com\Recorder\Recorder.exe /s" , vbNormalFocus

How do I do this in Visual C++
0
frankmorrison
Asked:
frankmorrison
2 Solutions
 
avizitCommented:
you use the "system" function


for the same example above in C++ you would do


system("C:\Program Files\Webconference.com\Recorder\Recorder.exe /s");
0
 
jkrCommented:
'system()' is a les--than-optimal approach towards this problem, since it opens a console window. 'CreateProcess()' is what the Windows SDK offers, and wrapped up, you can use it like

DWORD ExecuteAndWaitForCompletion   (   LPSTR   pszCmd)
{
   STARTUPINFO         si;
   PROCESS_INFORMATION pi;

   BOOL                bRes;

   DWORD               dwCode  =   0;

   ZeroMemory  (   &si,    sizeof  (   STARTUPINFO));

   si.cb           =   sizeof  (   STARTUPINFO);
   si.dwFlags      =   STARTF_USESHOWWINDOW;
   si.wShowWindow  =   SW_SHOWNORMAL;

   bRes    =   CreateProcess   (   NULL,
                                   pszCmd,
                                   NULL,
                                   NULL,
                                   TRUE,
                                   NORMAL_PRIORITY_CLASS,
                                   NULL,
                                   NULL,
                                   &si,
                                   &pi
                               );

   while   (   WAIT_OBJECT_0   !=  MsgWaitForMultipleObjects   (   1,
                                                                   &pi.hProcess,
                                                                   FALSE,
                                                                   INFINITE,
                                                                   QS_ALLINPUT
                                                               )
           )
           {
               while   (   PeekMessage (   &msg,   NULL,   0,  0,  PM_REMOVE))
                       {
                           DispatchMessage     (   &msg);
                       }
           }

   GetExitCodeProcess  (   pi.hProcess,    &dwCode);

   CloseHandle (   pi.hProcess);
   CloseHandle (   pi.hThread);

   return  (   dwCode);
}

Use it like

ExecuteAndWaitForCompletion("\"C:\Program Files\Webconference.com\Recorder\Recorder.exe /s\"");

If you don't need to wait for the process to finish, just make that

BOOL Execute   (   LPSTR   pszCmd)
{
   STARTUPINFO         si;
   PROCESS_INFORMATION pi;

   BOOL                bRes;

   DWORD               dwCode  =   0;

   ZeroMemory  (   &si,    sizeof  (   STARTUPINFO));

   si.cb           =   sizeof  (   STARTUPINFO);
   si.dwFlags      =   STARTF_USESHOWWINDOW;
   si.wShowWindow  =   SW_SHOWNORMAL;

   bRes    =   CreateProcess   (   NULL,
                                   pszCmd,
                                   NULL,
                                   NULL,
                                   TRUE,
                                   NORMAL_PRIORITY_CLASS,
                                   NULL,
                                   NULL,
                                   &si,
                                   &pi
                               );


   CloseHandle (   pi.hProcess);
   CloseHandle (   pi.hThread);

   return  bRes;
}

Use it like

Execute("\"C:\Program Files\Webconference.com\Recorder\Recorder.exe /s\"");

BTW, the path names need to be quoted if they contain spaces.
0
 
Anthony2000Commented:
jkr, would frankmorrison leave out the "while   (   WAIT_OBJECT_0   !=  MsgWaitForMultipleObjects   (   1,..." because the VB shell command does not wait for the shelled out command to complete?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
jkrCommented:
Thus the 2nd suggestion without that. And a little correction on the paths when using that, that should have been

ExecuteAndWaitForCompletion("\"C:\\Program Files\\Webconference.com\\Recorder\\Recorder.exe /s\"");

and/or

Execute("\"C:\\Program Files\\Webconference.com\\Recorder\\Recorder.exe /s\"");
0
 
bijopuliCommented:
U can use this function in Windows Platform SDK

HINSTANCE ShellExecute(
    HWND hwnd,
    LPCTSTR lpVerb,
    LPCTSTR lpFile,
    LPCTSTR lpParameters,
    LPCTSTR lpDirectory,
    INT nShowCmd
);

example:

ShellExecute(NULL,"open" ,"regsvr32.exe", "-?", NULL, 0); //just this line is enough

Regards
Bijo.
0
 
frankmorrisonAuthor Commented:
Your solution is great, however, I will not know for sure the path of the application.  I do know that it is in the same directory.  So in visual basic I do this:

Shell App.path & "\Recorder.exe /s" , vbNormalFocus

How do I do this in your solution?
0
 
jkrCommented:
Just try

ExecuteAndWaitForCompletion("Recorder.exe /s");

and/or

Execute("Recorder.exe /s");

(depending on the 'wait' issue)
0
 
bijopuliCommented:
Try this:


      TCHAR szAppPath[512];

      TCHAR szFileName[255];
      
      //specifying the file name of the executable which exist in ur application path
      strcpy(szFileName,"ur_exe.exe");
      //wcscpy(szFileName,"race.exe"); //If u r using unicode use this line instead

      //obtainig application path
      GetCurrentDirectory(255, szAppPath);

      //executing the external exe
      ShellExecute(NULL,"open", szFileName, NULL, szAppPath, W_SHOWNORMAL);

Regards
Bijo.
      
0
 
bijopuliCommented:
I ur case this will do:

TCHAR szAppPath[512];
TCHAR szFileName[255];

//specifying the file name of the executable which exist in ur application path
strcpy(szFileName,"Recorder.exe");
//wcscpy(szFileName,"Recorder.exe");//If u r using unicode this line instead

//obtainig application path
GetCurrentDirectory(255, szAppPath);

//executing the external exe
ShellExecute(NULL,"open", szFileName, "/s", szAppPath, W_SHOWNORMAL);
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now