You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
namespaceAAA.BBB.System.Native{internalstaticunsafepartialclassUser32{[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.LibraryImportGenerator","8.0.10.11423")][global::System.Runtime.CompilerServices.SkipLocalsInitAttribute]publicstaticpartialboolEnumWindows(global::AAA.BBB.System.Native.User32.EnumWindowsProclpEnumFunc,nintlParam){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)]staticexternunsafeint__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
The text was updated successfully, but these errors were encountered:
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:
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:
If it wont work make sure the whole project have
AAA.BBB.System
namespace.Exceptions (if any)
Possible fix
Use
global::System.IntPtr
instaed ofSystem.IntPtr
type.Further technical details
dotnet --info
The text was updated successfully, but these errors were encountered: