Solved

cl.exe and VC++ IDE

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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
Add content to output file 4 67
Separate files macro - Errors on larger data set 18 73
Modbus - whats the maximum I can store in one register? 4 80
firstChar challenge 13 120
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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 …
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

840 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