Author Topic: Redirecting cmd output  (Read 6394 times)

Don57

  • Guest
Redirecting cmd output
« on: October 12, 2012, 05:00:07 AM »
Trying to redirect cmd screen to a file, but i am gettind Invalid_User_Buffer in WriteFile. After more reading at Microsoft I'm not sure if the calls redirect to screen output to a file, or they redirect the file to the screen.

; create file

     invoke CreateFile, ADDR lpszSave_File_Name,\                     
                        GENERIC_READ or GENERIC_WRITE,\                   
                        FILE_SHARE_READ or FILE_SHARE_WRITE,\
                        NULL, CREATE_ALWAYS,\
                        FILE_ATTRIBUTE_NORMAL, NULL
 

     mov hSaveCreateFile ,eax         


; reset StdOut to file


      invoke SetStdHandle, STD_OUTPUT_HANDLE, hSaveCreateFile

      mov hConOut, eax


; set for console out write


      invoke WriteFile, hSaveCreateFile, hConOut, FileBufferSize, ADDR dwBytesRead, 0


Vortex

  • Member
  • *****
  • Posts: 1987
Re: Redirecting cmd output
« Reply #1 on: October 12, 2012, 05:04:54 AM »
Not sure but this article might help you :

How to spawn console processes with redirected standard handles

dedndave

  • Member
  • *****
  • Posts: 8823
  • Still using Abacus 2.0
    • DednDave
Re: Redirecting cmd output
« Reply #2 on: October 12, 2012, 05:13:37 AM »
Code: [Select]
      invoke WriteFile, hSaveCreateFile, hConOut, FileBufferSize, ADDR dwBytesRead, 0
that doesn't make any sense to me   :P
it seems like the first parm should be the newly redirected handle (hConOut)
the second parm is a pointer to a buffer

i can't say that i have ever played with this, but....
in addition to the link that Erol gave you...
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682499%28v=vs.85%29.aspx

also, in the old forum, Mike played with pipes a bit (slugsnack i think was his ID)

of course.....
you are creating this process
so, you could do it on the command line and let CMD.EXE take care of it   :t

Don57

  • Guest
Re: Redirecting cmd output
« Reply #3 on: October 12, 2012, 05:27:51 AM »
I tried in the CreateProcess lpCommandLine witth escape sequences but just couldn't get it to work kept on getting a macro nesting error so rather than loose my mind I thought that I would try another way

               .if !rv(CreateProcess,NULL,"c:\\Windows\\System32\\wevtutil.exe el /r test.text",0,0,0,CREATE_NEW_CONSOLE or CREATE_NEW_PROCESS_GROUP,0,0, ADDR sui,ADDR pi)
   
                .endif

I also tried qWords line verbatum and couldn't get it to work. I do very low level chip programming and don't use API's or Macros. The API's give me some problems, but my understanding of the Macros is almost nil

dedndave

  • Member
  • *****
  • Posts: 8823
  • Still using Abacus 2.0
    • DednDave
Re: Redirecting cmd output
« Reply #4 on: October 12, 2012, 05:32:15 AM »
try this...
"c:\\Windows\\System32\\wevtutil.exe el >test.txt"

Don57

  • Guest
Re: Redirecting cmd output
« Reply #5 on: October 12, 2012, 05:41:14 AM »
That generates the following errors

C:\Users\Mirage\Work\Scrub Logs\LogCleaner.asm(377) : error A2046: missing single or double quotation mark in string
 rv(2): Macro Called From
  C:\Users\Mirage\Work\Scrub Logs\LogCleaner.asm(377): Main Line Code
C:\Users\Mirage\Work\Scrub Logs\LogCleaner.asm(377) : fatal error A1008: unmatched macro nesting
 rv(4): Macro Called From
  C:\Users\Mirage\Work\Scrub Logs\LogCleaner.asm(377): Main Line Code

It's enough to drive you mad

dedndave

  • Member
  • *****
  • Posts: 8823
  • Still using Abacus 2.0
    • DednDave
Re: Redirecting cmd output
« Reply #6 on: October 12, 2012, 05:48:49 AM »
 :biggrin:

you'll just have to break it up...
"c:\\Windows\\System32\\wevtutil.exe el ",62,"test.txt"

not sure how well the rv macro will handle that
i never use it - i just make a string in the .DATA section and pass the pointer

sorry - i forgot that character has meaning in ASM

Don57

  • Guest
Re: Redirecting cmd output
« Reply #7 on: October 12, 2012, 06:31:57 AM »
Thanks for the help. The commas before and after the ASCII are inadmissable and generate assembler errors. If I put the command line in a .DATA declaration it assembles alright, but generates an command not recognized in the command window. I tried both method using the Macro and with a simple invoke CreateProcess. In an earlier post someone mentioned escape squences, which I use in PHP but I can find no reference list to MASM32 escape sequences.

dedndave

  • Member
  • *****
  • Posts: 8823
  • Still using Abacus 2.0
    • DednDave
Re: Redirecting cmd output
« Reply #8 on: October 12, 2012, 06:51:10 AM »
a little google and.....
http://blogs.msdn.com/b/oldnewthing/archive/2006/05/16/598893.aspx

ahhh, yes - of course   ;)
the CMD.EXE program is the one that chews on redirection - not the utility

you won't need a path for CMD.EXE - but you might for the utility to run under it
in this case, the utility is also in system32 (in PATH), so....

Code: [Select]
szCmdLine db "cmd.exe /C wevtutil.exe el ",62,"test.txt",0

Don57

  • Guest
Re: Redirecting cmd output
« Reply #9 on: October 12, 2012, 07:04:03 AM »
That's it Thank You very much. I was going a little crazy. :greenclp:

jj2007

  • Member
  • *****
  • Posts: 9632
  • Assembler is fun ;-)
    • MasmBasic
Re: Redirecting cmd output
« Reply #10 on: October 12, 2012, 07:40:09 AM »
No double backslashes in Masm32 - that is a C disease. Test it below.

include \masm32\include\masm32rt.inc

.code
start:
   mov eax, rv(StdOut, "C:\\Windows\\C_sucks\\Whatever")
   inkey chr$(10)
   exit

end start

qWord

  • Member
  • *****
  • Posts: 1473
  • The base type of a type is the type itself
    • SmplMath macros
Re: Redirecting cmd output
« Reply #11 on: October 12, 2012, 08:12:26 AM »
Don57,
rv and rvc differs in their function: only rvc supports escape sequences. Also, as said here, take a look in the documentation (hlhelp.chm) - there list with all usable escape sequences.
MREAL macros - when you need floating point arithmetic while assembling!

mineiro

  • Guest
Re: Redirecting cmd output
« Reply #12 on: October 12, 2012, 09:30:59 AM »
Have found a link that deals with pipe, the example redirects input of edit to console, and output from console to edit control. Coded in tasm with source code, but can be easy converted to masm dialect.

http://www.ols-lab.com/devcorner/tasm32/misc/