Solved

system function on windows NT

Posted on 2003-10-27
9
229 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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 125 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

813 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now