(cont'd) Callback interface to communicate from native C++ -> CLi\C++Options

  • Thread starter Thread starter DaTurk
  • Start date Start date
D

DaTurk

Sorry, some how I clicked post before I finished.

Hi,

I'm writing an application that has a native c++ layer, and a CLi/C++
layer above it. Above the CLi layer will be C# but that's not
relavent to this post. My question has to do with communicating
between the native layer to the CLi layer. Typically how I do this
is
this ...


**CLi/C++ Layer**

******************************
--> MTest.h

namespace Test
{
private delegate void TestDelegate(Int32);

public ref class Test
{
private:
GCHandle testHandle_;
TestDelegate^ testDelegate_;

UnmanagedTest* uTest_;

void init();
void registerNativeCallback();

void TestCallback(Int32);
public:
Test();
}
}

******************************

--> MTest.cpp

#include "Test.h"

using namespace Test;

Test::Test()
{
init();
registerNativeCallback();
}

void Test::init()
{
uTest_ = new UnmanagedTest();

testDelegate_ = gcnew TestDelegate( this, &Test::TestCallback );
testHandle_ = GCHandle::Alloc( testDelegate_, GCHandle::Normal );
}

void Test::registerNativeCallback()
{
IntPtr ip =
Marshall::GetFunctionPointerForDelegate( testDelegate_ );
uTest_->RegisterCallback( static_cast< void(__stdcall *)(int)
( ip.ToPointer() ) );
}

void Test::TestCallback(Int32 pointlessParam)
{
}

******************************

The unmanged class UnmanagedTest is just a native class that has a
function pointer variable that is set by the call to RegisterCallback.

My question is this, I need to do the exact same thing that I'm doing
here, but with an interface.

Is this possible? And if it is, can you please either show me how or
point me in the right direction?

Thank you in advance,

Matthew
 
**CLi/C++ Layer**

public ref class Test
{
private:
[...]
UnmanagedTest* uTest_;
[...]
******************************

--> MTest.cpp
[...]
void Test::init()
{
uTest_ = new UnmanagedTest();
[...]
The unmanged class UnmanagedTest is just a native class

I think that you should use the IDispose pattern for that class.
In fact, you create an instance of the *unmanaged* class UnmanagedTest() -
using C++ 'new' - but this instance must be freed properly (else you can
have leaks).

My question is this, I need to do the exact same thing that I'm doing
here, but with an interface.

Sorry, but I don't understand this part...

What should be defined as an interface?
What should this interface seem like? What should be the purpose of this
interface?

Thanks,
Giovanni
 
Back
Top