The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: dsouza123 on March 29, 2007, 12:43:28 PM

Title: Starting a console program minimized, is it possible ?
Post by: dsouza123 on March 29, 2007, 12:43:28 PM
What is needed to create a console program that starts in the minimized state ?

How can this be done using assembly code (along with options, directives etc)
and assembler and linker options.

If needed, an alternate linker such as polink or golink is OK.   
Maybe even using poasm if ml can't do it.
Title: Re: Starting a console program minimized, is it possible ?
Post by: Tedd on March 29, 2007, 12:46:38 PM
use CreateProcess - you can set the position/size/state of the window created in the STARTUPINFO struct
Title: Re: Starting a console program minimized, is it possible ?
Post by: dsouza123 on March 29, 2007, 02:29:46 PM
Thanks Tedd

Wouldn't that require two programs, one just to launch the intended one ?

Following links from the screen with the CreateProcess function
the function GetStartupInfo is mentioned, is it possible to get the info
modify it and have it take affect ?

What are other ways of getting a console program to start minimized ?
Title: Re: Starting a console program minimized, is it possible ?
Post by: Tedd on March 29, 2007, 04:22:04 PM
Yeh, pretty much (normally the 'starter' program is windows' shell)

GetStartupInfo gives you the values used to start the program - but it's already started, so you're modifying them a bit late :P

If you can get the console window's handle then you can minimize it yourself :wink

Quick guess..
invoke GetActiveWindow
invoke ShowWindow, eax,SW_MINIMIZE


..but that doesn't work ::)
GetActiveWindow returns the thread's current active window, but apparently a console doesn't count.
So you need to get the console's window handle.... somehow. I don't think you're really allowed to get it from 'inside' the console (FindWindow seems a bit of a hacky solution.)
Title: Re: Starting a console program minimized, is it possible ?
Post by: Vortex on March 29, 2007, 06:02:43 PM
Here is a quick demo based on Tedd's method:

.386
.model flat,stdcall
option casemap:none

include     \masm32\include\windows.inc
include     \masm32\include\kernel32.inc
include     \masm32\include\user32.inc
include     \masm32\include\masm32.inc

includelib  \masm32\lib\kernel32.lib
includelib  \masm32\lib\user32.lib
includelib  \masm32\lib\masm32.lib


.data
ClassName   db 'ConsoleWindowClass',0
msg1        db 'Press ENTER to minimize the console window',0
msg2        db 'Console window minimized',0
capt        db 'Status',0

.data?
hWnd        dd ?
buffer      db 100 dup(?)

.code

start:

    invoke  FindWindow,ADDR ClassName,0
    mov     hWnd,eax
    invoke  StdOut,ADDR msg1
    invoke  StdIn,ADDR buffer,100
    invoke  StdOut,ADDR msg2
    invoke  ShowWindow,hWnd,SW_SHOWMINIMIZED
    invoke  MessageBox,0,ADDR msg2,ADDR capt,MB_OK
    invoke  ExitProcess,0

END start


The only trick is to specify the correct classname of a console window to find the handle.

[attachment deleted by admin]
Title: Re: Starting a console program minimized, is it possible ?
Post by: dsouza123 on March 29, 2007, 10:07:02 PM
Thanks Vortex

That works very well.
Title: Re: Starting a console program minimized, is it possible ?
Post by: Tedd on March 30, 2007, 12:24:01 PM
But note that FindWindow will get the handle of the first console window it finds - which may not necessarily be yours (if there is more than one console window open at the same time.)
Do GetConsoleTitle, and then use that as the window-title parameter for FindWindow :wink
Title: Re: Starting a console program minimized, is it possible ?
Post by: dsouza123 on March 30, 2007, 05:19:07 PM
That is true Tedd, an alternative is to fill the second parameter of FindWindow
with the caption of the desired console window.


Issue, if there are multiple copies of the console running it will only get one
and it doesn't always work, example if other windows are on top of the console window(s).

Any ideas on console windows that aren't top most and if there are multiple copies running ?
Title: Re: Starting a console program minimized, is it possible ?
Post by: dsouza123 on March 31, 2007, 03:14:36 AM
Did a forum search, found code that finds visible windows, modified it quite a bit.

It will minimize any visible (not minimized) console windows, can also restore them.


[attachment deleted by admin]
Title: Re: Starting a console program minimized, is it possible ?
Post by: hutch-- on March 31, 2007, 05:02:17 AM
dsouza123,

To handle multiple instances of the console, see if the console window(s) show up in an enumeration of windows running in the system. If it does then you have a simple mechanism to handle all copies of the console.
Title: Re: Starting a console program minimized, is it possible ?
Post by: dsouza123 on March 31, 2007, 10:26:27 PM
Thanks Hutch

If I understand enumeration correctly that means using EnumWindows,
that is how the visible windows are found in the min_rest program.
Or is it something else ?
Title: Re: Starting a console program minimized, is it possible ?
Post by: dsouza123 on March 31, 2007, 10:32:09 PM
Finally got a self minimizing console program working.

It is a regular program but linked as a console program.
It starts, immediately minimizes itself, then sleeps forever.

The sleeping forever doesn't matter since it is an example,
and can be exited by restoring and clicking on the close button.



[attachment deleted by admin]