Skip to content
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

Problem with LibraryImport Generated Code #104567

Closed
MrVarens opened this issue Jul 7, 2024 · 2 comments · Fixed by #104758
Closed

Problem with LibraryImport Generated Code #104567

MrVarens opened this issue Jul 7, 2024 · 2 comments · Fixed by #104758

Comments

@MrVarens
Copy link

MrVarens commented Jul 7, 2024

Describe the bug

When i import some windows functions with use of LibraryImport i get an error stating that its unable to find IntPtr type in my namespace.

I have researched it and found out that generated code contains "System.IntPtr" type in some places and because my own project namespace contains "System" namespace, generated code try to use my namespace instead of global "System.IntPtr".

Generated code:

namespace AAA.BBB.System.Native
{
    internal static unsafe partial class User32
    {
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.LibraryImportGenerator", "8.0.10.11423")]
        [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute]
        public static partial bool EnumWindows(global::AAA.BBB.System.Native.User32.EnumWindowsProc lpEnumFunc, nint lParam)
        {
            int __lastError;
            System.IntPtr __lpEnumFunc_native;
            bool __retVal;
            int __retVal_native;
            // Marshal - Convert managed data to native data.
            __lpEnumFunc_native = lpEnumFunc != null ? global::System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(lpEnumFunc) : default;
            {
                global::System.Runtime.InteropServices.Marshal.SetLastSystemError(0);
                __retVal_native = __PInvoke(__lpEnumFunc_native, lParam);
                __lastError = global::System.Runtime.InteropServices.Marshal.GetLastSystemError();
            }

            // NotifyForSuccessfulInvoke - Keep alive any managed objects that need to stay alive across the call.
            global::System.GC.KeepAlive(lpEnumFunc);
            // Unmarshal - Convert native data to managed data.
            __retVal = __retVal_native != 0;
            global::System.Runtime.InteropServices.Marshal.SetLastPInvokeError(__lastError);
            return __retVal;
            // Local P/Invoke
            [global::System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "EnumWindows", ExactSpelling = true)]
            static extern unsafe int __PInvoke(System.IntPtr __lpEnumFunc_native, nint __lParam_native);
        }
    }
}

Problematic code is in this lines:

  • System.IntPtr __lpEnumFunc_native;
  • static extern unsafe int __PInvoke(System.IntPtr __lpEnumFunc_native, nint __lParam_native);

To Reproduce

Compile this code:

using System.Runtime.InteropServices;

namespace AAA.BBB.System.Native;

internal static partial class User32
{
    public delegate bool EnumWindowsProc(nint hWnd, nint lParam);

    [LibraryImport("user32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static partial bool EnumWindows(EnumWindowsProc lpEnumFunc, nint lParam);
}

If it wont work make sure the whole project have AAA.BBB.System namespace.

Exceptions (if any)

0>LibraryImports.g.cs(11,20): Error CS0234 : The type or namespace name 'IntPtr' does not exist in the namespace 'AAA.BBB.System' (are you missing an assembly reference?)
0>LibraryImports.g.cs(30,55): Error CS0234 : The type or namespace name 'IntPtr' does not exist in the namespace 'AAA.BBB.System' (are you missing an assembly reference?)

Possible fix

Use global::System.IntPtr instaed of System.IntPtr type.

Further technical details

  • sdk version: 8.0.203
  • target framework: net8.0-windows8.0
  • dotnet --info
.NET SDK:
 Version:           8.0.203
 Commit:            5e1ceea679
 Workload version:  8.0.200-manifests.8cf8de6d

Środowisko uruchomieniowe:
 OS Name:     Windows
 OS Version:  10.0.22631
 OS Platform: Windows
 RID:         win-x64
 Base Path:   C:\Program Files\dotnet\sdk\8.0.203\

Host:
  Version:      8.0.3
  Architecture: x64
  Commit:       9f4b1f5d66

.NET SDKs installed:
  8.0.202 [C:\Program Files\dotnet\sdk]
  8.0.203 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.28 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.28 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.28 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 7.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 8.0.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
  x86   [C:\Program Files (x86)\dotnet]
    registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
  Not set
@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Jul 7, 2024
@vitek-karas vitek-karas transferred this issue from dotnet/sdk Jul 8, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/interop-contrib
See info in area-owners.md if you want to be subscribed.

@AaronRobinsonMSFT
Copy link
Member

@jkoritzinsky @jtschuster I thought we prefixed everything with global for this very situation? Is the issue with the nint alias?

@agocke agocke added this to the 9.0.0 milestone Jul 8, 2024
@agocke agocke removed the untriaged New issue has not been triaged by the area owner label Jul 9, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Aug 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants