VSIX扩展使用第三方DLL无法加载其中一个依赖关系 [英] VSIX Extension uses 3rd party DLLs unable to load one of the dependency

查看:271
本文介绍了VSIX扩展使用第三方DLL无法加载其中一个依赖关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发VS2013的扩展。由于它将通过MSI进行安装,所以我将使用 ProvideBindingPath 属性将基本目录更改为安装文件夹。但是,在运行时加载的第三方dll参考并不是从探测路径中选择dll。它总是看Visual studio devenv.exe 文件夹。有没有办法强制dll查看我的安装文件夹。

 使用MD = Microsoft.VisualStudio.Modeling.Shell; 

MD.ProvideBindingPath(SubPath = @)]
public sealed class AutomationCommandPanePackage:Package
{

public AutomationCommandPanePackage()
{

string installationPath = HelperMethods.GetInstallationPath();

if(string.IsNullOrEmpty(HelperMethods.GetInstallationPath()))return;

//在运行时更改默认配置文件。
using(AutomationConfigurationManager.Change(installationPath,AutomationPro.config))
{
// Trace.WriteLine(string.Format(CultureInfo.CurrentCulture,输入构造函数:{0} ,this.ToString()));
}

装配a = Assembly.GetExecutingAssembly();
类型type = a.GetType(AutomationCommandPanePackage,true);
System.Reflection.MemberInfo info = type;
var attributes = info.GetCustomAttributes(true);

foreach(属性中的属性)
{
如果(attrib是MD.ProvideBindingPathAttribute)
{
((MD.ProvideBindingPathAttribute)attrib)。 SubPath = installationPath;
break;
}
}


解决方案

解决问题使用
LoadLibrary()

 系统。 Runtime.InteropServices; 

因为我要加载的DLL是一个COM迭代dll。

  public static class win32 
{
[DllImport(kernel32.dll)]
public static extern IntPtr LoadLibrary(string dllToLoad );
[DllImport(kernel32.dll)]
public static extern IntPtr GetProcAddress(IntPtr hModule,string procedureName); [DllImport(kernel32.dll)]
public static extern bool FreeLibrary(IntPtr hModule);
}

在package.cs中加载这样的程序集

  win32.LoadLibrary(Path.Combine(installationPath,apidsp_windows.dll)); 


I am developing an extension to VS2013. Since it will be installed through MSI, I am changing the base directory to installation folder using ProvideBindingPath attribute to package class. But the 3rd party dll reference which will be loaded in runtime is not picking dll from the probed path. Its always looking into Visual studio devenv.exe folder. Is there any way to force the dll to look into my installation folder.

using MD=Microsoft.VisualStudio.Modeling.Shell;

MD.ProvideBindingPath(SubPath = @"")]
public sealed class AutomationCommandPanePackage : Package
    { 

     public AutomationCommandPanePackage()        
     {

        string installationPath = HelperMethods.GetInstallationPath();

        if (string.IsNullOrEmpty(HelperMethods.GetInstallationPath())) return;

        // Change default config file at runtime.
        using (AutomationConfigurationManager.Change(installationPath, "AutomationPro.config"))
        {
            // Trace.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering constructor for: {0}", this.ToString()));
        }            

        Assembly a = Assembly.GetExecutingAssembly();
        Type type = a.GetType("AutomationCommandPanePackage", true);
        System.Reflection.MemberInfo info = type;
        var attributes = info.GetCustomAttributes(true);

        foreach (var attrib in attributes)
        {
            if (attrib is MD.ProvideBindingPathAttribute)
            {
                ((MD.ProvideBindingPathAttribute)attrib).SubPath = installationPath;
                break;
            }
        }            

解决方案

I resolved issue using LoadLibrary() from

System.Runtime.InteropServices; 

since my dll to be loaded is a COM iterop dll.

public static class win32 
{ 
[DllImport("kernel32.dll")] 
public static extern IntPtr LoadLibrary(string dllToLoad); 
[DllImport("kernel32.dll")] 
public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName); [DllImport("kernel32.dll")] 
public static extern bool FreeLibrary(IntPtr hModule);
}

in package.cs I loaded assembly like this

win32.LoadLibrary(Path.Combine(installationPath, "apidsp_windows.dll")); 

这篇关于VSIX扩展使用第三方DLL无法加载其中一个依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