• Welcome to Valhalla Legends Archive.
 

[MASM32] Console API's

Started by Joe[x86], January 08, 2006, 04:22 PM

Previous topic - Next topic

Joe[x86]

I'm trying to write to a console using MASM32, but theres a small problem. When I call AllocConsole, it creates a new console, not uses the one I'm running in. Then, when I call FreeConsole, that console is destroyed, and nobody can read my pretty output! I use Sleep(1000) to display it, so I know its printing to it, but I don't know how to use the console that's already being shown.

.486
.model flat, stdcall
option casemap :none

include \masm32\include\windows.inc

include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib

include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib


.data
msgHello       db "Hello World!", 0 ;Length: 13

.code
start:
    invoke AllocConsole
    ; Allocate a console

    invoke GetStdHandle, -11
    ; Get STD Handle
    ; We assume its returned in EAX. Warrior said so!
   
    invoke WriteConsoleA, eax, ADDR msgHello, 13, ecx, 0
    ; eax = STD Output handle
    ; ecx = Number of chars written

    invoke Sleep, 1000
    ; Sleep 1000 seconds
   
    invoke FreeConsole
    ; Destroy the console
   
    invoke ExitProcess, 0
    ; Exit
end start
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

Kp

This is most likely because your application is subsystem:windows not subsystem:console.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

MyndFyre

Do you even need to AllocConsole when you're in subsystem:console?
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

rabbit

Only if you want to spawn a new console window.
Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.

Yoni

Quote from: Kp on January 08, 2006, 07:46 PM
This is most likely because your application is subsystem:windows not subsystem:console.
Sounds more like subsystem:console and not subsystem:windows :P

Quote from: Joe on January 08, 2006, 04:22 PM
but I don't know how to use the console that's already being shown.
Don't call AllocConsole. GetStdHandle(STD_OUTPUT_HANDLE), which you are calling anyway, should return the stdout handle for the console that exists already.

Kp

Quote from: Yoni on January 10, 2006, 12:27 AM
Quote from: Kp on January 08, 2006, 07:46 PM
This is most likely because your application is subsystem:windows not subsystem:console.
Sounds more like subsystem:console and not subsystem:windows :P

Quote from: Joe on January 08, 2006, 04:22 PM
but I don't know how to use the console that's already being shown.
Don't call AllocConsole. GetStdHandle(STD_OUTPUT_HANDLE), which you are calling anyway, should return the stdout handle for the console that exists already.

Possibly, but if he's in subsystem:windows he'd need to call AllocConsole for one to show up at all.  I presumed that's why he was calling AllocConsole, and then he just tacked on a FreeConsole because the docs told him to.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

rabbit

Not using FreeConsole after AllocConsole?  That's like declaring a large array and only using a tiny bit of it, and then never destroying the extra.
Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.

Joe[x86]

#7
Quote from: rabbit on January 11, 2006, 06:32 PM
Not using FreeConsole after AllocConsole?  That's like declaring a large array and only using a tiny bit of it, and then never destroying the extra.

What happens a lot? =p

EDIT -
Rhar. Not calling AllocConsole didn't help any.
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

rabbit

I just meant it's a waste of memory, not that it will change anything.
Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.

Kp

Quote from: Joe on January 11, 2006, 11:08 PM
Quote from: rabbit on January 11, 2006, 06:32 PM
Not using FreeConsole after AllocConsole?  That's like declaring a large array and only using a tiny bit of it, and then never destroying the extra.

What happens a lot? =p

EDIT -
Rhar. Not calling AllocConsole didn't help any.

Have you checked yet what subsystem your program is running in?
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

Joe[x86]

Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

Skywing

Quote from: Joe on January 13, 2006, 05:03 PM
How?

You're telling the linker which subsystem to set in your linker parameters (or lack thereof).  Check for a /subsystem: line if you are using Microsoft link.exe.  You could also use dumpbin /headers exename if you are using Microsoft link.exe.

Kp

Quote from: Skywing on January 13, 2006, 06:23 PM
Quote from: Joe on January 13, 2006, 05:03 PM
How?

You're telling the linker which subsystem to set in your linker parameters (or lack thereof).  Check for a /subsystem: line if you are using Microsoft link.exe.  You could also use dumpbin /headers exename if you are using Microsoft link.exe.

Actually, that should work even if he linked it with the GNU toolchain. :)

Joe: if you're using GNU tools (or feel like using them in preference to Microsoft tools), use objdump -p exename to view the headers.  Look for /^Subsystem\s\+/.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

Mephisto

I love the absolute contrast of Windows & Linux specialists between Kp/Skywing.  :)  Such gifted programmers.

Skywing

Quote from: Kp on January 13, 2006, 06:40 PM
Quote from: Skywing on January 13, 2006, 06:23 PM
Quote from: Joe on January 13, 2006, 05:03 PM
How?

You're telling the linker which subsystem to set in your linker parameters (or lack thereof).  Check for a /subsystem: line if you are using Microsoft link.exe.  You could also use dumpbin /headers exename if you are using Microsoft link.exe.

Actually, that should work even if he linked it with the GNU toolchain. :)
Yes, though since dumpbin is a thin wrapper around link, it would make sense that if you had dumpbin, you would probably be using link.exe instead of the GNU toolchain (albeit not necessarily).