Win32 console output

Posted on 2005-03-13
Medium Priority
Last Modified: 2008-02-01
I'm trying to write a simple console output app under Windows XP and having no luck. The WriteConsole procedure wants a handle to the console screen buffer but I can't seem to get one. CreateConsoleScreenBuffer just gives an invalid handle error when I run it (with parameters GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL) so I tried GetStdHandle with the STD_OUTPUT_HANDLE parameter, which doesn't return an error but doesn't give me a handle either. The following is the section of code (compiled with MASM 6.14 and linked with MS incremental linker 5.12). Oh and it's the first thing to run.

; get stdout handle
call GetStdHandle
; eax is now 0

Anybody know why this is happening/how to do it properly?
Question by:boycy
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
  • 2

Author Comment

ID: 13533715
Ok, bit more info. Reading through the documentation for GetStdHandle again (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/getstdhandle.asp) I think the problem is that NULL is being returned because my program doesn't have any associated standard handles. So...how do I get my program to have standard handles? I remember reading that under windows, a bit in the PE header states whether the app is console or windowed...but how can I change this from the assembly source?

Author Comment

ID: 13533804
Right, managed to answer it myself - finally!
For those interested, the first thing that 'worked' was calling AllocConsole before attempting to retrieve the stdout handle, the only problem was that opened up a new command prompt and closed it as soon as ExitProcess was called.
The bit that did it for me was passing /subsystem:console instead of /subsystem:windows to the linker :D

Accepted Solution

ee_ai_construct earned 0 total points
ID: 13537796
Question answered by asker or dialog deemed valuable.
Closed, 100 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Admin

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The well known Cerber ransomware continues to spread this summer through spear phishing email campaigns targeting enterprises. Learn how it easily bypasses traditional defenses - and what you can do to protect your data.
Hey fellow admins! This time, I have a little fairy tale for you. As many tales do, it starts boring and then gets pretty gory. I hope you like it. TL;DR: It is about an important security matter, you should read it if you run or administer Windows …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month12 days, 22 hours left to enroll

777 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