Click here to Skip to main content
15,890,123 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: SendMessage for Interprocess communication stops working Pin
ForNow1-Nov-16 16:22
ForNow1-Nov-16 16:22 
GeneralRe: SendMessage for Interprocess communication stops working Pin
Midi_Mick1-Nov-16 17:36
professionalMidi_Mick1-Nov-16 17:36 
AnswerRe: SendMessage for Interprocess communication stops working Pin
Randor 1-Nov-16 17:56
professional Randor 1-Nov-16 17:56 
GeneralRe: SendMessage for Interprocess communication stops working Pin
ForNow2-Nov-16 13:59
ForNow2-Nov-16 13:59 
GeneralRe: SendMessage for Interprocess communication stops working Pin
Randor 2-Nov-16 16:42
professional Randor 2-Nov-16 16:42 
AnswerRe: SendMessage for Interprocess communication stops working Pin
leon de boer2-Nov-16 3:10
leon de boer2-Nov-16 3:10 
GeneralRe: SendMessage for Interprocess communication stops working Pin
ForNow2-Nov-16 14:32
ForNow2-Nov-16 14:32 
GeneralRe: SendMessage for Interprocess communication stops working Pin
leon de boer2-Nov-16 16:57
leon de boer2-Nov-16 16:57 
Okay lets first deal with your misconceptions

Windows console windows do have a window handle it just has a special function to get it because it isn't present by default because you start with main not winmain and main has no position for it on it's parameters which are arguments.
GetConsoleWindow function (Windows)[^]

You can also directly pull the message handler if you want to just provide your own that looks like this
WNDPROC ConsoleWindow = NULL;

static LRESULT CALLBACK MyWndProc(HWND Wnd, UINT Msg, WPARAM wParam, LPARAM lParam) {

	// Process your message here

	if (ConsoleWindow)
		return CallWindowProc(ConsoleWindow, Wnd, Msg, wParam, lParam);   // Call back old handler
	return(0);
}

// Usually first thing in main
    HWND hWnd = GetConsoleWindow();
    ConsoleWindow = (WNDPROC)SetWindowLong(hWnd, GWL_WNDPROC, (LONG)MyWndProc);

However since Win95 we tend not to do that anymore because there is a much easier way with a special class called Message-Only Windows. There are even a couple of articles on using them in the code project archives but it's just like any normal window just it has no visible parts.

The 3rd alternative is you make a normal windows program and create a console window from the launch using AllocConsole.
AllocConsole function (Windows)[^]
Lots of science and maths guys do this so they can dump data out onto the console window while running there graphics.

You do not have to have a graphics window however you can simply use the stdin and stdout ... here is the simplest sample. Compile it as a standard windows GUI project.
So again in this sample you have a standard message queue available and a console window.
#include <windows.h>
#include <tchar.h>

int APIENTRY WinMain(HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	LPSTR     lpCmdLine,
	int       nCmdShow)
{
	DWORD charsRead = 0;
	TCHAR keyBuffer[100];

	AllocConsole();

	HANDLE myConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
	DWORD cCharsWritten;
	TCHAR* str = TEXT("Hello I am the console\r\n");
	WriteConsole(myConsoleHandle, str, _tcslen(str), &cCharsWritten, NULL);
	HANDLE myConsoleHandleInput = GetStdHandle(STD_INPUT_HANDLE);
	ReadConsole(myConsoleHandleInput, keyBuffer, _countof(keyBuffer), &charsRead, NULL);
	
	str = TEXT("Okay lets mix it up with messages and console .. any key to exit\r\n");
	WriteConsole(myConsoleHandle, str, _tcslen(str), &cCharsWritten, NULL);
	DWORD events = 0;					// how many events took place
	INPUT_RECORD input_record = { 0 };	// a record of input events
	MSG Msg = { 0 };

	do {
		Msg.message = 0;
		input_record.EventType = 0;
		BOOL peek = PeekConsoleInput(myConsoleHandleInput, &input_record, 1, &events); // Peek for console input
		if (peek == FALSE) {										// No console input so do windows messages		
			if (PeekMessage(&Msg, 0, 0, 0, PM_REMOVE)) {			// Peek windows message
				TranslateMessage(&Msg);								// Translate each windows message
				DispatchMessage(&Msg);								// Dispatch each window message
			}
		} else {
			// We need to clear the console event we peeked .. we want next one
			ReadConsoleInput(myConsoleHandleInput,	&input_record, 1, &events);
		}
	} while ((Msg.message != WM_QUIT) && (input_record.EventType != KEY_EVENT));
}

In typical windows style there are multiple ways of doing the same thing Smile | :)
In vino veritas


modified 3-Nov-16 0:09am.

QuestionNeed help with cryptography Pin
SMD1111-Nov-16 8:54
SMD1111-Nov-16 8:54 
AnswerRe: Need help with cryptography Pin
Randor 1-Nov-16 19:04
professional Randor 1-Nov-16 19:04 
Question[C]Problem in creating a random string Pin
xXxRevolutionxXx27-Oct-16 1:31
xXxRevolutionxXx27-Oct-16 1:31 
AnswerRe: [C]Problem in creating a random string Pin
Richard MacCutchan27-Oct-16 1:46
mveRichard MacCutchan27-Oct-16 1:46 
GeneralRe: [C]Problem in creating a random string Pin
k505427-Oct-16 4:47
mvek505427-Oct-16 4:47 
GeneralRe: [C]Problem in creating a random string Pin
Richard MacCutchan27-Oct-16 4:55
mveRichard MacCutchan27-Oct-16 4:55 
AnswerRe: [C]Problem in creating a random string Pin
Jochen Arndt27-Oct-16 2:35
professionalJochen Arndt27-Oct-16 2:35 
AnswerRe: [C]Problem in creating a random string Pin
leon de boer27-Oct-16 3:43
leon de boer27-Oct-16 3:43 
SuggestionRe: [C]Problem in creating a random string Pin
David Crow27-Oct-16 4:48
David Crow27-Oct-16 4:48 
GeneralRe: [C]Problem in creating a random string Pin
Daniel Pfeffer27-Oct-16 5:11
professionalDaniel Pfeffer27-Oct-16 5:11 
AnswerRe: [C]Problem in creating a random string Pin
Krishnakumartg27-Oct-16 20:18
Krishnakumartg27-Oct-16 20:18 
GeneralRe: [C]Problem in creating a random string Pin
k505428-Oct-16 4:59
mvek505428-Oct-16 4:59 
PraiseRe: [C]Problem in creating a random string Pin
Krishnakumartg30-Oct-16 18:50
Krishnakumartg30-Oct-16 18:50 
QuestionRe: [C]Problem in creating a random string Pin
David Crow31-Oct-16 2:31
David Crow31-Oct-16 2:31 
QuestionUsing COM in DLL called by .Net application Pin
Leif Simon Goodwin26-Oct-16 3:02
Leif Simon Goodwin26-Oct-16 3:02 
AnswerRe: Using COM in DLL called by .Net application Pin
Richard Deeming26-Oct-16 3:11
mveRichard Deeming26-Oct-16 3:11 
PraiseSOLVED Pin
Leif Simon Goodwin9-Nov-16 5:32
Leif Simon Goodwin9-Nov-16 5:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.