从非托管函数触发事件

发布于 2024-12-11 05:35:52 字数 1256 浏览 1 评论 0原文

我正在尝试使用指向托管对象的指针从非托管函数触发事件。我收到以下错误:

错误 C3767:'ShashiTest::test::OnShowResult::raise':候选函数无法访问

我如何才能毫无问题地调用常规函数 ShowMessage?

#pragma once

#using<mscorlib.dll>
#using<System.Windows.Forms.dll> 

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Diagnostics;
using namespace System::Windows::Forms;

namespace ShashiTest {
    public delegate void ShowResult(System::String^);

    public ref class test
    {
    public:
        event ShowResult^ OnShowResult;
        test(void)
        {
        };
        void ShowMessage()
        {
            MessageBox::Show("Hello World");
        }
    };

    class ManagedInterface
    {
    public:
        gcroot<test^> m_test;
        ManagedInterface(){};
        ~ManagedInterface(){};

        void ResultWindowUpdate(std::string ResultString);
    };
}

void ShashiTest::ManagedInterface::ResultWindowUpdate(std::string ResultString)
{
    if(m_test)
    {
        System::String ^result = gcnew system::String(ResultString.c_str());
        m_test->OnShowResult(result);
    }
}

I am trying to fire an event from a unmanaged function using a pointer to managed object. I get the following error:

error C3767: 'ShashiTest::test::OnShowResult::raise': candidate function(s) not accessible

How ever I can call regular function ShowMessage without any issue?

#pragma once

#using<mscorlib.dll>
#using<System.Windows.Forms.dll> 

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Diagnostics;
using namespace System::Windows::Forms;

namespace ShashiTest {
    public delegate void ShowResult(System::String^);

    public ref class test
    {
    public:
        event ShowResult^ OnShowResult;
        test(void)
        {
        };
        void ShowMessage()
        {
            MessageBox::Show("Hello World");
        }
    };

    class ManagedInterface
    {
    public:
        gcroot<test^> m_test;
        ManagedInterface(){};
        ~ManagedInterface(){};

        void ResultWindowUpdate(std::string ResultString);
    };
}

void ShashiTest::ManagedInterface::ResultWindowUpdate(std::string ResultString)
{
    if(m_test)
    {
        System::String ^result = gcnew system::String(ResultString.c_str());
        m_test->OnShowResult(result);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨后咖啡店 2024-12-18 05:35:53

您可以通过手动定义事件并将 raise() 部分的访问级别更改为 internalpublic 来完成此操作。 (默认为 protectedprivate - 请参阅 ildjarn 的帖子 http://social.msdn.microsoft.com/forums/en-US/vclanguage/thread/ff743ebd-dba9-48bc-a62a-1e65aab6f2f9 了解详细信息。)

以下内容为我编译:

public ref class test
{
public:
    event ShowResult^ OnShowResult {
        void add(ShowResult^ d) {
            _showResult += d;
        }
        void remove(ShowResult^ d) {
            _showResult -= d;
        }
    internal:
        void raise(System::String^ str) {
            if (_showResult) {
                _showResult->Invoke(str);
            }
        }
    };
    test(void)
    {
    };
    void ShowMessage()
    {
        MessageBox::Show("Hello World");
    }
private:
    ShowResult^ _showResult;
};

另一种方法是定义一个可以触发事件的辅助函数。

You can do this by manually defining the event, and changing the access level of the raise() portion to internal or public. (The default is either protected or private -- see the post by ildjarn at http://social.msdn.microsoft.com/forums/en-US/vclanguage/thread/ff743ebd-dba9-48bc-a62a-1e65aab6f2f9 for details.)

The following compiled for me:

public ref class test
{
public:
    event ShowResult^ OnShowResult {
        void add(ShowResult^ d) {
            _showResult += d;
        }
        void remove(ShowResult^ d) {
            _showResult -= d;
        }
    internal:
        void raise(System::String^ str) {
            if (_showResult) {
                _showResult->Invoke(str);
            }
        }
    };
    test(void)
    {
    };
    void ShowMessage()
    {
        MessageBox::Show("Hello World");
    }
private:
    ShowResult^ _showResult;
};

An alternative would be to define a helper function that would fire the event.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文