提交c++回调到.net c++ /CLI Wrapper
Submitting C++ callback to .NET C++/CLI Wrapper
我一直在网上搜索以下问题的解决方案,但无法得到我的头围绕它。
我们有一个用c++编写的大型单片应用程序。为了将其"升级"到新的世界,我们将WPF视图嵌入到一个c++/CLI管理的包装器中。初始调用由c++通过一个smartpointer进行。
if (SUCCEEDED(ptr.CreateInstance(__uuidof(CloudServices))))
{
CRect rect;
pWndContainer->GetWindowRect(rect);
ptr->InitializeControl((LONG)pWndContainer->GetSafeHwnd(), ID_AIS_BALANCEMATRIX, bstr.m_str, rect.Width(), rect.Height());
}
在包装器类中,接口声明为
interface ICloudServices : IDispatch{
[id(1)] HRESULT InitializeControl([in] LONG hWndParent, [in] LONG controlTypeId, [in] BSTR parameters, [in] LONG width, [in] LONG height);
在包装器
中像这样实现STDMETHODIMP CCloudServices::InitializeControl(LONG hWndParent, LONG controlTypeId, BSTR parameters, LONG width, LONG height) { ... }
问题:一切正常,wpf视图在c++应用程序中呈现。现在我们需要从。net代码将信息发送回c++。
我如何提交一个非托管回调函数的包装器作为参数InitializeControl和我如何使用/转换它到相关的。net委托?
参见所需解决方案示意图
我们似乎没有找到这个问题的有效解决方案,所以我们最终使用WM_DATACOMPY发送回信息。这不是最有效的方法,但它有效。
public void SendCommand(Command command)
{
// Find the target window handle.
IntPtr hTargetWnd = NativeMethod.FindWindow("OurAppNameInCpp", null);
if (hTargetWnd == IntPtr.Zero)
{
throw new Exception("Sending '{0}'. Unable to find the "OurAppNameInCpp" window".Args(command.AsXmlMessage));
}
// Prepare the COPYDATASTRUCT struct with the data to be sent.
MyStruct myStruct;
myStruct.Message = command.AsXmlMessage;
// Marshal the managed struct to a native block of memory.
int myStructSize = Marshal.SizeOf(myStruct);
IntPtr pMyStruct = Marshal.AllocHGlobal(myStructSize);
try
{
Marshal.StructureToPtr(myStruct, pMyStruct, true);
COPYDATASTRUCT cds = new COPYDATASTRUCT();
cds.cbData = myStruct.Message.Length + 1;
cds.lpData = Marshal.StringToHGlobalAnsi(myStruct.Message);
// Send the COPYDATASTRUCT struct through the WM_COPYDATA message to
// the receiving window. (The application must use SendMessage,
// instead of PostMessage to send WM_COPYDATA because the receiving
// application must accept while it is guaranteed to be valid.)
NativeMethod.SendMessage(hTargetWnd, WM_COPYDATA, IntPtr.Zero, ref cds);
int result = Marshal.GetLastWin32Error();
if (result != 0)
throw new Exception("SendMessage(WM_COPYDATA) failed w/err 0x{0:X}".Args(result));
}
finally
{
Marshal.FreeHGlobal(pMyStruct);
}
}
相关文章:
- 在 C++/CLI 中将 .NET 事件从一个 DLL 引发到另一个 DLL
- C++/CLI System.AccessViolation在托管类中调用非托管函数时出现异常
- 是否可以在Linux上使用.Net Core 3.1创建C++/CLI代码的C#DLL
- C++ DLL(不是 CLI)是否可以调用 .NET Core 3.0 委托?
- VS2010 C++ 调试托管 (C++/CLI) DLL
- 如何将 C++/CLI 库链接到C++应用程序
- C++/CLI targetting .NET Core 3.1
- 模板函数签名在 C++/CLI 中解包
- C++/CLI 混合托管/本机 DLL 不起作用
- C++/CLI 和 C#/VB 与不安全和外部有什么区别?
- 是否有可能构建面向Linux和Windows的.Net Core C++ / CLI应用程序?
- DirectX 12 和 C++ CLI :无法使用IID_PPV_ARGS
- 如何为要在 C# 中使用的静态库创建 C++ 或 C++/CLI 包装器
- 将 C# 对象(包含静态对象成员)作为参数传递给 C++/CLI 程序
- C++/CLI -- 访问结构成员
- 如何将字符串从 C++/CLI 方法返回到调用它的非托管C++
- 如何将CLI类的函数指针作为参数传递给C++方法
- 用于LabView中使用的本机c++的c++/cli dll包装器
- C#WPF和C++/CLI以及C++在C++/CLI中添加外部库错误
- 在.NET Core 3.1中运行托管C++/CLI程序集时,映像格式错误