2013-08-03 30 views
6

我有一個VSIX擴展,它取決於從非託管DLL部署的代碼。我已經將DLL包含在VSIX中,我用zip程序破解了VSIX,以確認它已正確部署。但是,當我使用DllImport屬性時,.NET Framework聲稱它無法找到它。我怎樣才能從我的VSIX打包的DLL中導入函數?使用DllImport找不到VSIX dll

+1

也許缺少路徑?這有幫助嗎? http://stackoverflow.com/a/10800260/71312 – Diryboy

+0

你確認了非託管dll被正確提取到擴展的安裝目錄?我把一個簡單的VSIX放在一起,它部署了一個調用非託管dll的shell包。我使用Content的構建操作將非託管dll添加到項目,並將其包含在VSIX中。它在調試和常規部署擴展中均可正確運行。 – WarrenG

+0

@WarrenG:我不知道那是哪裏。我爲我的DLL構建動作也是「內容」,我將它設置爲包含在VSIX中。 – Puppy

回答

3

我不知道這裏出了什麼問題,但我重新安裝了Windows和Visual Studio,沒有對該項目進行任何更改,現在一切正常。我還遇到其他一些與其他應用程序查找DLL有關的其他問題,我猜他們是相關的,我必須搞砸了一些設置。

2

Windows無法打開嵌入到壓縮的.zip中的DLL文件,因此您必須將其解壓縮並放入您有權訪問的文件夾中。

.NET Framework將在%LocalAppData%中查找DLL的路徑,因此在其中解壓DLL是合理的。

+0

Visual Studio將文件從VSIX中提取到安裝目錄中。 – Puppy

+0

你可以打開事件日誌並檢查你的包嘗試加載DLL的路徑嗎? https://en.wikipedia.org/wiki/Event_Viewer –

+0

這個工具可能會有所幫助:http://technet.microsoft.com/en-us/sysinternals/bb896645 –

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)); 
     } 
    } 
} 
+0

是的,但我不是在說在這裏加載軟件包。非託管DLL和包使用完全不同的機制。 – Puppy