system function on windows NT

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.
martin11_arAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
>>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);
}

0
jkrCommented:
BTW, *how*are you trying to use 'system()'?
0
martin11_arAuthor 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.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

jkrCommented:
Hmm, try the above function.
0
martin11_arAuthor Commented:
it call the compiler but don´t find the file that i pass as first parameter.
0
jkrCommented:
>>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?
0
martin11_arAuthor 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.
0
jkrCommented:
>>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");
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
martin11_arAuthor Commented:
Thank you very much.
Excellent solution!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.