我有一个VSIX扩展,它依赖于从非托管DLL部署的代码。我已经将VSIX包含在DLL中了,我用zip程序破解了VSIX以确认它已正确部署。但是,当我使用DllImport属性时,.NET Framework声称它无法找到它。如何从VSIX中打包的DLL中导入函数?
答案 0 :(得分:3)
我不知道这里出了什么问题,但我重新安装了Windows和Visual Studio,没有对项目进行任何更改,现在一切都很好。我在为其他应用程序寻找DLL时遇到了一些其他问题,我猜它们是相关的,我必须搞砸了一些设置。
答案 1 :(得分:2)
Windows无法打开嵌入到压缩.zip
中的DLL文件,因此您必须将其解压缩并放入您有权写入的文件夹中。
.NET Framework将在%LocalAppData%
中查找DLL的路径,因此在那里解压缩DLL是合理的。
答案 2 :(得分:1)
我曾经在看似随机的情况下得到虚假的包加载失败。这些问题主要影响由多个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));
}
}
}