News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Capturing stdout

Started by TomRiddle, February 06, 2007, 06:05:39 PM

Previous topic - Next topic

TomRiddle

I think that topic should be good enough, here is what my question was.

I want to run a child process and capture it's stdout(the text in the console) and divert it into a RichEdit box, can anyone give me a leg up in the right direction?

Thanks

P.S. Just the API calls would be fine ::)

TNick

Hello TomRiddle!

I had somewhere an example using CreateFile - ReadFile, but I can't find it right now. Documentation for those two should help you...
QuoteHANDLE CreateFile(

    LPCTSTR  lpFileName,   // address of name of the file
    DWORD  dwDesiredAccess,   // access (read-write) mode
    DWORD  dwShareMode,   // share mode
    LPSECURITY_ATTRIBUTES  lpSecurityAttributes,   // address of security descriptor
    DWORD  dwCreationDistribution,   // how to create
    DWORD  dwFlagsAndAttributes,   // file attributes
    HANDLE  hTemplateFile    // handle of file with attributes to copy 
   );   
Parameters

lpFileName

Points to a null-terminated string that specifies the name of the file, pipe, communications resource, disk device, or console to create, open, or truncate.

Regards,
Nick

Biterider

Hi
I think your goal is to capture the output, lets say of the ML compiler. In that case, you should use "pipes" to capture it. The ExecuteCommand method of the OA32Tools project shows how to use it.

Regards,

Biterider

TomRiddle


Jibz

Here is some C code I've used in the past .. should give some idea about the handle- and pipe gymnastics involved :U.

BOOL execute_piped(char *cmdline, HWND hEdit)
{
    SECURITY_ATTRIBUTES sat;
    PROCESS_INFORMATION pinfo;
    STARTUPINFO sinfo;
    HANDLE hPipeRd, hPipeWr, hPipeRdDup;
    BOOL bRet;
    char buffer[1024];
    DWORD dwCount;

    /* create security attributes that inherit handles */
    sat.nLength              = sizeof(SECURITY_ATTRIBUTES);
    sat.lpSecurityDescriptor = NULL;
    sat.bInheritHandle       = TRUE;

    /* create pipe for stdout */
    bRet = CreatePipe(&hPipeRd, &hPipeWr, &sat, 0);

    if (!bRet)
    {
        MessageBox(0, "Unable to create pipe.", "Error", MB_ICONERROR + MB_OK);
        return FALSE;
    }

    /* create noninheritable handle and close original */
    bRet = DuplicateHandle(GetCurrentProcess(), hPipeRd, GetCurrentProcess(),
                           &hPipeRdDup, 0, FALSE, DUPLICATE_SAME_ACCESS);

    CloseHandle(hPipeRd);

    if (!bRet)
    {
        MessageBox(0, "Unable to duplicate handle.", "Error", MB_ICONERROR + MB_OK);
        return FALSE;
    }

    /* fill in startup info */
    sinfo.cb = sizeof(STARTUPINFO);
    GetStartupInfo(&sinfo);

    sinfo.hStdOutput  = hPipeWr;
    sinfo.hStdError   = hPipeWr;
    sinfo.dwFlags     = STARTF_USESHOWWINDOW + STARTF_USESTDHANDLES;
    sinfo.wShowWindow = SW_HIDE;

    /* create process */
    bRet = CreateProcess(NULL, cmdline, NULL, NULL, TRUE, 0, NULL, NULL,
                         &sinfo, &pinfo);

    if (!bRet)
    {
        MessageBox(0, "Unable to create process.", "Error",
                   MB_ICONERROR + MB_OK);
        return FALSE;
    }

    /* close write end of pipe */
    CloseHandle(hPipeWr);

    /* read from pipe */
    while (ReadFile(hPipeRdDup, buffer, sizeof(buffer) - 1, &dwCount, NULL) && dwCount)
    {
        buffer[dwCount] = 0;
        SendMessage(hEdit, EM_SETSEL, -1, 0);
        SendMessage(hEdit, EM_REPLACESEL, FALSE, (DWORD)buffer);
    }

    /* wait for process to terminate */
    WaitForSingleObject(pinfo.hProcess, INFINITE);

    /* close handles */
    CloseHandle(pinfo.hProcess);
    CloseHandle(pinfo.hThread);

    return TRUE;
}

TomRiddle

It's funny, I could never write in Pascal/C, but I always manage to read it!  :dazzled:

Thanks Jibz


TomRiddle

Need a vote, MDI(Multiple Document Interface) or TDI(Tabbed Document Interface) for an IDE?

If you have an reasons why one would be better, I'd love to hear them too.

Ohh, and I hope no one minds me re-using this thread.  :lol

P1

Quote from: TomRiddle on February 07, 2007, 02:55:21 PMNeed a vote, MDI(Multiple Document Interface) or TDI(Tabbed Document Interface) for an IDE?
Who said you had to limit yourself?  Options, Options. 

Regards,  P1   :8)

TomRiddle

Quote from: P1 on February 07, 2007, 03:08:02 PM
Who said you had to limit yourself?  Options, Options. 

Regards,  P1   :8)

I am only one man, just 1 bag of mostly water, Options = Time

Shantanu Gadgil

Hi,
Here's some quickly put together UI based thing I did some time back for something similar.

HTH,
Shantanu

[attachment deleted by admin]
To ret is human, to jmp divine!

Rockoon

MDI vs TDI

use TDI if you can support more than 1 tab visible (split window), otherwise MDI
When C++ compilers can be coerced to emit rcl and rcr, I *might* consider using one.

