Author Topic: I Try Call Win32 API WriteConsoleOutputA Use NASM  (Read 12628 times)

Offline michael_chan

  • New Member
  • Posts: 1
I Try Call Win32 API WriteConsoleOutputA Use NASM
« on: August 23, 2016, 08:08:30 AM »
i try call WriteConsoleOutputA use NASM

but the error message always display

87 (0x57)

The parameter is incorrect.

i don't know how to solve this problem

Code: [Select]
STD_OUTPUT_HANDLE   EQU -11
NULL                EQU 0

GLOBAL GobleyGook
EXTERN ExitProcess, GetLastError, GetStdHandle, WriteConsoleA, WriteConsoleOutputA, FormatMessageA

SECTION .data

STRUC lpBuffer
    .Char      : RESW 1
    .Attributes: RESD 1
ENDSTRUC

_lpBuffer ISTRUC lpBuffer
    at lpBuffer.Char, DW 'A'
    at lpBuffer.Attributes, DD 1H
IEND

STRUC dwBufferSize
    .X: RESW  1
    .Y: RESW  1
ENDSTRUC

_dwBufferSize ISTRUC dwBufferSize
    at dwBufferSize.X, DW 10
    at dwBufferSize.Y, DW 10
IEND

STRUC dwBufferCoord
    .X: RESW 1
    .Y: RESW 1
ENDSTRUC

_dwBufferCoord ISTRUC dwBufferCoord
    at dwBufferCoord.X, DW 0
    at dwBufferCoord.Y, DW 0
IEND

STRUC smallRect
    .Left  : RESW 1
    .Top   : RESW 1
    .Right : RESW 1
    .Bottom: RESW 1
ENDSTRUC

_smallRect ISTRUC smallRect
IEND

SECTION .bss
dummy   RESD 1
err     RESD 1
err2    RESD 1

SECTION .text
GobleyGook:
    PUSH    STD_OUTPUT_HANDLE
    CALL    GetStdHandle

    PUSH    _smallRect
    PUSH    _dwBufferCoord
    PUSH    _dwBufferSize
    PUSH    _lpBuffer
    PUSH    EAX
    CALL    WriteConsoleOutputA

    CALL    GetLastError

    PUSH    NULL
    PUSH    99
    PUSH    err
    PUSH    NULL
    PUSH    EAX
    PUSH    NULL
    PUSH    1000H
    CALL    FormatMessageA

    PUSH    STD_OUTPUT_HANDLE
    CALL    GetStdHandle

    push    NULL
    push    dummy
    push    32
    push    err
    push    EAX
    call    WriteConsoleA

    PUSH    NULL
    CALL    ExitProcess

Link & compiler

Code: [Select]
nasm -f win32 print.asm
golink.exe /console /entry GobleyGook print.obj kernel32.dll

on the windows 8.1 64bit

The API document

https://msdn.microsoft.com/zh-tw/library/windows/desktop/ms687404(v=vs.85).aspx

please help me....

Offline durelin

  • Jr. Member
  • *
  • Posts: 3
Re: I Try Call Win32 API WriteConsoleOutputA Use NASM
« Reply #1 on: October 25, 2016, 04:46:15 PM »
hello
replace
 PUSH    _dwBufferCoord
 PUSH   _dwBufferSize
for
 PUSH    dword [_dwBufferCoord]
 PUSH    dword [_dwBufferSize]

ok ?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: I Try Call Win32 API WriteConsoleOutputA Use NASM
« Reply #2 on: November 07, 2016, 07:24:01 PM »
Well... "ok"? Does it work? You Windows users have gotta help each other! I can't do it!

Best,
Frank


Offline Mich

  • Jr. Member
  • *
  • Posts: 8
Re: I Try Call Win32 API WriteConsoleOutputA Use NASM
« Reply #3 on: November 20, 2016, 12:47:01 AM »
durelin was correct.

compiles and links:
C:\>prnt
The parameter is incorrect.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: I Try Call Win32 API WriteConsoleOutputA Use NASM
« Reply #4 on: November 20, 2016, 02:01:21 AM »
Well it isn't clear to me which parameter is incorrect. I'm thinking that "smallrect.bottom" and "smallrect.right" should probably be nonzero. Say 10,10? But I dunno.

