Solved

cl.exe and VC++ IDE

Posted on 2001-07-26
3
398 Views
Last Modified: 2013-11-20
How does the cl.exe (which is a standalone console application?) passes string data to another application, the VC++ IDE?

I am writing a compiler (which is a standalone console application) and i need to add an Window GUI to it.  The compiler project workspace should not be coupled to the Window GUI code.  
0
Comment
Question by:leowlf
[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
3 Comments
 
LVL 12

Accepted Solution

by:
migel earned 50 total points
ID: 6321309
Hi!
it uses stdout/stderr reditrection mechanism
where your GUI run console application it can provide your owns Pipes to replace stdout stream.
code can be like this:

DWORD Run()
{
      HANDLE                              PipeReadHandle;
      HANDLE                              PipeWriteHandle;
      PROCESS_INFORMATION            ProcessInfo;
      SECURITY_ATTRIBUTES            SecurityAttributes;
      STARTUPINFO                        StartupInfo;
      BOOL                              Success;

      ZeroMemory( &StartupInfo,                  sizeof( StartupInfo ));
      ZeroMemory( &ProcessInfo,                  sizeof( ProcessInfo ));
      ZeroMemory( &SecurityAttributes,      sizeof( SecurityAttributes ));

      //      Create a pipe for the child's STDOUT.
      SecurityAttributes.nLength              = sizeof(SECURITY_ATTRIBUTES);
      SecurityAttributes.bInheritHandle       = TRUE;
      SecurityAttributes.lpSecurityDescriptor = NULL;

      Success = CreatePipe
      (
            &PipeReadHandle,            // address of variable for read handle
            &PipeWriteHandle,            // address of variable for write handle
            &SecurityAttributes,      // pointer to security attributes
            0                                    // number of bytes reserved for pipe (use default size)
      );

      if ( !Success )
      {
            //Error (_T("Error creating pipe"));
            return errCantRun;
      }      

      //      Set up members of STARTUPINFO structure.
      StartupInfo.cb           = sizeof(STARTUPINFO);
      StartupInfo.dwFlags      = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
      StartupInfo.wShowWindow  = SW_HIDE;
      StartupInfo.hStdOutput   = PipeWriteHandle;
      StartupInfo.hStdError    = PipeWriteHandle;

      //      Create the child process.

      Success = CreateProcess
      (
            NULL,                              // pointer to name of executable module
            LPTSTR(szCommand),      // command line
            NULL,                              // pointer to process security attributes
            NULL,                              // pointer to thread security attributes (use primary thread security attributes)
            TRUE,                              // inherit handles
            0,                                    // creation flags
            NULL,                              // pointer to new environment block (use parent's)
            m_szCurrentDirectory,      // pointer to current directory name
            &StartupInfo,                  // pointer to STARTUPINFO
            &ProcessInfo                  // pointer to PROCESS_INFORMATION
      );                

      if ( !Success )
      {
//            ShowError(_T("Error creating process"));
            return errCantRun;
      }

      DWORD      BytesLeftThisMessage = 0;
      DWORD      NumBytesRead;
      TCHAR      PipeData[BUF_SIZE];
      DWORD      TotalBytesAvailable = 0;

      for ( ; ; )
      {
            NumBytesRead = 0;

            Success = PeekNamedPipe
            (
                  PipeReadHandle,                        // handle to pipe to copy from
                  PipeData,                              // pointer to data buffer
                  1,                                          // size, in bytes, of data buffer
                  &NumBytesRead,                        // pointer to number of bytes read
                  &TotalBytesAvailable,            // pointer to total number of bytes available
                  &BytesLeftThisMessage            // pointer to unread bytes in this message
            );

            if ( !Success )
            {
                  // ShowError(_T("PeekNamedPipe fialed"));
                  break;
            }

            if ( NumBytesRead )
            {
                  Success = ReadFile
                  (
                        PipeReadHandle,            // handle to pipe to copy from
                        PipeData,                  // address of buffer that receives data
                        BUF_SIZE - 1,            // number of bytes to read
                        &NumBytesRead,            // address of number of bytes read
                        NULL                        // address of structure for data for overlapped I/O
                  );

                  if ( !Success )
                  {
                        //ShowError(_T("ReadFile fialed"));
                        break;
                  }

                  //      Zero-terminate the data.
                  PipeData[NumBytesRead] = '\0';

                  //      Append the output to the CEdit control.
                  AppendText(PipeData);

                  //      Peek and pump messages.
                  PeekAndPump();
            }
            else
            {
                  //      If the child process has completed, break out.
                  if ( WaitForSingleObject(ProcessInfo.hProcess, 0) == WAIT_OBJECT_0 )
                  {
                        break;
                  }

                  //      Peek and pump messages.
                  PeekAndPump();

                  //      If the user cancelled the operation, terminate the process.
                  if ( m_bStopped )
                  {
                        Success = TerminateProcess
                        (
                              ProcessInfo.hProcess,
                              0
                        );

                        if ( Success )
                        {
                              AppendText(_T("\r\nCancelled.\r\n\r\nProcess terminated successfully.\r\n"));
                        }
                        else
                        {
                              //ShowError(_T("Error terminating process."));
                        }

                        break;
                  }

                  //      Sleep.
                  Sleep(m_dwSleepMilliseconds);
            }
            
      }

      DWORD dwRetCode = 0;
      ::GetExitCodeProcess(ProcessInfo.hProcess, &dwRetCode);
      if (dwRetCode == STILL_ACTIVE)
            dwRetCode = 0;
      //--------------------------------------------------------------------------
      //      Close handles.
      //--------------------------------------------------------------------------
      Success = CloseHandle(ProcessInfo.hThread);
      if ( !Success )
            {
            //ShowError(_T("Error closing thread handle."));
            }

      Success = CloseHandle(ProcessInfo.hProcess);
      if ( !Success )
            {
//            ShowError(_T("Error closing process handle."));
            }


      Success = CloseHandle(PipeReadHandle);
      if ( !Success )
            {
            //ShowError(_T("Error closing pipe read handle."));
            }
      
      Success = CloseHandle(PipeWriteHandle);
      if ( !Success )
            {
//            ShowError(_T("Error closing pipe write handle."));
            }
      return dwRetCode;
}

where
AppendText add string to the GUI control
and
void PeekAndPump()
{
    MSG Msg;
    while (::PeekMessage(&Msg, NULL, 0, 0, PM_NOREMOVE))
            {
        AfxGetApp()->PumpMessage();
         }
}


0
 
LVL 3

Expert Comment

by:cypherljk
ID: 6321995
listening...

0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9490555
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by : migel

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Roshan Davis
EE Cleanup Volunteer
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

705 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