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

Managed C++/CLI

 
GeneralRe: memory leak Pin
nineofhearts7-Nov-05 13:21
nineofhearts7-Nov-05 13:21 
GeneralRe: memory leak Pin
Christian Graus7-Nov-05 13:41
protectorChristian Graus7-Nov-05 13:41 
GeneralRe: memory leak Pin
Christian Graus7-Nov-05 13:47
protectorChristian Graus7-Nov-05 13:47 
GeneralRe: memory leak Pin
nineofhearts7-Nov-05 15:59
nineofhearts7-Nov-05 15:59 
GeneralRe: memory leak Pin
Christian Graus7-Nov-05 16:02
protectorChristian Graus7-Nov-05 16:02 
GeneralRe: memory leak Pin
Kevin McFarlane13-Nov-05 4:22
Kevin McFarlane13-Nov-05 4:22 
AnswerRe: memory leak Pin
Joe Woodbury11-Nov-05 20:19
professionalJoe Woodbury11-Nov-05 20:19 
QuestionCallback with "object" parameter fails Pin
TriLogic7-Nov-05 10:39
TriLogic7-Nov-05 10:39 
I am looking to learn how to implement non-simple callbacks to managed code unmanged C++. I can get a simple non-parametered callback examples working. But, I want to pass an object along for the ride through unamanged and have it show up as a parameter to the managed callback - perhaps the object will be in unmanaged code for along time before the callback - almost certainly during garbage collection. I would appreciate any help I can get - I know others have gocne through this and solved it.

Here is the code I am trying to use...

using namespace System;
using namespace System::Runtime::InteropServices;

#pragma unmanaged

// declare the unmanaged function prototype
typedef void (*MY_CALLBACK)(void *);

// Declaration of the unmanaged class
class CMyUnmanaged
{
public:
CMyUnmanaged() { }

// The unmanaged class method where the
// function pointer will be invoked.
void DoCallback( MY_CALLBACK myCallback, void *myParam )
{
// invoke the method
myCallback( myParam );
}
};

#pragma managed

namespace CallbackTest
{
// Protoype of the Delegate the matches the function pointer
// declaration the unmanaged code expects to receive.
public __delegate void CallbackDelegate( Object *obj );

// This is for marchalling the function pointer.
[StructLayoutAttribute( LayoutKind::Sequential, CharSet = CharSet::Unicode) ]
public __gc struct MyDelegate_Struc
{
[MarshalAsAttribute(UnmanagedType::FunctionPtr)]
CallbackDelegate* _Delegate;
};

// This is an attempt to marshall the object data we want to
// "pass through" unamanged code.
[StructLayoutAttribute( LayoutKind::Sequential, CharSet = CharSet::Unicode) ]
public __gc struct MyParam_Struc
{
[MarshalAsAttribute(UnmanagedType::Struct)]
Object* _Param;
};

// Managed class wrapper to be used for the callbacks.
public __gc class CMyManaged
{
private:
CMyUnmanaged __nogc *pUnManaged;
public:
CMyManaged()
{
// unmanaged class instance
pUnManaged = new CMyUnmanaged();
}
~CMyManaged()
{
// delete it because the GC won't
delete pUnManaged;
}

// here is where we try to get unmanaged code to invoke the callback.
void DoCallback( CallbackDelegate *pDelegate, Object * pObject )
{
// pin the object pointers
CallbackDelegate __pin *_cb = pDelegate;
Object __pin *_ob = pObject;

// these are the unmanaged types we need to pass
// to the unmanaged code that will be making the
// callback.
MY_CALLBACK fn;
void* ob;

// use interop to create the pointers we need
// from the objects we are passed.

// Create the Function Pointer
MyDelegate_Struc *s = new MyDelegate_Struc();
s->_Delegate = _cb;
Marshal::StructureToPtr( s, &fn, false );

// Attempt to create a void * pointer
MyParam_Struc *u = new MyParam_Struc();
u->_Param = _ob;
Marshal::StructureToPtr( u, &ob, false );

// make the method call (which fails...)
pUnManaged->DoCallback( fn, ob );
}
};

}

//
// and lastly the C# code that tries to run it all...
//
using System;
using CallbackTest;

namespace MyTest
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
string _test = "Hello World";

CMyManaged cmm = new CMyManaged();
cmm.DoCallback( new CallbackDelegate( ThisCallback ), _test );
}
static void ThisCallback( Object obj )
{
Console.WriteLine( obj.ToString() );
}
};
}

You know neither the day nor the hour.
QuestionC++ console Pin
iamboy7-Nov-05 9:05
iamboy7-Nov-05 9:05 
AnswerRe: C++ console Pin
Christian Graus7-Nov-05 9:50
protectorChristian Graus7-Nov-05 9:50 
QuestionAccessing control from another file Pin
Eran6-Nov-05 22:49
Eran6-Nov-05 22:49 
AnswerRe: Accessing control from another file Pin
Christian Graus7-Nov-05 9:53
protectorChristian Graus7-Nov-05 9:53 
Questionfstream help Pin
afrodriguez6-Nov-05 15:54
afrodriguez6-Nov-05 15:54 
AnswerRe: fstream help Pin
Christian Graus6-Nov-05 15:58
protectorChristian Graus6-Nov-05 15:58 
GeneralRe: fstream help Pin
qfegd8-Nov-05 14:28
qfegd8-Nov-05 14:28 
AnswerRe: fstream help Pin
Jeremy Thornton12-Nov-05 14:03
Jeremy Thornton12-Nov-05 14:03 
GeneralRe: fstream help Pin
Jeremy Thornton12-Nov-05 13:13
Jeremy Thornton12-Nov-05 13:13 
QuestionUnicode File I/O problem Pin
scottwalk5-Nov-05 16:56
scottwalk5-Nov-05 16:56 
AnswerRe: Unicode File I/O problem Pin
Jeremy Thornton12-Nov-05 16:14
Jeremy Thornton12-Nov-05 16:14 
QuestionC++ object in C Pin
djtosh5-Nov-05 15:53
djtosh5-Nov-05 15:53 
AnswerRe: C++ object in C Pin
toxcct6-Nov-05 0:29
toxcct6-Nov-05 0:29 
AnswerRe: C++ object in C Pin
Andre xxxxxxx17-Jan-06 0:11
Andre xxxxxxx17-Jan-06 0:11 
Questionquery about giving inputs thru keboard Pin
bug1015-Nov-05 1:49
bug1015-Nov-05 1:49 
AnswerRe: query about giving inputs thru keboard Pin
Christian Graus6-Nov-05 14:05
protectorChristian Graus6-Nov-05 14:05 
QuestionTrain free @Microsoft Pin
vikas amin4-Nov-05 22:43
vikas amin4-Nov-05 22:43 

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.