当前位置:编程学习 > C#/ASP.NET >>

C#调用C++编写的DLL文件,这个DLL文件是用OpenGL编写的,但C#调用的时候总是报错:System.AccessViolationException:

编译出DLL文件的源代码如下:
// C++3.cpp : 定义 DLL 应用程序的导出函数。
//

#include "stdafx.h"
#include <GL/glut.h>
#include <math.h>

extern "C" _declspec(dllexport) void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
glutDisplayFunc(&myDisplay);
glFlush();
}

我把产生的DLL文件C++3.dll放入对应的C#工程下面以后去调用,源代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SharpGL;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication21
{
    public partial class Form1 : Form
    {
        [DllImport("C++3.dll")]
        public static extern void myDisplay();

        public Form1()
        {
            InitializeComponent();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            myDisplay();
        }
    }
}

结果总是报错:System.AccessViolationException: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。有些网友说问题在于非托管的代码回调,我不明白什么意思,望大神指点! C# C++ OpenGL 内存
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,