TomRiddle

Thanks Shantanu

On a side note, I like the idea of Nasm(Portability), but it's syntax is horrid.

Shantanu Gadgil

QuoteOn a side note, I like the idea of Nasm(Portability), but it's syntax is horrid.
Eh?  :(

On that note, I just remembered...I might have written the above code for POASM (POASM.EXE to be installed in C:\POASM\BIN\ and so on)  :dazzled: :eek !!!

The code should work with MASM...might need a few modifcations

Cheers,
Shantanu
To ret is human, to jmp divine!

TomRiddle

Ok, let me further explain my opinion on "horrid", so as no one gets the wrong impression.

I am pedantic, and I mean uber pedantic. (See Below)

I probably hit Modify on my posts at least 2 times after they actually get posted, just to fix small semantical errors.

As far as my horrid comment went, its probably just fear of change. :dazzled:

My pedantically absurd self is probably gonna stop my project from releasing another 3 months, but I have problems releasing stuff with imperfections.   

On yet one more side-note, does anyone see errors in the below code? :red
; "BSMacro" "/Cpu/8086_Exclusive/" "0.0.0.0"
; "AddTag" "MultiplyBy*"
MultiplyBy4 Macro V
  Shl V,1
  Shl V,1
EndM
MultiplyBy8 Macro V
  Shl V,1
  Shl V,1
  Shl V,1
EndM
MultiplyBy16 Macro V
  Shl V,1
  Shl V,1
  Shl V,1
  Shl V,1
EndM
MultiplyBy32 Macro V
  Shl V,1
  Shl V,1
  Shl V,1
  Shl V,1
  Shl V,1
EndM
MultiplyBy64 Macro V
  Shl V,1
  Shl V,1
  Shl V,1
  Shl V,1
  Shl V,1
  Shl V,1
EndM
MultiplyBy128 Macro V
  Shl V,1
  Shl V,1
  Shl V,1
  Shl V,1
  Shl V,1
  Shl V,1
  Shl V,1
EndM
MultiplyBy256 Macro V
  Shl V,1
  Shl V,1
  Shl V,1
  Shl V,1
  Shl V,1
  Shl V,1
  Shl V,1
  Shl V,1
EndM
; "AddTag" "DivideBy*"
DivideBy4 Macro V
  Shr V,1
  Shr V,1
EndM
DivideBy8 Macro V
  Shr V,1
  Shr V,1
  Shr V,1
EndM
DivideBy16 Macro V
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
EndM
DivideBy32 Macro V
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
EndM
DivideBy64 Macro V
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
EndM
DivideBy128 Macro V
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
EndM
DivideBy256 Macro V
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
EndM
; "AddTag" "Swap*"
SwapNibble Macro V
  Rol V,1
  Rol V,1
  Rol V,1
  Rol V,1
EndM
SwapNibble8 Macro V
  Rol V,1
  Rol V,1
  Rol V,1
  Rol V,1
EndM
SwapByte Macro V
  Rol V,1
  Rol V,1
  Rol V,1
  Rol V,1
  Rol V,1
  Rol V,1
  Rol V,1
  Rol V,1
EndM
SwapByte16 Macro V
  Rol V,1
  Rol V,1
  Rol V,1
  Rol V,1
  Rol V,1
  Rol V,1
  Rol V,1
  Rol V,1
EndM
; "AddTag" "Set*"
SetNibble1 Macro V
  And V,0F0h
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
EndM
SetNibble2 Macro V
  And V,0F00h
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
EndM
SetNibble3 Macro V
  And V,0F000h
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
EndM
; "AddTag" "Hi*"
HiByte16 Macro V
  Shl V,1
  Shl V,1
  Shl V,1
  Shl V,1
  Shl V,1
  Shl V,1
  Shl V,1
  Shl V,1
EndM
HiNibble8 Macro V
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
EndM
; "AddTag" "Shift*"
ShiftRight2 Macro V
  Shr V,1
  Shr V,1
EndM
ShiftRight3 Macro V
  Shr V,1
  Shr V,1
  Shr V,1
EndM
ShiftRight4 Macro V
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
EndM
ShiftRight5 Macro V
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
EndM
ShiftRight6 Macro V
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
EndM
ShiftRight7 Macro V
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
EndM
ShiftRight8 Macro V
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
  Shr V,1
EndM
; "AddTag" "ShiftLeft*"
ShiftLeft2 Macro V
  Shl V,1
  Shl V,1
EndM
ShiftLeft3 Macro V
  Shl V,1
  Shl V,1
  Shl V,1
EndM
ShiftLeft4 Macro V
  Shl V,1
  Shl V,1
  Shl V,1
  Shl V,1
EndM
ShiftLeft5 Macro V
  Shl V,1
  Shl V,1
  Shl V,1
  Shl V,1
  Shl V,1
EndM
ShiftLeft6 Macro V
  Shl V,1
  Shl V,1
  Shl V,1
  Shl V,1
  Shl V,1
  Shl V,1
EndM
ShiftLeft7 Macro V
  Shl V,1
  Shl V,1
  Shl V,1
  Shl V,1
  Shl V,1
  Shl V,1
  Shl V,1
EndM
ShiftLeft8 Macro V
  Shl V,1
  Shl V,1
  Shl V,1
  Shl V,1
  Shl V,1
  Shl V,1
  Shl V,1
  Shl V,1
EndM


Edit: I saw an error and hit Modify...twice :\