20 April 2013

A Simple BAT to EXE Wrapper

I'm looking for a straightforward way to execute bat file inside bash and vice versa (execute shellscript in cmd box). At first I thinking about an encrypter that store content of batch file as string that later feed into shell invocation. But that's appear to be daunting to me right now :-D

I look around sourceforge and codeplex for existing solution, as expected there are several project based on 7z sfx but all of them (sorry if I missed) popped up new cmd window and can't work when called from PATH environment (aka different Curernt Directory).

So I decide to take 7zip SFX source code and apply some patches:

- To have 3 predefined environment variables replacement at RunProgram:
   %%T=temp directory where bat file extracted (supplied by oficial 7zip)
   %%S=path where sfx module located that is the exe file (similar to 7zsfxmod)
   %%W=path of current directory where sfx called (this is what I need)
- Assign CreateProcess's ApplicationName as COMSPEC environment
- Disabling progress bar, thus no need to set Progress="no"
- Redirect IO and prevent popped up windows as described in SO question here
- Finally, Change the sfx's submodule from gui to console using editbin or postw32 (of freepascal)

And it works :) (for my case)

Here you go:
http://osspack32.googlecode.com/files/7zSDcon.sfx

the patched main.cpp of SFXsetup (CPP) and the example converted bat (my old app Cabit, a Cabinet archiver):
http://osspack32.googlecode.com/files/cabit.exe
it use config file:

;!@Install@!UTF-8!
RunProgram="/c cd /d %%W && %%T\\cabit.bat "
;!@InstallEnd@!

Meaning after extraction run cabit.bat under the corrected working directory. Also note the extra space after the bat filename is important otherwise argument will not passed.
Then as usual do copy /b  7zSDcon.sfx+config.7z+cabit.7z cabit.exe

Unfortunately Igor Pavlov haven't publish the source code of his super small sfx (only 27kb) otherwise it would be neater.

I might gonna make sfxmaker or sort of with PE resource/icon insertion ability. Should be easy using wrc and linkres2exe tools.

1 comment:

  1. It work good in Windows 8.1 and can work also on Windows 9x and DOS with HX DOS extender (http://www.japheth.de/HX.html) with these syntax:

    RunProgram="%ComSpec% /c cd %%W|%%T\file.bat "

    with the "Alt 124" code between %%W and %%T

    ReplyDelete