[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

cl.exe and VC++ IDE

Posted on 2001-07-26
3
Medium Priority
?
403 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 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

649 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