Skip to content

LibraryImport with CLong produces SYSLIB1051 and requires disabling runtime marshalling #73975

Closed

Description

@alexhelms

Description

Creating a LibraryImport P/Invoke function with CLong parameters produces a SYSLIB1051 error saying that runtime marshalling must be disabled in order to use a CLong as a parameter.

Reproduction Steps

I have this C function signature and the corresponding P/Invoke function.

/*
 * int CFITS_API ffghpr(fitsfile *fptr, int maxdim, int *simple, int *bitpix, int *naxis,
 *     long naxes[], long *pcount, long *gcount, int *extend, int *status);
 */
[LibraryImport(cfitsio, EntryPoint = "ffghpr")]
internal static partial ErrorCode ReadImageHeader(
    nint handle,
    int maxdim,
    out int simple,
    out ImageType bitpix,
    out int naxis,
    ref CLong naxes,
    out CLong pcount,
    out CLong gcount,
    out int extend,
    out ErrorCode status);

And the compiler will produce three errors (one for each CLong) -- error SYSLIB1051: Runtime marshalling must be disabled in this project ...

Expected behavior

CLong should work with LibraryImport without disabling runtime marshalling.

Actual behavior

CLong requires [assembly: DisableRuntimeMarshalling] in order to work.

Regression?

No response

Known Workarounds

Using a traditional DllImport works as expected.

Configuration

Windows 10 1909 x64

.NET SDK:
 Version:   7.0.100-preview.7.22377.5
 Commit:    ba310d9309

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.18363
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\7.0.100-preview.7.22377.5\

Host:
  Version:      7.0.0-preview.7.22375.6
  Architecture: x64
  Commit:       eecb028078

Other information

It seems special consideration for CLong/UCLong may be required in the source generators for LibraryImport. I think there is precedence for this kind of special treatment seeing as the jit compiler also has special support for CLong/UCLong --

return strcmp(typeName, "CLong") == 0 || strcmp(typeName, "CULong") == 0 || strcmp(typeName, "NFloat") == 0;

Activity

  1. ghost added
    untriaged
    on Aug 15, 2022
  2. AaronRobinsonMSFT commented on Aug 15, 2022

    @AaronRobinsonMSFT
    Member

    @alexhelms Appreciate this being called out. At this point I think we are unlikely to try and mitigate this for .NET 7, but this is something we should address in .NET 8. Please let us know if this is heavily impacting.

  3. removed
    untriaged
    on Aug 15, 2022
  4. added this to the 8.0.0 milestone on Aug 15, 2022
  5. alexhelms commented on Aug 15, 2022

    @alexhelms
    Author

    That is unfortunate but I understand, release is getting close! The DllImport workaround allows me to move forward and I don't require AOT support so this is not a blocker. I'm just experimenting with the new NativeLibrary feature.

  6. AaronRobinsonMSFT commented on Aug 16, 2022

    @AaronRobinsonMSFT
    Member

    That is unfortunate but I understand, release is getting close!

    Right. Thanks for understanding. We will get this done in .NET 8. Please ping us early next year if you don't see any traction so we can get on this and you can try it out in an early preview.

  7. 390620652 commented on Jan 17, 2023

    @390620652

    I look forward to this function too.

  8. ghost added
    in-pr
    on Jun 29, 2023
  9. ghost removed
    in-pr
    on Jun 30, 2023
  10. ghost locked as resolved and limited conversation to collaborators on Jul 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions