Hi,
I have a little problem with managed/unmanaged in Visual Studio 2005
(Compiler setting /clr). I need to overhand several function pointers
from managed to unmanaged. These function pointers are stored in an
unmanaged struct and this struct is then overhanded by reference to the
unmanaged code (See code pieces below).
I thought the way to go is to create a delegate, pin it via
GCHandel::Alloc and use the function
Marshal::GetFun ctionPointerFor Delegate to get the pointer of the
function (http://msdn2.microsoft.com/en-us/library/367eeye0.aspx). With
this technique my managed function gets called once but as soon as I
leave it I get the following exception:
First-chance exception at 0x008fa059 in GUI ASIO.exe: 0xC0000005:
Access violation writing location 0x0000000c.
The thread 'Win32 Thread' (0x694) has exited with code -1073740791
(0xc0000409).
The thread 'Win32 Thread' (0xf90) has exited with code -1073740791
(0xc0000409).
The thread 'Win32 Thread' (0xb34) has exited with code -1073740791
(0xc0000409).
The thread 'Win32 Thread' (0xbbc) has exited with code -1073740791
(0xc0000409).
The program '[3040] GUI ASIO.exe: Native' has exited with code
-1073740791 (0xc0000409).
The program '[3040] GUI ASIO.exe: Managed' has exited with code
-1073740791 (0xc0000409).
That is who I call it:
typedef long ( __cdecl *asio_MessagesF P)(long selector, long value,
void* message, double* opt);
IntPtr ptr =
Marshal::GetFun ctionPointerFor Delegate(m_Mess ageHandle_del);
m_asioCallbacks->asioMessage =
static_cast<asi o_MessagesFP>(p tr.ToPointer()) ;
ASIOCreateBuffe rs(..., m_ asioCallbacks);
The funny thing is, if I use a static function, with out the use of a
delegate, it works fine. The function is define on the top of the cpp
file. I know, the cast here is not really necessary!
typedef long ( __cdecl *asio_MessagesF P)(long selector, long value,
void* message, double* opt);
m_asioCallbacks->asioMessage = static_cast<asi o_MessagesFP>(
&StaticFunc) ;
ASIOCreateBuffe rs(..., m_ asioCallbacks);
So, does any one have any idea why the first way with the delegate not
works? I tried really every thing, but I'm getting a bit frustrated
right now. The static function doesn't realy help, because I need my
object.
THANKS FOR ANY SUGGESTIONS!!!
Thorsten
The unmanaged struct with the function pointer:
typedef struct ASIOCallbacks
{
void (*bufferSwitch) (long doubleBufferInd ex, ASIOBool directProcess);
void (*sampleRateDid Change) (ASIOSampleRate sRate);
long (*asioMessage) (long selector, long value, void* message, double*
opt);
ASIOTime* (*bufferSwitchT imeInfo) (ASIOTime* params, long
doubleBufferInd ex, ASIOBool directProcess);
} ASIOCallbacks;
The unmanaged function:
ASIOError ASIOCreateBuffe rs(ASIOBufferIn fo *bufferInfos, long
numChannels, long bufferSize, ASIOCallbacks *callbacks)