新发的日常小实验——c#的.net工程(winform桌面窗体应用)如何将引用的dll嵌入到exe中

20 篇文章 16 订阅
5 篇文章 4 订阅

一、前言

最近在搞c#winform工程(.NET Framework),需要引用一个第三方dll,正常情况下,打包成.exe,会自动拷贝这个dll文件到exe所在的目录中,能不能将.dll嵌入到exe中呢?答案肯定是可以滴,下面就介绍做法。

二、嵌入dll到exe的具体步骤

1、libs文件夹

在工程目录中,新建一个libs文件夹,将你要引用的dll放进来。
在这里插入图片描述

2、添加dll引用,设置不复制

添加dll引用
在这里插入图片描述
打开属性窗口
在这里插入图片描述
在属性窗口中,对dll设置:复制本地 False
在这里插入图片描述

3、设置嵌入嵌入的资源

点击libs文件夹中的dll,在属性窗口中设置 生成操作: 嵌入的资源
在这里插入图片描述

4、在代码中动态处理dll加载

在程序入口处注册AppDomain.CurrentDomain.AssemblyResolve,处理dll的加载。
例:

using System;
using System.Reflection;
using System.Windows.Forms;

namespace winform1
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            //注册委托
            AppDomain.CurrentDomain.AssemblyResolve += CurrentDomainAssemblyResolve;


            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }


        /// <summary>
        /// 加载嵌入工程中的dll
        /// </summary>
        private static Assembly CurrentDomainAssemblyResolve(object sender, ResolveEventArgs e)
        {
            //项目的命名空间为winform1, 嵌入dll资源在libs文件夹下,所以这里用的命名空间为: winform1.libs.
            string dllName = "winform1.libs." + new AssemblyName(e.Name).Name + ".dll";
            using (var dllStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(dllName))
            {
                byte[] dllData = new byte[dllStream.Length];
                dllStream.Read(dllData, 0, dllData.Length);
                return Assembly.Load(dllData);
            }
        }
    }
}

三、发布exe,测试成功

发布成exe,我们可以使用ILSpy反编译一下exe,可以看到dll已经嵌入到exe中了。
在这里插入图片描述

注:关于ILSpy反编译,有兴趣的同学可以玩玩
ILSpy下载地址:https://github.com/icsharpcode/ILSpy/releases

  • 1
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
要在C# Winform嵌入基恩士应用程序exe,可以使用Process类来启动应用程序,并将其嵌入窗体。以下是实现步骤: 1. 打开Visual StudioWinform项目,并在工具箱添加一个Panel控件。 2. 在代码使用Process类的Start方法来启动基恩士应用程序exe,并将其嵌入到Panel控件。 3. 设置基恩士应用程序exe的属性和方法,以便与其进行交互。 以下是代码示例: ```csharp using System.Diagnostics; using System.Windows.Forms; namespace WinformApp { public partial class Form1 : Form { private Process process; public Form1() { InitializeComponent(); // 启动基恩士应用程序exe process = new Process(); process.StartInfo.FileName = "KeenShine.exe"; process.StartInfo.Arguments = ""; process.StartInfo.UseShellExecute = false; process.StartInfo.CreateNoWindow = true; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.Start(); // 将基恩士应用程序exe嵌入到Panel控件 process.WaitForInputIdle(); SetParent(process.MainWindowHandle, panel1.Handle); // 设置窗体大小和位置 process.StartInfo.WindowStyle = ProcessWindowStyle.Normal; process.WaitForInputIdle(); MoveWindow(process.MainWindowHandle, 0, 0, panel1.Width, panel1.Height, true); // 设置基恩士应用程序exe的属性和方法 process.StandardInput.WriteLine("Command1"); var result = process.StandardOutput.ReadLine(); // ... } protected override void OnFormClosing(FormClosingEventArgs e) { base.OnFormClosing(e); // 关闭基恩士应用程序exe process.Kill(); } [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); [DllImport("user32.dll")] private static extern bool MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint); } } ``` 在上面的代码,panel1是Winform的一个Panel控件,用来显示基恩士应用程序exe。你需要根据实际情况设置基恩士应用程序exe的属性和方法。需要注意的是,在关闭窗体时,需要使用Process类的Kill方法来关闭基恩士应用程序exe

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林新发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值