Click here to Skip to main content
15,910,211 members
Home / Discussions / Managed C++/CLI
   

Managed C++/CLI

 
GeneralRe: Scrollbar in c++/cli winform Pin
Lighter Joul24-May-11 16:55
Lighter Joul24-May-11 16:55 
QuestionStatic and Global Variables? Pin
VonHagNDaz19-May-11 3:08
VonHagNDaz19-May-11 3:08 
AnswerRe: Static and Global Variables? Pin
Mark Salsbery19-May-11 8:28
Mark Salsbery19-May-11 8:28 
QuestionDateTime->Parse Pin
VonHagNDaz16-May-11 4:34
VonHagNDaz16-May-11 4:34 
AnswerRe: DateTime->Parse [modified] Pin
Mark Salsbery16-May-11 7:26
Mark Salsbery16-May-11 7:26 
GeneralRe: DateTime->Parse Pin
VonHagNDaz16-May-11 10:56
VonHagNDaz16-May-11 10:56 
GeneralRe: DateTime->Parse Pin
Mark Salsbery16-May-11 12:57
Mark Salsbery16-May-11 12:57 
GeneralRe: DateTime->Parse Pin
VonHagNDaz17-May-11 1:57
VonHagNDaz17-May-11 1:57 
Question[C++/CLI]problem with unmanaged C library and callback functions Pin
re dei giovani16-May-11 3:39
re dei giovani16-May-11 3:39 
AnswerRe: [C++/CLI]problem with unmanaged C library and callback functions Pin
John Schroedl16-May-11 5:22
professionalJohn Schroedl16-May-11 5:22 
GeneralRe: [C++/CLI]problem with unmanaged C library and callback functions Pin
re dei giovani17-May-11 3:27
re dei giovani17-May-11 3:27 
GeneralRe: [C++/CLI]problem with unmanaged C library and callback functions Pin
re dei giovani17-May-11 3:55
re dei giovani17-May-11 3:55 
AnswerRe: [C++/CLI]problem with unmanaged C library and callback functions Pin
Luc Pattyn17-May-11 4:22
sitebuilderLuc Pattyn17-May-11 4:22 
GeneralRe: [C++/CLI]problem with unmanaged C library and callback functions Pin
re dei giovani18-May-11 2:15
re dei giovani18-May-11 2:15 
Hi luc,
thantks for advice.
Reading your article, I had an idea:
- I haven't used gcroot but directly GCHandle.
- I must casting because parameter library function is void*.

This is what I've done:
1) I 've used GCHandle to build an handle to object (for my example FORM1) and I've used GCHandle.Alloc() to reserve memory when i send object refer from managed code to unmanaged library. I' ve done this in the constructor. So it is:
Form1(void)
{
    InitializeComponent();
    evento=gcnew eventRegistered(Form1::cdeclEventCallback);
    gch =GCHandle::Alloc(evento);
    ip=Marshal::GetFunctionPointerForDelegate(evento);
    cb=static_cast<eventCallback_t>(ip.ToPointer());
    GC::Collect();
    gch3=GCHandle::Alloc(this);  //<-**************
    this->registerEvent();
}

2) I've used GCHandle::ToIntPtr to obtain pointer of handle to send like parameter.I've done this in my registerEvent() function when i call library funciton. registerEvent() now is:
void Form1::registerEvent()
{
    error_status er_return=TED_PixRad_RegisterEventCallback(cb,(void*)(GCHandle::ToIntPtr(gch3)));  //<******

    if (this->TestError(er_return)){
        this->button2->Visible=true;
    }
}


3) Since library function receives void* customData parameter and it returns this in callback function, I must cast my pointer Form1 refer from IntPtr to void*. After, in the callback function I must casting from void* to IntPtr and in second time I must casting from intPtr to Form1^.
The implementation of the callback funciton now is:
static void Form1::cdeclEventCallback(const event_id eventID, const Event *eventData, void* customData )
{
    GCHandle gch4 = GCHandle::FromIntPtr((IntPtr)(customData)); //<-***************
    Form1^ frm = (Form1^)gch4.Target;                           //<-***************
    frm->label3->Text="GESTIONE EVENTO";
    frm->label3->Refresh();
    frm->processEvent(eventID,"");                           //<-***************

}


Now I must call processEvent like an object function and not like Form1 external function.
---------------------------------------------------------------

I compiled without problems and doing some tests I've seen that behavior is correct, that the occurrence of an event correctly calls the callback function and that this uses the correctly reference at Form1.
Less than denials I think the problem is solved, thank you all for your help.

Smile | :)
AnswerRe: [C++/CLI]problem with unmanaged C library and callback functions Pin
Luc Pattyn18-May-11 2:46
sitebuilderLuc Pattyn18-May-11 2:46 
QuestionChanging a C++/CLI project from static lib to DLL causing app to crash under Win7 but works under WinXP Pin
TrevorPT13-May-11 7:20
TrevorPT13-May-11 7:20 
AnswerRe: Changing a C++/CLI project from static lib to DLL causing app to crash under Win7 but works under WinXP Pin
John Schroedl13-May-11 11:01
professionalJohn Schroedl13-May-11 11:01 
GeneralRe: Changing a C++/CLI project from static lib to DLL causing app to crash under Win7 but works under WinXP Pin
TrevorPT13-May-11 11:59
TrevorPT13-May-11 11:59 
GeneralRe: Changing a C++/CLI project from static lib to DLL causing app to crash under Win7 but works under WinXP Pin
John Schroedl13-May-11 14:31
professionalJohn Schroedl13-May-11 14:31 
GeneralRe: Changing a C++/CLI project from static lib to DLL causing app to crash under Win7 but works under WinXP Pin
TrevorPT16-May-11 11:19
TrevorPT16-May-11 11:19 
AnswerRe: Changing a C++/CLI project from static lib to DLL causing app to crash under Win7 but works under WinXP Pin
jschell13-May-11 12:32
jschell13-May-11 12:32 
AnswerRe: Changing a C++/CLI project from static lib to DLL causing app to crash under Win7 but works under WinXP Pin
TrevorPT17-May-11 6:12
TrevorPT17-May-11 6:12 
QuestionPassing an ArrayList by Reference? [modified] Pin
VonHagNDaz13-May-11 2:39
VonHagNDaz13-May-11 2:39 
AnswerRe: Passing an ArrayList by Reference? Pin
John Schroedl13-May-11 3:29
professionalJohn Schroedl13-May-11 3:29 
GeneralRe: Passing an ArrayList by Reference? Pin
VonHagNDaz13-May-11 4:15
VonHagNDaz13-May-11 4:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.