[转]将C#程序嵌入资源中(C# 调用嵌入资源的EXE文件方法)

1. 我们有一个test.exe的WinForm程序,这是我们要加壳的目标程序。
2. 新建一个WinForm工程,删除Form1,然后新建一个类。如下。
3. 将test.exe 拷贝到该工程目录,作为嵌入式资源。
编译后的程序会自动将资源中的目标文件加载运行。继续发挥一下,我们可以增加启动密码;将目标程序进

代码
1 using System;
2  using System.Windows.Forms;
3  using System.Resources;
4  using System.Reflection;
5  using System.IO;
6 namespace test
7 {
8 staticclass Program
9 {
10 [STAThread]
11 staticvoid Main(string[] args)
12 {
13 Stream stream = Assembly. GetExecutingAssembly_r(). GetManifestResourceStream_r("test.Code.exe");
14 byte[] bs =newbyte[stream.Length];
15 stream.Read(bs, 0, (int)stream.Length);
16 Assembly asm = Assembly.Load(bs);
17 MethodInfo info = asm.EntryPoint;
18 ParameterInfo[] parameters = info. GetParameters_r();
19 if ((parameters !=null) && (parameters.Length >0))
20 info.Invoke(null, (object[])args);
21 else
22 info.Invoke(null, null);
23 }
24 }
25 }
26

---------------------------------------------------------------------------------------------------

代码
1 Stream streamObj =this. GetType_r().Assembly. GetManifestResourceStream_r("Test_ExeInResource.AllTest.exe");
2
3 byte[] b =newbyte[streamObj.Length];
4 streamObj.Read(b, 0, b.Length);
5
6 Assembly a = Assembly.Load(b);
7 Type[] mytypes = a. GetTypes_r();
8 BindingFlags flags = (BindingFlags.NonPublic | BindingFlags.Public |
9 BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);
10
11 foreach(Type t in mytypes)
12 {
13 MethodInfo[] mi = t. GetMethods_r(flags);
14 Object obj = Activator.CreateInstance(t);
15
16 foreach(MethodInfo m in mi)
17 {
18 if (m.Name =="Main")
19 {
20 MainDelegate md = (MainDelegate)Delegate.CreateDelegate(typeof(MainDelegate), m);
21 md.BeginInvoke(null, null);
22 }
23 }
24 }
25

 

解决

 

posted @ 2010-03-31 13:12  root7  阅读(504)  评论(0编辑  收藏  举报