有关unmanaged函数调用managed实例

Scorpiour Scorpiour 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
最近遇到一个很困扰的难题:

在进行clr开发的时候,将数据从managed code中转成unmanaged并不麻烦,但是在unmanaged部分处理完毕之后希望将数据送回Managed部分时却遇到了麻烦。

代码的结构:

[code=c/c++]
//程序会生成一个managed WinForm实例

Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);

Application::Run(gcnew(mainForm));

//mainForm class的定义是

public ref class mainForm : public System::Windows::Forms::Form
{

//在某些Event触发的时候,会将String转成wchar_t[],并且送去unmanaged进行处理

private: System::Void textBox2_KeyPress(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e)
{
if(e->KeyChar == 13)
{
if(this->textBox2->Text->Length)
{
entryPtr->SendAsyncMessage(this->textBox2->Text,(void*)(pDataQueue));

//这里的entryPtr是一个unmanaged class指针,SendAysncMessage可以将String^转变成wchar_t[]并发送给其他的unmanaged 函数


//然后问题出现了,在对数据处理结束之后,我希望能够将数据送回到Form中
//比如最后一步的处理函数

static int formProcFunc(pIQueueNode ptr)
{
wchar_t* wstr = (wchar_t*)(ptr->getValue());
size_t len = wcslen(wstr);
array<wchar_t>^ arr = gcnew array<wchar_t>(len);
for(size_t i=0;i<len;i++)
{
arr[i] = wstr[i];
}
System::String^ str = gcnew System::String(arr);

return 0;
}

[/code]
到这里,数据被重新封装进了String^可供使用了,但是我发现无论如何我都无法获取Form的实例入口,更不要说调用其内部的函数了……传统的Unmanaged指针对Managed Code并不适用,而即使我可以传出this指针,我也不知道这个this指针的类型到底是什么……

因此各种求助……
...全文
评论 6
收藏 已收藏
分享
返回顶部
Scorpiour
VC.NET 7,538 成员 27,670 内容