ExcelDnaPackNativeLibraryDependencies not expected result

601 views
Skip to first unread message

Jorrit-Jaap de Jong

unread,
Nov 10, 2023, 9:46:35 AM11/10/23
to Excel-DNA
Hi All,

To test how things work in Excel DNA I created a dummy c++ library wrapped in c# and am now trying to call in Excel. After compiling the Excel function didn't work as hoped. Instead resulting in this error

>> The type initializer for 'QcdPINVOKE' threw an exception.

However, if the c++ adn c# dlls are copied in the same folder as the generated xll the function does work. Unpacking the xll shows that the dlls are not being packed as hoped.

In the project file we have;

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework>
<ExcelDnaCreate32BitAddIn>false</ExcelDnaCreate32BitAddIn>
<ExcelDnaPackNativeLibraryDependencies>true</ExcelDnaPackNativeLibraryDependencies>
<ExcelDnaPackManagedDependencies>true</ExcelDnaPackManagedDependencies>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="qcd_windows" Version="0.1.73" />
<PackageReference Include="ExcelDna.AddIn" Version="1.6.0" />
</ItemGroup>

</Project>

This based on a discussion in this group here; https://groups.google.com/g/exceldna/c/xZHE2SouxXw 

The deps.json does list the two dlls that are required. For completeness the json is pasted bellow.

Can somebody help me with the correct configuration to pack these two dlls?

Regards,

Jorrit

{
  "runtimeTarget": {
    "name": ".NETCoreApp,Version=v6.0",
    "signature": ""
  },
  "compilationOptions": {},
  "targets": {
    ".NETCoreApp,Version=v6.0": {
      "ExcelDemoUDF/1.0.0": {
        "dependencies": {
          "ExcelDna.AddIn": "1.6.0",
          "qcd_windows": "0.1.73"
        },
        "runtime": {
          "ExcelDemoUDF.dll": {}
        }
      },
      "ExcelDna.AddIn/1.6.0": {
        "dependencies": {
          "ExcelDna.Integration": "1.6.0"
        }
      },
      "ExcelDna.Integration/1.6.0": {
        "runtime": {
          "lib/net6.0-windows7.0/ExcelDna.Integration.dll": {
            "assemblyVersion": "1.1.0.0",
            "fileVersion": "1.6.0.0"
          }
        }
      },
      "qcd_windows/0.1.73": {
        "runtimeTargets": {
          "runtimes/win10-x64/native/Qcd.dll": {
            "rid": "win10-x64",
            "assetType": "native",
            "fileVersion": "0.0.0.0"
          },
          "runtimes/win10-x64/native/Qcd_csharp.dll": {
            "rid": "win10-x64",
            "assetType": "native",
            "fileVersion": "0.0.0.0"
          }
        }
      }
    }
  },
  "libraries": {
    "ExcelDemoUDF/1.0.0": {
      "type": "project",
      "serviceable": false,
      "sha512": ""
    },
    "ExcelDna.AddIn/1.6.0": {
      "type": "package",
      "serviceable": true,
      "sha512": "sha512-A8fzWI++Gf6g4RQGbm5wkFu0w1Xqu7wcds/XhcLi6eELSc6Y0AuPx889V6iuC/zLAetFo9xEVOjjSc4nmJ1o8w==",
      "path": "exceldna.addin/1.6.0",
      "hashPath": "exceldna.addin.1.6.0.nupkg.sha512"
    },
    "ExcelDna.Integration/1.6.0": {
      "type": "package",
      "serviceable": true,
      "sha512": "sha512-2TMP1gNW5SUrXS3xD6ZauY/I9kgJZlKTi2Fz8clb78yIaS6BJPL3/PbqBXNzeVEBJ/HxKj/8sb9CEpghn8DyVA==",
      "path": "exceldna.integration/1.6.0",
      "hashPath": "exceldna.integration.1.6.0.nupkg.sha512"
    },
    "qcd_windows/0.1.73": {
      "type": "package",
      "serviceable": true,
      "sha512": "sha512-02J0MpLYTIrawIMClxmXRy9/6VXf0HYv1/4irz6a7iQ9OQonpM+L9JxK6SC4YsBwAJtQt27sPygz0v7+xSAlyQ==",
      "path": "qcd_windows/0.1.73",
      "hashPath": "qcd_windows.0.1.73.nupkg.sha512"
    }
  }
}

