C#向C++封送窗口类,报错:ArgumentException: 类型“MainWindow”不能作为非托管结构进行封送处理

docker_007 2020-05-07 11:41:59
情况如下:
在C#中调用C++的DLL,DLL中有一个接口原型如下:


void Func(CWnd *pParent);

调用Func时,需要传递一个CWnd*类型的变量,在C#这边窗口类是Window,
我的做法是将Window类型的对象转换成IntPtr类型,再传入Func中。
但是报错:ArgumentException: 类型“WpfApp.MainWindow”不能作为非托管结构进行封送处理;无法计算有意义的大小或偏移量。
要怎么封送才对呢?有知道的吗?谢谢了


//MainWindow.xaml.cs 文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
///

public partial class MainWindow : Window
{

public MainWindow()
{
InitializeComponent();
}

}
}




//App.xaml.cs文件
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Runtime.InteropServices;

namespace WpfApp
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
[DllImport("xxx.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public extern static void CreatUSBDLG([In, Out] IntPtr pParent); //CWnd* pParent

static MainWindow MWnd = new MainWindow();
static int size = Marshal.SizeOf(MWnd);
IntPtr pWnd = Marshal.AllocHGlobal(size);

public void init()
{
Marshal.StructureToPtr(this, pWnd, true);
Func(pWnd);
}
protected override void OnStartup(StartupEventArgs e)
{

init();
}

}
}
...全文
404 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wanghui0380 2020-05-07
  • 打赏
  • 举报
回复
https://blog.csdn.net/BYH371256/article/details/83347163?utm_medium=distribute.pc_relevant.none-task-blog-OPENSEARCH-1&depth_1-utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-1 这个都一样,不管是啥手段把,先想法拿到handler
wanghui0380 2020-05-07
  • 打赏
  • 举报
回复
哦,你是wpf的,那么看这里 https://blog.csdn.net/jeromewolf/article/details/7842679
docker_007 2020-05-07
  • 打赏
  • 举报
回复
引用 1 楼 wanghui0380 的回复:
MWnd.handler
MWnd好像没有handle这个成员变量
wanghui0380 2020-05-07
  • 打赏
  • 举报
回复
MWnd.handler
docker_007 2020-05-07
  • 打赏
  • 举报
回复
代码修改之后:

public partial class App : Application
    {
        [DllImport("xxx.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public extern static void Func([In, Out] IntPtr pParent);  //CWnd* pParent
  
        static MainWindow MWnd = new MainWindow();
        IntPtr pHandle = new WindowInteropHelper(MWnd).Handle;
        IntPtr pWnd = Marshal.AllocHGlobal(4);

        public void init()
        {
            Marshal.StructureToPtr(pHandle, pWnd, true);
            Func(pWnd);
        }
        protected override void OnStartup(StartupEventArgs e)
        {
              
            init();
        }
  
    }
docker_007 2020-05-07
  • 打赏
  • 举报
回复
引用 4 楼 wanghui0380 的回复:
https://blog.csdn.net/BYH371256/article/details/83347163?utm_medium=distribute.pc_relevant.none-task-blog-OPENSEARCH-1&depth_1-utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-1 这个都一样,不管是啥手段把,先想法拿到handler
搞定了,谢谢了。

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