-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorrect PInvoke when parameter has base class with no members #49857
Comments
The issue here only appears when the parameter is the base class type, but the passed in argument is the derived type. If you change the parameter type to the derived type, then the PInvoke will behave as expected. Would that workaround work for your scenario? |
In our scenario there are multiple subtypes, any of which could be passed in for any given call. (Think command messages.) Since the object type is not known at compile time, I don't think that workaround could be applied. We're not blocked though, since we do have a workaround where we manually allocate and marshal with Long-term I would be concerned if a fix for this didn't make it into the 5.0.* servicing line eventually, as it would mean that forever into the future any pinvoke code with polymorphic parameters would need to either continue to use this workaround for all targets or else know to special-case the net5.0 target. |
Description
PInvoke marshalling is incorrect when there is a parameter whose type has a base class with no members.
I hit this when removing our manual marshaling workaround for #46643, and it feels like the same bug in a different place.
#46643 describes a problem where given:
The result of
Marshal.StructureToPtr<MyClass1>(...)
was incorrect, containing a non-collapsed zero-field for the base type.The fix for that is in net5.0.3, and in testing against 5.0.4, while I do see the fix for
Marshal.StructureToPtr<>()
, I'm still seeing the same symptoms on a PInvoke parameter:Configuration
runtime version: 5.0.4
OS: Windows 20H2 19042.867 64-bit
Arch: x64 PC, 32-bit and 64-bit runtimes
Specific to configuration: unknown
Regression?
net5.0 regression from netfx and netcore 3.1.
The fix for the #46643 is in 5.0.3 PR 46697, but this related issue remains.
Other information
With this native export:
And these managed imports:
And this instance:
Here's the output of some test code on netcoreapp 3.1.13 and net 5.0.4. (net472 is the same as netcoreapp so I won't re-paste.)
The text was updated successfully, but these errors were encountered: