从非托管函数触发事件
我正在尝试使用指向托管对象的指针从非托管函数触发事件。我收到以下错误:
错误 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过手动定义事件并将
raise()
部分的访问级别更改为internal
或public
来完成此操作。 (默认为protected
或private
- 请参阅 ildjarn 的帖子 http://social.msdn.microsoft.com/forums/en-US/vclanguage/thread/ff743ebd-dba9-48bc-a62a-1e65aab6f2f9 了解详细信息。)以下内容为我编译:
另一种方法是定义一个可以触发事件的辅助函数。
You can do this by manually defining the event, and changing the access level of the
raise()
portion tointernal
orpublic
. (The default is eitherprotected
orprivate
-- 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:
An alternative would be to define a helper function that would fire the event.