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

Managed C++/CLI

 
GeneralRe: Add/Remove items in a listBox Pin
led mike31-Jan-08 5:44
led mike31-Jan-08 5:44 
GeneralRe: Add/Remove items in a listBox Pin
rover_boy1-Feb-08 3:57
rover_boy1-Feb-08 3:57 
GeneralRe: Add/Remove items in a listBox Pin
led mike1-Feb-08 4:19
led mike1-Feb-08 4:19 
GeneralRe: Add/Remove items in a listBox Pin
newkid1-Feb-08 4:42
newkid1-Feb-08 4:42 
QuestionHow to wrap MFC control in C++/CLI Pin
BinName30-Jan-08 19:06
BinName30-Jan-08 19:06 
AnswerRe: How to wrap MFC control in C++/CLI Pin
led mike31-Jan-08 4:47
led mike31-Jan-08 4:47 
AnswerRe: How to wrap MFC control in C++/CLI Pin
Zeinpresiyo11-Feb-08 3:18
Zeinpresiyo11-Feb-08 3:18 
QuestionNeed help with twisted callback issue Pin
MissingLinkError30-Jan-08 13:13
MissingLinkError30-Jan-08 13:13 
I have callback[delegate] in managed code being called by native code. It works fine the first time but on the next call the native code blows up. Bear with me the structure is a little twisted, but I'll try my best to explain.

Here's the general structure:
Client
|
--->ServiceInterface
|
--->NativeService

The main question is how should I be passing the callback to the native code from the ServiceInterface initialization?.
Should the delegate be pinned before passing it to the native code?
Confused | :confused:

The following is not exact code but a snapshot of how the code is interacting.

// Managed code
#include "ServiceInterface.h"
ref class Client
{
public:
ServiceInterface ^mngService;
Client()
{
mngService = gcnew ServiceInterface(this);
}
void initialize()
{...}
void Task1()
{
mngService->registerCallback(Task1_Callback);
}
void Task2()
{
mngService->registerCallback(Task2_Callback);
}

static int Task1_Callback(void *pdata, MessageType event)
{
Console::WriteLine(L"Task 1 completed...");
// cast and remove listener for task 1
return 1;
}
static int Task2_Callback(void *pdata, MessageType event)
{
Console::WriteLine(L"Task 2 completed...");
// cast and remove listener for task 2
return 2;
}
};

// ServiceInterface.h

#include <nativecode.h>

public delegate int Callbackfunction(void *pdata, MessageType event);
ref class ServiceInterface
{
private:
static list<Callbackfunction> callbacklist;
NativeService *service;
static Callbackfunction ^MessageProcessor;
static GCHandle ghMasterCallback;
static IntPtr ipMasterCallback;

ServiceInterface(Object ^client)
{
service = new NativeService();

//// Thought this was the way to pass function pointer to native code,
//// but crashed on the first callback.
//MessageProcessor= gcnew Callbackfunction(&Service::MessageEngine);
//ghMasterCallback = GCHandle::Alloc(MessageProcessor);
//ipMasterCallback = Marshal::GetFunctionPointerForDelegate(MessageProcessor);
//service->RegisterEventProcessor((Callbackfunction)ipMasterCallback.ToPointer(), client(Void * Value));

// Instead this method seem to work, at least for the first callback but dies on the second callback[Task 2]
service->RegisterEventProcessor((Callbackfunction)&MessageEngine, client(Void * Value));
callbacklist = new list<Callbackfunction>;
}

void registerCallback(Callbackfunction ^callback)
{
callbacklist->push_back(callback);
}
void removeCallback(Callbackfunction ^callback)
{ // removes the given callback function}

// Registered as EventProcessor with NativeCode
// Calls delegates in managed code
int MessageEngine(void *pdata, MessageType event)
{
list <Callbackfunction>::iterator Iter;
for ( Iter = callbacklist.begin( ); Iter != callbacklist.end( ); Iter++ )
static_cast<Callbackfunction>(*Iter)(pdata,event);
}

};



// UNManaged code
// nativecode.h

enum MessageType
{
EVENT_START,...
};

typedef int (* Callbackfunction)(void *pdata, MessageType event);

class NativeService
{
private:
void *clientContext;
public:
NativeService(){...}
Initialize(){...}

// Callback function to be defined in managed interface
Callbackfunction EventProcessor;
DoWorkFunction
{
:
:
// Message generated from service
MessageType newMessage;
DispatchCallback(EventProcessor(clientContext, newMessage));
}

RegisterEventProcessor(Callbackfunction processor, void * client)
{
EventProcessor = processor;
clientContext = client;
}
};
GeneralRe: Need help with twisted callback issue [modified] Pin
Luc Pattyn30-Jan-08 15:14
sitebuilderLuc Pattyn30-Jan-08 15:14 
GeneralRe: Need help with twisted callback issue Pin
MissingLinkError1-Feb-08 9:29
MissingLinkError1-Feb-08 9:29 
GeneralInterop problem Pin
teejayem30-Jan-08 5:37
teejayem30-Jan-08 5:37 
GeneralRe: Interop problem Pin
led mike30-Jan-08 5:43
led mike30-Jan-08 5:43 
GeneralRe: Interop problem Pin
teejayem30-Jan-08 6:58
teejayem30-Jan-08 6:58 
GeneralRe: Interop problem Pin
led mike30-Jan-08 7:44
led mike30-Jan-08 7:44 
GeneralRe: Interop problem Pin
teejayem30-Jan-08 8:03
teejayem30-Jan-08 8:03 
QuestionProperties: How Do You Declare/Observe in VS2008? Pin
W Balboos, GHB29-Jan-08 4:49
W Balboos, GHB29-Jan-08 4:49 
AnswerRe: Properties: How Do You Declare/Observe in VS2008? Pin
W Balboos, GHB29-Jan-08 5:30
W Balboos, GHB29-Jan-08 5:30 
GeneralRe: Properties: How Do You Declare/Observe in VS2008? Pin
Scott Dorman29-Jan-08 8:27
professionalScott Dorman29-Jan-08 8:27 
GeneralRe: Properties: How Do You Declare/Observe in VS2008? Pin
W Balboos, GHB31-Jan-08 3:06
W Balboos, GHB31-Jan-08 3:06 
GeneralRe: Properties: How Do You Declare/Observe in VS2008? Pin
Scott Dorman31-Jan-08 3:32
professionalScott Dorman31-Jan-08 3:32 
GeneralRe: Properties: How Do You Declare/Observe in VS2008? Pin
W Balboos, GHB1-Feb-08 1:16
W Balboos, GHB1-Feb-08 1:16 
QuestionXMLWriter over internet Pin
dSolariuM28-Jan-08 21:15
dSolariuM28-Jan-08 21:15 
GeneralRe: XMLWriter over internet Pin
led mike29-Jan-08 4:59
led mike29-Jan-08 4:59 
QuestionHow to use mutual access to public data in parent and mdi child Pin
Chesnokov Yuriy28-Jan-08 21:10
professionalChesnokov Yuriy28-Jan-08 21:10 
GeneralRe: How to use mutual access to public data in parent and mdi child Pin
led mike29-Jan-08 4:51
led mike29-Jan-08 4:51 

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.