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

本文详细介绍如何使用C#在WinForm程序中嵌入另一个exe作为资源,并在运行时动态加载执行。通过修改主程序入口,可以实现目标程序的密码保护及更复杂的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 我们有一个test.exe的WinForm程序,这是我们要加壳的目标程序。
2. 新建一个WinForm工程,删除Form1,然后新建一个类。如下。
3. 将test.exe 拷贝到该工程目录,作为嵌入式资源。

ExpandedBlockStart.gif代码
using System;
using System.Windows.Forms;
using System.Resources;
using System.Reflection;
using System.IO;
namespace test
{
    
static class Program
    {
        [STAThread]
        
static void Main(string[] args)
        {
            Stream stream 
= Assembly. GetExecutingAssembly_r(). GetManifestResourceStream_r("test.Code.exe");
            
byte[] bs = new byte[stream.Length];
            stream.Read(bs, 
0, (int)stream.Length);
            Assembly asm 
= Assembly.Load(bs);
            MethodInfo info 
= asm.EntryPoint;
            ParameterInfo[] parameters 
= info. GetParameters_r();
            
if ((parameters != null&& (parameters.Length > 0))
                info.Invoke(
null, (object[])args);
            
else
                info.Invoke(
nullnull);
        }
    }
}

 

编译后的程序会自动将资源中的目标文件加载运行。继续发挥一下,我们可以增加启动密码;将目标程序进

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

ExpandedBlockStart.gif代码
Stream   streamObj   =   this. GetType_r().Assembly. GetManifestResourceStream_r("Test_ExeInResource.AllTest.exe");  
   
byte[]   b   =   new   byte[streamObj.Length];  
streamObj.Read(b,   
0,   b.Length);  
   
Assembly   a   
=   Assembly.Load(b);  
Type[]   mytypes   
=   a. GetTypes_r();  
BindingFlags   flags   
=   (BindingFlags.NonPublic   |   BindingFlags.Public   |    
BindingFlags.Static   
|   BindingFlags.Instance   |   BindingFlags.DeclaredOnly);  
   
foreach(Type   t   in   mytypes)  
{  
            MethodInfo[]   mi   
=   t. GetMethods_r(flags);  
            Object   obj   
=   Activator.CreateInstance(t);  
   
           
foreach(MethodInfo   m   in   mi)  
           {  
                  
if   (m.Name   ==   "Main")  
                 {  
                        MainDelegate   md   
=    (MainDelegate)Delegate.CreateDelegate(typeof(MainDelegate),   m);  
                       md.BeginInvoke(
null,   null);  
                }  
          }  
}

 

 

转载于:https://www.cnblogs.com/sofire/archive/2010/08/02/1790592.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值