Jump to content
Sign in to follow this  
AJStevens

CUI with GUI Hybrid

Recommended Posts

AJStevens

Ok, so in order to have ConsoleWrite output to a command prompt Window, you have to compile as CUI instead of GUI right?

I'm trying to emulate similiar functionality of other exe's in that if you just run the exe via double-clicking Mouse, it runs and shows the GUI, however if you call it from a Console (DOS Window) with parameters, it processes those parameters, eg.

myscript.exe /h

Will display a help message, or /v version information.

Meanwhile, passing nothing, should just open the program normally.

If I build it as a GUI, then ConsoleWrite doesn't show up in the Console (DOS Window).

If I build it as a CUI and open it via mouse double-click, on Windows XP, I get a Console Window behind my GUI, which I don't want. :/

How can I get the best of both worlds? Thanks

Edited by AJStevens

Share this post


Link to post
Share on other sites
UEZ

Here the method I used for SIC2:

#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_UseX64=n
#AutoIt3Wrapper_Change2CUI=y
#include <Process.au3>

If $CmdLine[0] = 0 Then
    If _ProcessGetName(ProcessGetParent(@AutoItPID)) = "cmd.exe" Then
        ConsoleWrite("Program was started in CMD" & @CRLF)
    Else
        MsgBox(0, "Information", "Program was started by double click!", 10)
    EndIf
EndIf

Exit

Func ProcessGetParent($i_PID) ;get PID from parent process done by SmOke_N
    Local $TH32CS_SNAPPROCESS = 0x00000002
    Local $a_tool_help = DllCall("Kernel32.dll", "long", "CreateToolhelp32Snapshot", "int", $TH32CS_SNAPPROCESS, "int", 0)
    If IsArray($a_tool_help) = 0 Or $a_tool_help[0] = -1 Then Return SetError(1, 0, $i_PID)
    Local $tagPROCESSENTRY32 = _
            DllStructCreate( _
            "dword dwsize;" & _
            "dword cntUsage;" & _
            "dword th32ProcessID;" & _
            "uint th32DefaultHeapID;" & _
            "dword th32ModuleID;" & _
            "dword cntThreads;" & _
            "dword th32ParentProcessID;" & _
            "long pcPriClassBase;" & _
            "dword dwFlags;" & _
            "char szExeFile[260]" _
            )
    DllStructSetData($tagPROCESSENTRY32, 1, DllStructGetSize($tagPROCESSENTRY32))
    Local $p_PROCESSENTRY32 = DllStructGetPtr($tagPROCESSENTRY32)
    Local $a_pfirst = DllCall("Kernel32.dll", "int", "Process32First", "long", $a_tool_help[0], "ptr", $p_PROCESSENTRY32)
    If IsArray($a_pfirst) = 0 Then Return SetError(2, 0, $i_PID)
    Local $a_pnext, $i_return = 0
    If DllStructGetData($tagPROCESSENTRY32, "th32ProcessID") = $i_PID Then
        $i_return = DllStructGetData($tagPROCESSENTRY32, "th32ParentProcessID")
        DllCall("Kernel32.dll", "int", "CloseHandle", "long", $a_tool_help[0])
        If $i_return Then Return $i_return
        Return $i_PID
    EndIf
    While @error = 0
        $a_pnext = DllCall("Kernel32.dll", "int", "Process32Next", "long", $a_tool_help[0], "ptr", $p_PROCESSENTRY32)
        If DllStructGetData($tagPROCESSENTRY32, "th32ProcessID") = $i_PID Then
            $i_return = DllStructGetData($tagPROCESSENTRY32, "th32ParentProcessID")
            If $i_return Then ExitLoop
            $i_return = $i_PID
            ExitLoop
        EndIf
    WEnd
    DllCall("Kernel32.dll", "int", "CloseHandle", "long", $a_tool_help[0])
    Return $i_return
EndFunc ;==>ProcessGetParent

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
AJStevens

UEZ, thanks that's a great little script and works for differentiating between running from console and running by double-click.

However, what can I do when running by double-click to remove the extra console window behind my GUI?

post-51287-12819531686123_thumb.jpg

Share this post


Link to post
Share on other sites
UEZ

UEZ, thanks that's a great little script and works for differentiating between running from console and running by double-click.

However, what can I do when running by double-click to remove the extra console window behind my GUI?

post-51287-12819531686123_thumb.jpg

Try this:

