Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

system function on windows NT

Posted on 2003-10-27
9
Medium Priority
?
231 Views
Last Modified: 2010-04-01
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.
0
Comment
Question by:martin11_ar
  • 5
  • 4
9 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 9627343
>>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
 
LVL 86

Expert Comment

by:jkr
ID: 9627382
BTW, *how*are you trying to use 'system()'?
0
 

Author Comment

by:martin11_ar
ID: 9627757
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
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.

 
LVL 86

Expert Comment

by:jkr
ID: 9627788
Hmm, try the above function.
0
 

Author Comment

by:martin11_ar
ID: 9627952
it call the compiler but don´t find the file that i pass as first parameter.
0
 
LVL 86

Expert Comment

by:jkr
ID: 9628038
>>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
 

Author Comment

by:martin11_ar
ID: 9628299
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
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 9628334
>>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
 

Author Comment

by:martin11_ar
ID: 9628463
Thank you very much.
Excellent solution!!!
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question