ElektroStudios ElektroStudios - 1 month ago 14
Vb.net Question

About AttachConsole/Freeconsole API function

When I attach the parent CMD to my app, after releasing the attached CMD using Freeconsole, there is an empty line in CMD where I can write... is like an "@Echo OFF", when I press "ENTER" returns the echo (@Echo ON).

enter image description here

this is a VB code to give an example:

Private Sub Parse_Arguments()
AttachConsole(-1)
For I As Integer = 0 To My.Application.CommandLineArgs.Count - 1
If My.Application.CommandLineArgs.Item(I) = "/?" Then
Dim Logo As String = <a><![CDATA[Blah blah blah...]]></a>.Value
Console.WriteLine(Logo)
FreeConsole()
End
End If
Next
end sub


I hope do you know what I'm trying to say, I need the CMD to be fully verbose, I mean I want the CMD to print the last line "C:\Visual Studio Projects\Aero Switcher\Aero Switch\bin\Debug>" when releasing the attached CMD (when my program finishes the execution), and not to printing a empty line without "echo".

Another example:

This is what I Want:

C:\>Process.exe
C:\>Text wrote from the process and process finished
c:\>Verbose CMD line, "c:\>" is shown, @Echo is ON, all good!.


This is what I get:

C:\>Process.exe
C:\>Text wrote from the process and process finished
Damn empty line where I can write commands, not verbose line, @Echo is OFF.
C:\>Verbose CMD line.

Answer

An old question, but one I was struggling with this myself, so I thought I would post my findings...

After spending a lot of time on this, I came to the conclusion that I was looking at it completely wrong.

The Current Directory is listed immediately after the call to your executable - your first Console.WriteLine() will be on the same line as this.

After the call to FreeConsole() we are back at the console. The fact that the line is blank is almost irrelevant - the CMD prompt did show the current directory, but it appears before all of your application output. If you want to make it look like the user is back at a "normal" console then simply output the current directory before the call to FreeConsole():

Console.Write(FileIO.FileSystem.CurrentDirectory & ">")

This does feel like a hack, but with the way attaching to a console process works, it's probably all you can do.

Comments