Solved

cl.exe and VC++ IDE

Posted on 2001-07-26
3
383 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
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to determine file rename from EventLog ? 2 36
centeredAverage challenge 8 124
has22 challenge 11 72
Making an alias 7 54
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

747 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

11 Experts available now in Live!

Get 1:1 Help Now