Skip to content

Problem with LibraryImport Generated Code #104567

@MrVarens

Description

@MrVarens

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

Metadata

Metadata

Assignees

Type

No type

Projects

Status

No status

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions