Hi,
I was wondering, is it possible to make a program which uses a console user interface when started from prompt and GUI if its started from explorer for example?
I think I need to make a GUI app and somehow detect if its started from console and then make that console (not a new one) to accept my prints. But how to do that?
Sami
I was wondering, is it possible to make a program which uses a console user interface when started from prompt and GUI if its started from explorer for example?
I think I need to make a GUI app and somehow detect if its started from console and then make that console (not a new one) to accept my prints. But how to do that?
Sami
One thing you may be able to use is that when you run the program from a command prompt, it will have a console window, and when it's run from the GUI, it won't.
However I did a quick test with GetStdHandle, too see if it returned INVALID_HANDLE_VALUE when run from the GUI, but it didn't, although GetLastError returned "The handle is invalid"...
Thomas
However I did a quick test with GetStdHandle, too see if it returned INVALID_HANDLE_VALUE when run from the GUI, but it didn't, although GetLastError returned "The handle is invalid"...
Thomas
I did a check of cmd.exe, and it has a huge amount of stuff that can be passed to it on the command line, my guess is it wouldn't be too hard to ShellExecute it, and pass it a command on the command line, i.e. to print some stuff to stdout. If you have a bunch of commands to execute, you could try putting them in a batch file, and execute that. Check the attached file for the command line options available for cmd.exe.
I would be curious to know how you get on with this one.
I would be curious to know how you get on with this one.
Thomas, if the subsystem is set to IMAGE_SUBSYSTEM_WINDOWS_CUI,
you should always get a console no matter if you run the app from
a shell or from explorer.
So the solution is probably to make a GUI app, and make it AllocConsole
if it's run from a shell. But I don't know How to detect whether it's
run from a console or a GUI app :/.
you should always get a console no matter if you run the app from
a shell or from explorer.
So the solution is probably to make a GUI app, and make it AllocConsole
if it's run from a shell. But I don't know How to detect whether it's
run from a console or a GUI app :/.
Thomas, if the subsystem is set to IMAGE_SUBSYSTEM_WINDOWS_CUI,
you should always get a console no matter if you run the app from a shell or from explorer.
.
you should always get a console no matter if you run the app from a shell or from explorer.
Yes but when the subsystem is the default GUI subsystem, it will only have a console when run from the shell, as it takes over the shell command line. It won't have one in the GUI.
However the problem is detecting it. Although getlasterror returned the handle was invalid, it wasn't (i.e. it wasn't invalid_handle_value what GetStdHandle returned).
Thomas
Afternoon, SamiP.
Why do you want to have your proggy output to the console if it's executed from there? I'm just curious.
This all seems obvious. However, it's the obvious stuff which I'm always overlooking:tongue: , so I thought I might mention it:
You do realise that a Win32 proggy will run perfectly fine whether it's executed with Explorer or from a console.
Plus, remember: a console is not DOS. If you restarted your 'puter in DOS mode, your proggy won't run.
I, also, do not know how to get a proggy to detect how it's been executed. So I'm no help:rolleyes: . I'd be interested to know how to do this as well.
Cheers,
Scronty
Why do you want to have your proggy output to the console if it's executed from there? I'm just curious.
This all seems obvious. However, it's the obvious stuff which I'm always overlooking:tongue: , so I thought I might mention it:
You do realise that a Win32 proggy will run perfectly fine whether it's executed with Explorer or from a console.
Plus, remember: a console is not DOS. If you restarted your 'puter in DOS mode, your proggy won't run.
I, also, do not know how to get a proggy to detect how it's been executed. So I'm no help:rolleyes: . I'd be interested to know how to do this as well.
Cheers,
Scronty
Hi, I know it can be done (but I really don't know HOW!), and it can really be useful.
I have a tool, made by MS, that manipulates images (it adds mipmaps, and converts formats for DirectX compression).
If you run it via explorer, it has a gui, and you can see the differences while you edit the pic.
If instead you need to canvert a large number of textures to other formats, you can simply write a batch, launch the program via console, and you can process several of them without constant input from the user.
Useful, isn't it?
'Bye, Kefren
I have a tool, made by MS, that manipulates images (it adds mipmaps, and converts formats for DirectX compression).
If you run it via explorer, it has a gui, and you can see the differences while you edit the pic.
If instead you need to canvert a large number of textures to other formats, you can simply write a batch, launch the program via console, and you can process several of them without constant input from the user.
Useful, isn't it?
'Bye, Kefren
This is just an idea, I have never actually done anything even near to it, but the GetConsoleTitle API can help. If I understand it correctly, it returns the title bar of the current console window. If it does, it means we are starting from one. If not, we are starting from GUI.
This comes from Docs:
The GetConsoleTitle function retrieves the title bar string for the current console window.
DWORD GetConsoleTitle(
LPTSTR lpConsoleTitle, // address of buffer for title
DWORD nSize // size of the buffer
);
Parameters
lpConsoleTitle
Points to a buffer that receives a null-terminated string containing the text that appears in the title bar of the console window.
nSize
Specifies the size, in characters, of the buffer pointed to by the lpConsoleTitle parameter.
Return Value
If the function succeeds, the return value is the length, in characters, of the string copied to the buffer.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
Remarks
To set the title bar string for a console window, use the SetConsoleTitle function.
HTH, Kefren
This comes from Docs:
The GetConsoleTitle function retrieves the title bar string for the current console window.
DWORD GetConsoleTitle(
LPTSTR lpConsoleTitle, // address of buffer for title
DWORD nSize // size of the buffer
);
Parameters
lpConsoleTitle
Points to a buffer that receives a null-terminated string containing the text that appears in the title bar of the console window.
nSize
Specifies the size, in characters, of the buffer pointed to by the lpConsoleTitle parameter.
Return Value
If the function succeeds, the return value is the length, in characters, of the string copied to the buffer.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
Remarks
To set the title bar string for a console window, use the SetConsoleTitle function.
HTH, Kefren
When I make a CON program it allways opens console, even when executed from explorer. So GetConsoleTitle and/or GetConsoleWindow both returns as they return when executed from console. So no luck...
When I make a GUI program GetConsoleTitle return allways empty string... so this cuold be used to determine if program is started from explorer or from console.
Now the problem is how to tell to GUI program that it needs to use existing console and not to allocate a new one?
Scronty: I need this because when I work in console its annoying that I can't use my programs from there. And sometimes I'm so lazy that I like just clickin different options :) Well, I can make two programs, one which runs in console mode and one which runs with gui... but it would be nice if I can join both programs to one exe.
Sami
When I make a GUI program GetConsoleTitle return allways empty string... so this cuold be used to determine if program is started from explorer or from console.
Now the problem is how to tell to GUI program that it needs to use existing console and not to allocate a new one?
Scronty: I need this because when I work in console its annoying that I can't use my programs from there. And sometimes I'm so lazy that I like just clickin different options :) Well, I can make two programs, one which runs in console mode and one which runs with gui... but it would be nice if I can join both programs to one exe.
Sami
Of course, you need to compile your prog as GUI, otherwise it will always open a console, then return the title bar of that console.
The CreateFile API can retrieve the console's handle, then you go on with the norma Console APIs.
HTH, Kefren
The CreateFile API can retrieve the console's handle, then you go on with the norma Console APIs.
HTH, Kefren
Afternoon, SamiP.
yep. It'd definitely be useful. i.e. for an example as kefren suggested.
Where are you using "GetConsoleTitle" in your GUI proggy ? is it at the "start:" or in WinMain, or somewhere else?
Once you've found out whether the proggy's been started in a console, you don't have to create a new one. Just don't create a window/message loop/etc. and send all ouput to STDout.
(I haven't tried this, so it could be all BS);)
Cheers,
Scronty
yep. It'd definitely be useful. i.e. for an example as kefren suggested.
Where are you using "GetConsoleTitle" in your GUI proggy ? is it at the "start:" or in WinMain, or somewhere else?
Once you've found out whether the proggy's been started in a console, you don't have to create a new one. Just don't create a window/message loop/etc. and send all ouput to STDout.
(I haven't tried this, so it could be all BS);)
Cheers,
Scronty
When I make a GUI program GetConsoleTitle return allways empty string... so this cuold be used to determine if program is started from explorer or from console.
I wasn't thinking clearly. GetConsoleTitle returns allways empty string if it is called from program that is compiled to run from gui.
So it can't be used to determine if program is started from gui or con.
Because of that we are again at step one :(
I can't use CreateFile to obtain console handle if I call it from gui program. Atleast it doesn't obtain current consoles output handle.
The easy solution... if commandline arguments are passed, work
in console mode. If not, start in GUI mode =). Still the issue of using
the existing console remains...
in console mode. If not, start in GUI mode =). Still the issue of using
the existing console remains...
I need to give up :(
I can't find a way to make console and GUI application on one exe.
If you have done it, I appreciate if you can mail to me or post here how this happens.
Sami
I can't find a way to make console and GUI application on one exe.
If you have done it, I appreciate if you can mail to me or post here how this happens.
Sami
Afternoon, SamiP.
Don't give up yet ;).
There'll always be some work-around.
All I can think of at the moment:
Use a GUI proggy.
If a file is in the commandline, create a console (using CreateFile) and process it (as kefren suggested).
Otherwise, carry on with the usual GUI setup.
If a file is "drag/drop"ed onto your proggy, it'll process it in a console. Plus, you could use it in a batch file.
With regards to starting the proggy from the command prompt (console), I can't see why it'd be necessary to process a file in the *same* console.
Cheers,
Scronty
Don't give up yet ;).
There'll always be some work-around.
All I can think of at the moment:
Use a GUI proggy.
If a file is in the commandline, create a console (using CreateFile) and process it (as kefren suggested).
Otherwise, carry on with the usual GUI setup.
If a file is "drag/drop"ed onto your proggy, it'll process it in a console. Plus, you could use it in a batch file.
With regards to starting the proggy from the command prompt (console), I can't see why it'd be necessary to process a file in the *same* console.
Cheers,
Scronty