0

I am using scitersharp with WinForms and trying to build a single standalone compiled c# executable. It's a simple "Hello World" app which loads an HTML file. Initially after compilation 3 DLLs (sciter.dll, sciter64.dll and SciterSharpWindows.dll) were being generated in the bin\Debug folder. So, i googled and tried to find a solution to embed those dll files into the executable. Here is what i tried:

  • Loading the DLL in project window in visual studio and setting the build option as "Embedded resource"

  • Add references to the resources but this method didn't worked and it's giving me the following error:

    `A reference to resource 'pathto\sciter.dll' could not be added. Please make sure the file is accesible and that it is a valid assembly or COM component`
    
  • Read the solutions from this stackoverflow question

  • Installed Cosutra.Fody package and rebuild the project. This time i managed to embed only the SciterSharpWindows.dll

Here is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sciter;
using SciterSharp;
using SciterSharp.WinForms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        // Create a class-accesible sciter control
        private SciterControl SciterElement;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Create a new instance of the sciter control
            SciterElement = new SciterControl();
            // Set the callback once the element is ready to be used
            SciterElement.HandleCreated += SciterControl1_HandleCreated;

            // Add the Sciter Control to the Form and fill it
            this.Controls.Add(SciterElement);
            SciterElement.Dock = DockStyle.Fill;
            
        }

        private void SciterControl1_HandleCreated(object sender, EventArgs e)
        {
            // Initialize with some HTML
            SciterElement.SciterWnd.LoadHtml(@"
                <div style='padding: 0px 8px;'>
                <h1>
                    Hello World
                </h1>
                </div>
            ");
        }
    }
}

Is there any way to embed these resources into the executable? Also, please let me know if i am missing something? Thank you in advance.

GAURAV KUMAR JHA
  • 190
  • 1
  • 11

1 Answers1

0

Here is a workaround maybe you can refer to.

First, add the dll to Reference and set Copy Local to False.

enter image description here

Then create a new project folder "libs", copy the referenced dll into this folder, and set Build Action to "Embedded Resources"

enter image description here

In the main() method, add code to handle the exception "dll not found".

static void Main()
{
    AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}
private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs e)
{
    //The namespace of the project is embeddll, and the embedded dll resources are in the libs folder, so the namespace used here is: embeddll.libs.
    string _resName = "embeddll.libs." + new AssemblyName(e.Name).Name + ".dll";
    using (var _stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(_resName))
    {
        byte[] _data = new byte[_stream.Length];
        _stream.Read(_data, 0, _data.Length);
        return Assembly.Load(_data);
    }
}
大陸北方網友
  • 3,696
  • 3
  • 12
  • 37