Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by EternallyCurious. 04/18/24 10:45
StartWeek not working as it should
by Zheka. 04/18/24 10:11
folder management functions
by VoroneTZ. 04/17/24 06:52
lookback setting performance issue
by 7th_zorro. 04/16/24 03:08
zorro 64bit command line support
by 7th_zorro. 04/15/24 09:36
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:48
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:46
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (flink, AndrewAMD), 656 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
EternallyCurious, howardR, 11honza11, ccorrea, sakolin
19047 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
External Console #403403
06/19/12 14:41
06/19/12 14:41
Joined: Feb 2009
Posts: 3,207
Germany, Magdeburg
Rei_Ayanami Offline OP
Expert
Rei_Ayanami  Offline OP
Expert

Joined: Feb 2009
Posts: 3,207
Germany, Magdeburg
Hey,

Just a small contribution, as I needed it for myself. (Or, better said - I wanted to make one for fun :P)

The contribution contains 3 files:
- LiteConsole.exe
- LiteConsole.dll
- LiteConsole.h

Start the LiteConsole.exe, then you can start writing into it with "LCexternOut", either with a char* or a STRING *.

The exe can be placed anywhere you want.

You will get no error or anything when you write into the console when it is not open.


Download: Here




Would be cool, if you would report bugs, if you find them.


Regards,
Rei

Re: External Console [Re: Rei_Ayanami] #403404
06/19/12 15:25
06/19/12 15:25
Joined: Jul 2001
Posts: 6,904
H
HeelX Offline
Senior Expert
HeelX  Offline
Senior Expert
H

Joined: Jul 2001
Posts: 6,904
Hey Marian, this is cool! Would you mind posting the source of the .exe and the .dll as well? I would like to read your source to learn the way you can do this.

Best regards and thank you!,
-Christian

Re: External Console [Re: HeelX] #403406
06/19/12 15:52
06/19/12 15:52
Joined: Feb 2009
Posts: 3,207
Germany, Magdeburg
Rei_Ayanami Offline OP
Expert
Rei_Ayanami  Offline OP
Expert

Joined: Feb 2009
Posts: 3,207
Germany, Magdeburg
Hey,

The exe code is very ugly, please excuse this, but this was my first time using "real" Win32 (project) stuff.


Click to reveal..

DLL
Code:
/*
*	LiteConsole
*
*	DLL Code.
*/

#define WIN32_LEAN_AND_MEAN
#define DLL_USE

#include <windows.h> 
#include "adll.h"

BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved) 
{
	engine_bind(); 
	return TRUE;
}

DLLFUNC void LCexternOutC(char *str)
{
	HWND hWnd = FindWindow(NULL, "LiteConsole");
	COPYDATASTRUCT cds;
	cds.dwData = 1;
	cds.cbData = strlen(str) + 1;
	cds.lpData = str;
	SendMessage(hWnd, WM_COPYDATA, (WPARAM)hWnd, (LPARAM)(LPVOID)&cds);
}
DLLFUNC void LCexternOutS(STRING *stri)
{
	char *str = _chr(stri);
	HWND hWnd = FindWindow(NULL, "LiteConsole");
	COPYDATASTRUCT cds;
	cds.dwData = 1;
	cds.cbData = strlen(str) + 1;
	cds.lpData = str;
	SendMessage(hWnd, WM_COPYDATA, (WPARAM)hWnd, (LPARAM)(LPVOID)&cds);
}



exe, needs a "real" window, so that message can be processed. Way easier than just using a console.
Code:
/*
*	LiteConsole
*
*	EXE Code.
*/

#include "stdafx.h"
#include "LiteConsole.h"
#include <iostream>

#define MAX_LOADSTRING 100

HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name

ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);

class outbuf : public std::streambuf {
public:
    outbuf() 
	{
        setp(0, 0);
    }

