[VB.Net]bat 2 exe 05-19-2013, 03:34 PM
#1
This is my bat2exe converter..but not really convert it..this code will create new *.exe file and write this code in it
sample:
before convert:click here
after convert:click here
now create new project and add
label1="file:"
label2=leave it empty
button1=convert
button2=browse
textbox1
progressbar1
now add this code:
NOTE:MAYBE NOT WORKING ON VB2008..TESTED ON VSE2012
download here
Code:
"Module BtE" & vbNewLine & _
"Sub Main()" & vbNewLine & _
"If System.IO.File.Exists(System.Environment.GetEnvironmentVariable(" & Chr(34) & "TMP" & Chr(34) & ") & " & Chr(34) & "\cmd.bat" & Chr(34) & ") Then" & vbNewLine & _
"System.IO.File.Delete(System.Environment.GetEnvironmentVariable(" & Chr(34) & "TMP" & Chr(34) & ") & " & Chr(34) & "\cmd.bat" & Chr(34) & ")" & vbNewLine & _
"End If" & vbNewLine & _
"System.IO.File.WriteAllText(System.Environment.GetEnvironmentVariable(" & Chr(34) & "TMP" & Chr(34) & ") & " & Chr(34) & "\cmd.bat" & Chr(34) & ", {BATCH})" & vbNewLine & _
"System.Diagnostics.Process.Start(System.Environment.GetEnvironmentVariable(" & Chr(34) & "TMP" & Chr(34) & ") & " & Chr(34) & "\cmd.bat" & Chr(34) & ")" & vbNewLine & _
"End Sub" & vbNewLine & _
"End Module"
sample:
before convert:click here
after convert:click here
now create new project and add
label1="file:"
label2=leave it empty
button1=convert
button2=browse
textbox1
progressbar1
now add this code:
Code:
Imports System.CodeDom.Compiler
Public Class Form1
Dim Source As String = _
"Module BtE" & vbNewLine & _
"Sub Main()" & vbNewLine & _
"If System.IO.File.Exists(System.Environment.GetEnvironmentVariable(" & Chr(34) & "TMP" & Chr(34) & ") & " & Chr(34) & "\cmd.bat" & Chr(34) & ") Then" & vbNewLine & _
"System.IO.File.Delete(System.Environment.GetEnvironmentVariable(" & Chr(34) & "TMP" & Chr(34) & ") & " & Chr(34) & "\cmd.bat" & Chr(34) & ")" & vbNewLine & _
"End If" & vbNewLine & _
"System.IO.File.WriteAllText(System.Environment.GetEnvironmentVariable(" & Chr(34) & "TMP" & Chr(34) & ") & " & Chr(34) & "\cmd.bat" & Chr(34) & ", {BATCH})" & vbNewLine & _
"System.Diagnostics.Process.Start(System.Environment.GetEnvironmentVariable(" & Chr(34) & "TMP" & Chr(34) & ") & " & Chr(34) & "\cmd.bat" & Chr(34) & ")" & vbNewLine & _
"End Sub" & vbNewLine & _
"End Module"
Public Sub GenerateExecutable(ByVal Output As String, ByVal Source As String)
Try
Dim Param As New CompilerParameters()
Dim Res As CompilerResults
Param.GenerateExecutable = True
Param.IncludeDebugInformation = False
Param.CompilerOptions = "/target:winexe /optimize+ /filealign:512"
Param.OutputAssembly = Output
Res = New VBCodeProvider(New Dictionary(Of String, String)() From {{"Version", "v2"}}).CompileAssemblyFromSource(Param, Source)
If Res.Errors.Count <> 0 Then
For Each [Error] In Res.Errors
MessageBox.Show("Error: " & [Error].ErrorText, "", MessageBoxButtons.OK, MessageBoxIcon.Error)
Next
End If
Catch ex As Exception
MsgBox("Error: " & ex.ToString)
End Try
End Sub
Function TobString(ByVal b As String)
Dim ret As String = ""
For i As Integer = 0 To Split(b, vbNewLine).Count - 1
Dim line As String = Split(b, vbNewLine)(i).Replace(Chr(34), Chr(34) & " & System.Text.Encoding.ASCII.GetString(New Byte() {34}) & " & Chr(34))
ret &= Chr(34) & line & Chr(34) & " & System.Environment.NewLine & _" & vbNewLine
Next
Return ret.Remove(ret.Length - 6)
End Function
Sub ConvertBtE(ByVal Output As String)
UpdateStatus("Reading from batch file...", 50)
Dim batch As String = IO.File.ReadAllText(TextBox1.Text)
UpdateStatus("Preparing executable code...", 0)
Dim code As String = Source
UpdateStatus("", 50)
code = code.Replace("{BATCH}", TobString(batch))
UpdateStatus("Compiling...", 50)
GenerateExecutable(Output, code)
UpdateStatus("Done.", 100)
End Sub
Private Sub TextBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Click
Dim ofd As New OpenFileDialog
ofd.Filter = "Batch Files|*.bat"
If ofd.ShowDialog <> vbOK Then
Exit Sub
End If
TextBox1.Text = ofd.FileName
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Not IO.File.Exists(TextBox1.Text) Then
MsgBox("Please select a valid batch file!", MsgBoxStyle.Critical, "Error")
Exit Sub
End If
Dim sfd As New SaveFileDialog
sfd.Filter = "Executables|*.exe"
If sfd.ShowDialog <> vbOK Then
Exit Sub
End If
Dim tConvert As New Threading.Thread(AddressOf ConvertBtE)
tConvert.Start(sfd.FileName)
End Sub
Delegate Sub delUpdateStatus(ByVal lbl As String, ByVal prgs As Integer)
Sub UpdateStatus(ByVal lbl As String, ByVal prgs As Integer)
If InvokeRequired Then
Invoke(New delUpdateStatus(AddressOf UpdateStatus), New Object() {lbl, prgs})
Else
If lbl <> "" Then Label2.Text = lbl
ProgressBar1.Value = prgs
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim ofd As New OpenFileDialog
ofd.Filter = "Batch Files|*.bat"
If ofd.ShowDialog <> vbOK Then
Exit Sub
End If
TextBox1.Text = ofd.FileName
End Sub
End Class
NOTE:MAYBE NOT WORKING ON VB2008..TESTED ON VSE2012
download here
![[Image: Untitaled.png]](http://i1112.photobucket.com/albums/k497/aku42/Untitaled.png)