Govert van Drimmelen

unread,
Nov 10, 2023, 11:30:53 AM11/10/23
to exce...@googlegroups.com
Can you check with the latest Excel-DNA version 1.7.0-rcfinal ?

-Govert

--
You received this message because you are subscribed to the Google Groups "Excel-DNA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to exceldna+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/exceldna/3e481d10-d590-4e05-af76-d3c683f4d85an%40googlegroups.com.

Jorrit-Jaap de Jong

unread,
Nov 13, 2023, 7:42:29 AM11/13/23
to Excel-DNA
Hi Govert,

Thanks for the reply. Using 1.7.0-rcfinal the packing is indeed happening. If I use the unpack tool


to check I see all the files are there. Only my .net assembly is marked as Native_Library not Assembly. Can this cause the error I am getting;

Loading ExcelDna.ManagedHost failed L 0x80070005

From one of you previous post I tried using;

<ExcelDnaUnpack>true</ExcelDnaUnpack>

This "works", but now the dlls are not packages and thus don't show up in the unpacked version when installing the xll. Is there an additional flag that can be used to force adding my dlls?

Regards,

Jorrit



Op vrijdag 10 november 2023 om 17:30:53 UTC+1 schreef Govert van Drimmelen:

Govert van Drimmelen

unread,
Nov 13, 2023, 9:05:37 AM11/13/23
to exce...@googlegroups.com

--------------------------------------------------
Excel-DNA is now registered on 
GitHub Sponsors.
Your small monthly contribution will directly fund further development.
-------------------------------------------------- 

 

Hi Jorrit,

 

> my .net assembly is marked as Native_Library not Assembly

 

This might be a problem.

Make sure that you are not compiling your .NET assembly with a RuntimeIdentifier like x64.

I think that makes it compile it as a mixed assembly, instead of a manage .NET assembly.

You can look at the build output to see how what is packed:

 

1>Target ExcelDnaPack:

1>  ExcelDnaPack: bin\Debug\net6.0-windows\TestUnpackNative-AddIn64.dna -> bin\Debug\net6.0-windows\publish\TestUnpackNative-AddIn64-packed.xll

1>  PackExcelAddIn: Running PackExcelAddIn Task

1>  PackExcelAddIn: Using base add-in bin\Debug\net6.0-windows\TestUnpackNative-AddIn64.xll

1>  PackExcelAddIn:   ~~> ExternalLibrary path TestUnpackNative.dll resolved to bin\Debug\net6.0-windows\TestUnpackNative.dll.

1>  PackExcelAddIn:   ->  Updating resource: Type: ASSEMBLY_LZMA, Name: SQLITEPCLRAW.BATTERIES_V2, Source: Managed deps.json, Length: 2043

1>  PackExcelAddIn:   ->  Updating resource: Type: ASSEMBLY_LZMA, Name: TESTUNPACKNATIVE, Length: 1662

1>  PackExcelAddIn:   ->  Updating resource: Type: DNA, Name: __MAIN__, Length: 583

1>  PackExcelAddIn:   ->  Updating resource: Type: ASSEMBLY_LZMA, Name: SQLITEPCLRAW.PROVIDER.E_SQLITE3, Source: Managed deps.json, Length: 11783

1>  PackExcelAddIn:   ->  Updating resource: Type: ASSEMBLY_LZMA, Name: SQLITEPCLRAW.CORE, Source: Managed deps.json, Length: 16456

1>  PackExcelAddIn:   ->  Updating resource: Type: NATIVE_LIBRARY_LZMA, Name: E_SQLITE3.DLL, Source: Native deps.json, Length: 739835

