The MASM Forum

General => The Laboratory => Topic started by: AW on March 12, 2019, 04:19:26 AM

Title: Windows desktop application with a console child
Post by: AW on March 12, 2019, 04:19:26 AM
I am leaving here a challenge:
How to run a console application inside a Windows desktop application?  :icon_eek:

I have a general idea and have done a demo - only to show that the concept is possible (for those that think it is not) - but my demo has a few glitches.  :(

Anyone?

(https://www.dropbox.com/s/0zozeny0idfkgyw/winconsole10.png?dl=1)

(https://www.dropbox.com/s/am4wpj43jvyvxzx/winconsole7.png?dl=1)
Title: Re: Windows desktop application with a console child
Post by: HSE on March 12, 2019, 05:12:41 AM
Remember that this forum is not about RE. Where is the code?
Title: Re: Windows desktop application with a console child
Post by: felipe on March 12, 2019, 05:19:51 AM
 :biggrin: Good one!

Maybe using CreatePseudoConsole? too bad i can't try it because that function it's only available in the kernel32.dll of windows 10... :(
Title: Re: Windows desktop application with a console child
Post by: AW on March 12, 2019, 05:39:54 AM
Remember that this forum is not about RE. Where is the code?
This is a challenge! Do you know what is that?
I don't know what is an RE and  don't remember ever seen any code from you in this forum.
Title: Re: Windows desktop application with a console child
Post by: AW on March 12, 2019, 05:41:28 AM
:biggrin: Good one!

Maybe using CreatePseudoConsole? too bad i can't try it because that function it's only available in the kernel32.dll of windows 10... :(

Sorry, I will not help you anymore you will have to sort out alone all your buggy code.
Title: Re: Windows desktop application with a console child
Post by: felipe on March 12, 2019, 05:45:51 AM
you are so funny. i don't need your help at all. even if i need help with some code or whatever, there are more people in the planet than you. so don't feel sorry for me...do you feel sorry for you?  :idea:
Title: Re: Windows desktop application with a console child
Post by: felipe on March 12, 2019, 06:04:29 AM
and what buggy code anyway?  :biggrin:
Title: Re: Windows desktop application with a console child
Post by: felipe on March 12, 2019, 06:30:04 AM
Maybe something like this (attached .asm and .exe in the .zip file). I have used the "generic" example from the sdk and added this lines of code:
Code: [Select]
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
   
        call    AllocConsole
        call    GetConsoleWindow
        push    hWnd
        push    eax
        call    SetParent 

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

It can be improved to look better, i think...but i have other challenges to attend... :bgrin:
Title: Re: Windows desktop application with a console child
Post by: hutch-- on March 12, 2019, 10:53:33 AM
Jose,

Unless you are looking for something more exotic, a normal UI app can create a console and use console text IO with no problems. I have seen it used for debugging purposes and you just do a conditional assembly that you can turn on and off.
Title: Re: Windows desktop application with a console child
Post by: jj2007 on March 12, 2019, 11:58:17 AM
a normal UI app can create a console and use console text IO with no problems.

José's example does a bit more: It embeds the console into the window as if it was an edit control.
Title: Re: Windows desktop application with a console child
Post by: AW on March 12, 2019, 05:17:14 PM
The idea of all this is here (https://www.codeproject.com/Articles/101367/Code-to-Host-a-Third-Party-Application-in-our-Proc). A console poses additional problems because does not process messages.

A new challenge for Felipe and I will pay $100.00 for a solution  :exclaim:
Add an item to the System menu of a console application and call a MessageBox by clicking on it.
Title: Re: Windows desktop application with a console child
Post by: LiaoMi on March 12, 2019, 08:58:54 PM
 :t

AllocConsole()
https://docs.microsoft.com/en-us/windows/console/attachconsole

Quote
A process can be attached to at most one console. If the calling process is already attached to a console, the error code returned is ERROR_ACCESS_DENIED (5). If the specified process does not have a console, the error code returned is ERROR_INVALID_HANDLE (6). If the specified process does not exist, the error code returned is ERROR_INVALID_PARAMETER (87).

A process can use the FreeConsole function to detach itself from its console. If other processes share the console, the console is not destroyed, but the process that called FreeConsole cannot refer to it. A console is closed when the last process attached to it terminates or calls FreeConsole. After a process calls FreeConsole, it can call the AllocConsole function to create a new console or AttachConsole to attach to another console.

To compile an application that uses this function, define _WIN32_WINNT as 0x0501 or later. For more information, see Using the Windows Headers.

Requirements
Minimum supported client

Windows XP [desktop apps only]

Minimum supported server

Windows Server 2003 [desktop apps only]

Header

ConsoleApi.h (via Wincon.h, include Windows.h)
Library

Kernel32.lib
DLL

Kernel32.dll

https://github.com/Maximus5/ConEmu

Redirecting or capturing output from processes
https://fixedbycode.blogspot.com/2014/07/redirecting-or-capturing-output-from.html

Real-Time Console Output Redirection
https://www.codeproject.com/Articles/16163/Real-Time-Console-Output-Redirection
Title: Re: Windows desktop application with a console child
Post by: TimoVJL on March 13, 2019, 02:04:03 AM
Totally  useless code:
Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "msvcrt.lib")

void CheckMenu(HMENU hMenu)
{
UINT uMenu = GetMenuState(hMenu, 1000, MF_BYCOMMAND);
SHORT st =  GetKeyState(VK_LBUTTON);
if (uMenu && st < 0) {
printf("hMenu: %Xh uMenu: %Xh st:%dh\n", hMenu, uMenu, st);
}
}

void __stdcall TimerThr(void* hMenu)
{
MSG msg;
SetTimer(0, 0, 100, 0);
while(GetMessage(&msg, NULL, 0, 0))
CheckMenu(hMenu);
}

void __cdecl mainCRTStartup(void)
{
char szTmp[260];
HWND hWnd = GetConsoleWindow();
HMENU hMenu = GetSystemMenu(hWnd, FALSE);
AppendMenu(hMenu, MF_STRING, 1000, TEXT("Info..."));
CreateThread(NULL, 0, TimerThr, hMenu, 0, NULL);
while (!_kbhit()) {
printf("just waisting time... check system menu info...\n");
Sleep(1000);
}
ExitProcess(0);
}
and more ...
Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "msvcrt.lib")

void CheckMenu(HMENU hMenu, int iButton)
{
UINT uMenu = GetMenuState(hMenu, 1000, MF_BYCOMMAND);
SHORT st = GetAsyncKeyState(iButton);
if (uMenu && st < 0) {
printf("hMenu: %Xh uMenu: %Xh st:%dh\n", hMenu, uMenu, st);
MessageBox(0, "test", 0, 0);
}
}

void __stdcall TimerThr(void* pParam)
{
MSG msg;
HWND hWnd = GetConsoleWindow();
HMENU hMenu = GetSystemMenu(hWnd, FALSE);
AppendMenu(hMenu, MF_STRING, 1000, TEXT("Info..."));
HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
int iButton;
if (GetSystemMetrics(SM_SWAPBUTTON)) iButton = VK_RBUTTON;
else iButton = VK_LBUTTON;
while (WaitForSingleObject(hEvent, 100))
CheckMenu(hMenu, iButton);
}

void __cdecl mainCRTStartup(void)
{
CreateThread(NULL, 0, TimerThr, 0, 0, NULL);
while (!_kbhit()) {
printf("just waisting time... check system menu info...\n");
Sleep(1000);
}
ExitProcess(0);
}
EDIT:
Quote
GetKeyState returns the virtual key state. In other words, GetKeyState reports the state of the keyboard based on the messages you have retrieved from your input queue. This is not the same as the physical keyboard state:
so for my second example GetKeyState is correct, as with swapped mouse buttons GetAsyncKeyState still use left button.
Quote
Remarks

The GetAsyncKeyState function works with mouse buttons. However, it checks on the state of the physical mouse buttons, not on the logical mouse buttons that the physical buttons are mapped to. For example, the call GetAsyncKeyState(VK_LBUTTON) always returns the state of the left physical mouse button, regardless of whether it is mapped to the left or right logical mouse button. You can determine the system's current mapping of physical mouse buttons to logical mouse buttons by calling GetSystemMetrics(SM_SWAPBUTTON).
EDIT: fix for Windows 10 in second example.
Title: Re: Windows desktop application with a console child
Post by: AW on March 13, 2019, 02:33:35 AM
Quote
Totally  useless code:
Yeap, particularly for Felipe.  :t ( :bgrin:)

*
Quote
and more ...
May need GetAsyncKeyState because messages are not being removed from the queue.

@LiaoMi
Good links. This one is also good:
https://github.com/rprichard/win32-console-docs

Title: Re: Windows desktop application with a console child
Post by: felipe on March 13, 2019, 04:52:35 AM
Quote
Totally  useless code:
Yeap, particularly for Felipe.  :t ( :bgrin:)

You mean because of the c language?  :idea:
My knowledge of the win api it's still very limited. By now i have a console with a new item (a bitmap) in the system menu. But it dosen't do anything when clicking on it...I will probably fail this challenge.  :idea:
By the way: it's a good idea to put challenges to try on... :icon14:
Title: Re: Windows desktop application with a console child
Post by: guga on March 13, 2019, 05:48:41 AM
Hi Aw

Nice test app :t :t :t

Did´t tested the code below, but maybe this can be useful for your needs.

https://support.microsoft.com/en-au/help/190351/how-to-spawn-console-processes-with-redirected-standard-handles
Title: Re: Windows desktop application with a console child
Post by: AW on March 13, 2019, 06:05:35 AM
so for my example GetKeyState is correct,
In the 2nd example, it works 1st time you click, will not work the second time you click. In the 1st example it works.
Title: Re: Windows desktop application with a console child
Post by: daydreamer on March 17, 2019, 01:00:14 AM
shouldnt it be possibe to use winapi Textarea and fake a console with the right colors and font?

but the next challenge maybe should be to make something that still runs fast despite you extremely underclock it inside a emulator that is turned down so the code runs at less than 10mhz,maybe also be forbidden to use any cpu directive

this becomes my 686th post :biggrin: