简体   繁体   English

在运行时引用.dll?

[英]Reference .dlls on runtime?

If you observe the following picture, my 'RSR.exe' application runs perfectly fine as the two .dlls are located on the same file-path; 如果您观察到以下图片,我的“RSR.exe”应用程序运行完全正常,因为两个.dll位于同一文件路径上; however, if those 2 .dlls are not located on the same file path/directory as the RSR.exe, it will result in a crash/'program has stopped working'. 但是,如果这两个.dll不与RSR.exe位于同一文件路径/目录中,则会导致崩溃/'程序已停止工作'。

阅读上述评论

Here is the following piece of code I am trying to use with no success. 以下是我试图使用的以下代码,但没有成功。

public static MainForm _mainForm;

    static string subPath = @"C:\Users\Public\Documents\RSR";

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        if (!Directory.Exists(subPath))
        {
            Directory.CreateDirectory(subPath);

            if (IntPtr.Size == 4)
            {
                File.WriteAllBytes("C:\\Users\\Public\\Documents\\RSR\\MouseKeyboardActivityMonitor.dll", Properties.Resources.MouseKeyboardActivityMonitor);

                File.WriteAllBytes("C:\\Users\\Public\\Documents\\RSR\\WindowsFormsAero.dll", Properties.Resources.WindowsFormsAero);
            }
            else
            {
                File.WriteAllBytes("C:\\Users\\Public\\Documents\\RSR\\MouseKeyboardActivityMonitor.dll", Properties.Resources.MouseKeyboardActivityMonitor1);

                File.WriteAllBytes("C:\\Users\\Public\\Documents\\RSR\\WindowsFormsAero.dll", Properties.Resources.WindowsFormsAero);
            }
        }

        AssemblyName asm1 = AssemblyName.GetAssemblyName(@"C:\Users\Public\Documents\RSR\MouseKeyboardActivityMonitor.dll");
        Assembly.Load(asm1);

        AssemblyName asm2 = AssemblyName.GetAssemblyName(@"C:\Users\Public\Documents\RSR\WindowsFormsAero.dll");
        Assembly.Load(asm2);

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        using (_mainForm = new MainForm())
        {
            Application.Run(_mainForm);
        }

        Cursor normalCursor1 = new Cursor(new System.IO.MemoryStream(Properties.Resources.cursor_normal));
        SetSystemCursor(normalCursor1.Handle, 32512);
    }

Here is the following picture of the crash information when the 2 .dlls are not located within the same directory/path. 以下是2 .dll不在同一目录/路径中时的崩溃信息图。

阅读上面的评论

Any assistance would be appreciated. 任何援助将不胜感激。

Thanks. 谢谢。

If you must package your DLL as embedded resource you can load it through dynamic assembly resolve. 如果必须将DLL打包为嵌入式资源,则可以通过动态程序集解析来加载它。 Your code must run permission to load as well. 您的代码也必须运行加载权限。

public static MainForm _mainForm;
{ 
  //Add this in your main initialization   
  AppDomain.CurrentDomain.AssemblyResolve+=new ResolveEventHandler(CurrentDomain_AssemblyResolve);
}

private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    if (args.Name.Contains("FullNameSpace.MouseKeyboardActivityMonitor"))
    {
        return Assembly.Load(Properties.Resources.MouseKeyboardActivityMonitor);
    }

    if (args.Name.Contains("FullNameSpace.MouseKeyboardActivityMonitor"))
    {
        return Assembly.Load(Properties.Resources.WindowsFormsAero);
    }

    return null;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM