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

(急!急!急!在线等啊!)C# 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。

(急!急!急!在线等啊!)C#调用C++Dll文件里的方法时引发异常,异常信息“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。” C# --------------------编程问答-------------------- 一般是函数原型定义不正确,或者传递了无效的指针。 --------------------编程问答-------------------- 网上看了很多,有的说是结构体定义有问题,有的说是DLL文件里动态申请的内存没有释放.... --------------------编程问答-------------------- 这些说法都有可能,原因很多。 --------------------编程问答--------------------  这种问题 没有确切答案 只有一个个排除 --------------------编程问答--------------------
C++结构体:
typedef struct _Test 
    {
        int             a;           
        int             b;          
        unsigned long   c;       
        unsigned char   d;         
        unsigned char   e;      
        unsigned char   f;       
        int             g;  
        int             h;           
        char            i[64];
int j;     
char k[64];    
        char            l[260];
        int             m;
        void*           n;
        int             o;         
        int             p;       
        int             q;   
char r[128];
int s;

    }Test;


C#转换后的结构体
public struct Test
        {
            public int a;           
            public int b;          
            public ulong c;      
            public byte d;        
            public byte e;                  
    public byte f;       
            public int g;    
            public int h;           
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
            public string i;
            public int j;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
            public string k;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
            public string l;
            public int m;   
            public IntPtr n;
            public int o;         
            public int p;       
            public int q;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
            public string r;
            public int s;
        }

请大家帮我看看结构体的转换是不是有问题,谢谢!急! --------------------编程问答-------------------- C++结构体:
typedef struct _Test 
    {
        int             a;           
        int             b;          
        unsigned long   c;       
        unsigned char   d;         
        unsigned char   e;      
        unsigned char   f;       
        int             g;  
        int             h;           
        char            i[64];
int j;     
char k[64];    
        char            l[260];
        int             m;
        void*           n;
        int             o;         
        int             p;       
        int             q;   
char r[128];
int s;

    }Test;


C#转换后的结构体
public struct Test
        {
            int a;           
            int b;          
            ulong c;      
            byte d;        
            byte e;                  
    byte f;       
            int g;    
            int h;           
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
            string i;
            int j;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
            string k;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
            string l;
            int m;   
            IntPtr n;
            int o;         
            int p;       
            int q;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
            string r;
            int s;
        } --------------------编程问答-------------------- 除 --------------------编程问答-------------------- 目测struct加上
[StructLayout(LayoutKind. Sequential)] --------------------编程问答-------------------- 还是不行,求救啊,大神们,快出来帮帮小弟啊 --------------------编程问答-------------------- 在线等啊,UP UP UP UP --------------------编程问答-------------------- 除
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,