    virtual int_type overflow(int_type c = traits_type::eof()) 
	{
        return fputc(c, stdout) == EOF ? traits_type::eof() : c;
    }
};

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

	MSG msg;
	HACCEL hAccelTable;

	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_LITECONSOLE, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_LITECONSOLE));
	
	if(AllocConsole()) 
	{
		freopen("CONOUT$", "w", stdout);
		freopen("CONIN$", "r", stdin);
		SetConsoleTitle(_T("Gamestudio Debug Console"));
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);  
	}

	outbuf ob;
	std::streambuf *sb = std::cout.rdbuf(&ob);
	

	std::cout.rdbuf(sb);
	HWND hwnd = FindWindow(NULL, _T("LiteConsole"));

	ShowWindow(hwnd, SW_HIDE);


	bool bRet;
	while( (bRet = GetMessage( &msg, hwnd, 0, 0 )) != 0)
	{
		if (bRet != -1)
		{
			TranslateMessage(&msg); 
			DispatchMessage(&msg); 
		}
	}

	return 0;
}

ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_LITECONSOLE));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_LITECONSOLE);
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	return RegisterClassEx(&wcex);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance;

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;

	switch (message)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	case WM_COPYDATA:
	{
		COPYDATASTRUCT* pcds = (COPYDATASTRUCT*)lParam;
		if (pcds->dwData == 1)
		{
			std::cout << (char*)pcds->lpData;
		}
		break;
	}
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}



Here are the important parts from the exe:
Code:
if(AllocConsole()) 
	{
		freopen("CONOUT$", "w", stdout);
		freopen("CONIN$", "r", stdin);
		SetConsoleTitle(_T("Gamestudio Debug Console"));
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);  
	}

	outbuf ob;
	std::streambuf *sb = std::cout.rdbuf(&ob);
	

	std::cout.rdbuf(sb);
	HWND hwnd = FindWindow(NULL, _T("LiteConsole"));

//////////////////////

	bool bRet;
	while( (bRet = GetMessage( &msg, hwnd, 0, 0 )) != 0)
	{
		if (bRet != -1)
		{
			TranslateMessage(&msg); 
			DispatchMessage(&msg); 
		}
	}

/////////////////////

	case WM_COPYDATA:
	{
		COPYDATASTRUCT* pcds = (COPYDATASTRUCT*)lParam;
		if (pcds->dwData == 1)
		{
			std::cout << (char*)pcds->lpData;
		}
		break;
	}




So, all in all my approach is the following: SendMessage and GetMessage with WM_COPYDATA, and the string in the COPYDATASTRUCT.

Re: External Console [Re: Rei_Ayanami] #403471
06/20/12 20:34
06/20/12 20:34
Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
Superku Offline
Senior Expert
Superku  Offline
Senior Expert

Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
Fantastic work, thank you!


"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual

Check out my new game: Pogostuck: Rage With Your Friends
Re: External Console [Re: Superku] #403474
06/20/12 21:00
06/20/12 21:00
Joined: Dec 2008
Posts: 1,218
Germany
Rackscha Offline
Serious User
Rackscha  Offline
Serious User

Joined: Dec 2008
Posts: 1,218
Germany
instead of using a seperated exe, you might just use allocconsole inside the dll initialization to create a console window.

Maybe you'll have to register a second invisible handle first, didnt use it for a long time.

edit: btw: you can use allocconsole directly in LiteC BUT stupid LiteC redeclares read/write methods which means they dont work anymore with the console -.-

Last edited by Rackscha; 06/20/12 21:55.

MY Website with news of my projects:
(for example my current
Muliplayer Bomberman,
GenesisPrecompiler for LiteC
and TileMaster, an easy to use Tile editor)
Sparetime-Development

Re: External Console [Re: Rackscha] #403488
06/21/12 09:05
06/21/12 09:05
Joined: Feb 2009
Posts: 3,207
Germany, Magdeburg
Rei_Ayanami Offline OP
Expert
Rei_Ayanami  Offline OP
Expert

Joined: Feb 2009
Posts: 3,207
Germany, Magdeburg
Hehe, I know, I had tried it.

But I wanted a console that stays open all the time, so that different programs can write to it wink
(for example: my LiteGUI dll and liteC programs write to it, and I can still see the output after close)

Re: External Console [Re: Rei_Ayanami] #403495
06/21/12 11:29
06/21/12 11:29
Joined: Dec 2008
Posts: 1,218
Germany
Rackscha Offline
Serious User
Rackscha  Offline
Serious User

Joined: Dec 2008
Posts: 1,218
Germany
@Rei: Ah ok, good point


MY Website with news of my projects:
(for example my current
Muliplayer Bomberman,
GenesisPrecompiler for LiteC
and TileMaster, an easy to use Tile editor)
Sparetime-Development


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1