I create the console in a windowed program:

console label dword
contitlelen dword sizeof contitle
contitle db 128 dup (0)
conhandle dword 0
constdinput dword STD_INPUT_HANDLE
constdoutput dword STD_OUTPUT_HANDLE
misc dword 0

coninit dd offset @coninit
pop esi

call AllocConsole
call GetConsoleWindow
mov [conhandle],eax

push conhandle
call SetStdHandle

push constdinput
call GetStdHandle

push conhandle
call SetStdHandle

push constdoutput
call GetStdHandle;redundant calls to set and get. I get the same value I put in(for testing)

push contitlelen
lea eax, contitle
push eax
call GetConsoleTitle;this works fine, everything below does not work:

invoke SetConsoleMode,conhandle,ENABLE_LINE_INPUT or \

invoke ReadFile, conhandle, addr contitle, 32, addr misc, NULL
invoke WriteFile, conhandle, addr contitle, 32, addr misc, NULL

jmp esi

I looked at STDIN and STDOUT in MASM32. My first question is which way is in a which way is out? Is my program writeing to the console, or is the console reading from my program?

StdOut proc lpszText:DWORD;MASM STDOUT


invoke GetStdHandle,STD_OUTPUT_HANDLE
mov hOutPut, eax

invoke StrLen,lpszText
mov sl, eax

invoke WriteFile,hOutPut,lpszText,sl,ADDR bWritten,NULL

Everytime I call GetStdHandle without first using SetStdHandle, I get a return of 3 or 7. I'm pretty sure that means an error.

I'm using GetConsoleWindow to get the value hOutPut(conhandle). I've got the feeling I'm doing it wrong... Using error codes from GetStdHanlde is probably not right either....
Yes, I'm trying both Read and WriteFile, because I'm trying to get something to work. If GetStdHandle would not fail, I would probably have no problem. Any ideas why it would fail?

Posted on 2002-07-12 11:06:39 by ThoughtCriminal
I don't know why GetStdHandle would fail. But here's a sample on program that would do just that http://www.asmcommunity.net/board/index.php?topic=5919&highlight=password+console

Did you link it with /SUBSYSTEM:CONSOLE ???
Posted on 2002-07-12 11:29:04 by stryker
Did you link it with /SUBSYSTEM:CONSOLE ???

No, unless I can link with two subsystems. I am creating a console in my windowed program.

I have figured out this much:

Dont use SetStdHandle, it just redirects the handle.

Posted on 2002-07-12 11:33:51 by ThoughtCriminal
Ok! lemme get this straight. Your creating a console window under a win32 program with standard windows using CreateWindowEx...If this is so, GetStdHandle would always fail.

If your trying to mimic the command line on a windowed program, then you have to create an edit/richedit control then use the standard WM_SETTEXT, SetDlgItemText, to print a text.

If your just doing a console(meaning, purely using the command line), then by all means use the GetStdHandle and linking with /SUBSYSTEM:CONSOLE. It should work.

Did I undertstood your question??
Posted on 2002-07-12 11:48:12 by stryker
Did I undertstood your question??

Kinda, I want a GUI window and a console window in one app.

I am trying to use GetCurrentProcessId with AttachConsole, but AttachConsole is not in windows.inc:(

edit: I mean kernel32.inc
Posted on 2002-07-12 11:59:03 by ThoughtCriminal
Oh I see, you want to attach a console on a standard window. Honestly, I haven't tried this yet. Are you in some sort of debugging stuff?

Try dynamically loading the function. LoadLibrary/GetProcAddress to call AttachConsole.
Posted on 2002-07-12 12:07:43 by stryker
Are you in some sort of debugging stuff?

Thats the basic idea. Or whatever stupid stuff I want to shoot over.

I updated kernel32.inc, but kernel32.lib does not have AttachConsole. Accoring to MSDN, it should be there in win2k:

Posted on 2002-07-12 12:12:39 by ThoughtCriminal
Create a new lib if you want. Use implib to create a lib from kernel32.dll

LoadLibrary/GetProcAddress should be used since MSDN said on 95/98 it's not supported. Just to be safe.
Posted on 2002-07-12 12:20:15 by stryker
Seems I dont have implib, and it seems AttachConsole does not exist. GetProcAddress returns null. If is does not exist in 98, I'm not sure how LoadLibaray will help. Rhight now, it seems it does not exist in w2k:(

Edit: just dumped kernel32.dll form sysroot. Not there:( :( :(
Posted on 2002-07-12 12:36:57 by ThoughtCriminal
The MSDN link does not say it exists in Win2k.
XP is later than Win2k.
Posted on 2002-07-12 12:58:05 by tenkey
But it is in NET server, and I have NET, hmmm

Just found it in the kernel32.lib in the NET platform SDK directory!!! I can assemble now!!!

I will post the lib to the board, only if it is okayed first.
Posted on 2002-07-12 13:02:45 by ThoughtCriminal
Unofficially, .NET Server can be called XP Server. I'm not sure it has come out of beta yet. It's not the same as the .NET add-on.
Posted on 2002-07-12 13:10:45 by tenkey
The value returned by GetStdHandle is not the error code, but a handle. If an error occurs, GetStdHandle returns -1.
STD_OUTPUT_HANDLE will give the handle for output, and STD_INPUT_HANDLE gives the handle for input.
Call AllocConsole, and then call GetStdHandle to get the output handle.
Posted on 2002-07-12 18:03:55 by Sephiroth3

The problem is that usually handles are not 3, or 7. Usually it is some full 32bit number, an address of something, or some such.
Posted on 2002-07-12 21:48:50 by ThoughtCriminal
What Sephiroth3 tells is right.

After calling AllocConsole, this new console is "attached" to your process. No explicit "AttachConsole" function call is needed (and doesnt exist either). And AllocConsole will set standard handles which can be retrieved with GetStdHandle function.
Posted on 2002-07-13 00:56:36 by japheth

call AllocConsole

invoke GetStdHandle,STD_INPUT_HANDLE
mov coninputh, eax;** always returns 3

invoke GetLastError;**returns 578

lea eax, contitle;get window title
push eax
call GetConsoleTitle

lea edx, offset contitle
invoke WriteFile, coninputh, edx, 32, addr misc, NULL

invoke GetLastError;**always returns 6

GetLastError codes:

6 The handle is invalid. ERROR_INVALID_HANDLE
There is no error 578 listed.

GetStdHandle returns non-zero, if the function is successful. So 3 may be a handle, but according to WriteFile, it is invalid.

Unless someone has an idea, I think I'm gonna have to write this in C and debug thru it to see what type of values I should be getting.
Posted on 2002-07-13 04:12:03 by ThoughtCriminal
The last time I checked the values of the standard file handles, it was under Win95:

0 - standard input file handle
1 - standard output file handle
2 - standard error file handle

Looks a lot like UNIX and DOS.
Posted on 2002-07-14 03:15:27 by tenkey
I finally figured it out. Had trouble figuring out who is the writer, and who is the writee.

GUI writes to Console or Console Write to GUI(made my head spin a little).

I usually associate low return values as needing a call to GetLastError to figure whats up.

Found some MSDN info about handles:
The value of the handles returned by GetStdHandle are not 0, 1, and 2, so the standard predefined stream constants in Stdio.h (STDIN, STDOUT, and STDERR) cannot be used in functions that require a console handle.

What this means to me, is that a low number like 3, is probaly what I was looking for. Thanks for the help everyone.
Posted on 2002-07-14 03:36:34 by ThoughtCriminal