2012-05-25 1 views
1

기능, 바인딩, C++ 및 관리 코드의 결합 : 관리되는 C++ 프로젝트 내에서나는 따라서 전달되는 함수 객체 (AuthenticateNotifyFunc)를 기대하는 C++ 기능이

class lc_Authenticate 
{ 
public: 
    typedef enum { 
    kAbort, 
    kContinue 
    } lc_AuthenticateStatus; 

    typedef std::tr1::function<lc_AuthenticateStatus (const string &msg)> AuthenticateNotifyFunc; 

    bool Authenticate(lc_AuthenticateParams &params, 
        AuthenticateNotifyFunc notifyFunc); 
} 

을, 나는를 시도하고있다 따라서 위의 함수에 전달할 매개 변수를 정의 :

public ref class Form1 : public System::Windows::Forms::Form 
{ 
    public: 
    lc_Authenticate::lc_AuthenticateStatus UpdateStatus(const string &msg) 
    { 
     <<DO SOMETHING>> 
     return(lc_Authenticate::kContinue); 
    } 
    void test() 
    { 
     string appKey, appSecret; 
     appKey = GetString(this->appKeyTextBox->Text); 
     appSecret = GetString(this->appSecretTextBox->Text); 

     lc_Authenticate dbauth; 
     lc_AuthenticateParams params(appKey, appSecret); 

     // DOESN'T COMPILE won't let me take address of member function 
     // or know about _1 
     lc_Authenticate::AuthenticateNotifyFunc func = 
      std::tr1::bind(&Form1::UpdateStatus, this, _1); 

     dbauth.Authenticate(params, func); 
    } 
}; 

그래서 나는 그것이 전달 기능이 있는지 여부를 상관하지 않는 방식으로 방법 ++는 C에 함수를 전달하는 일반적인 방법을 구현하기 위해 노력하고 정적 또는 멤버 함수. 그리고 관리 코드에서이를 어떻게 수행해야하는지 명확하지 않습니다.

+0

이 작업은 ['Marshal :: GetFunctionPointerForDelegate'] (http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.getfunctionpointerfordelegate.aspx)를 사용하여 수행 할 수 있지만 끔찍한 못생긴. _ 왜 이걸하고 싶니? C++ 코드와 C++/CLI 코드를 명확히 구분하는 것은 좋은 일입니다. 그들을 단단히 묶어두면 고통 스러울 것입니다. – ildjarn

+0

관련 : http://stackoverflow.com/questions/163757/how-to-use-boostbind-in-c-cli-to-bind-a-member-of-a-managed-class –

답변

3

디자인에 따라 관리되는 클래스의 인스턴스 메서드에 바인딩 할 수 없습니다. 가비지 수집기는 힙을 압축 할 때 객체를 움직여 이 변경됩니다. 관리 된 대리인을 사용해야합니다. 따라서 함수 <>에 필요한 안정적인 콜백을 제공하는 원시 도우미 클래스는 피할 수 없습니다. Marshal :: GetFunctionPointerForDelegate()를 사용하여 관리되는 코드로 돌아갈 수 있습니다.

관련 문제