用DllImport找不到VSIX dll

我有一个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 { ///  /// 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" (ie the location of the pkgdef file) as a directory to be probed /// for assemblies to load. ///  [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)] public sealed class ProvideBindingPathAttribute : RegistrationAttribute { ///  /// 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. ///  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)); } } }