Jump to content

Editbin to make an AutoIt3 exe a console app.


Jos
 Share

Recommended Posts

  • Developers

Somebody (I lost the EMail so don't remember the name) sent me an Email a while ago to ask if I could support the EditBin program that comes with the MASM32 package from Microsoft.

I was wondering if anybody has played with it and knows the PRO's and Con when doing this.

I played a bit with it and when I compiled this script called test.au3 :

ConsoleWrite("test" & @crlf)
ConsoleWrite("test" & @crlf)
ConsoleWrite("test" & @crlf)

..and then ran it from the CMD prompt it obviously didn;t display anything.

Then did this command: editbin.exe /subsystem:console test.exe

After that ran it again from the commandprompt and this time it did display the consolewrites.

I have seen many people asking for Console app support and this looks too easy to be true ...

:rolleyes:

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Replies 44
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Somebody (I lost the EMail so don't remember the name) sent me an Email a while ago to ask if I could support the EditBin program that comes with the MASM32 package from Microsoft.

I was wondering if anybody has played with it and knows the PRO's and Con when doing this.

I played a bit with it and when I compiled this script called test.au3 :

ConsoleWrite("test" & @crlf)
ConsoleWrite("test" & @crlf)
ConsoleWrite("test" & @crlf)

..and then ran it from the CMD prompt it obviously didn;t display anything.

Then did this command: editbin.exe /subsystem:console test.exe

After that ran it again from the commandprompt and this time it did display the consolewrites.

I have seen many people asking for Console app support and this looks too easy to be true ...

:rolleyes:

Yes..... Yes it does :rambo: Great Job JdeB! Would it maybe be posible to make a simple script, to parse every line of code, and create a new script, having the function of the other script, but doing a console write on every line?? I might just go do that :x
Link to comment
Share on other sites

  • Developers

Console support is only a linker switch after all, so it could be that easy.

Yea I know but was wondering if anybody knew the implications of doing this.

I have seen many people asking for Console app support and when its this easy to safely change it, I could add support for it in the wrapper, but think its important to understand all implications first and warn about it when supporting it.

(guess I am to lazy to test and search for it myself at this moment :rolleyes: )

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

JdeB - just tried this on an application that does some work and displays a debug screen. Very pleased so far. (This will allow this appilication to survive a logoff - at the moment as a windows app - this application will close when a user logs off)

I had no problem running editbin.exe /subsystem:console test.exe on my works laptop (this has Visual Studio 6 installed), however, when I copied editbin.exe to my home PC I could not run editbin.exe without error

Error message said something like "linker error" - I will retest and post full error message this evening.

Good find :rolleyes:

Link to comment
Share on other sites

  • Developers

JdeB - just tried this on an application that does some work and displays a debug screen. Very pleased so far. (This will allow this appilication to survive a logoff - at the moment as a windows app - this application will close when a user logs off)

I had no problem running editbin.exe /subsystem:console test.exe on my works laptop (this has Visual Studio 6 installed), however, when I copied editbin.exe to my home PC I could not run editbin.exe without error

Error message said something like "linker error" - I will retest and post full error message this evening.

Good find :rolleyes:

You need these files as well:

link.exe

mspdb50.dll

Not sure any others are needed yet, need to test some on a clean PC...

:rambo:

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

So basically this is a 1 byte change to the PE header. No need for M$ linkers and dlls, one could even write a simple Autoit script to do it... :rolleyes:

True and it was done before but it needs to be "fools proof" ... (or full proof if you like) ...

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

If I remember correctly, the script that changed an AutoIt3 EXE into a consoleapp only worked on a particular version of AutoIt3 ... and you don't want to feed the mouths that tried to make it work anyways .. :rolleyes:

Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

No, the users are not reliable.....

But that has nothing to do with the tool being used, whether it's editbin+linker from VS6, Autoit script (as a part of AutoIt3Wrapper, lets say) or some small application coded in any other language.

Just don't add those 600kb of worthless ballast to Autoit package, pretty plz.

If I remember correctly, the script that changed an AutoIt3 EXE into a consoleapp only worked on a particular version of AutoIt3 ... and you don't want to feed the months that tried to make it work anyways .. :rolleyes:

Ok, here's example which works on any program:

$openpath = FileOpenDialog( "Select executable to patch into Console...", @ScriptDir, "Exe (*.exe)", 1)
$hRead = FileOpen($openpath, 16)
$bR = FileRead($hRead)
FileClose($hRead)
If BinaryMid($bR, 1, 2) <> "MZ" Then
    MsgBox(0, "Error", "File is not an executable.")
    Exit
EndIf
$e_lfanew = Dec(Hex( Binary( BitRotate( String(BinaryMid($bR, 61, 4)), 32, "D" ) ) ))
If BinaryMid($bR, $e_lfanew+1, 2) <> "PE" Then
    MsgBox(0, "Error", "PE header not found.")
    Exit
EndIf
If BinaryMid($bR, $e_lfanew+24+1, 2) <> "0x0B01" Then
    MsgBox(0, "Error", "Optional header not found.")
    Exit
EndIf
$new = BinaryMid($bR, 1, $e_lfanew+24+68) & Binary("0x0300") & BinaryMid($bR, $e_lfanew+24+68+2+1)
$savepath = FileSaveDialog( "Save Console executable as...", "", "Exe (*.exe)", 18)
$hWrite = FileOpen($savepath, 18)
FileWrite($hWrite, $new)
FileClose($hWrite)

Nevermind my lolsome code, I'm sure you can optimize it, but what's important is that the logic of locating the Subsystem value is about as generic and "fool proof" as it gets, provided that the executable is valid to begin with (it's possible to get more paranoic, but come on...).

PE header explained here.

Edited by Siao

"be smart, drink your wine"

Link to comment
Share on other sites

This is awesome. I've always wanted console output in autoit. Thanks for sharing. Where would we be without JDEB. :rolleyes:

P.S.

Is this the script you were talking about?

http://www.autoitscript.com/forum/index.php?showtopic=17380

Edited by SolidSnake
HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.
Link to comment
Share on other sites

Are there any implications for signed applications here? Or is this just going to be a script I run as a post-compile

command on my console interactive programs?

... I'm starting to smell a post-processor directive, if this becomes the de facto methodology for creating console mode applications.

I'd still _really_ love to be able to select the option to choose the 'standard windows app','true console mode', 'service mode' natively at

compile time, but this would be a great step in the right direction.

Reading the help file before you post... Not only will it make you look smarter, it will make you smarter.

Link to comment
Share on other sites

I don't understand the want for console applications anymore.

Some things just don't lend themselves well to a windows environment.

Do you have any windows services running on your machine? They're not windows apps -- they may have windows front ends, but they aren't windows applications.

Need an application running without a user logged on?

Desire a small, quick application to act as a logger or server?

Want a utility that does conversions, accepts console input, and writes messages / progress directly to the screen w/o bothering to code a gui?

CUI apps are becoming more, not less common -- netsh , ipconfig , monad (powershell) , all are finally giving windows users the ability to utilize /live from a command line.

Heck, even mac users can have a command line now that they've gone *nix based -- used to be the only way you could get to a console prompt on a mac was to

purchase the developers kit....

Correspondingly, the need for admins / technicians who understand what's really happening when you click an icon is increasing, rather than decreasing.

Edited by flyingboz

Reading the help file before you post... Not only will it make you look smarter, it will make you smarter.

Link to comment
Share on other sites

Doing this is safe, I guess. At least for compiled scripts. AutoIt3.exe is signed, obviously, and changing it will break the signature. Having looked closely at all the startup code MSVC compiles into an application and also comparing executables compiled with and without the switch, the binaries are identical save for the single byte (Technically not true, there's a timestamp which is different too, but that's easily ignored if you're expecting that difference). My previous comments and reservations about switching the flag were based on me having never taken the time to investigate things completely.

Ideally, this should just be implemented in Aut2Exe as an option. It's a rather trivial thing to do as it just requires reading a little bit of the PE Header and re-saving with 1 byte changed. I've known this for well over 6 months now. The problem during that time for me has always been testing. You can't change AutoIt3.exe from one subsystem to the other because it's signed. AutoIt3.exe is a GUI application. That means there would be no way for people to test the CUI version without compiling the script with that flag set in Aut2Exe, then running that. This is inconsistent which is why I never pursued implementing the flag in Aut2Exe. My preference is for as much to be common between non-compiled and compiled as possible.

I'm sure Jon is familiar enough with the PE Format by now to effect this change in Aut2Exe with little effort. However, it's imperative that it be documented that running a script non-compiled will *ALWAYS* run in GUI mode and that only compiled scripts have the option of being compiled as either CUI or GUI applications.

Link to comment
Share on other sites

Valik, i didn't test this with the current version but back there when you wrote this stub there was a different behaviour between your stub and running a modified version.

The way you implemented the user input feature in the test script which was in the archive you posted only worked with your stub.

Back there i didn't find a way to get user input without an external tool.

ConsoleRead always hung using the changed subsytem way.

And i remember some console related api's behaving different too, but i don't know which one any more.

Edit:

ConsoleRead still behaves that way.

For reference the topic is here.

Edited by piccaso
CoProc Multi Process Helper libraryTrashBin.nfshost.com store your AutoIt related files here!AutoIt User Map
Link to comment
Share on other sites

I decided to check overhead, and concluded that a For loop is 0.0008~ milliseconds faster on each iteration when run in a console. that number scales up into a still unnoticeable amount when doing very large loops, but I like the idea. So there is a gain to be had when running in console! :rolleyes:

Edited by Zacharot
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...