日期:2014-05-17  浏览次数:21321 次

c#调用c的动态链接库(vector的读A2L文件的动态链接库)出现“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”
本来是用c++调用动态链接库的,调用代码为    
    OpenDialog1->DefaultExt="a2l";
    OpenDialog1->Filter="a2l(*.a2l)|*.a2l";
    path=OpenDialog1->FileName;
    content->LoadFromFile(path);
    m_asap2_parse(path.c_str() ,FALSE,dirlist);
此时能够成功,m_asap2_parse定义为int asap2_parse(char *filename, BOOL only_a2ml, char *dirlist[]);
但是我现在用C#做时出现问题“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”我c#代码如下
 [DllImport("asap2prs.dll", EntryPoint = "asap2_parse", CallingConvention = CallingConvention.Cdecl)]
        public static extern int asap2_parse(string filename, bool only_a2ml, string[] dirlist);

        openFileDialog1.Title = "打开a2l文件";
        openFileDialog1.Filter = "A2L flie(*.a2l)|*.a2l";
        openFileDialog1.ShowDialog();
        string[] dir = { ".", "\\MySpecialDirForAmls",null };
           
        string path;
        path = openFileDialog1.FileName;
        asap2_parse( path,false, dir);
当运行到asap2_parse时就出现问题了,就指导啊~~

------解决方案--------------------
char *dirlist[] 这属于二级,你用 ref string 或 ref stringbuilder试试
------解决方案--------------------
char* C#中转成 System.String 或 System.Text.StringBuilder