473,769 Members | 6,799 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function Pointers, Delegates, Legacy Code...and some sympathy

First off, the sympathy is for all you poor buggers out there who have to figure out how to marry
Managed Extensions for C++ onto your legacy code. My condolences; my brief experience with the
process leads me to believe the Microsoft hates all of its VC++ developers :)

Now, the question. I'm getting a NullReferenceEx ception in a __gc class that wraps functionality in
a legacy C/C++ app (it's actually part of the Nero burning rom api).

The legacy API expects to be given a configuration structure that contains, among other things, a
pair of function pointers which the codebase calls at various times. After much hair-pulling, I came
up with the following:

__delegate bool IdleCallbackHan dler( void* pUserData );
__delegate NeroUserDlgInOu t UserDialogCallb ackHandler( void* pUserData, NeroUserDlgInOu t type, void*
data );

[StructLayout(La yout::Sequentia l)]
__gc public struct NeroIdleCallbac k
{
public:
IdleCallbackHan dler* ncCallbackFunct ion;
void* ncUserData;
};

[StructLayout(La yout::Sequentia l)]
__gc public struct NeroUserDialogC allback
{
public:
UserDialogCallb ackHandler* ncCallbackFunct ion;
void* ncUserData;
};

[StructLayout(La yout::Sequentia l)]
__gc public struct NeroSettings
{
public:
const char *nstNeroFilesPa th;
const char *nstVendor, *nstSoftware;
const char *nstLanguageFil e;
NeroIdleCallbac k* nstIdle;
NeroUserDialogC allback* nstUserDialog;
bool nstEnableOverbu rn;
DWORD nstOverburnSize ;
};

I configure this structure, and the callback substructures, in the constructor of my wrapper class
as follows:

theSettings = new NeroSettings();
theSettings->nstNeroFilesPa th = "NeroFiles" ;
theSettings->nstLanguageFil e = "Nero.txt";

theSettings->nstEnableOverb urn = false;
theSettings->nstOverburnSiz e = 0;

theSettings->nstIdle = new NeroIdleCallbac k();
theSettings->nstIdle->ncCallbackFunc tion = new IdleCallbackHan dler(this, &Nero::FireIdle Event);
theSettings->nstIdle->ncUserData = idleData;

theSettings->nstUserDialo g = new NeroUserDialogC allback();
theSettings->nstUserDialo g->ncCallbackFunc tion = new UserDialogCallb ackHandler(this ,
&Nero::FireUser DialogEvent);
theSettings->nstUserDialo g->ncUserData = userdlgData;

theSettings->nstVendor = "ahead";
theSettings->nstSoftware = "Nero - Burning Rom";

Everything works fine during initialization, and in retrieving certain basic information from the
legacy codebase.

But when I call a legacy function that triggers the IdleCallback, I get the NullReferenceEx ception:

void AvailableDrives ( NERO_MEDIA_TYPE mtFlags )
{
// this next line blows up... but the IdleCallback gets called first
NERO_SCSI_DEVIC E_INFOS* drives = NeroGetAvailabl eDrivesEx(MEDIA _CD, NULL);
...
}

The interesting part is the part mentioned in that comment: the IdleCallback method gets called,
and returns, but then the exception gets thrown.

And what's really interesting is that setting the ncCallbackFunct ion delegate/function pointer to
NULL in the NeroIdleCallbac k structure -- which forces the legacy codebase to NOT call the
IdleCallback -- avoids the exception.

Sorry for being so long-winded.

What I'm wondering is, am I handling the function pointer concept correctly here? Is it possible
that I've done something wrong so that when the callback returns, it tries to return to a place that
doesn't exist?

- Mark
Nov 17 '05 #1
2 1644
Hi Mark:

The idea is to write an inner __nogc class inside our __gc class. The inner
class will contain the callback function (as a static method).

This article was very helpful to me:

http://www.codeproject.com/managedcpp/cbwijw.asp

Best regards
Ernesto

<Mark Olbert> wrote in message
news:ck******** *************** *********@4ax.c om...
First off, the sympathy is for all you poor buggers out there who have to figure out how to marry Managed Extensions for C++ onto your legacy code. My condolences; my brief experience with the process leads me to believe the Microsoft hates all of its VC++ developers :)
Now, the question. I'm getting a NullReferenceEx ception in a __gc class that wraps functionality in a legacy C/C++ app (it's actually part of the Nero burning rom api).

The legacy API expects to be given a configuration structure that contains, among other things, a pair of function pointers which the codebase calls at various times. After much hair-pulling, I came up with the following:

__delegate bool IdleCallbackHan dler( void* pUserData );
__delegate NeroUserDlgInOu t UserDialogCallb ackHandler( void* pUserData, NeroUserDlgInOu t type, void* data );

[StructLayout(La yout::Sequentia l)]
__gc public struct NeroIdleCallbac k
{
public:
IdleCallbackHan dler* ncCallbackFunct ion;
void* ncUserData;
};

[StructLayout(La yout::Sequentia l)]
__gc public struct NeroUserDialogC allback
{
public:
UserDialogCallb ackHandler* ncCallbackFunct ion;
void* ncUserData;
};

