question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot P/Invoke a function named "Process()"

See original GitHub issue

Steps to reproduce

Define the following function in C and compile as a DLL:

__declspec(dllexport) int Process()
{
    return 42;
}

Run the following in PowerShell with the path to the DLL in the DllImport:

$type=Add-Type -MemberDefinition @'
[DllImport("C:\\PathToRepro.dll")]
public static extern int Process();
'@ -Name TestProcess -PassThru

$type::Process()

Expected behavior

Function returns 42.

Actual behavior

The following error occurs: The result type 'System.Int32' of the dynamic binding produced by the object with type 'System.Management.Automation.PSObject' for the binder 'PSInvokeMember: static Process ver:0 args:0 constraints:<>' is not compatible with the result type 'System.Object' expected by the call site.

Changing the name of the function to something else such as Process2() works as expected.

I was not able to reproduce this when using a C# program instead of PowerShell.

Environment data

Name                           Value
----                           -----
PSVersion                      7.1.0
PSEdition                      Core
GitCommitId                    7.1.0
OS                             Microsoft Windows 10.0.20231
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:9

github_iconTop GitHub Comments

2reactions
SeeminglySciencecommented, Dec 1, 2020

Oh also probably a better repro:

class Test {
    static [int] Process() {
        return 10
    }
}

[Test]::Process()
1reaction
SeeminglySciencecommented, Nov 30, 2020

Looks like it’s any static method named either Process, or End (incidentally, not Begin because it has a void return) without arguments that returns a value type.

Easier repro:

Add-Type -TypeDefinition 'public class Test { public static int Process() => 10; }'
[Test]::Process()

I would guess that it’s caused by the special casing here:

https://github.com/PowerShell/PowerShell/blob/db94377b3a69511280d329e64375475f6d7513b8/src/System.Management.Automation/engine/runtime/Binding/Binders.cs#L6915-L6927

It’s not immediately obvious how that’s happening, but it sure seems like it is.

/cc @daxian-dbw you might find this interesting. Looks like the binder is somehow returning true here and exiting early, skipping the cast to object right afterwards.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to PInvoke an Instance Method by disabling Name ...
I ended up going with a c-style function that wraps the instance method and takes an instance of an a as a parameter....
Read more >
Solved: C++ and C# p/invoke Not Working Cannot Start Creo
Hi all,. I'm using Creo Parametric 7.0.2. I'm working on trying to use C# p/invoke interop method to call a Creo function from...
Read more >
CA1060: Move P/Invokes to NativeMethods class
P/Invoke methods that cannot be safely called and that could cause side effects should be put in a class that is named UnsafeNativeMethods....
Read more >
Calling Native Functions from Managed Code
If a native function returns the pointer that has been marshaled to it by PInvoke, memory corruption and exceptions may ensue.
Read more >
OpenNurbs and PInvoke - Rhino Developer - McNeel Forum
I am testing methods written in C++ and calling them in C# via PInvoke. If I compile dynamic .dll without OpenNurbs I can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found