-
Trying to activate an IAudioClient through this code: internal AudioClient CreateAudioClient()
{
_device.Activate(typeof(IAudioClient).GUID, CLSCTX.CLSCTX_ALL, pActivationParams: null, out object client);
return new AudioClient((IAudioClient)client);
} results in an ArgumentException afterwards when trying to initialize internal void Initialize(long requestedDuration, WAVEFORMATEX waveFormat)
{
client.Initialize
(
AUDCLNT_SHAREMODE.AUDCLNT_SHAREMODE_SHARED,
StreamFlags: 0x80000000 | 0x08000000, // AutoConvertPCM | SrcDefaultQuality.
hnsBufferDuration: requestedDuration,
hnsPeriodicity: 0,
pFormat: waveFormat,
AudioSessionGuid: null
);
_isInitialized = true;
} The exception is caused here: [global::System.CodeDom.Compiler.GeneratedCode("Microsoft.Windows.CsWin32", "0.3.106+a37a0b4b70")]
internal static partial class Media_Audio_IAudioClient_Extensions
{
/// <inheritdoc cref="winmdroot.Media.Audio.IAudioClient.Initialize(winmdroot.Media.Audio.AUDCLNT_SHAREMODE, uint, long, long, winmdroot.Media.Audio.WAVEFORMATEX*, global::System.Guid*)"/>
internal static unsafe void Initialize(this winmdroot.Media.Audio.IAudioClient @this, winmdroot.Media.Audio.AUDCLNT_SHAREMODE ShareMode, uint StreamFlags, long hnsBufferDuration, long hnsPeriodicity, in winmdroot.Media.Audio.WAVEFORMATEX pFormat, global::System.Guid? AudioSessionGuid)
{
fixed (winmdroot.Media.Audio.WAVEFORMATEX* pFormatLocal = &pFormat)
{
global::System.Guid AudioSessionGuidLocal = AudioSessionGuid ?? default(global::System.Guid);
@this.Initialize(ShareMode, StreamFlags, hnsBufferDuration, hnsPeriodicity, pFormatLocal, AudioSessionGuid.HasValue ? &AudioSessionGuidLocal : null);
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
What is in your I can't be sure whether that's what is causing your ArgumentException, but that's my guess. And my prescription would be to always deal with |
Beta Was this translation helpful? Give feedback.
-
Got it. Thank you for resolving my issue. |
Beta Was this translation helpful? Give feedback.
Given
cbSize
is 22, then per the struct's documentation there are bytes after the struct that are important. But you're taking theWAVEFORMATEX*
and copying only the struct itself onto your stack, and then freeing the non-managed memory that held the original struct and the data you never copied.A struct like this must not ever be handed around as
WAVEFORMATEX
. You must only ever hand aroundWAVEFORMATEX*
because of this un-managed size of the struct, meaning it can never exist on the stack.