Solved

system function on windows NT

Posted on 2003-10-27
9
230 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

728 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