how to use things in the <windows.h> header file? (C++)

Lumia925

Estimable
Oct 16, 2014
68
0
4,590
3
I want more control over how text is displayed on the console.
Google search recommends using the <windows.h> header in MS Windows platform, and avoiding system() calls.
But MSDN help is brutal. I have no idea what they're talking about!

Here's an example:
SetConsoleTextAttribute function Sets the attributes of characters written to the console screen buffer by the WriteFile or WriteConsole function, or echoed by the ReadFile or ReadConsole function. This function affects text written after the function call.


BOOL WINAPI SetConsoleTextAttribute(
_In_ HANDLE hConsoleOutput,
_In_ WORD wAttributes
);



hConsoleOutput [in]

A handle to the console screen buffer. The handle must have the GENERIC_READ access right. For more information, see Console Buffer Security and Access Rights.
wAttributes [in]

The character attributes.

What does this even mean? What is "WriteFile"? What is "WriteConsole"? ReadFile and ReadConsole? What are these? What happened to cout and cin? What on earth is a "HANDLE"?

What is BOOL WINAPI SetConsoleTextAttribute() ? A "bool" type fucntion? Then what is WINAPI? Another return type? Why are there TWO return types mentioned in front of a single "SetConsoleTextAttribute()" function?

I'm totally confused. Maybe this isn't the proper place to start learning, but my text book doesn't cover these things at all. ALL data is entered with simple stuff, like cin, gets(), getchar(), getch(), and all data is displayed using cout, puts(), etc...

I actually want to learn these weird things from the msdn website, but where do I start? I know these cin cout gets puts stuff, but how do i learn the more advanced stuff? Where do I begin???

Please help
 
Install Visual Studio (any recent version, they are free from Microsoft), and start C# project. It will be much easier for you to create rich graphics application in C# (or Visual Basic .NET) than in C/C++.

Get familiar with msdn.com. You'll find a lot of info there for beginners, too.
 

DeadlyDays

Honorable
Mar 29, 2013
15
0
10,590
5
the functions you are used to are c++ standard library functions, when you are trying to control the console you are interacting with windows, so you have to use windows library(windows API) functions to modify it.
a handle would be a reference to the object, that is the console in this case, an object is an instance of a class/etc. in order to interact with the specific console you have open, you have to be able to reference which console window that is open you are talking about. If you just reference the class, you aren't talking about the active console, you are talking about the class the console in generate from.

The WriteConsole, ReadFile, ReadConsole, those are all .Net functions(called methods).

That is how I would explain it.

Your book is telling you how c++ works and its syntax. What you are trying to do is interact with programs outside of your own program. This take additional knowledge of the program you are trying to interact with, in this case Windows. Windows provides an interface to interact with it via the Windows API/.NET

If your book mentioned anything about it it'd be in the advanced section, and it would only talk about it generically because it is training you on using c++, not C++ with windows.

Lookup tutorials on c++ with windows

EDIT:
Sorry, this is sort of a poor post, but not a lot of time atm to write
 
You cannot start Windows programming from scratch, you need to know how Windows operates.
- Are you writing console application, MFC appliction, or .NET application?

Go to local library, and get one of Charles Petzold' books on Windows programming. Spend a day or two browsing it, learning the terminology, then you will know what this "BOOL WINAPI" actually means.
 

Lumia925

Estimable
Oct 16, 2014
68
0
4,590
3


