Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

c++ cli - C++/cli to c++: Error s incompatible with "void (__stdcall *)(int i, std::string str)"

I am new to C++/cli so please excuse me for silly questions or core mistakes:

My C++/CLI class Library that is called by C# has only one method:

ThreadsCppWrapper.h

namespace namespace ThreadsCppWrapper {

public ref class ThreadsCppWrapperClass
    {
        public:
        ThreadsCppWrapperClass(); // Constructor

        System::String^ mytrainOp(System::MulticastDelegate^ callbackfunction);
    private:
        MyThreadsCpp::MyThreadsCppClass* ptr;
    };
}

ThreadsCppWrapper.cpp

System::String^ ThreadsCppWrapper::ThreadsCppWrapperClass::mytrainOp(System::MulticastDelegate^ callbackfunction){

System::String^ toReturn = gcnew System::String("");

msclr::interop::marshal_context context;
System::IntPtr intPtr = System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(callbackfunction);
if (intPtr != System::IntPtr::Zero)
{
    myUpdateProgress functPtr = static_cast<myUpdateProgress>(intPtr.ToPointer());
    string stdstringResult = ptr->train(functPtr); // Error Here   // ERRORLINE

    //toReturn = context.marshal_as<System::String^>(stdstringResult);
}
else
{
    System::Console::WriteLine("Error: Could not cast delegate!!");
}
return toReturn;
}

The C++ class that is called from c++/cli class is:

MyThreadsCpp.h

namespace MyThreadsCpp{

class MyThreadsCppClass
{
public:
    MyThreadsCppClass();
    string train(void(CALLBACK *myUpdateProgress)(int i, string str));
};

}

MyThreadsCpp.cpp

string MyThreadsCpp::MyThreadsCppClass::train(void(CALLBACK *myUpdateProgress)(int i, string str)){

double sum = 0.0;
long max = 100 * 1000;
int perc = 0;;
string trainstat = "Training Status: Starting Training...";
ostringstream oss;

    myUpdateProgress(perc, trainstat);
for (long n = 0; n < max; n++){
    sum += 4.0*pow(-1.0, n) / (2 * n + 1.0);
    int rem = n % (max/10);
    if (rem == 0){
        perc = 100 * n / max;
        cout << perc << "%" << endl;
        /* report completion status */
        oss << "Training Status: " << perc;
        myUpdateProgress(perc, oss.str());
    }
}
cout << "100%" << endl;
ostringstream oss;
oss << "Result = " << sum;
return oss.str();
}

It compiles fine.

Intellisense Error in wrapper class ERRORLINE: 1 argument of type "myUpdateProgress" is incompatible with parameter of type "void (__stdcall *)(int i, std::string str)" in ThreadsCppWrapper.cpp

Experts, Please help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You cannot use std::string and System::String^ interchangeably. You must convert them to one type. Forceful conversion of type or the function-pointer/callback won't work - it will break at runtime, if not compile time.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...