0

这是一个类库 clr/c++ 项目。A 类是非托管 c++,B 类是托管 c++。我想从 C# 应用程序创建 B 的对象,并用该对象调用“void Sign”并在 C# 中捕获 StatusEvent。如何从 A::call_A 调用 B::Showsts 以实现此目的?请记住 call_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);
    }
4

1 回答 1

0

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

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
}
于 2013-08-01T08:34:19.950 回答