473,385 Members | 1,606 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,385 software developers and data experts.

Good way to use native C++ callback as managed C++ delegates / events

Hello

I don't have C++/CLI 2.0 reference handy and the code below was written
after long hours of research and shallow readings. From what I see, the code
works and looks logical to me. Can you please review it and tell me if I
overlooked something.

On the other hand, can you suggest a good C++/CLI 2.0 book for starters?

Code purpose: To use native EnumWindows() and have its callback handled
within any user created delegate.

[begin]
#include "stdafx.h"

using namespace System;

class EnumWindowsProcThunk;

public ref class MyClass
{
private:
EnumWindowsProcThunk *m_thunkEnumWindows;
public:
delegate bool delOnEnum(int h);
delOnEnum ^OnEnum;

void EnumWindows();

MyClass();
};

private class EnumWindowsProcThunk
{
private:
msclr::auto_gcroot<MyClass^m_clr;
public:
static BOOL CALLBACK fwd(
HWND hwnd,
LPARAM lParam);

EnumWindowsProcThunk(MyClass ^clr);
};

MyClass::MyClass()
{
OnEnum = nullptr;
m_thunkEnumWindows = new EnumWindowsProcThunk(this);
}

void MyClass::EnumWindows()
{
::EnumWindows(&EnumWindowsProcThunk::fwd, (LPARAM)(m_thunkEnumWindows));
}

EnumWindowsProcThunk::EnumWindowsProcThunk(MyClass ^clr)
{
m_clr = clr;
}

BOOL CALLBACK EnumWindowsProcThunk::fwd(HWND hwnd,LPARAM lParam)
{
return static_cast<EnumWindowsProcThunk *>((void
*)lParam)->m_clr->OnEnum((int)hwnd) ? TRUE : FALSE;
}

ref class MyGUI
{
private:
public:
bool OnEnumHandler(int h)
{
Console::WriteLine("MyGUI: {0}", h);
return true;
}
};

int main(array<System::String ^^args)
{
MyClass ^mc = gcnew MyClass();

MyGUI ^g = gcnew MyGUI;

mc->OnEnum += gcnew MyClass::delOnEnum(g, &MyGUI::OnEnumHandler);

mc->EnumWindows();

return 0;
}
[end]
Mar 27 '07 #1
1 5835
On 27 mar, 16:28, "lallous" <lall...@lgwm.orgwrote:
Hello

I don't have C++/CLI 2.0 reference handy and the code below was written
after long hours of research and shallow readings. From what I see, the code
works and looks logical to me. Can you please review it and tell me if I
overlooked something.
<snip>

In .NET 2.0, you shall use
System::Runtime::InteropServices::Marshal::GetFunc tionPointerForDelegate

Arnaud
MVP - VC

Mar 27 '07 #2

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

Similar topics

3
by: Sai Kit Tong | last post by:
I posted for help on legacy code interface 2 days ago. Probably I didn't make it clear in my original mail. I got a couple of answers but none of them address my issues directly (See attached...
2
by: Gerda | last post by:
Hi! I've implemented many times an asynchronous call of a method with a call backfunction successfully. But to implement this with VB.NET is not so successfully. I can implement all events...
9
by: Lonewolf | last post by:
Hi everyone.. I was trying to implement callback fucntions in native codes in my VC8 class, and referred to some online codes which use native class nested within managed class. basically of the...
3
by: Lonewolf | last post by:
Hi all, I'm having difficulties passing data back to managed class from my native class when the data is generated from within a native thread in the native class itself. I will give the following...
0
by: draskin | last post by:
Hello, We have a situation where a managed C++ assembly links with native C++ dll. There is a callback mechanism which calls back into the managed code asynchronously. Since native classes...
2
by: Dave Burns | last post by:
Hello, We have a situation where a managed C++ assembly links with native C++ dll. There is a callback mechanism which calls back into the managed code asynchronously. Since native classes...
6
by: per9000 | last post by:
An interesting/annoying problem. I created a small example to provoke an exception I keep getting. Basically I have a C-struct (Container) with a function-pointer in it. I perform repeated calls...
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: smmk25 | last post by:
Before I state the problem, I just want to let the readers know, I am knew to C++\CLI and interop so please forgive any newbie questions. I have a huge C library which I want to be able to use in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.