| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| C-Sharp Programming Post questions, answers, and comments about C#. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Delegate Proplem
I trying use 1 DLL write by C++ in project C# but i hava a proplem!
In C++, when i create 1 event, using callback.For example : create event MouseMove : create 1 procedure is void CALLBACK MouseMoveProc(), and to handle events that it uses CadOnEventMouseMove(MouseMoveProc);in the form CadOnEventMouseMove void CadOnEventMouseMove ( F_MOUSEMOVE pFunc / / pointer to event procedure ); To subscribe to the events which, in C # you do the following: 1 declare delegate to take the place of cursor to the function: public delegate void MouseDelegate(int hDwg, int Button, int Key, int Xwin, int Ywin, double Xdwg, double Ydwg, double Zdwg); And then, create event : CadOnEventMouseMove(new VeCAD.MouseDelegate(MouseMoveProc)); When compiled no error but a few seconds after the application is crash, and errors arising : "A callback was made on a garbage collected delegate of type 'VeCad_CS1!VeCAD.MouseDelegate::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called." Please help me! |
|
#2
|
|||
|
|||
|
Re: Delegate Proplem
Sorry I could not clearly understand your english.
You mean to say you have written the handler in a c++ dll ? and trigerring the event happens in the C# application? |
|
#3
|
|||
|
|||
|
Re: Delegate Proplem
Never seen this error before or tried to do what you're doing so i may not be that much help. You might try the following code though. Its possible that the GC doesn't like you instanciating a new object in the parameter of an unmanaged function. You might need to bring the
Code:
new VeCAD.MouseDelegate(MouseMoveProc) Heres the new code you can try Code:
public delegate void MouseDelegate(int hDwg, int Button, int Key, int Xwin, int Ywin, double Xdwg, double Ydwg, double Zdwg);
public event MouseDelegate MouseMove;
MouseMove += new MouseDelegate(MouseMoveProc);
CadOnEventMouseMove(MouseMove);
|
|
#4
|
||||
|
||||
|
Re: Delegate Proplem
Quote:
Code:
private void AnyMethodSettingTheDelegate(){
// because you create it directly as the callback method parameter
// you have no chance to have the pointer to this delegate
// method stored anywhere in your class. So this runs out of
// scope instantly when the method is finished
CadOnEventMouseMove(new VeCAD.MouseDelegate(MouseMoveProc));
...
} // just when the method is finished the delegate is out of scope !!!
Code:
namespace VeCad{
// define the delegate
public delegate void MouseDelegate(int hDwg, int Button, int Key, int Xwin, int Ywin, double Xdwg, double Ydwg, double Zdwg);
public class VeCad{
// create a field in your class so you have the delegate living as long as this class lives
public event MouseDelegate MouseMove;
// in the code wherever it is needed maybe in the classes CTor or
// where you want to set the callback you initialize the delegate to this adress
MouseMove += new MouseDelegate(MouseMoveProc);
// and now you set it to your method that uses the callback
CadOnEventMouseMove(MouseMove);
__________________
Jonny PoetTo be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. ! ![]() Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code] If anyone felt he has got help, show it in rating the post. Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ? My latest articles : Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7 |
![]() |
| Bookmarks |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|