Solved

cl.exe and VC++ IDE

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
mixString challenge 36 137
C++ BOOL WINAPI ReadFile fails on windows 10 when reading from USB cable 9 547
How to know only "File created" with EventLog 3 35
sum67 challenge 35 128
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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