I have a big class from extern library and I need to use a unmanaged callback in managed code. Unmanaged code simplified:
typedef std::function<void(const std::string &, Float)> ProgressCallback;
class MeshGenerator{
public:
ProgressCallback progress;
/// <summary>
/// set callback
/// </summary>
/// <param name="_callback"></param>
inline void SetCallback(ProgressCallback _callback){ this-> progress =_callback; }
};
Now my manage Code simplified:
public delegate void CallbackDelegate(String^ cap, float data);
public ref class MeshWrapper
{
private:
MeshGenerator *gen;
public:
[MarshalAsAttribute(UnmanagedType::FunctionPtr)]
CallbackDelegate^ _Delegate;
inline MeshWrapper(){
this->gen = new MeshGenerator();
_Delegate = gcnew CallbackDelegate(this, &MeshWrapper::Progress);
auto ptr = Marshal::GetFunctionPointerForDelegate(_Delegate).ToPointer();
ProgressCallback *p = static_cast<ProgressCallback*>(ptr);
this->gen->SetCallback(*p);
}
inline void Progress(String^ cap, float s){ System::Console::WriteLine(cap + s);}
};
this code compile, but in MeshWrapper constructor the static cast ("static_cast<ProgressCallback*>(ptr);") give me an empty delegate.
I'm probably making some conceptual mistakes. Thanks for your help.
std::stringis not compatible with dotnet string