Skip to content

Fix unhelpful error when P/Invoke references a nonexistent DLL#27352

Open
gvsrgh wants to merge 4 commits intoPowerShell:masterfrom
gvsrgh:fix/pinvoke-missing-dll-error-message
Open

Fix unhelpful error when P/Invoke references a nonexistent DLL#27352
gvsrgh wants to merge 4 commits intoPowerShell:masterfrom
gvsrgh:fix/pinvoke-missing-dll-error-message

Conversation

@gvsrgh
Copy link
Copy Markdown

@gvsrgh gvsrgh commented Apr 24, 2026

Fixes #26941

PR Summary

When calling a P/Invoke method via Add-Type that references a missing DLL,
PowerShell 7 threw an obscure ArgumentNullException:

Value cannot be null. (Parameter 'path1')

This was caused by NativeDllHandler in CorePsAssemblyLoadContext.cs calling
Path.Combine(folder, ...) where folder was null — because in-memory
assemblies compiled by Add-Type have an empty Location, causing
Path.GetDirectoryName("") to return null.

The fix returns IntPtr.Zero early when folder is null/empty, letting the CLR
fall through to its own resolution which correctly surfaces a DllNotFoundException:

Unable to load DLL 'NoSuch.dll' or one of its dependencies: The specified module could not be found. (0x8007007E)

PR Checklist

  • PR has a meaningful title
  • Summarized changes
  • Make sure all .h, .cpp, .cs, .ps1 and .psm1 files have the correct copyright header
  • This PR is ready to merge.

Breaking Changes

  • None

User-facing Changes

Testing - New and feature

  • Added a new test in test/powershell/Language/Interop/DotNet/DotNetInterop.Tests.ps1

Copilot AI review requested due to automatic review settings April 24, 2026 08:30
@gvsrgh gvsrgh requested a review from a team as a code owner April 24, 2026 08:30
@gvsrgh
Copy link
Copy Markdown
Author

gvsrgh commented Apr 24, 2026

@microsoft-github-policy-service agree

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes an unhelpful ArgumentNullException (path1) surfaced when invoking a P/Invoke method compiled via Add-Type that references a missing native DLL, allowing the CLR to report the correct DllNotFoundException instead.

Changes:

  • Prevent NativeDllHandler from calling Path.Combine() with a null/empty folder when the invoking assembly has an empty Location.
  • Add a Pester test validating that a missing P/Invoke DLL produces a DllNotFoundException mentioning the requested library name.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/System.Management.Automation/CoreCLR/CorePsAssemblyLoadContext.cs Early-return from NativeDllHandler when the assembly location directory is null/empty to avoid ArgumentNullException and allow normal unmanaged DLL resolution/failure.
test/powershell/Language/Interop/DotNet/DotNetInterop.Tests.ps1 Adds a regression test that exercises Add-Type + missing DllImport and asserts DllNotFoundException includes the missing DLL name.

Comment thread test/powershell/Language/Interop/DotNet/DotNetInterop.Tests.ps1 Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add-Type: Misleading / obscure error message reported by P/Invoke calls to methods from a nonexistent DLL

2 participants