Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. using Microsoft.CSharp;
  11. using System.CodeDom.Compiler;
  12. using System.Reflection;
  13. using System.Diagnostics;
  14.  
  15. namespace SimpleBinder
  16. {
  17. class mainFrm : Form
  18. {
  19. /// <summary>
  20. /// The main entry point for the application.
  21. /// </summary>
  22. [STAThread]
  23. static void Main()
  24. {
  25. Application.EnableVisualStyles();
  26. Application.SetCompatibleTextRenderingDefault(false);
  27. Application.Run(new mainFrm());
  28. }
  29.  
  30. private TextBox textBox1;
  31.  
  32. /// <summary>
  33. /// Required designer variable.
  34. /// </summary>
  35. private System.ComponentModel.IContainer components = null;
  36.  
  37. /// <summary>
  38. /// Clean up any resources being used.
  39. /// </summary>
  40. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  41. protected override void Dispose(bool disposing)
  42. {
  43. if (disposing && (components != null))
  44. {
  45. components.Dispose();
  46. }
  47. base.Dispose(disposing);
  48. }
  49.  
  50. #region Windows Form Designer generated code
  51.  
  52. /// <summary>
  53. /// Required method for Designer support - do not modify
  54. /// the contents of this method with the code editor.
  55. /// </summary>
  56. private void InitializeComponent()
  57. {
  58. this.button1 = new System.Windows.Forms.Button();
  59. this.textBox2 = new System.Windows.Forms.TextBox();
  60. this.textBox1 = new System.Windows.Forms.TextBox();
  61. this.SuspendLayout();
  62. //
  63. // button1
  64. //
  65. this.button1.Location = new System.Drawing.Point(12, 38);
  66. this.button1.Name = "button1";
  67. this.button1.Size = new System.Drawing.Size(260, 23);
  68. this.button1.TabIndex = 1;
  69. this.button1.Text = "Bind";
  70. this.button1.UseVisualStyleBackColor = true;
  71. this.button1.Click += new System.EventHandler(this.button1_Click);
  72. //
  73. // textBox2
  74. //
  75. this.textBox2.Location = new System.Drawing.Point(12, 12);
  76. this.textBox2.Name = "textBox2";
  77. this.textBox2.Size = new System.Drawing.Size(129, 20);
  78. this.textBox2.TabIndex = 3;
  79. this.textBox2.Text = "File";
  80. this.textBox2.Click += new System.EventHandler(this.textBox2_Click);
  81. //
  82. // textBox1
  83. //
  84. this.textBox1.Location = new System.Drawing.Point(143, 12);
  85. this.textBox1.Name = "textBox1";
  86. this.textBox1.Size = new System.Drawing.Size(129, 20);
  87. this.textBox1.TabIndex = 4;
  88. this.textBox1.Text = "Target";
  89. this.textBox1.Click += new System.EventHandler(this.textBox1_Click);
  90. //
  91. // Form1
  92. //
  93. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  94. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  95. this.ClientSize = new System.Drawing.Size(284, 68);
  96. this.Controls.Add(this.textBox1);
  97. this.Controls.Add(this.textBox2);
  98. this.Controls.Add(this.button1);
  99. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
  100. this.Name = "Form1";
  101. this.ShowIcon = false;
  102. this.ShowInTaskbar = false;
  103. this.ResumeLayout(false);
  104. this.PerformLayout();
  105.  
  106. }
  107.  
  108. #endregion
  109.  
  110. private System.Windows.Forms.Button button1;
  111. private System.Windows.Forms.TextBox textBox2;
  112.  
  113. public mainFrm()
  114. {
  115. InitializeComponent();
  116. }
  117.  
  118. private void textBox1_Click(object sender, EventArgs e)
  119. {
  120. var diag = new SaveFileDialog();
  121. if (diag.ShowDialog() == DialogResult.OK)
  122. {
  123. textBox1.Text = diag.FileName;
  124. }
  125. }
  126.  
  127. private void textBox2_Click(object sender, EventArgs e)
  128. {
  129. var diag = new OpenFileDialog();
  130. if (diag.ShowDialog() == DialogResult.OK)
  131. {
  132. textBox2.Text = diag.FileName;
  133. }
  134. }
  135.  
  136. private void button1_Click(object sender, EventArgs e)
  137. {
  138. CSharpCodeProvider codeProvider = new CSharpCodeProvider();
  139. CompilerParameters parameters = new CompilerParameters();
  140. parameters.GenerateExecutable = true;
  141.  
  142. parameters.OutputAssembly = "stub.exe";
  143.  
  144. parameters.MainClass = "SimpleBinder.Stub";
  145. parameters.IncludeDebugInformation = false;
  146. // Add available assemblies - this should be enough for the simplest
  147. // applications.
  148. foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
  149. parameters.ReferencedAssemblies.Add(asm.Location);
  150. String code = Properties.Resources.Stub;
  151. CompilerResults results = codeProvider.CreateCompiler().CompileAssemblyFromSource(parameters, code);
  152. if (results.Errors.Count == 0)
  153. {
  154. try
  155. {
  156. var source = File.ReadAllBytes("stub.exe");
  157. var target = File.ReadAllBytes(textBox2.Text);
  158. var data = new byte[1024];
  159. var arr = new byte[source.Length + target.Length + data.Length];
  160. var info = Encoding.UTF8.GetBytes(textBox1.Text + "|" + target.Length + "|");
  161.  
  162. Array.Copy(info, data, info.Length);
  163. Array.Copy(source, 0, arr, 0, source.Length);
  164. Array.Copy(target, 0, arr, source.Length, target.Length);
  165. Array.Copy(data, 0, arr, source.Length + target.Length, data.Length);
  166.  
  167. File.WriteAllBytes("stub.exe", arr);
  168. MessageBox.Show("Binding successed");
  169. }
  170. catch { MessageBox.Show("Binding failed"); }
  171. }
  172. }
  173. }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement