在c++中开发的应用程序(目前也在vs2005中开发)的.exe程序,在c#环境中使用DllImport引用进来,出现“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”错误。 希望各位高手解决一下。
C++:
EXTERN_C int _declspec(dllexport) GetInt()
{

CFile f("C:\\t.txt",CFile::modeReadWrite);//加上这句就出错,如果注销,程序可以顺利执行,文件路径没问题。 return 222;
}c#:
[DllImport("DrawSystem1.exe", EntryPoint = "_GetInt@0", CharSet = CharSet.Auto)]
        public static extern int GetInt();谢谢各位高手,必须在今天解决这个问题。已经好几天了,如果解决不了要耽误进程了。

解决方案 »

  1.   

    当前用户对t.txt有没有权限?
      

  2.   

    问题好像不是处在文件上。
    我用下面这个做测试也是不好用。
    EXTERN_C int _declspec(dllexport) GetInt()
    {

    //CFile f("C:\\t.txt",CFile::modeReadWrite);
    MessageBox(0,"TTTTT","CCCCC",0); return 222;
    }还请各位帮忙分析一下
      

  3.   

    为什么用EXE里的方法?用DLL不好么?
      

  4.   

    把c++函数调用方式改成stdcall试试(猜的)
      

  5.   

    我也有同样问题,我的C++ dll 头文件如下:
    namespace MathFuncs
    {
        class MyMathFuncs
        {
        public:
            // Returns a + b
            static __declspec(dllexport) double Add(double a, double b);
            
        // Returns a - b
            static __declspec(dllexport) double Subtract(double a, double b);        // Returns a * b
            static __declspec(dllexport) double Multiply(double a, double b);        // Returns a / b
            // Throws DivideByZeroException if b is 0
            static __declspec(dllexport) double Divide(double a, double b);
        };
    }
    然后在C++里面可以调用,但在C#里面就遇到问题。
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.InteropServices;namespace MyCSharpExecRefsDll
    {
        class Program
        {
            static void Main(string[] args)
            {
                myWin32 mywin32 = new myWin32();
                Console.WriteLine(myWin32.Add(3, 4));        }
                public class myWin32
            {
                [DllImport("MathFuncsDll1.dll")]
                public static extern double Add(double a, double b);
                [DllImport("MathFuncsDll1.dll")]
                public static extern double Subtract(double a, double b);
                [DllImport("MathFuncsDll1.dll")]
                public static extern double Multiply(double a, double b);
                [DllImport("MathFuncsDll1.dll")]
                public static extern double Divide(double a, double b);        }    }
    }
    调用的时候总是说没有这个入口。
    Unable to find entry point named "Add" in DLL "MathFuncDll1.dll"
      

  6.   

    写成下面这个样子,就找不到程序的入口点了。
    EXTERN_C void __stdcall GetInt()
    {

    //CFile f("C:\\t.txt",CFile::modeReadWrite);
    MessageBox(0,"TTTTT","CCCCC",0); //return 222;
    }
      

  7.   

    TO:veritasfx() 
    你把方法写到类的外面,应该就可以了。然后你用depends打开你编辑的dll文件,在里面你可以查看入口函数名。
      

  8.   

    这个东西,觉得可能是一种调用约定?
    我给MSN写的插件也是不能调用本地文件,否则就出错
      

  9.   

    TO:veritasfx() 
    >>你把方法写到类的外面,应该就可以了。
    C++ build error message:
    error C2201: 'myAdd' : must have external linkage in order to be exported/imported>>然后你用depends打开你编辑的dll文件,在里面你可以查看入口函数名。
    我看了有这个entry Add,但怎么回事无法调用
      

  10.   

    TO:veritasfx()
    extern "C" static _declspec(dllexport) double Add(double a, double b){...}[DllImport("MathFuncsDll1.dll",EntryPoint = "你在depends中看到的名字", CharSet = CharSet.Auto)]
    public static extern double Add(double a, double b);
      

  11.   

    自己顶一下,请各位高手帮忙。我这里有一个别人开发的绘图程序,用VC6.0开发的。而且有代码。我现在要开发另一套程序,用.net开发。在这套程序中需要使用绘图程序以便绘制特有图形。所以,我必须在.net程序中调用绘图程序,以便绘图。请问各位高手,有没有好的解决方案!
      

  12.   

    1)修改原程序如下:
    extern "C" static _declspec(dllexport) double myAdd(double a, double b);
    出错:error C2201: 'myAdd' : must have external linkage in order to be exported/imported2)去掉static就可以了非常感谢!