Best,
Frank


Offline durelin

  • Jr. Member
  • *
  • Posts: 3
Re: I Try Call Win32 API WriteConsoleOutputA Use NASM
« Reply #5 on: November 21, 2016, 01:39:26 PM »
Hello
Try this code (he is OK under windows 10) :

STD_OUTPUT_HANDLE   EQU -11
NULL                EQU 0
GENERIC_READ      equ   080000000h
GENERIC_WRITE      equ   040000000h
FILE_SHARE_READ  equ 1h
FILE_SHARE_WRITE  equ 2h
CONSOLE_TEXTMODE_BUFFER equ 0x00000001
INVALID_HANDLE_VALUE equ -1
GLOBAL Main
EXTERN ExitProcess, GetLastError, GetStdHandle, WriteConsoleA, WriteConsoleOutputA, FormatMessageA
SECTION .data

STRUC lpBuffer
    .Char      : RESW 1
    .Attributes: RESD 1
ENDSTRUC

_lpBuffer ISTRUC lpBuffer
    at lpBuffer.Char, Dw 'A'
    at lpBuffer.Attributes, DD 21H
IEND
_lpBuffer2 ISTRUC lpBuffer
    at lpBuffer.Char, DW 'B'
    at lpBuffer.Attributes, DD 21H
IEND

STRUC dwBufferSize
    .X: RESW  1
    .Y: RESW  1
ENDSTRUC

_dwBufferSize ISTRUC dwBufferSize
    at dwBufferSize.X, DW 2
    at dwBufferSize.Y, DW 20
IEND

STRUC dwBufferCoord
    .X: RESW 1
    .Y: RESW 1
ENDSTRUC

_dwBufferCoord ISTRUC dwBufferCoord
    at dwBufferCoord.X, DW 0
    at dwBufferCoord.Y, DW 0
IEND

STRUC smallRect
    .Left  : RESW 1
    .Top   : RESW 1
    .Right : RESW 1
    .Bottom: RESW 1
ENDSTRUC

_smallRect ISTRUC smallRect
     at smallRect.Left, DW 1
    at smallRect.Top, DW 1
    at smallRect.Right, DW  1
    at smallRect.Bottom, DW 1
IEND

SECTION .bss
hOutput resd 1
hScreen resd 1
dummy   RESD 1
err     RESD 1
err2    RESD 1
buffer  resb 200
SECTION .text
Main:
    PUSH    STD_OUTPUT_HANDLE
    CALL    GetStdHandle
 
    PUSH    _smallRect
    PUSH    dword [_dwBufferCoord]
    PUSH    dword [_dwBufferSize]
    PUSH    _lpBuffer
    PUSH    eax
    CALL    WriteConsoleOutputA

    CALL    GetLastError

    PUSH    NULL
    PUSH    99
    PUSH    err
    PUSH    NULL
    PUSH    EAX
    PUSH    NULL
    PUSH    1000H
    CALL    FormatMessageA

    PUSH    STD_OUTPUT_HANDLE
    CALL    GetStdHandle

    push    NULL
    push    dummy
    push    32
    push    err
    push    EAX
    call    WriteConsoleA

.fin:   
    PUSH    NULL
    CALL    ExitProcess

Offline Mich

  • Jr. Member
  • *
  • Posts: 8
Re: I Try Call Win32 API WriteConsoleOutputA Use NASM
« Reply #6 on: December 12, 2016, 01:18:50 AM »
compiling and running on Vista 32bit produces the following output:
C:\X-Asm\X_Template>prnrect2
The operation completed successf
C:\X-Asm\X_Template>

I somehow think this is not the output desired??

Changing the values for smallRect in the above example:
Code: [Select]
_smallRect ISTRUC smallRect
     at smallRect.Left, DW 0
    at smallRect.Top, DW 6
    at smallRect.Right, DW  10
    at smallRect.Bottom, DW 10
IEND

yields the following out put:
Code: [Select]
C:\X-Asm\X_Template>prnrect2
The operation completed successf
C:\X-Asm\X_Template>
A
!?

d
each of the symbols displayed has a different back ground, red,green and yellow. Not sure if this was the intended output? or just a coincidence?

Klod