Home > Mobile >  How to convert bat to exe and make the exe receive parameters?
How to convert bat to exe and make the exe receive parameters?

Time:09-29

I have a bat file that I want to convert into an exe so I can add a icon and a name. But I also need it to accept parameters like my bat file.

Here's the bat file receive parameters like this:%*

The bat file works fine on it own.

@echo off
title Parsing...
set v=%temp%\cmd.vbs
set d=%temp%\decode.wsc
(echo ^<?xml version="1.0"?^>^<component^>^<?component error="true" debug="true"?^>
echo ^<registration description="Url Decode Helper" progid="JSEngine.Url" version="1.0" classid="{80246bcc-45d4-4e92-95dc-4fd9a93d8529}" /^>
echo ^<public^>
echo ^<method name="decode"^>^<PARAMETER name="s"/^>^</method^>
echo ^</public^>
echo ^<script language="JScript"^>
echo ^<!^[CDATA^[
echo var description=new UrlEncodeDecodeHelper;
echo function UrlEncodeDecodeHelper^(^)^{this.decode=decode;^}
echo function decode^(s^)^{return decodeURIComponent^(s.replace^(/\ /g," "^)^);^}
echo ^]^]^>
echo ^</script^>
echo ^</component^>)>%d%
(echo Set JSEngine=GetObject^("Script:C:\Users\vanes\AppData\Local\Temp\decode.wsc"^)
echo Set obj=WScript.CreateObject^("WScript.Shell"^)
echo stre=JSEngine.decode^(Wscript.Arguments^(0^)^)
echo str=Mid^(stre,5^)
echo If Len^(stre^)^>5 Then
echo 'Remove the ^(^'^) from run and move it to para for it to run without confirmation
echo para^(^)
echo 'run^(^)
echo Else
echo obj.Run^("cmd.exe"^)
echo End If
echo.
echo Function para^(^)
echo MsgBox str,vbOKOnly vbInformation 4096,"Code passed by program:"
echo user=MsgBox^("Do you allow it to pass to the Command prompt and run?",vbQuestion 4096 vbYesNo vbDefaultButton2,str^)
echo If user=vbYes Then
echo run^(^)
echo Else
echo End If
echo End Function
echo Function run^(^)
echo obj.Run^("cmd.exe /k " ^& str^)
echo End Function)>%v%

rem creates the wsc and vbs files needed.

wscript %v% %*
del %v% %d%

I've tried all sorts of converters but they don't allow me to pass parameters. Is there any other converter or way to do it manually that will allow me to pass parameters into the exe file created?

I don't need it to have a icon and name right away I can add them through Resource hacker.

I'm trying to make a URL parser that will parser a URL like cmd:shutdown /s so the command shutdown /s will be sent into the command prompt and run. But only if the user allows it to be passed to the cmd! Here the idea_ https://youtu.be/46pBeyHKQuQ

CodePudding user response:

I tried Bat To Exe Converter from MajorGeeks.com and it seems to work fine with parameters. One thing I noticed is that when passing arguments to a batch file, %* doesn't include the path of the batch file being run, but the converted exe does.

CodePudding user response:

In this case, the best option may be to "convert" your batch file to an exe by rewriting it in C#. Below is a simple C# program you can use with your URL protocol handler. It converts the URI encoded parameter to plain text, splits off the command by looking for the protocol handler name (assumed to be "cmd:"), and then executes the command using Cmd /c. If you don't want a Cmd console screen popping up, then you could modify this code to run the command directly with a bit more parsing. And, of course, you can add a confirmation prompt as well.

using System;
using System.Diagnostics;

namespace Parser
{
    class Program
    {
        static void Main(string[] args)
        {
            string x = System.Uri.UnescapeDataString(Environment.CommandLine);
            string[] y = x.Split(new string[] {"cmd:"}, StringSplitOptions.None);
            Process.Start("Cmd.exe", " /c "   y[1]);
        }
    }
}

Here is the URL protocol handler reg entries I used to test this:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\SOFTWARE\Classes\cmd]
"URL Protocol"="\"\""
@="\"URL:cmd Protocol\""

[HKEY_CURRENT_USER\SOFTWARE\Classes\cmd\DefaultIcon]
@="\"parser.exe,1\""

[HKEY_CURRENT_USER\SOFTWARE\Classes\cmd\shell]

[HKEY_CURRENT_USER\SOFTWARE\Classes\cmd\shell\open]

[HKEY_CURRENT_USER\SOFTWARE\Classes\cmd\shell\open\command]
@="C:\\Parser\\Parser.exe %1"

BTW, you can run a .vbs via the Shell Open registry key by prefixing it with Wscript.exe or Cscript.exe. However, the browser will then ask if you want to run Windows Scripting Host. If you create your own exe, the browser will show the exe name (and you can add an icon) so an exe is probably the way you'll want to go.

  • Related