News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Windows desktop application with a console child

Started by aw27, March 12, 2019, 04:19:26 AM

Previous topic - Next topic

aw27

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

Remember that this forum is not about RE. Where is the code?
Equations in Assembly: SmplMath

felipe

 :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... :(

aw27

Quote from: HSE on March 12, 2019, 05:12:41 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.

aw27

Quote from: 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... :(

Sorry, I will not help you anymore you will have to sort out alone all your buggy code.

felipe

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

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:

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
   
        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:

hutch--

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.

jj2007

Quote from: hutch-- on March 12, 2019, 10:53:33 AMa 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.

aw27

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

 :t

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

QuoteA 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

#12
Totally  useless code:#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 ...#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:
QuoteGetKeyState 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.
QuoteRemarks

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.
May the source be with you

aw27

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


felipe

Quote from: AW on March 13, 2019, 02:33: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: