none
Load C++ DLL from C# Excel AddIn RRS feed

  • Question

  • Hi together,

    i implemented an Application based AddIn for Excel and want to work with objects defined in my C++ project.

    To do this i created a new C++ project with /clr -option with calls the unmanaged C++ project. In my console application it worked after I copied the unmanaged DLL to the bin folder.

    But in my addin it doesn´t work. The visual studio traces this Output:

    Eine Ausnahme (erste Chance) des Typs "System.IO.FileNotFoundException" ist in Microsoft.Office.Tools.Common.Implementation.dll aufgetreten.
    Eine Ausnahme (erste Chance) des Typs "System.Reflection.TargetInvocationException" ist in Microsoft.Office.Tools.Common.Implementation.dll aufgetreten.

    (These are german error messages, I´m sorry for that...)

    Is it possible to use the DLL this way? Or do i have to load the DLL manually?

    Thanks in advance!

    Matthias

    Monday, December 12, 2011 11:46 AM

Answers

All replies

  • Hi Matthias,

    Thanks for your post.

    Please check the following threads  to see if they are helps:

    Unmanaged DLL not found from a Word VSTO Add-In

    And

    Excel Add-in using VSTO 

    I hope this helps.


    Calvin Gao[MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, December 13, 2011 8:28 AM
    Moderator
  • Hi,

    thanks for your hints!

    I added this code, now everything works:

    public class NativeMethods
      {
        [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool SetDllDirectory(string lpPathName);
    
        public void SetDllDir(string path)
        {
          SetDllDirectory(path);
        }
      }
    
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
          var dll = Assembly.GetExecutingAssembly().GetName();
          UriBuilder uri = new UriBuilder(dll.CodeBase);
          string path = Uri.UnescapeDataString(uri.Path);
          var dllPath = Path.GetDirectoryName(path);
    
          var native = new NativeMethods();
    
          native.SetDllDir(dllPath);
        }
    

    Thanks a lot!

    Matthias

    Tuesday, December 13, 2011 10:28 AM