Cannot find VSIX DLL with DllImport

I have a VSIX extension, which depends on code deployed from an unmanaged DLL. I enabled the DLL with VSIX and I hacked VSIX with a zip program to confirm that it was deployed correctly. However, when I use the DllImport attribute, the .NET Framework claims that it cannot find it. How to import functions from a DLL packaged inside my VSIX?

+6
source share
3 answers

I do not know what is wrong here, but I reinstalled Windows and Visual Studio, did not make changes to the project, and now everything is in order. I had some other problems finding DLLs for other applications, and I suppose they were related, I must have just messed up some settings.

+3
source

Windows cannot open the DLL files embedded in the compressed .zip , so you have to unzip it and put it in the folder in which you have write access.

The .NET Framework will look for the paths of your DLLs in %LocalAppData% , so it’s wise to unzip your DLL there.

+2
source

I used to get false packet loading crashes in seemingly random situations. The problems primarily concerned extensions consisting of more than one DLL file. I finally resolved them by applying the [ProvideBindingPath] attribute to the main Package specified in the extension.

You will need to specify the source for the attribute in your project.

 /*************************************************************************** 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" (ie 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)); } } } 
+1
source

Source: https://habr.com/ru/post/950973/


All Articles