Overhand a function pointer from managed to unmanaged

I

interX

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::GetFunctionPointerForDelegate 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_MessagesFP)(long selector, long value,
void* message, double* opt);

IntPtr ptr =
Marshal::GetFunctionPointerForDelegate(m_MessageHandle_del);
m_asioCallbacks->asioMessage =
static_cast<asio_MessagesFP>(ptr.ToPointer());
ASIOCreateBuffers(..., 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_MessagesFP)(long selector, long value,
void* message, double* opt);

m_asioCallbacks->asioMessage = static_cast<asio_MessagesFP>(
&StaticFunc);
ASIOCreateBuffers(..., 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 doubleBufferIndex, ASIOBool directProcess);
void (*sampleRateDidChange) (ASIOSampleRate sRate);
long (*asioMessage) (long selector, long value, void* message, double*
opt);
ASIOTime* (*bufferSwitchTimeInfo) (ASIOTime* params, long
doubleBufferIndex, ASIOBool directProcess);
} ASIOCallbacks;


The unmanaged function:

ASIOError ASIOCreateBuffers(ASIOBufferInfo *bufferInfos, long
numChannels, long bufferSize, ASIOCallbacks *callbacks)
 
G

Guest

typedef long ( __cdecl *asio_MessagesFP)(long selector, long value,
void* message, double* opt);

Hi,
By default, GetFunctionPointerForDelegate returns function pointers that use
__stdcall calling convention.
if you treat it as a __cdecl function you will get an access violation.

Try using this attribute:
[UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
public delegate bool CallBack(........);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top