473,801 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::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)

Oct 30 '06 #1
2 2981
typedef long ( __cdecl *asio_MessagesF P)(long selector, long value,
void* message, double* opt);
Hi,
By default, GetFunctionPoin terForDelegate 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:
[UnmanagedFuncti onPointerAttrib ute(CallingConv ention.Cdecl)]
public delegate bool CallBack(...... ..);
--
Kind regards,
Bruno.
br************* *********@hotma il.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
1749
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 struct myStruct{ struct { UINT_PTR anything; UCHAR anything2; } myInnerStruct;
0
1703
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, CharSet=CharSet.Ansi ) ] public struct MYSTRUCT { public String Text;
1
2605
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, System::EventArgs * e) { MyDLLInit(MyAppDisplayFunction); }
0
2019
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 jump to this unmanaged function, but the values of the parameters inside this function Im seeing are not correct(they have some garbage values). //unmanaged class (C++ application)
12
11372
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), I am calling a managed C# method(I use gcnew to instantiate the managed class). One of the parameters of this C# method is a delegate. I need to pass the none static member function as a delegate(function pointer) as a parameter in the managed C#...
7
2620
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 (prContext)this, //line 2 (prViewFinderCB*)&ViewFinderCallBackFun ); //line 3 prContext is actually a typedef for unsigned long. ViewFinderCallBackFun is a callback function. There is two error that I get when i tried above code unchanged:
6
5900
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 will prove useful. structure MyStruct { // this is a complicated struct declaration in the sense
3
4713
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 application. Furthermore, I use a pointer to an unmanaged function to jump back into the managed dll. The managed part is basically a remoting enhancement which asynchronly
9
3568
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 3rd party unmanaged DLL will pass this reference into standard Win32 unmanaged static callback function in my code. Inside this unmanaged callback function I need to cast this unmnaged pointer that I have received from 3rd party back into the...
0
9698
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9558
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10524
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10298
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10278
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10055
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9105
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7594
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.