dotnet / runtimelab

This repo is for experimentation and exploring new ideas that may or may not make it into the main dotnet/runtime repo.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[NativeAot] Document DirectPInvoke's requirements

SupinePandora43 opened this issue · comments

At first glance, i thought i can add any DllImport'ed library.
But then i added "Ultralight" and few more.
I received a lot of errors.
Beginning of errors

Создается библиотека bin\Release\net5.0\win-x64\native\UltralightNet.Veldrid.TestApp.lib и объект bin\Release\net5.0\win-x64\native\UltralightNet.Veldrid.TestApp.exp
UltralightNet.Veldrid.TestApp.obj : error LNK2001: неразрешенный внешний символ ulApplyProjection. [C:\Users\Дом\source\repos\SupinePandora43\UltralightSharp\UltralightNet.Veldrid.TestApp\UltralightNet.Veldrid.TestApp.csproj]
UltralightNet.Veldrid.TestApp.obj : error LNK2001: неразрешенный внешний символ ulCreateRenderer. [C:\Users\Дом\source\repos\SupinePandora43\UltralightSharp\UltralightNet.Veldrid.TestApp\UltralightNet.Veldrid.TestApp.csproj]
UltralightNet.Veldrid.TestApp.obj : error LNK2001: неразрешенный внешний символ ulDestroyRenderer. 

Sounds like it requires lib files.
I have them!
But where they should be?
How do i specify where It should search for them?!

Documentation doesn't answer these questions.
I tried looking at corert repo, but didn't found.
Could documentation be improved?

i tried asking this on discord (DotNetEvolution) but it wasn't answered

Try adding <NativeLibrary Include="path\to\your\lib\file.lib" /> to an ItemGroup in your project file.

Try adding <NativeLibrary Include="path\to\your\lib\file.lib" /> to an ItemGroup in your project file.

it works!

Fixed by #980

i tried to call CreateFile from kernel32.dll but im getting unresolved external symbol CreateFile. i have <DirectPInvoke Include="kernel32" /> in my csproj.

Do you happen to have ExactSpelling=true on your PInvoke? There is not CreateFile entrypoint in kernel32 actually. It is either CreateFileA or CreateFileW. The precise spelling has to be used with ExactSpelling=true.

Also, you should not need to use <DirectPInvoke Include="kernel32" />. The build scripts will do it automatically for you.

@jkotas
here is my definition:

const string KERNEL32_DLL = "kernel32.dll";
        [DllImport(KERNEL32_DLL, SetLastError = true)]
        public static extern IntPtr CreateFile(
            string lpFileName,
            [MarshalAs(UnmanagedType.U4)] FileAccess dwDesiredAccess,
            [MarshalAs(UnmanagedType.U4)] FileShare dwShareMode,
            IntPtr lpSecurityAttributes,
            [MarshalAs(UnmanagedType.U4)] FileMode dwCreationDisposition,
            int dwFlagsAndAttributes,
            IntPtr hTemplateFile);

if i dont use DirectPInvoke for kernel32 wont it just be lazily resolved at runtime then?

ok but it also errors on setupapi.dll and user32.dll which i dont see there. here is one of my usage:

[DllImport("setupapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern bool SetupDiGetDeviceRegistryProperty(
        IntPtr DeviceInfoSet,
        ref SP_DEVINFO_DATA DeviceInfoData,
        SPDRP Property,
        IntPtr PropertyRegDataType,
        IntPtr PropertyBuffer,
        uint PropertyBufferSize,
        out UInt32 RequiredSize);

[DllImport("user32.dll", SetLastError = true)]
        internal static extern bool UnregisterDeviceNotification(IntPtr Handle);

setupapi.dll and user32.dll

You need to add these manually. Make sure to add .lib files from Windows SDK as <NativeLibrary> items too (https://github.com/dotnet/runtime/blob/main/src/coreclr/nativeaot/docs/interop.md).