Skip to content

SYSLIB1054 code fixer should not generate a code that try to access to LibraryImport.ThrowOnUnmappableChar #79159

Closed

Description

@Tan90909090

Version Used:
Microsoft Visual Studio Community 2022 (64-bit) - Current Version 17.4.2

Steps to Reproduce:

  1. Write <TargetFramework>net7.0</TargetFramework> in csproj
  2. Write a following code:
using System;
using System.Runtime.InteropServices;

class NativeMethods
{
    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true)]
    static extern IntPtr GetProcAddress(
        IntPtr hModule,
        [MarshalAs(UnmanagedType.LPStr)] string lpProcName);

    static void Main() { }
}
  1. Apply a quick-fix Convert to 'LibraryImport' at GetProcAddress

Diagnostic Id:
SYSLIB1054 Mark the method 'GetProcAddress' with 'LibraryImportAttribute' instead of 'DllImportAttribute' to generate P/Invoke marshalling code at compile time

Expected Behavior:
Code fixer generate a code that does not have an error.

Actual Behavior:
Code fixer generate a following code:

using System;
using System.Runtime.InteropServices;

partial class NativeMethods
{
    [LibraryImport("kernel32.dll", SetLastError = true, StringMarshalling = StringMarshalling.Custom, ThrowOnUnmappableChar = true, StringMarshallingCustomType = typeof(System.Runtime.InteropServices.Marshalling.AnsiStringMarshaller))]
    static partial IntPtr GetProcAddress(
        IntPtr hModule,
        [MarshalAs(UnmanagedType.LPStr)] string lpProcName);

    static void Main() { }
}

Because LibraryImportAttribute does not have ThrowOnUnmappableChar member so the above code has an error: CS0246 The type or namespace name 'ThrowOnUnmappableChar' could not be found (are you missing a using directive or an assembly reference?)

Note
I realized that same issue happens to the following code because there is not LibraryImportAttribute.BestFitMapping:

    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Ansi, BestFitMapping = true)]
    static extern IntPtr GetProcAddress(
        IntPtr hModule,
        [MarshalAs(UnmanagedType.LPStr)] string lpProcName);

Activity

  1. ghost added
    untriaged
    on Dec 2, 2022
  2. CyrusNajmabadi commented on Dec 2, 2022

    @CyrusNajmabadi
    Contributor

    SYSLIB1054

    This is not roslyn. Moving to runtime.

  3. AaronRobinsonMSFT commented on Dec 9, 2022

    @AaronRobinsonMSFT
    Member

    @jkoritzinsky I thought we stripped invalid field when converting attributes or indicated the fixer couldn't apply. Am I misremembering this? I think we either need to drop the unsupported field when converting or not flag it. @jtschuster Any chance you could quickly address this?

  4. removed
    untriaged
    on Dec 9, 2022
  5. added this to the 8.0.0 milestone on Dec 9, 2022
  6. Tan90909090 commented on Dec 14, 2022

    @Tan90909090
    Author

    Another related issue.
    When I write a following code then there is a SYSLIB1054:

    using System;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    
    internal static partial class NativeMethods
    {
        [DllImport("msvcrt.dll", CallingConvention=CallingConvention.Cdecl)]
        internal static extern int printf([MarshalAs(UnmanagedType.LPStr)] string s, __arglist);
    }

    If I apply a quick-fix Convert to 'LibraryImport' then I will get a following code:

    using System;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    
    internal static partial class NativeMethods
    {
        [LibraryImport("msvcrt.dll")]
        [UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvCdecl) })]
        internal static partial int printf([MarshalAs(UnmanagedType.LPStr)] string s, __arglist);
    }

    The above code has an error:

    Error CS8795 Partial method 'NativeMethods.printf(string, __arglist)' must have an implementation part because it has accessibility modifiers.
    

    It seems that LibraryImportGenerator does not support __arglist currently.

  7. ghost added
    in-pr
    on Jun 13, 2023
  8. ghost removed
    in-pr
    on Jun 22, 2023
  9. ghost locked as resolved and limited conversation to collaborators on Jul 23, 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