microsoft / win32metadata

Tooling to generate metadata for Win32 APIs in the Windows SDK.
Other
1.34k stars 116 forks source link

Can't handle struct pointer? #2022

Closed HppZ closed 14 hours ago

HppZ commented 1 day ago

mpv.h (for testing)

extern "C" {
typedef struct mpv_handle mpv_handle;
__declspec(dllexport) mpv_handle* mpv_create(void);
}

mpvnet.modified.cs

namespace mpvnet
{
    public static unsafe partial class Apis
    {
        [DllImport("libmpv-2", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
        public static extern mpv_handle* mpv_create();
    }
}

\obj\crossarch\common\mpvnet.modified.cs(271,30): error CS0246: The type or namespace name 'mpv_handle' could not be found (are you missing a using directive or an assembly reference?)

riverar commented 1 day ago

Win32Metadata uses the --config exclude-empty-records by default. Not sure why we do, but it's dropping your "empty" structure.

Do you need Win32Metadata tooling? Perhaps you just need:

console:

dotnet tool install --global ClangSharpPInvokeGenerator
ClangSharpPInvokeGenerator -f .\main.h -n Namespace -o .\main.cs "@options.rsp"

options.rsp:

--config
log-exclusions
--with-librarypath
mpv_create=libmpv-2

main.cs

using System.Runtime.InteropServices;

namespace Namespace
{
    public partial struct mpv_handle
    {
    }

    public static unsafe partial class Methods
    {
        [DllImport("libmpv-2", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        public static extern mpv_handle* mpv_create();
    }
}
HppZ commented 1 day ago

Actually I need Microsoft.Windows.WinmdGenerator to generate winmd file and cswin32 to generate c# code.

riverar commented 23 hours ago

You can set the UseWinSDKAssets property to false to omit inclusion of some of the Windows-specific response files.

Example:

<PropertyGroup Label="Globals">
  <OutputWinmd>Generated.winmd</OutputWinmd>
  <WinmdVersion>0.0.0.0</WinmdVersion>
+ <UseWinSDKAssets>false</UseWinSDKAssets>
</PropertyGroup>
...