1>  PackExcelAddIn: Completed Packing bin\Debug\net6.0-windows\publish\TestUnpackNative-AddIn64-packed.xll.

 

 

<ExcelDnaUnpack>true</ ExcelDnaUnpack> doesn’t build the list of all the dependencies that you need for the output in ‘publish’ to work, so you probably need some managed dependencies and the files from the “runtimes” directory, and possibly the .deps.json file too. Or you might get lucky just copying the right native libraries into the publish directory directly. But I don’t think it’s a scenario we’re trying to cater for automatically.

 

-Govert

Jorrit-Jaap de Jong

unread,
Nov 13, 2023, 12:09:48 PM11/13/23
to Excel-DNA
Hi Govert,

Everything looks in oder now. The two dll are packed into the XLL. The runtime identifier is set to "Any CPU". The function call for Excel works in Unit Test when called directly. Excel does work when using <ExcelDnaUnpack>true</ExcelDnaUnpack> and copying the dlls into the folder. When everything is packed into XLL (with or without compressiong). There is still this error on loading the XLL in Excel;


Loading ExcelDna.ManagedHost failed L 0x80070005

Do you have any ideas what I could try to resolve this issue? It would be great to pack everything into the XLL and have just the one file to deal with.

Regards,

Jorrit

Op maandag 13 november 2023 om 15:05:37 UTC+1 schreef Govert van Drimmelen:

Govert van Drimmelen

unread,
Nov 13, 2023, 4:08:20 PM11/13/23
to Excel-DNA
Hi Jorrit,

That error code means 'Access Denied'.
The mechanism used to support packed add-ins with .NET 6, is to extract some files (including the native ones on demand) to a temp directory and load from there.
The per add-in temp directories are organized under %Temp%\ExcelDna.Host - typically that would be  C:\Users\<UserName>\AppData\Local\Temp\ExcelDna.Host
You can load the add-in, then look for a directory made at a recent time. I see a new Guid-named subdirectory under ExcelDna.Host with (at least) two files:
* ExcelDna.ManagedHost.dll
* ExcelDna.Host.runtimeconfig.json

Do you see these files?
Could it be that your Excel process cannot create directories or files under your %Temp% directory?
You might need to check the temp environment variable and somehow check for permissions there.

-Govert

Jorrit-Jaap de Jong

unread,
Nov 14, 2023, 3:46:14 AM11/14/23
to Excel-DNA
Hi Govert,

Thanks for the suggestion. The folder is indeed created in the temp folder. There are two files there;
* ExcelDna.ManagedHost.dll
* ExcelDna.Host.runtimeconfig.json

The json has the runtime options, which in my case is net6.0.

Upon closing Excel the folder is neatly removed again from disk.

Should there be more files? 

Funny thing if I run Excel as admin the behaviour is same, but running Visual Studio as admin solves the issue. Now I am a bit puzzled how to distrubute the XLL to end-user machines.

Regards,

Jorrit

Op maandag 13 november 2023 om 22:08:20 UTC+1 schreef Govert van Drimmelen:

Govert van Drimmelen

unread,
Nov 14, 2023, 3:57:02 AM11/14/23
to exce...@googlegroups.com

Hi Jorrit,

 

Can you confirm that the simplest .NET 6 add-in gives the same error?

 

This project file:

 

<Project Sdk="Microsoft.NET.Sdk">

 

  <PropertyGroup>

    <TargetFramework>net6.0-windows</TargetFramework>

  </PropertyGroup>

 

  <ItemGroup>

    <PackageReference Include="ExcelDna.AddIn" Version="1.7.0-rcfinal" />

  </ItemGroup>

 

</Project>

 

This code:

 

namespace TestNet6

{

    public static class Functions

    {

        public static object SayHello() => "Hello World!";

Jorrit-Jaap de Jong

unread,
Nov 14, 2023, 4:25:52 AM11/14/23
to Excel-DNA
Hi Govert,

Yes, that gives same behavior.

Regards,

Jorrit

Op dinsdag 14 november 2023 om 09:57:02 UTC+1 schreef Govert van Drimmelen:

Govert van Drimmelen

unread,
Nov 14, 2023, 4:48:59 AM11/14/23
to exce...@googlegroups.com

Hi Jorrit,

 

