Skip to content

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

@alexhelms

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;

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions