smourier / DirectN

Direct interop Code for .NET Framework, .NET Core and .NET 5+ : DXGI, WIC, DirectX 9 to 12, Direct2D, Direct Write, Direct Composition, Media Foundation, WASAPI, CodecAPI, GDI, Spatial Audio, DVD, Windows Media Player, UWP DXInterop, WinUI3, etc.
MIT License
311 stars 28 forks source link

Why AddStream last argument is IntPtr, not out uint? #40

Closed oven425 closed 1 year ago

oven425 commented 1 year ago

IMFCaptureSink::AddStream last argumnt define out uint, and easy use more

HRESULT AddStream(/* [annotation][in] _In_ */ uint dwSourceStreamIndex, /* [annotation][in] _In_ */ IMFMediaType pMediaType, /* [annotation][in] _In_opt_ */ IMFAttributes pAttributes, /* (DWORD) */ out uint pdwSinkStreamIndex);
smourier commented 1 year ago

Hi,

It's because the parameter is defined in the SDK headers mfcaptureengine.h as optional:

IMFCaptureSink : public IUnknown
{
  ...

    virtual HRESULT STDMETHODCALLTYPE AddStream( 
        /* [annotation][in] */ 
        _In_  DWORD dwSourceStreamIndex,
        /* [annotation][in] */ 
        _In_  IMFMediaType *pMediaType,
        /* [annotation][in] */ 
        _In_opt_  IMFAttributes *pAttributes,
        /* [annotation][out] */ 
        _Out_opt_  DWORD *pdwSinkStreamIndex) = 0;

  ...
};

_Out_opt_ means optional parameter https://learn.microsoft.com/en-us/cpp/code-quality/understanding-sal?view=msvc-170#sal-basics so you can pass IntPtr.Zero when you don't need it.

With DirectN, you can use some helper that will allocate and free memory for you like this:

IMFCaptureSink sink ...
using var cm = new ComMemory(Marshal.SizeOf<uint>()); // size will be 4 obviously...
sink.AddStream(0, type, at, cm.Pointer);
// read back from pointer
var index = Marshal.ReadInt32(cm.Pointer);
oven425 commented 1 year ago

Although according to msdn's instructions, it is possible to give IntPtr.Zero, but set IntPtr.Zero cause return E_POINTER Invalid pointer error. AddStream founction retrun streamindex is very import for after AddEffect function. so i recommend last agrument change to out uint

smourier commented 1 year ago

Hi,

The code is generated so no, I won't change this, I don't want to start adding too much exceptions to the ~10000 files in the project.

Report the wrong annotation to Microsoft and when they change it will be generated differently.

oven425 commented 1 year ago

OK, I got it