2 registered members (flink, AndrewAMD),
656
guests, and 1
spider. |
Key:
Admin,
Global Mod,
Mod
|
|
|
External Console
#403403
06/19/12 14:41
06/19/12 14:41
|
Joined: Feb 2009
Posts: 3,207 Germany, Magdeburg
Rei_Ayanami
OP
Expert
|
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: HeelX]
#403406
06/19/12 15:52
06/19/12 15:52
|
Joined: Feb 2009
Posts: 3,207 Germany, Magdeburg
Rei_Ayanami
OP
Expert
|
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. DLL
/*
* 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.
/*
* 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:
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: Superku]
#403474
06/20/12 21:00
06/20/12 21:00
|
Joined: Dec 2008
Posts: 1,218 Germany
Rackscha
Serious User
|
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
OP
Expert
|
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 (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
Serious User
|
Serious User
Joined: Dec 2008
Posts: 1,218
Germany
|
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
|
|
|
|