473,397 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Overhand a function pointer from managed to unmanaged

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_MessageHa ndle_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)

Oct 30 '06 #1
2 2963
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(CallingConventio n.Cdecl)]
public delegate bool CallBack(........);
--
Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"

Oct 30 '06 #2
Thanks Bruno, now it works fine!

Regards
Thorsten

Oct 31 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: hykim | last post by:
I want to call a unmanaged dll's function returning some STRUCT's pointer. the next is definition of a STRUCT. ----------------------------------------------------------------------- typedef...
0
by: nygiantswin2005 | last post by:
I would like to know how do I pass a pointer to a struct from managed code to unmanaged code. For example if I create structure like this in managed code. StructLayout( LayoutKind.Sequential,...
1
by: H.B. | last post by:
Hi, I need to make a function that can display data on my Managed C++ app and be called by an unmanaged C++ DLL. Something like : void Form1::Form1_Load(System::Object * sender,...
0
by: Haxan | last post by:
Hi, I have an unmanaged application that converts a function pointer to a delegate and then pass this as a parameter(delegate) to a managed function which then invokes it. Currently Im able to...
12
by: Haxan | last post by:
Hi, I have my main application class(unmanaged) that has a none static member function that I need to pass as a delegate to managed C# method. In one of the methods of this class(unmamanged),...
7
by: harishashim | last post by:
I am wrapping a digital camera API using Managed C++ VS .NET 2003). I have this function that called as bellow in the API sample. err = PR_RC_StartViewFinder( m_hCamera, //line 1...
6
by: Aston Martin | last post by:
Hi All, ********************** My Situation ********************** I am working on project that involves passing a structure to unmanaged code from .Net world (well using C#). Perhaps an example...
3
by: Klaus | last post by:
Hi, I have an existing VC 6 MFC application which communicates asynchronly with a VC 2005 managed code dll. I use an unmanaged base class with virtual functions to access methods in the MFC...
9
by: =?Utf-8?B?RWR3YXJkUw==?= | last post by:
I would greatly appreciate some help on passing managed object into unmanaged code. I need to pass a reference (address of) of a managed class into unmanaged code (written by a thrid party). The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.