Here, the first argument is the console’s output handle. This function is slightly more complicated than the previous ones. This is because of a technical issue with the way strings are passed and stored in C. lpBuffer must be passed by value, even though it is of the long pointer type. IMPORTANT NOTE: The Microsoft API Text Viewer declaration is incorrect. There are a few ways to print to the console, the most simplistic of them all being WriteConsole. To perform output to the console you must, of course, first have a handle to the output buffer. So far you have learned how to create a console process and how to get a reference to it but you haven’t learned anything terribly practical. Private Const STD_ERROR_HANDLE = -12& Writing to the Console The mnemonic constants have three possible values to pass are: Private Const STD_INPUT_HANDLE = -10& The argument nStdHandle determines which handle it returns. We retrieve a handle with a call to GetStdHandle. This is because each can be redirected, but that doesn’t matter to us. A console window has an input, output, and error handle. When you use an API call to interact with a console window, you’ll now have to provide one of its three handles–that’s right, three. When you use an API call to interact with a form window, you’ll almost always have to provide the form’s hWnd. Handles to the console window are just like handles to standard windows. With this done, one can already write a program that brings up a console box and closes it: Private Declare Function _īefore we can call any other console functions, we’ll need a handle to the console. We destroy the console process with the FreeConsole function. Since it is a process, it must always be destroyed when we are done with it, or before the application terminates. Think of all operations that we perform on it as “requests” (and these requests can be denied). This console window – this is important to remember – is a process controlled by the operating system. This will create a console window in which we can print and receive input. This is done by a call to the API function AllocConsole, whose prototype is as follows: Private Declare Function _ It’s not a resource of any kind, it’s a process that we request the operating system to allocate for us. Our application has no attachment to the console box. This means that all of our interfacing with the console will be done through calls to the Win32 API. Visual Basic, as was mentioned earlier, has no built-in support for console work of any kind. That one module is the only component that is needed in a console project, just like a windowless project. So, start a new Standard EXE project and remove the default form (Form1) from the project. This is because since there is no built-in support by VB for console work, we have to write all of the low-level stuff by hand. Most people think it is impossible to write a console-based application in VB because there isn’t a project for it in the New Project dialog but I’m going to show you how it can indeed be done.Ī console-based program in Visual Basic starts off with a windowless base. It doesn’t use up much memory and console applications tend to be smaller than windowed applications. This is all fine but what about the good old console? Sometimes it is the best way to provide a simple interface to a user. What a lot of people don’t know is that VB can also be used to write windowless applications, where the application has no standard user interface of it’s own. Visual Basic has always been there for those of us that have wanted to write windowed applications.
0 Comments
Leave a Reply. |