  • To be clear – is it just the -packed add-in that gives the error?

 

  • What is the output of “dotnet –list-runtimes” ?

 

  • Is there another machine you can check on?

 

  • And does it work if you switch to .NET Framework?

<TargetFramework>net472</TargetFramework>

Jorrit-Jaap de Jong

unread,
Nov 14, 2023, 5:13:33 AM11/14/23
to Excel-DNA
Hi Govert,

Running the small example code you posted we have

> Error in packed and unpacked error.
-- It only works when complining in Visual Studio as normal user. Next open Visual Studio as admin to debug.

> dotnet --list-runtimes
*********************************************************************
** Visual Studio 2022 Developer PowerShell v17.7.6
** Copyright (c) 2022 Microsoft Corporation
**********************************************************************
PS C:\Users\<<user>>\source\repos\ExcelDemoUDF> dotnet --list-runtimes
Microsoft.AspNetCore.App 6.0.24 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.24 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.24 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.13 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

>Net472
Works as expected with all steps as normal user or as admin user.

Regards,

Jorrit

Op dinsdag 14 november 2023 om 10:48:59 UTC+1 schreef Govert van Drimmelen:

Govert van Drimmelen

unread,
Nov 14, 2023, 5:27:34 AM11/14/23
to exce...@googlegroups.com

Hi Jorrit,

 

Are you running any security software that might block files from that temp directory from being loaded?

 

Anything else that is unusual about the environment?

Jorrit-Jaap de Jong

unread,
Nov 15, 2023, 7:30:24 AM11/15/23
to Excel-DNA
Hi Govert,

As a test I switched of all scans for the folder 

>    C:\Users\<UserName>\AppData\Local\Temp\ExcelDna.Host

Than everything works as expected. Many thanks for your guidiance.

Regards,

Jorrit

Op dinsdag 14 november 2023 om 11:27:34 UTC+1 schreef Govert van Drimmelen:

Govert van Drimmelen

unread,
Nov 15, 2023, 7:59:19 AM11/15/23
to exce...@googlegroups.com

OK great – what security software was blocking this?

Jorrit-Jaap de Jong

unread,
Nov 15, 2023, 8:05:31 AM11/15/23
to Excel-DNA
Used  Add-MpPreference cmdlet in PoweShell to modifiy settings for Windows Defender. That did the trick.

Op woensdag 15 november 2023 om 13:59:19 UTC+1 schreef Govert van Drimmelen:

A Parmar

unread,
Jan 20, 2024, 5:06:11 AM1/20/24
to Excel-DNA
I'm seeing this same issue.

Hi Jorrit - What exclusions etc did you put using:

Used  Add-MpPreference cmdlet in PoweShell to modifiy settings for Windows Defender. That did the trick.

A Parmar

unread,
Jan 25, 2024, 4:54:58 AM1/25/24
to Excel-DNA
now the file ( ExcelDna.ManagedHost.dll )  is saving to the Temp directory  C:\Users\<UserName>\AppData\Local\Temp\ExcelDna.Host 

But error: Could not load file assembly .... Access is denied.

any ideas?

Jorrit-Jaap de Jong

unread,
Feb 4, 2024, 2:00:49 PM2/4/24
to Excel-DNA
Hi Parmar,

Sorry for the late replay. 

I think it was something like:

> Add-MpPreference -ExclusionPath 'C:\Users\<UserName>\AppData\Local\Temp\'

It was quite some time ago some I am not 100% sure. Since this issue others in my team we able to run the add-in without needing this exception so it could have been just on my machine.
Op donderdag 25 januari 2024 om 10:54:58 UTC+1 schreef A Parmar:

A Parmar

unread,
Feb 7, 2024, 9:22:11 AM2/7/24
to Excel-DNA
Hi, no worries, all resolved, I put some details here:
Reply all
Reply to author
Forward
0 new messages