New to CPP, when using WriteConsole from Windows.h problem

Hey there, I'm new to both this forum and C++

I've been following this tutorial and up until now everything went flawless,
but all of a sudden I had to use a lot of windows.h functions which are quite
a pain in the ass.

I have listed parts of some of my files, ConLib.h, ConLib.cpp and game.cpp
The function WriteLine is grinding my gears, the only output I get are question marks :/

(The program is a consoleprogram)

Header file containing declaration
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// ConLib.h

// Avoiding redefenition
#pragma once

// including windows header file
#include <Windows.h>

// ConLib color codes
enum ConColor
{
	ConRed		= 1,
	ConGreen	= 2,
	ConBlue		= 4
};

// ConLib controll class
class ConLib
{
	// Handling screen and keyboard handles
	HANDLE m_Screen;
	HANDLE m_Keyboard;

	// COlor Attributes
	WORD m_TextColor;
	WORD m_BackgroundColor;

public:
	// con- and destructor
	ConLib();
	~ConLib();

	// Setting Attributes
	void SetBackgroundColor(WORD Color);
	void SetTextColor (WORD Color);
	void SetPosition (COORD Position);

	// Output methods
	void Clear();
	void OutputString(char * String);

	// Input methods
	void Read (char * Buffer, DWORD BufferSize);
	int GetKey();
};

__
source file where WriteConsole is being covered

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// ConLib.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "targetver.h"
#include "ConLib.h"

...

m_Screen = GetStdHandle (STD_OUTPUT_HANDLE);

// Sending string to screen
void ConLib::OutputString (char * String)
{
	DWORD Written;

	WriteConsole (m_Screen, String, strlen (String), &Written, NULL);
}

___
Example of usage

#include "stdafx.h"
// including game header file
#include "Game.h"

...
m_Console->OutputString("\tWelcome to Monster 1.0\n\n");
...
Last edited on
I'm assuming Written will contain how many characters were written. You may want to check that it is the same as strlen(String). Also if you capture the output for WriteConsole() you can check to see if it failed.

1
2
3
4
5
6
7
8
9
void ConLib::OutputString (char * String)
{
  DWORD Written;
  BOOL result = WriteConsole (m_Screen, String, strlen (String), &Written, NULL);
  if(!result || Written != strlen(String))
  {
    // something went wrong...
  }
}

Well, being the noob that I am, I just obediently followed the instructions, and the tutorial told me I should just ignore Written, because it was not necessary. As a fix I tried:

1
2
3
4
5
6
7
8

void ConLib::OutputString (char * String)
{
  Written = strlen(String);
  WriteConsole (m_Screen, String, strlen (String), &Written, NULL);

}


But no avail..
As for what you suggested, I'm going to try that one out now. I think it's also possible that the way to get the handle for the console is outdated, the tutorial is from 2001.

1
2
3
4
5
6
7
8
9
10
11
12
class ConLib
{
private:
	// Handling screen and keyboard handles
	HANDLE m_Screen;
	HANDLE m_Keyboard;

	// COlor Attributes
	WORD m_TextColor;
	WORD m_BackgroundColor;
...
};


Constructor:
1
2
3
4
5
6
7
8
9
ConLib::ConLib()
{
	m_Screen = GetStdHandle (STD_OUTPUT_HANDLE);
	m_Keyboard = GetStdHandle (STD_INPUT_HANDLE);

	SetTextColor (ConRed | ConGreen | ConBlue);
	SetBackgroundColor (0);

}
Last edited on
Topic archived. No new replies allowed.