dll错误:试图读取或写入受保护的内存

本文关键字:受保护 内存 读取 错误 dll | 更新日期: 2023-09-27 18:15:47

下面是我的c++代码:

ExportAPI.cpp

extern "C"
{
    EXPORT_API int test(int a, int b){
        return a*b;
    }
    EXPORT_API bool InitializeApplication( HWND hwnd)
    {
        CCEGLView::SetParentHwnd(hwnd);
        CCApplication::sharedApplication()->run();
        return true;
    }
}
c#:

public partial class MainWindow : Window
    {
      [DllImport("libcc.dll", CallingConvention=CallingConvention.Cdecl)]
      public static extern bool InitializeApplication(IntPtr hwnd);
    [DllImport("libcc.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern int test(int a, int b);
    public MainWindow()
    {
        InitializeComponent();
        Loaded += MainWindow_Loaded;
        Closed += MainWindow_Closed;
    }
    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        Window window = Window.GetWindow(this);
        var wih = new WindowInteropHelper(window);
       Debug.WriteLine(Multiply(3, 4).ToString()); //this runs
        InitializeApplication(wih.Handle); //this gives error
    }
}

所以基本上我要做的是将WPF窗口设置为我的c++应用程序的父窗口(我在.dll中"打包")。但是当我运行时,我得到"附加信息:试图读取或写入受保护的内存。"这通常表明其他内存已损坏。"

对不起,我的英语不好。任何帮助都非常感谢!

dll错误:试图读取或写入受保护的内存

听起来像是CCEGLView::SetParentHwnd(hwnd);CCApplication::sharedApplication()->run();的bug。启用本机代码调试以查找问题的根源。

相关文章: