Managed C++ How to pass a pointer to a class instance to a static callback?

So in my quest to integrate a native C++ dll callback into a managed C++ project, I have now succeeded (with much help!) to import the dll calls including the callback call into the project by the use of delegates. The callback is a static member function of a managed class, in fact the class of the only Form of that small .NET project.

Now my problem is that I don't know how to get access to the members of the managed class from the callback function. Indeed since it is a static function, I cannot use the handy "this->" function.

Back in the MFC days, I would have used the AfxGetApp() function to grant me access to the outside world, is there an equivalent in .NET?

Another option might be this: I can pass a "void* pContext" pointer to the dll function which registers the callback function. So I thought I should cast the "this" pointer to my Form class instance to a "void*" pointer and do the reverse from inside my callback.

The code looks like this :


typedef void (__clrcall *func_t2)(TestCamera2::Form1 ^const);//for casting from a Form^ type to a void* type

func_t fp2 = func_t(Marshal::GetFunctionPointerForDelegate(fp).ToInt32());//delegate to the callback function

m_lCallbackRegistration = CameraAddStreamingCallback(m_hCamera, fp2, func_t2(this));//this compiles fine


Then in my callback I have to cast back my pContext pointer from void* to Form^.

I tried to do it the following way:


typedef TestCamera2::Form1 ^const (__clrcall *func_t3)(System::Void*);
func_t3(pContext)->Preview->Name = L"Preview";//doing something on a member of the Form class


However I get the following compiler error which makes me think that my cast did not succeed:

error C2227: left of '->Preview' must point to class/struct/union/generic type

What am I doing wrong...?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

evilrixSenior Software Engineer (Avast)Commented:
The typedef you've created is creating a function pointer and casting the void * to that function pointer, this is why it's not working. That trick will work when you can't to cast a static function pointer to void * and back but not for this. Have you tried something like the following?

typedef TestCamera2::Form1 ^const pForm pform_t;
pform_t pForm = form_t(pContext);

This is attempting to constructor cast a form pointer (similar to constructor casting a function pointer).

All this is doing is the same as this...

typedef int * pint_t;
int i = 0;
void * pvoid = &i;
int *pint = pint_t(pvoid);

It's constructing a new pointer object and using it to initialize your pointer type. It's not different from this...

int i = int(0);

NB. I am unable to test this myself -- sorry.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bobsinclare666Author Commented:
Thanks Evilrix,

However your code does not compile. here are the errors:

error C2146: syntax error : missing ';' before identifier 'pform_t'
error C2065: 'pform_t' : undeclared identifier      
error C2146: syntax error : missing ';' before identifier 'pForm'
error C3861: 'form_t': identifier not found


evilrixSenior Software Engineer (Avast)Commented:

Ok Bob, I'll try and recreate here and get back to you.

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Which version of Visual Studio are you using?
evilrixSenior Software Engineer (Avast)Commented:
>> Which version of Visual Studio are you using?
His previous thread on a similar track suggests VS2005.
bobsinclare666Author Commented:
Yes, using VS2005.
Actually I went around the problem by making the callback non-static (originally I assumed it had to be static). So now I can call the class-members from my callback and it seems to work fine.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.