Skip to content

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

@mklement0

Description

@mklement0

Prerequisites

Steps to reproduce

  • Windows PowerShell provides a meaningful error message in response to attempts to call P/Invoke declarations compiled via Add-Type that mistakenly reference a nonexistent DLL ("Unable to load DLL 'NoSuch.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)")

  • In PowerShell Core, the following misleading / obscure error is reported instead:
    "Value cannot be null. (Parameter 'path1')"

# Try to call a function from a NONEXISTENT DLL via P/Invoke.
(Add-Type -ErrorAction Stop -NameSpace NS$PID -Name Aux -PassThru  @'
  [DllImport("NoSuch.dll", CharSet = CharSet.Unicode)]
  public static extern System.UInt16 Foo();
'@)::Foo()

Expected behavior

"Unable to load DLL 'NoSuch.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)"

Actual behavior

"Value cannot be null. (Parameter 'path1')"

That is, the inability to locate the referenced DLL is unexpectedly surfaced via implementation details, confusingly referring to a path1 parameter that is unrelated to the signature of the function being invoked.

Error details

Environment data

PowerShell 7.6.0-rc.1

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productNeeds-TriageThe issue is new and needs to be triaged by a work group.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions