Cannot P/Invoke a function named "Process()"
See original GitHub issueSteps 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:
- Created 3 years ago
- Comments:9
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Oh also probably a better repro:
Looks like it’s any static method named either
Process
, orEnd
(incidentally, notBegin
because it has a void return) without arguments that returns a value type.Easier repro:
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 toobject
right afterwards.