Author Topic: Windows desktop application with a console child  (Read 2604 times)

AW

  • Member
  • *****
  • Posts: 2583
  • Let's Make ASM Great Again!
Windows desktop application with a console child
« 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?




HSE

  • Member
  • *****
  • Posts: 1377
  • <AMD>< 7-32>
Re: Windows desktop application with a console child
« Reply #1 on: March 12, 2019, 05:12:41 AM »
Remember that this forum is not about RE. Where is the code?

felipe

  • Member
  • *****
  • Posts: 1332
  • Eagles are just great!
Re: Windows desktop application with a console child
« Reply #2 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... :(
Felipe.

AW

  • Member
  • *****
  • Posts: 2583
  • Let's Make ASM Great Again!
Re: Windows desktop application with a console child
« Reply #3 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.

AW

  • Member
  • *****
  • Posts: 2583
  • Let's Make ASM Great Again!
Re: Windows desktop application with a console child
« Reply #4 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.

felipe

  • Member
  • *****
  • Posts: 1332
  • Eagles are just great!
Re: Windows desktop application with a console child
« Reply #5 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:
Felipe.

felipe

  • Member
  • *****
  • Posts: 1332
  • Eagles are just great!
Re: Windows desktop application with a console child
« Reply #6 on: March 12, 2019, 06:04:29 AM »
and what buggy code anyway?  :biggrin:
Felipe.

felipe

  • Member
  • *****
  • Posts: 1332
  • Eagles are just great!
Re: Windows desktop application with a console child
« Reply #7 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:
Felipe.

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 7539
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Windows desktop application with a console child
« Reply #8 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.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

jj2007

  • Member
  • *****
  • Posts: 10543
  • Assembler is fun ;-)
    • MasmBasic
Re: Windows desktop application with a console child
« Reply #9 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.

AW

  • Member
  • *****
  • Posts: 2583
  • Let's Make ASM Great Again!
Re: Windows desktop application with a console child
« Reply #10 on: March 12, 2019, 05:17:14 PM »
The idea of all this is here. 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.

LiaoMi

  • Member
  • ****
  • Posts: 698
Re: Windows desktop application with a console child
« Reply #11 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

TimoVJL

  • Member
  • ****
  • Posts: 554
Re: Windows desktop application with a console child
« Reply #12 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.
« Last Edit: March 13, 2019, 06:31:04 AM by TimoVJL »
May the source be with you

AW

  • Member
  • *****
  • Posts: 2583
  • Let's Make ASM Great Again!
Re: Windows desktop application with a console child
« Reply #13 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

« Last Edit: March 13, 2019, 04:17:17 AM by AW »

felipe

  • Member
  • *****
  • Posts: 1332
  • Eagles are just great!
Re: Windows desktop application with a console child
« Reply #14 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:
Felipe.