1

I am using .NET 8 Preview 6 and Visual Studio 2022, updated today.

I updated a library today, changing from DllImport to LibraryImport, but it indicates CS0755.

Here is my imported method declaration:

public unsafe static partial class CppCoreFuncs
{
    #region private
    [LibraryImport(DLL, EntryPoint = "Poly2Cofs_multi_buf"), SuppressUnmanagedCodeSecurity]
    private static partial FuncResult Poly2CofsMultiBuf(void* buffer, InputMode mode);
    #endrgion

...
}

This generates the following code:

public unsafe static partial class CppCoreFuncs
{
    [System.Runtime.InteropServices.DllImportAttribute("CPP Core.dll", EntryPoint = "Poly2Cofs_multi_buf", ExactSpelling = true)]
    private static extern partial global::RWT.ROCT.Core.Public.FuncResult Poly2CofsMultiBuf(void* buffer, global::RWT.ROCT.Core.Public.InputMode mode);
}

When I compile, I get this:

Error CS0755 Both partial method declarations must be extension methods or neither may be an extension method... Core\Microsoft.Interop.LibraryImportGenerator\Microsoft.Interop.LibraryImportGenerator\LibraryImports.g.cs

Documentation: Source generation for platform invokes.

There is no extension method. If I change back to DLLImport then the next method import declaration with LibraryImport gets this error. It only appears once.

EDIT

For reference, here is a working code generator produced stub for GeneratedRegexAttribute:

[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Text.RegularExpressions.Generator", "8.0.8.32907")]
    internal static partial global::System.Text.RegularExpressions.Regex QuotedOrUnquotedNewLineRx() => global::System.Text.RegularExpressions.Generated.QuotedOrUnquotedNewLineRx_7.Instance;
IamIC
  • 17,747
  • 20
  • 91
  • 154
  • Possibly. I don't see what attribute is mismatched, though. – IamIC Jul 13 '23 at 18:22
  • `DLLImportAttribute` is compiler generated. The top code is mine, and the bottom is from the code generator. – IamIC Jul 13 '23 at 18:27
  • Here is what the specification says: "The attributes in the resulting method declaration are the combined attributes of the defining and the implementing partial method declaration in unspecified order. Duplicates are not removed." – Ben Voigt Jul 13 '23 at 20:24
  • I still don't get what mistake I'm making, if any. My code looks the same as in the documentation. There is no extension method in either my or the generated code. – IamIC Jul 13 '23 at 21:16
  • Based on what I read, that source generator should generate a wrapper that takes care of parameter marshalling (in and out), calling the DLL function in between. So the wrapper function, which should be a c# function, should be a partial method, and then its internal call to the DLL should have a different name. I don't know why it is generating the DllImport as a partial method. I guess if it decided no wrapper is needed that could be useful? – Ben Voigt Jul 13 '23 at 22:53
  • Unfortunately the documentation doesn't give any example of what the generated code should look like. – Ben Voigt Jul 13 '23 at 22:55
  • The `partial` on the generated code is correct. I upgraded my Regex expression to use the new code generator, and they look similar in style to the code above. But they work. In this case, it looks like a compiler bug of some sort. I have replicated what the instructions state. I have reported it to Microsoft. Will likely need to revert to `DLLImport` for now. – IamIC Jul 13 '23 at 22:57
  • According to my understanding there should be a partial method in the generated code, but it should be for a wrapper/thunk written in C#. Not a `DllImport`. There might also be a `DllImport` which the wrapper calls internally, but that wouldn't be partial. – Ben Voigt Jul 14 '23 at 14:11
  • I agree since that makes sense. – IamIC Jul 15 '23 at 19:29

0 Answers0