473,406 Members | 2,312 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,406 software developers and data experts.

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 NullReferenceException 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 IdleCallbackHandler( void* pUserData );
__delegate NeroUserDlgInOut UserDialogCallbackHandler( void* pUserData, NeroUserDlgInOut type, void*
data );

[StructLayout(Layout::Sequential)]
__gc public struct NeroIdleCallback
{
public:
IdleCallbackHandler* ncCallbackFunction;
void* ncUserData;
};

[StructLayout(Layout::Sequential)]
__gc public struct NeroUserDialogCallback
{
public:
UserDialogCallbackHandler* ncCallbackFunction;
void* ncUserData;
};

[StructLayout(Layout::Sequential)]
__gc public struct NeroSettings
{
public:
const char *nstNeroFilesPath;
const char *nstVendor, *nstSoftware;
const char *nstLanguageFile;
NeroIdleCallback* nstIdle;
NeroUserDialogCallback* nstUserDialog;
bool nstEnableOverburn;
DWORD nstOverburnSize;
};

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

theSettings = new NeroSettings();
theSettings->nstNeroFilesPath = "NeroFiles";
theSettings->nstLanguageFile = "Nero.txt";

theSettings->nstEnableOverburn = false;
theSettings->nstOverburnSize = 0;

theSettings->nstIdle = new NeroIdleCallback();
theSettings->nstIdle->ncCallbackFunction = new IdleCallbackHandler(this, &Nero::FireIdleEvent);
theSettings->nstIdle->ncUserData = idleData;

theSettings->nstUserDialog = new NeroUserDialogCallback();
theSettings->nstUserDialog->ncCallbackFunction = new UserDialogCallbackHandler(this,
&Nero::FireUserDialogEvent);
theSettings->nstUserDialog->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 NullReferenceException:

void AvailableDrives( NERO_MEDIA_TYPE mtFlags )
{
// this next line blows up... but the IdleCallback gets called first
NERO_SCSI_DEVICE_INFOS* drives = NeroGetAvailableDrivesEx(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 ncCallbackFunction delegate/function pointer to
NULL in the NeroIdleCallback 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 1623
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.com...
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 NullReferenceException 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 IdleCallbackHandler( void* pUserData );
__delegate NeroUserDlgInOut UserDialogCallbackHandler( void* pUserData, NeroUserDlgInOut type, void* data );

[StructLayout(Layout::Sequential)]
__gc public struct NeroIdleCallback
{
public:
IdleCallbackHandler* ncCallbackFunction;
void* ncUserData;
};

[StructLayout(Layout::Sequential)]
__gc public struct NeroUserDialogCallback
{
public:
UserDialogCallbackHandler* ncCallbackFunction;
void* ncUserData;
};

[StructLayout(Layout::Sequential)]
__gc public struct NeroSettings
{
public:
const char *nstNeroFilesPath;
const char *nstVendor, *nstSoftware;
const char *nstLanguageFile;
NeroIdleCallback* nstIdle;
NeroUserDialogCallback* nstUserDialog;
bool nstEnableOverburn;
DWORD nstOverburnSize;
};

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

theSettings = new NeroSettings();
theSettings->nstNeroFilesPath = "NeroFiles";
theSettings->nstLanguageFile = "Nero.txt";

theSettings->nstEnableOverburn = false;
theSettings->nstOverburnSize = 0;

theSettings->nstIdle = new NeroIdleCallback();
theSettings->nstIdle->ncCallbackFunction = new IdleCallbackHandler(this, &Nero::FireIdleEvent); theSettings->nstIdle->ncUserData = idleData;

theSettings->nstUserDialog = new NeroUserDialogCallback();
theSettings->nstUserDialog->ncCallbackFunction = new UserDialogCallbackHandler(this, &Nero::FireUserDialogEvent);
theSettings->nstUserDialog->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 NullReferenceException:
void AvailableDrives( NERO_MEDIA_TYPE mtFlags )
{
// this next line blows up... but the IdleCallback gets called first
NERO_SCSI_DEVICE_INFOS* drives = NeroGetAvailableDrivesEx(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 ncCallbackFunction delegate/function pointer to NULL in the NeroIdleCallback 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
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,...
3
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...
6
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...
10
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(...
10
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 {...
5
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...
6
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...
10
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. ...
6
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 :...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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
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...
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.