////////////////update 2006.4.25 15:20 支持64kb以上的执行文件///////////////////
/////////////////////复制以上内容到文本文件内,并保存为bat 类型运行即可///////////////////////
//////以下为仅支持64kb以内的执行文件的转换代码///////////
记得1997年那个叫"OMNISCENT"的DEMO引起了很多人的兴趣,今天我也写了个,基于.net 2.0 环境的一个工具软件,叫做EXE2BAT,可以转换64KB以下的执行文件为批处理文本文件,只需要执行批处理即可运行程序。
以下是工具的执行代码,请复制到文本文件内,并修改名称为 exe2bat.bat ,并运行它。
运行界面为:
程序原始代码为:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
namespace ExeToBatch
{
public partial class FormMain : Form
{
public FormMain()
{
CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
}
Thread t = null;
private void btnLoad_Click(object sender, EventArgs e)
{
DialogResult dr = openFileDialog1.ShowDialog(this);
btnConvert.Enabled = dr == DialogResult.OK;
}
private void Convert(string fileName)
{
ThreadWithState tws = new ThreadWithState(fileName, new ProgressCallback(ResultCallback), new CompleteCallback(Complete));
t = new Thread(new ThreadStart(tws.ThreadProc));
t.Start();
}
//回调
public void ResultCallback(int Max, int Min, int Position)
{
this.progressBar1.Maximum = Max;
this.progressBar1.Minimum = Min;
this.progressBar1.Value = Position;
}
public void Complete()
{
MessageBox.Show("任务完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
btnConvert.Enabled = false;
}
private void btnExit_Click(object sender, EventArgs e)
{
if (t != null)
{
t.Abort();
t.Join();
}
this.Dispose();
}
private void btnConvert_Click(object sender, EventArgs e)
{
Convert(openFileDialog1.FileName);
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("explorer.exe", "http://chinasf.cnblogs.com");
System.Diagnostics.Process.Start("explorer.exe", "mailto:chinasf@hotmail.com");
}
}
public delegate void ProgressCallback(int Max, int Min, int Position);
public delegate void CompleteCallback();
public class ThreadWithState
{
private string fileName;
private ProgressCallback callback;
private CompleteCallback cmpback;
public ThreadWithState(string fileName, ProgressCallback callbackDelegate, CompleteCallback cmpbackDelegate)
{
this.fileName = fileName;
this.callback = callbackDelegate;
this.cmpback = cmpbackDelegate;
}
public void ThreadProc()
{
//处理文件
try
{
if (File.Exists(this.fileName))
{
FileStream fileReader = new FileStream(this.fileName, FileMode.Open, FileAccess.Read);
byte[] fileBytes = new byte[fileReader.Length];
fileReader.Read(fileBytes, 0, fileBytes.Length);
fileReader.Close();
int k = 256;
StringBuilder echo = new StringBuilder();
echo.AppendFormat("@echo 正在装入数据,请等待 \r\n");
int b = 0;
int cb = 0;
for (int i = 0; i < fileBytes.Length; i++)
{
b++;
if (b == 1)
{
echo.AppendFormat("@echo e{0} ", k.ToString("x4"));
}
echo.AppendFormat("{0} ", fileBytes[i].ToString("x"));
if (b >= 16)
{
b = 0;
echo.AppendFormat(">>Datares\r\n");
}
k++;
cb++;
if (callback != null && cb % 1000==0 )
callback(fileBytes.Length, 0, i);
}
if (b < 16 && b!=0)
echo.AppendFormat(">>Datares\r\n");
//echo.AppendFormat("@echo g >> Datares\r\n");
echo.AppendFormat("@echo 重建执行程序 {0} \r\n", new FileInfo(this.fileName).Name);
echo.AppendFormat("@echo rcx >> Datares\r\n");
echo.AppendFormat("@echo {0} >> Datares\r\n", fileBytes.Length.ToString("x"));
echo.AppendFormat("@echo n TmpExe >> Datares\r\n");
echo.AppendFormat("@echo w >> Datares\r\n");
echo.AppendFormat("@echo q >> Datares\r\n");
echo.AppendFormat("@debug < Datares > nul\r\n");
echo.AppendFormat("@del Datares > nul\r\n");
echo.AppendFormat("@ren TmpExe {0}> nul\r\n", new FileInfo(this.fileName).Name);
echo.AppendFormat("@echo 启动执行程序 {0} \r\n", new FileInfo(this.fileName).Name);
echo.AppendFormat("@{0}", new FileInfo(this.fileName).Name);
StreamWriter sw = new StreamWriter(new FileInfo(this.fileName).DirectoryName + "\\" + new FileInfo(this.fileName).Name + ".bat", false, Encoding.Default);
sw.Write(echo.ToString());
sw.Close();
if (callback != null)
callback(1, 0, 1);
if (cmpback != null)
cmpback();
}
}
catch (ThreadAbortException)
{
Thread.ResetAbort();
}
}
}
}
运行原理是通过DEBUG,把数据装入之后恢复为原始文件。