system function on windows NT

martin11_ar
martin11_ar used Ask the Experts™
on
Can I use the system function in a GUI program on Windows NT ?
I´m trying but it doesn´t work well. It doesn´t call the program that i´m invoking.How ever it return NO ERROR in errno variable.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
jkr
Top Expert 2012

Commented:
>>Can I use the system function in a GUI program on Windows NT ?

Yes, sure - I would not recommentd that, however. Better use sth. like

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

   BOOL                bRes;

   DWORD               dwCode  =   0;

   MSG                           msg;

   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,
                                                     s              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);
}

jkr
Top Expert 2012

Commented:
BTW, *how*are you trying to use 'system()'?

Author

Commented:
I´m invoking the gcc compiler and other command line programs.
And i´m trying to call a bat file too , but anything works well.
Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

jkr
Top Expert 2012

Commented:
Hmm, try the above function.

Author

Commented:
it call the compiler but don´t find the file that i pass as first parameter.
jkr
Top Expert 2012

Commented:
>>but don´t find the file that i pass as first parameter

It seems that you are not correctly specifying the file or are working in a different directory? Where is your application located and where are the files you want to compile?

Author

Commented:
I´m indicating the complete path for the gcc , and i tried with the complete path and only the file name for the file to compile but don´t work.The gcc and the c file are in the same directory, but it´s different that the application directory.
Top Expert 2012
Commented:
>>The gcc and the c file are in the same directory, but it´s different that the application directory

There we have the problem - try

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

  BOOL                bRes;

  DWORD               dwCode  =   0;

  MSG                       msg;

  ZeroMemory  (   &si,    sizeof  (   STARTUPINFO));

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

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

  while   (   WAIT_OBJECT_0   !=  MsgWaitForMultipleObjects   (   1,
                                                                  &pi.hProcess,
                                                                  FALSE,
                                                    s              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);
}

and call it like


ExecuteAndWaitForCompletion   ( "gcc -o myfile.exe myfile.cpp", "c:\\path\\to\\gcc_and_file");

Author

Commented:
Thank you very much.
Excellent solution!!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial