【问题标题】:c# DllImport c++ dll error: Attempted to read or write protected memoryc# DllImport c++ dll错误:试图读取或写入受保护的内存
【发布时间】:2015-10-02 13:22:10
【问题描述】:

这是我的 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 中)。但是当我运行时,我得到“附加信息:试图读取或写入受保护的内存。这通常表明其他内存已损坏。”例外。

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

【问题讨论】:

  • 听起来像是CCEGLView::SetParentHwnd(hwnd);CCApplication::sharedApplication()->run(); 中的错误。启用Native Code Debugging 并更新您的问题,提供有关故障点出现问题的更多详细信息。
  • 是的,就是这样,我忘记将一些图像复制到资源文件夹。谢谢,我不知道我可以“启用本机代码调试”,天哪,真是个菜鸟错误xD
  • 我将评论转换为答案。

标签: c# c++ dll


【解决方案1】:

听起来像是CCEGLView::SetParentHwnd(hwnd);CCApplication::sharedApplication()->run(); 中的错误。 Enable Native Code Debugging查找问题根源。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多