Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

embedding-c-libraryexe-inside-net-assembly #3

Open
bugbit opened this issue Jun 9, 2019 · 0 comments
Open

embedding-c-libraryexe-inside-net-assembly #3

bugbit opened this issue Jun 9, 2019 · 0 comments

Comments

@bugbit
Copy link
Owner

bugbit commented Jun 9, 2019

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace MyApplication
{
static public class NativeMethods
{
[DllImport("kernel32.dll")]
public static extern IntPtr LoadLibrary(string dllToLoad);

    [DllImport("TestLibrary.dll", 
    CallingConvention = CallingConvention.StdCall)]
    public static extern IntPtr GetString();
}

public static class CommonUtils
{
    /// <summary>
    /// Loads given unmanaged library from the assembly.
    /// This allows to embed .dll file as resource inside assembly.
    /// Example: 
    /// string tempDllPath = LoadUnmanagedLibraryFromResource( Assembly.GetExecutingAssembly(),
    ///                                                         "PInvokeTest.TestLibrary.dll", 
    ///                                                         "TestLibrary.dll");
    /// </summary>
    /// <param name="assembly"></param>
    /// <param name="libraryResourceName"></param>
    /// <param name="libraryName"></param>
    /// <returns></returns>
    public static string LoadUnmanagedLibraryFromResource(Assembly assembly, 
        string libraryResourceName, 
        string libraryName)
    {
        string tempDllPath = string.Empty;
        using (Stream s = assembly.GetManifestResourceStream(libraryResourceName))
        {
            byte[] data = new BinaryReader(s).ReadBytes((int)s.Length);

            string assemblyPath = Path.GetDirectoryName(assembly.Location);
            tempDllPath = Path.Combine(assemblyPath, libraryName);
            File.WriteAllBytes(tempDllPath, data);

        }

        NativeMethods.LoadLibrary(libraryName);
        return tempDllPath;
    }
}
class Program
{
    static void Main(string[] args)
    {
        string resourceName = "MyApplication.TestLibrary.dll";
        string libraryName = "TestLibrary.dll";

        // create and load library from the resource
        string tempDllPath = CommonUtils.LoadUnmanagedLibraryFromResource(Assembly.GetExecutingAssembly(),
            resourceName, 
            libraryName);
        // invoke native library function
        string output = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(NativeMethods.GetString());

        Console.WriteLine(output);
         
    }
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant