Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't find VSIX dlls with DllImport

I have a VSIX extension which depends on code deployed from an unmanaged DLL. I've included the DLL with the VSIX, and I cracked open the VSIX with a zip program to confirm that it is deployed correctly. However, when I use the DllImport attribute, the .NET Framework claims that it can't find it. How can I import functions from a DLL packaged inside my VSIX?

like image 876
Puppy Avatar asked Aug 03 '13 18:08

Puppy


3 Answers

I dunno what is going wrong here, but I reinstalled Windows and Visual Studio, made no changes to the project, and now everything is fine. I had some other issues with finding DLLs for other applications, and I guess they were related, I must have just screwed up some setting.

like image 74
Puppy Avatar answered Nov 11 '22 05:11

Puppy


Windows is unable to open DLL files embedded into compressed .zip, so you will have to unpack it and put in a folder where you have the access to write to.

The .NET Framework will look for the paths of your DLLs in %LocalAppData%, so it is reasonable to unpack your DLL there.

like image 28
Sergey K. Avatar answered Nov 11 '22 04:11

Sergey K.


I used to get spurious package load failures in seemingly random situations. The problems primarily affected extensions comprised of more than one DLL file. I finally got them resolved by applying the [ProvideBindingPath] attribute to the main Package provided in the extension.

You'll need to include 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" (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));
        }
    }
}
like image 1
Sam Harwell Avatar answered Nov 11 '22 03:11

Sam Harwell