Pretty much everyone I know uses a version of Windows on their computer. It's not such a bad idea to learn Windows-specific C++ is it? Or is it necessary to keep all coding as neutral and platform independent as possible?
I would eventually like to create small graphical software like a calculator, or an analogue clock, or maybe a unit converter, I could make graphical programs in Visual Basic, but for some reason VB6 is not getting installed on Windows 8.1 :-(

I figured it's probably too early to try to do graphics in C++, so thought I'd try to learn some interesting things about Windows console programming..

I've read about/used classes, but never in this way. Isn't it class.object? Didn't see the class being defined and an object being created, or the class.object syntax and got confused.

Is Windows programming very difficult to learn? Is it a LOT different from normal C++ in my book? It looks like it is.. I'm not understanding anything at all :-(
 

Lumia925

Estimable
Oct 16, 2014
68
0
4,590
3


Okay, this is what I know. I know some VB6, some C, and some C++.
But the C++ I know is not at all like this. It's as if I'm trying to understand a new language which somewhat looks like my C++, but isn't quite the C++ I've been using for the past few months..

So then, to learn this, I need to get a book on "Windows programming", not "C++ programming", right?
Are there any free online books available?

The compiler m using is C-Free 5.0 (mingw). Will this stuff work in my compiler?

Thanks..
 

DeadlyDays

Honorable
Mar 29, 2013
15
0
10,590
5
Don't think about code neutral stuff, its complex. All code is platform dependent at its heart, all the code you write is broken down into machine code which is what the computer understands. The compiler you use does that for you. The only reason code can work on different systems is some languages have compilers that you have downloaded on your machine in order to run the program. Java for instance you need to download the java runtime to get it to work, it is compiling the program live while you are running the java program into machine code. That is why java works on everything, you compile to a generic type of code, and each machine has a specific compiler installed on it for the specific hardware the program is being run on. So a compiler has to be written separately for different hardware, downloaded and installed. So the compiler is making your code "neutral" in the case of java.
Java compilers also translate your calls for creating windows into whatever code the operating system needs as well for you.
C++ is not that forgiving. C++ doesn't have that intermediate language or the compiler that runs on the fly. You have to compile using the correct compiler for each type of hardware yourself or include multiple versions of the compiled code for different types of hardware. C++ doesn't translate your calls to the operating system to do something like create a window for you. You need to code it yourself or rely on a library/api that does it for you. Basically, java will interpret your call to create a window and pass that call onto the correct functions that work with the operating system you are on. So you call a java function, and that java function has different ways to go depending on the OS. C++, typically you'd call the OS function directly, so there is no in between function. Unless you go find a library that has a intermediate function to call such as SDL. So in c++ I could call the SDL function, and it would call the appropriate OS functions.
C++ is the language. You need to know the language first, how it works which is the syntax.

Once you know the syntax of a programming language and how it works, then you start to expand on that from there. Most of your programs will probably run on Windows, so yes, knowing the Windows API is important. Understand when someone says windows programming, that is not specifying a language. They are saying programming in some language and using the windows API with that language. There is still going to be a language being used to develop the program. Most C++ programs on windows will require you to know how to interact with windows at least to the extent in creating a window.

To understand it better, you have Programming Languages, and you have Programming Libraries. A program language is the structure, the syntax, how you say things. Most languages come with a standard library that has some basic functions/methods that let you do some more complex stuff than simply add/subtract numbers and store variables. A library is basically any function/method you use. You may not realize it, but you are probably including at least some of the standard c++ libraries at the top of every file you make already. cout, that is a function in one of the standard c++ libraries. The only reason you can use it is because you are calling it from the library, the include at the top of the file makes this easy.
When you do "windows programming" you include windows API libraries at the top of the file, and you use the functions in their libraries. Why? because c++ is just a language. One tricky thing with learning a language is using the knowledge of what you learn and expanding on that intuitively. You probably already know how it works, you are just getting overwhelmed and thinking its something more. Write 2 class files and keep them separate from your main file. Put functions in those other classes. Now how do you interact with those classes? Now if you wanted to change a variable in an object you generate from one of those classes that doesn't allow you to edit the value directly, but requires you to use a function in that object to change the variable, then you'd call that function yes? Windows is dozens of separate things running all at the same time, from the time the computer starts up. For the sake of programming safety, you cannot change many of the variables directly, you have to use functions to change the variables. The reason is the function checks to make sure you don't enter complete nonsense and cause windows to crash. Also, how would you interact with objects that are already running? since most of windows is already running at startup, the object handle isn't in your own code yet so you have no way to control it. So there are functions in the windows api to retrieve those objects so you can interact with them, and some of the functions will interact with the running processes without having to touch the object.

If my talk about objects confuses you, then you probably want to read up on Object Oriented Programming. OOP covers the design concepts used in OOP languages, which most programming languages you come across will be. A good c++ book would cover some basic OOP concepts hopefully anyway, but maybe yours didn't. A class is a template. An object is one instance of a class, you instantiate an instance of a class and it becomes its own separate object. So you create 1 dog class, and you can then instantiate that class as 12 different objects. They are all dogs, but you may have their variable Name all say something different. This is one way to use a class, and the heart of OOP. Its similar to creating a variable. When you create a variable "int a = 3;" that is an instance of a int, you can have many instances of int all have different values, but they are all still ints.

You can do "windows programming" with any programming language that has windows libraries available for it. C++ is very popular and so is supported. You can even use .NET in C++ if you know what you are doing or use Visual Studio and learn about .Net programming. It gets a little complex as Microsoft introduces different terminology for "C++.Net" and you start using .NET standard libraries instead of C++ standard libraries in order to maintain interoperability with other .NET languages.

EDIT
This is kind of simplified btw, but realize that a computer is doing things in binary. 1's and 0's, you add a level and there is machine code. One thing in machines code is many more characters in binary. Add another level and you have something like C, one line is many many lines in machine code. Add another level and you have something like c#, where one line may be many lines in a Intermediate languages, each line of that being many lines in machine code, etc. Programming is doing complex things using a lot of very simple things. The very simple things in the c++ language, when used together in the right order, form very complicated systems. But they can all be broken down to the very simple parts of the language it was written in. One complex function is likely calling a dozen simpler functions, each other those doing a dozen simpler things, etc. Learning how a language works is the most important step, after that you can figure out how the programs work that you need to work with while writing your own program.

In MSDN, you may need to follow a lot of blue links to reach a point where you understand something, and then work from there if you need to know exactly how it works. But that takes a lot of time, other than that you can read more books/tutorials on the subjects.

There is too much to write on this tbh
 
Install Visual Studio (any recent version, they are free from Microsoft), and start C# project. It will be much easier for you to create rich graphics application in C# (or Visual Basic .NET) than in C/C++.

Get familiar with msdn.com. You'll find a lot of info there for beginners, too.
 
Thread starter Similar threads Forum Replies Date
A Apps General Discussion 1
G Apps General Discussion 0
A Apps General Discussion 2
C Apps General Discussion 1
C Apps General Discussion 1
P Apps General Discussion 3
T Apps General Discussion 7
ARICH5 Apps General Discussion 7
M Apps General Discussion 5
K Apps General Discussion 1
A Apps General Discussion 4
H Apps General Discussion 4
A Apps General Discussion 1
ISetsunaI Apps General Discussion 1
William Norberg Apps General Discussion 3
T Apps General Discussion 20
R Apps General Discussion 6
G Apps General Discussion 6
aidanklenk Apps General Discussion 1
j_a_k_e_24 Apps General Discussion 1

ASK THE COMMUNITY