Events 从非托管类对象调用托管类函数

Events 从非托管类对象调用托管类函数,events,delegates,function-pointers,managed-c++,Events,Delegates,Function Pointers,Managed C++,这是一个类库clr/c++项目。 类是非托管C++,B类管理C++。 我想从一个C#应用程序中创建一个B对象,用该对象调用“void Sign”,并捕获C#中的StatusEvent。 如何从A::call_A调用B::Showsts来实现这一点?请记住,调用A是从B类对象的委托调用的。 提前谢谢你 public class A{ public: int call_A(); }; public ref class B{ pr

这是一个类库clr/c++项目。 类是非托管C++,B类管理C++。 我想从一个C#应用程序中创建一个B对象,用该对象调用“void Sign”,并捕获C#中的StatusEvent。 如何从A::call_A调用B::Showsts来实现这一点?请记住,调用A是从B类对象的委托调用的。 提前谢谢你

public class A{
        public:
            int call_A();
    };
    public ref class B{
        private: 
            A* a1;
    public:
        void Sign(String^ ufile);
        void Showsts(string sts);
        delegate void GetResult(String^);
        event GetResult^ StatusEvent;
        SpyrusLib(void){
            a1=new A();
        }
    protected: ~SpyrusLib(){
            delete a1;
        }
    private:
        String^ str;
        delegate int MySignDelegate(String^);
        int MySign(String^ file);
        void Callbacksign(IAsyncResult ^ar);    
    };
    void B::Sign(String^ ufile){
        MySignDelegate^ signDel = gcnew MySignDelegate( this, &B::MySign );
        AsyncCallback^ cb = gcnew AsyncCallback( this, &B::Callbacksign);
        signDel->BeginInvoke(  ufile , cb, signDel );
    }
    int B::MySign(String^ file){
        stdstr=msclr::interop::marshal_as<std::string>(file);
        a1->call_A(stdstr);
    }
    void B::Showsts(string sts){
            str = gcnew String(sts.c_str());
            StatusEvent(str);
    }
    int A::call_A(string stat){
            ?-Showsts(stat);
    }
公共A类{
公众:
int调用_A();
};
B级公共参考文献{
私人:
A*a1;
公众:
无效符号(字符串^ufile);
void Showsts(字符串sts);
委托void GetResult(字符串^);
事件GetResult^StatusEvent;
SpyrusLib(无效){
a1=新的A();
}
受保护:~SpyrusLib(){
删除a1;
}
私人:
字符串^str;
delegate int MySignDelegate(字符串^);
int MySign(字符串^file);
无效回拨符号(IAsyncResult^ar);
};
void B::符号(字符串^ufile){
MySignDelegate ^signDel=gcnewmysigndelegate(this,&B::MySign);
AsyncCallback ^cb=gcnew AsyncCallback(this,&B::Callbacksign);
signDel->BeginInvoke(ufile、cb、signDel);
}
int B::MySign(字符串^file){
stdstr=msclr::interop::封送(文件);
a1->呼叫A(STDSR);
}
void B::Showsts(字符串sts){
str=gcnewstring(sts.c_str());
状态事件(str);
}
int A::call_A(字符串统计){
?Showsts(stat);
}

我不确定这是不是最好的解决方案,但我通过向类中添加以下内容解决了它:

typedef void (__stdcall * Unmanagedstatus)(string sts);
using namespace std;

public class A{
    private: 
        Unmanagedstatus sendmsg;
    public:
        int call_A();
        spyrus(Unmanagedstatus unm)
        {
            sendmsg=unm;
        }
};
public ref class B
{
    private: 
        delegate void Managedstatus(string);
        Managedstatus^ managed;
        IntPtr unmanaged;
        A* a1;
    public:
        SpyrusLib(void)
        {
            managed = gcnew Managedstatus(this, &B::Showsts);
            unmanaged = Marshal::GetFunctionPointerForDelegate(managed);
            a1=new A((Unmanagedstatus)(void*)unmanaged);
        }
}
int A::call_A(string stat){
    sendmsg(stat); // this will call B::Showsts and the events raised 
          //from Showsts are also working in the C# app
}