CMD Prompt with WIN32 APP

Hi, I need help with running CMD Prompt with WIN32 App program that I'm working on. I just want to use it for debugging.

So it runs and opens up a Console but it does not output any text to the console.

Source: https://justcheckingonall.wordpress.com/2008/08/29/console-window-win32-app/


CODE:
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#ifndef UNICODE
#define UNICODE
#endif 

//#include "Console.h"

#include <iostream>
#include <Windows.h>
#include <stdio.h>
#include <io.h>
#include <fcntl.h>

using namespace std;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);


int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	//Setup Console 
	AllocConsole();

	HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
	int hCrt = _open_osfhandle((long)handle_out, _O_TEXT);
	FILE* hf_out = _fdopen(hCrt, "w");
	setvbuf(hf_out, NULL, _IONBF, 1);
	*stdout = *hf_out;

	HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE);
	hCrt = _open_osfhandle((long)handle_in, _O_TEXT);
	FILE* hf_in = _fdopen(hCrt, "r");
	setvbuf(hf_in, NULL, _IONBF, 128);
	*stdin = *hf_in;


	//Console console;
	//console.InitConsole();


	std::cout << "Hello World" << endl;

	MSG  msg;
	WNDCLASSW wc = { 0 };
	wc.lpszClassName = L"Static Control";
	wc.hInstance = hInstance;
	wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
	wc.lpfnWndProc = WndProc;
	wc.hCursor = LoadCursor(0, IDC_ARROW);


	RegisterClassW(&wc);
	CreateWindowW(wc.lpszClassName, L"Criminal",
		WS_OVERLAPPEDWINDOW | WS_VISIBLE,
		100, 100, 330, 270, 0, 0, hInstance, 0);

	while (GetMessage(&msg, NULL, 0, 0)) {

		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return (int)msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {

	LPCWSTR strings = L"WTF IS THIS";

	switch (msg) {

	case WM_CREATE:

		CreateWindowW(L"Static", strings,
			WS_CHILD | WS_VISIBLE | SS_LEFT,
			20, 20, 300, 230,
			hwnd, (HMENU)1, NULL, NULL);
		break;

	case WM_DESTROY:

		PostQuitMessage(0);
		break;
	}

	return DefWindowProcW(hwnd, msg, wParam, lParam);
}
Last edited on
You need to allocate a console window.
https://docs.microsoft.com/en-us/windows/console/allocconsole

If you use Visual Studio an alternative might be OutputDebugString
https://msdn.microsoft.com/en-us/library/windows/desktop/aa363362(v=vs.85).aspx
I allocate a console.
Sorry, I didn't spot it.
Try WriteFile(handle_out, "Console output", 14, nullptr, nullptr);
Thankyou that worked.

Is there a reason why std::cout doesn't work? and WriteFile does?
I'm getting some ugly results using the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
	//TEST

	string filename = "Test.txt";
	ifstream input;
	input.open(filename);

	if (!input.is_open()){
		return 1;
	}

	while (input){
		string line;
		getline(input, line);

		if (!input)
			break;

		WriteFile(handle_out, &line, sizeof(line), nullptr, nullptr);
	}

	input.close();
Is there a reason why std::cout doesn't work? and WriteFile does?
I don't know.

sizeof doesn't work with a std::string, you need to use line.size() or line.length()
std::cout works with AllocConsole, it just needs a handle to it, not the default one:
AllocConsole();
freopen("CONOUT$", "w", stdout);
std::cout << "This works" << std::endl;
Topic archived. No new replies allowed.