I try to call a DllImport on the following Alsa function (from the documentation:
#define snd_seq_client_info_alloca( ptr ) __snd_alloca(ptr, snd_seq_client_info)allocate a snd_seq_client_info_t
container on stack
Here is an implementation that I have found in C++ while reading the code of an application similar to what I want to accomplish in Mono:
snd_seq_client_info_t* cinfo;
snd_seq_client_info_alloca(&cinfo);
And here is what I have so far, but it is not working:
[DllImport(libasound.so.2)]
private static extern void snd_seq_client_info_alloca(out IntPtr ptr);
internal static void MyFunction ()
{
IntPtr clientInfo = IntPtr.Zero;
snd_seq_client_info_alloca(out clientInfo);
// and then some more ...
}
But I get the following exception:
System.EntryPointNotFoundException: snd_seq_client_info_alloca
at (wrapper managed-to-native)
MonoMultiJack.ConnectionWrapper.Alsa.LibAsoundWrapper:snd_seq_client_info_alloca (intptr&)
snd_seq_client_info_allocais a pre-processor macro. You cannot P/Invoke pre-processor macros, only properly exported functions.snd_seq_client_info_allocaexpands to__snd_allocawhich is a preprocessor macro as well.__snd_allocaessentially expands to a call toalloca. You could try to rewrite this in C#. But I believe it doesn’t make a lot of sense to allocate memory in the stack frame of the caller if the caller is managed code.Just allocate some memory somewhere else (for example, using Marshal.AllocHGlobal).