...
If $CmdLine[0] = 0 Then
    If _ProcessGetName(ProcessGetParent(@AutoItPID)) = "cmd.exe" Then
    ConsoleWrite("Program was started in CMD" & @CRLF)
    Else
        WinSetState("[CLASS:ConsoleWindowClass]", "", @SW_HIDE) ;class might be different on different OS!
    MsgBox(0, "Information", "Program was started by double click!")
    EndIf
EndIf
...

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
AJStevens

Try this:

...
If $CmdLine[0] = 0 Then
    If _ProcessGetName(ProcessGetParent(@AutoItPID)) = "cmd.exe" Then
    ConsoleWrite("Program was started in CMD" & @CRLF)
    Else
        WinSetState("[CLASS:ConsoleWindowClass]", "", @SW_HIDE) ;class might be different on different OS!
    MsgBox(0, "Information", "Program was started by double click!")
    EndIf
EndIf
...

Br,

UEZ

Thanks UEZ, that does hide it (on XP, haven't tried Vista/Win7 yet), but.... is there way for it not to show in the first place? It appears, then dissapears and the GUI appears.

It appears to open, close, open from it appearing in the task bar, dissapearing, then reappearing with the GUI.

Edited by AJStevens

Share this post


Link to post
Share on other sites
UEZ

As far as I know it is not possible to suppress the CMD console when compiling it with #AutoIt3Wrapper_Change2CUI=y parameter.

If you compile it with #AutoIt3Wrapper_Change2CUI=n then ConsoleWrite() will not be seen in CMD but also not text to CMD via ConsoleWrite() is possible!

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
AJStevens

As far as I know it is not possible to suppress the CMD console when compiling it with #AutoIt3Wrapper_Change2CUI=y parameter.

If you compile it with #AutoIt3Wrapper_Change2CUI=n then ConsoleWrite() will not be seen in CMD but also not text to CMD via ConsoleWrite() is possible!

Br,

UEZ

Yeah... this is the problem... :-(

No way around this? Anyone? Is there a chance this could be made a new feature?

#HideConsole

Then do the logic above, and if it has been launched from cmd.exe then Show it?

Edited by AJStevens

Share this post


Link to post
Share on other sites
AJStevens

Yeah... this is the problem... :-(

No way around this? Anyone? Is there a chance this could be made a new feature?

#HideConsole

Then do the logic above, and if it has been launched from cmd.exe then Show it?

I've looked around the forum and not found anything that's worked for me.

I'm guessing it's Plan B, have a seperate exe for use by commandline where I want the output to show in the Console window... I suppose there's always a msgbox, it's a pity that holds the script though...

Share this post


Link to post
Share on other sites
AJStevens

Use AllocConsole and FreeConsole to show and hide the console.

Yeah Freeconsole hides it, same as UEZ suggestion earlier, problem is the console window appears then hides which I don't want.

I've looked at AllocConsole, tried some examples but it hasn't worked out as I hoped. Basically only if the exe is run from a command prompt I want the script to know and output any ConsoleWrite in the script to it, otherwise just behave like a GUI without having a Console window appear behind it at all.

This link can also be of interest: CMD.au3

Br,

UEZ

UEZ, thanks, will give that a look.

I guess really it's not a CUI I want as the only input it will take is command line switches as apposed to a real CUI withe menus and options, entering text, etc. If going that far then seperate EXE makes sense usually.

Share this post


Link to post
Share on other sites
Richard Robertson

Yes I know that UEZ said to hide it too. I was telling you the correct way to do it though. FreeConsole releases the resources while UEZ's method only hides the window.

Share this post


Link to post
Share on other sites
AJStevens

I guess it depends if you want it back later whether to hide it or free it, in either case both suffer the problem that it appears in the first place.

Share this post


Link to post
Share on other sites
Richard Robertson

To keep it from appearing, you can't compile it as a CUI application. You'll have to specify GUI and then somehow attach to the parent process's console.

Share this post


Link to post
Share on other sites
AJStevens

UEZ, that CMD.au3 is interesting if I need to do things with existing command prompts... a possibilitiy... however doesn't appear to help with this current situation sadly.

To keep it from appearing, you can't compile it as a CUI application. You'll have to specify GUI and then somehow attach to the parent process's console.

Yeah, seems so, unfortunately it's that "somehow" that's getting in the way... I'm trying a DLL call with attach using the PID of parent process, the CMD, yet I'm not getting an output from ConsoleWrite().

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.