在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();谢谢各位高手,必须在今天解决这个问题。已经好几天了,如果解决不了要耽误进程了。
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();谢谢各位高手,必须在今天解决这个问题。已经好几天了,如果解决不了要耽误进程了。
我用下面这个做测试也是不好用。
EXTERN_C int _declspec(dllexport) GetInt()
{
//CFile f("C:\\t.txt",CFile::modeReadWrite);
MessageBox(0,"TTTTT","CCCCC",0); return 222;
}还请各位帮忙分析一下
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"
EXTERN_C void __stdcall GetInt()
{
//CFile f("C:\\t.txt",CFile::modeReadWrite);
MessageBox(0,"TTTTT","CCCCC",0); //return 222;
}
你把方法写到类的外面,应该就可以了。然后你用depends打开你编辑的dll文件,在里面你可以查看入口函数名。
我给MSN写的插件也是不能调用本地文件,否则就出错
>>你把方法写到类的外面,应该就可以了。
C++ build error message:
error C2201: 'myAdd' : must have external linkage in order to be exported/imported>>然后你用depends打开你编辑的dll文件,在里面你可以查看入口函数名。
我看了有这个entry Add,但怎么回事无法调用
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);
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就可以了非常感谢!