[StructLayout(La yout::Sequentia l)]
__gc public struct NeroSettings
{
public:
const char *nstNeroFilesPa th;
const char *nstVendor, *nstSoftware;
const char *nstLanguageFil e;
NeroIdleCallbac k* nstIdle;
NeroUserDialogC allback* nstUserDialog;
bool nstEnableOverbu rn;
DWORD nstOverburnSize ;
};

I configure this structure, and the callback substructures, in the constructor of my wrapper class as follows:

theSettings = new NeroSettings();
theSettings->nstNeroFilesPa th = "NeroFiles" ;
theSettings->nstLanguageFil e = "Nero.txt";

theSettings->nstEnableOverb urn = false;
theSettings->nstOverburnSiz e = 0;

theSettings->nstIdle = new NeroIdleCallbac k();
theSettings->nstIdle->ncCallbackFunc tion = new IdleCallbackHan dler(this, &Nero::FireIdle Event); theSettings->nstIdle->ncUserData = idleData;

theSettings->nstUserDialo g = new NeroUserDialogC allback();
theSettings->nstUserDialo g->ncCallbackFunc tion = new UserDialogCallb ackHandler(this , &Nero::FireUser DialogEvent);
theSettings->nstUserDialo g->ncUserData = userdlgData;

theSettings->nstVendor = "ahead";
theSettings->nstSoftware = "Nero - Burning Rom";

Everything works fine during initialization, and in retrieving certain basic information from the legacy codebase.

But when I call a legacy function that triggers the IdleCallback, I get the NullReferenceEx ception:
void AvailableDrives ( NERO_MEDIA_TYPE mtFlags )
{
// this next line blows up... but the IdleCallback gets called first
NERO_SCSI_DEVIC E_INFOS* drives = NeroGetAvailabl eDrivesEx(MEDIA _CD, NULL);
...
}

The interesting part is the part mentioned in that comment: the IdleCallback method gets called, and returns, but then the exception gets thrown.

And what's really interesting is that setting the ncCallbackFunct ion delegate/function pointer to NULL in the NeroIdleCallbac k structure -- which forces the legacy codebase to NOT call the IdleCallback -- avoids the exception.

Sorry for being so long-winded.

What I'm wondering is, am I handling the function pointer concept correctly here? Is it possible that I've done something wrong so that when the callback returns, it tries to return to a place that doesn't exist?

- Mark

Nov 17 '05 #2
Thanx, Ernesto! That article was a big help!
Nov 17 '05 #3

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

Similar topics

5
2498
by: Ian Richardson | last post by:
I'm writing some code which does one thing when onreadystatechange occurs, e.g. handle.onreadystatechange = function() { blah(handle,other_params) }; ....but sometimes I need to add another, so it becomes: handle.onreadystatechange = function() { whatever_was_previously_defined(whatever);some_other_function(handle) };
3
1490
by: ray.ackley | last post by:
I know you can do this in C++ as I remember doing it in my college days (just a few years ago), but I'm having trouble finding reference on how to do it with a managed language. What I have is a wrapper function that performs a somewhat complex enumeration of different objects that can be used to accomplish many different tasks on each object. I have each task seperated into it's own function because embedding all the functions into...
6
1240
by: Peter Oliphant | last post by:
Here is a simplification of my code. Basically, I have a class (A) that can be constructed using a function pointer to a function that returns a bool with no parameters. I then want to create an instance of this class (A) in another class (B) which uses one of its own methods of the 'proper form' to initialize the instance. But I get an error: __gc class ClassA { public: ClassA( bool (*func)(void) ) {//---some code---//} // constructor
10
15182
by: ChrisB | last post by:
Coming from a C/C++ background, how would I pass a function pointer to a function? I want to write a function that handles certain thread spawning. Here's what I'm trying to invision: function( thesub as <functionptr?> ) dim t as new system.threading.thread( _ new system.threading.threadstart( Addressof thesub )) .... How can I get something like that going in VB.Net?
10
3422
by: banleong | last post by:
To all gurus, I am currently converting some of C++ codes to VB.net The C++ Codes is as follows : ================= C++ CODE ================== typedef struct _tagBBCameraParameter { unsigned int preview_width;
5
1949
by: sajin | last post by:
Hi All.. We are using VB .Net 2005 for implementing an API. API needs to generate events. For this client wants us to use Windows Callback (delegate implementation). The intention of using Windows Callback is to generalise our API so that it can be compatible with any other language e.g. C++. Using normal delegates will not help us since delegates is a VB.Net feature and any other language cant make a use of it. Could please
6
2163
by: jmDesktop | last post by:
In a function that takes another function (function pointer) as a argument, or the callback function, which is the one that "calls back"? I'm having a hard time understanding the language. Am I right that if function A is: function A(*function pointer to B aka callback func, other arguments) { call (B); //calls }
10
2130
by: vcquestions | last post by:
Hi. Is there way to have a function pointer to a delegate in c++/cli that would allow me to pass delegates with the same signatures as parameters to a method? I'm working with managed code. Let's say we have 2 delegates: public delegate void FirstDelegate( int i ); public delegate void SecondDelegate( int i );
6
8545
by: sasha | last post by:
I have a c++ code that callls csharp. Now I want to be able to pass a function pointer from C++ to Csharp code and have c# callback to it. Is it possible and how? Here is what I have so far : #include "windows.h" #include <stdio.h> #import "CSDll.tlb" named_guids int main(int argc, char* argv)
0
10051
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
10000
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
9866
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
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3968
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3571
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.