可以';找不到具有DllImport的VSIX dll
本文关键字:DllImport VSIX dll 找不到 可以 | 更新日期: 2023-09-27 18:26:29
我有一个VSIX扩展,它依赖于从非托管DLL部署的代码。我在VSIX中包含了DLL,并用zip程序打开了VSIX,以确认它已正确部署。但是,当我使用DllImport属性时,.NET Framework声称找不到它。如何从VSIX中打包的DLL中导入函数?
我不知道这里出了什么问题,但我重新安装了Windows和Visual Studio,没有对项目进行任何更改,现在一切都很好。我在为其他应用程序查找DLL时遇到了一些其他问题,我想它们是相关的,我一定只是搞砸了一些设置。
Windows无法打开嵌入压缩.zip
中的DLL文件,因此您必须将其解压缩并放在有权写入的文件夹中。
.NET Framework将在%LocalAppData%
中查找DLL的路径,因此在那里解压缩DLL是合理的。
我过去常常在看似随机的情况下遇到虚假的包加载失败。这些问题主要影响由多个DLL文件组成的扩展。通过将[ProvideBindingPath]
属性应用于扩展中提供的主Package
,我最终解决了这些问题。
您需要在项目中包含属性的源。
/***************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
This code is licensed under the Visual Studio SDK license terms.
THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
***************************************************************************/
using System;
using System.Text;
namespace Microsoft.VisualStudio.Shell
{
/// <summary>
/// This attribute registers a path that should be probed for candidate assemblies at assembly load time.
///
/// For example:
/// [...'VisualStudio'10.0'BindingPaths'{5C48C732-5C7F-40f0-87A7-05C4F15BC8C3}]
/// "$PackageFolder$"=""
///
/// This would register the "PackageFolder" (i.e. the location of the pkgdef file) as a directory to be probed
/// for assemblies to load.
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public sealed class ProvideBindingPathAttribute : RegistrationAttribute
{
/// <summary>
/// An optional SubPath to set after $PackageFolder$. This should be used
/// if the assemblies to be probed reside in a different directory than
/// the pkgdef file.
/// </summary>
public string SubPath { get; set; }
private static string GetPathToKey(RegistrationContext context)
{
return string.Concat(@"BindingPaths'", context.ComponentType.GUID.ToString("B").ToUpperInvariant());
}
public override void Register(RegistrationContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
using (Key childKey = context.CreateKey(GetPathToKey(context)))
{
StringBuilder keyName = new StringBuilder(context.ComponentPath);
if (!string.IsNullOrEmpty(SubPath))
{
keyName.Append("''");
keyName.Append(SubPath);
}
childKey.SetValue(keyName.ToString(), string.Empty);
}
}
public override void Unregister(RegistrationContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
context.RemoveKey(GetPathToKey(context));
}
}
}