这是VC DLL中定义的结构
typedef struct
{
char* strFontName;      
int iFontSize;  
BOOL bFontBold;  
BOOL bFontItaic;  
BOOL bFontUnderline;  
COLORREF colorFont;  
int iAlignStyle;        }User_FontSet;typedef struct
{
int iX;  
int iY;  
int iWidth;  
int iHeight;      
int iFrameMode;  
COLORREF FrameColor;  
}User_PartInfo;typedef struct _User_MoveSet
{
int iActionType;      
int iActionSpeed;      
BOOL bClear;  
int iHoldTime;  
}User_MoveSet;
typedef struct _User_SingleText
{
char    *chContent;        
User_PartInfo     PartInfo;  
COLORREF BkColor;  
User_FontSet     FontInfo;  
User_MoveSet  MoveSet;  
}User_SingleText;调用的方法是:
int User_AddSingleText(int iCardNum,User_SingleText *pSingleText,int iProgramIndex);
我在C#中写了如下代码:
   [StructLayout(LayoutKind.Sequential)]
    public struct User_PartInfo
    {
        [MarshalAs(UnmanagedType.I4)]
        public int iX;
        [MarshalAs(UnmanagedType.I4)]
        public int iY;
        [MarshalAs(UnmanagedType.I4)]
        public int iWidth;
        [MarshalAs(UnmanagedType.I4)]
        public int iHeight;
        [MarshalAs(UnmanagedType.I4)]
        public int iFrameMode;
        [MarshalAs(UnmanagedType.I4)]
        public int FrameColor;    }
    public struct User_FontSet
    {
        [MarshalAs(UnmanagedType.ByValTStr,SizeConst =30)]
        public string  strFontName;
        [MarshalAs(UnmanagedType.I4)]
        public int iFontSize;
        [MarshalAs(UnmanagedType.Bool)]
        public bool bFontBold;
        [MarshalAs(UnmanagedType.Bool)]
        public bool bFontItaic;
        [MarshalAs(UnmanagedType.Bool)]
        public bool bFontUnderline;
        [MarshalAs(UnmanagedType.I4)]
        public int colorFont;
        [MarshalAs(UnmanagedType.I4)]
        public int iAlignStyle;    }
    public struct User_MoveSet
    {
        [MarshalAs(UnmanagedType.I4)]
        public int iActionType;
        [MarshalAs(UnmanagedType.I4)]
        public int iActionSpeed;
        [MarshalAs(UnmanagedType.Bool)]
        public bool bClear;
        [MarshalAs(UnmanagedType.I4)]
        public int iHoldTime;    }
    public struct User_SingleText
    {
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 30)]
        public string chContent;
        [MarshalAs(UnmanagedType.Struct)]
        public User_PartInfo PartInfo;
        [MarshalAs(UnmanagedType.I4)]
        public int BkColor;
        [MarshalAs(UnmanagedType.Struct)]
        public User_FontSet FontInfo;
        [MarshalAs(UnmanagedType.Struct)]
        public User_MoveSet MoveSet;    }和
        [DllImport("Dll.Dll")]
        public static extern int User_AddSingleText(int iCardNum, User_SingleText pSingleText, int iProgramIndex);
但调用此方法时提示出错“尝试读取或写入受保护的内存。这通常指示其他内存已损坏”,
             User_SingleText me_text=new User_SingleText();
            me_text.chContent = "bbbbbbbbb";
            User_SingleText me_text=new User_SingleText();
            me_text.chContent = "bbbbbbbbb";
            User_PartInfo me_PartInfo = new User_PartInfo();
            me_PartInfo.iX = 0;
            me_PartInfo.iY = 0;
            me_PartInfo.iHeight = 32;
            me_PartInfo.iWidth = 192;
            me_PartInfo.iFrameMode = 0;            me_text.PartInfo = me_PartInfo;
            User_MoveSet me_MoveSet = new User_MoveSet();
            me_MoveSet.iActionType = 0;
            me_MoveSet.iActionSpeed = 5;
            me_MoveSet.iHoldTime = 20;
            me_text.MoveSet = me_MoveSet;
            User_FontSet me_FontSet = new User_FontSet();
            me_text.FontInfo = me_FontSet;
    int b= User_AddSingleText(6,me_text,0);//这行出错

解决方案 »

  1.   


    跟楼主一样,讨厌封装不完整的API:初看你:
    public unsafe  static   extern   int   User_AddSingleText(int   iCardNum,   User_SingleText *   pSingleText,   int   iProgramIndex); 
      

  2.   

    DLL中User_FontSet结构用的COLORREF 定义的colorFont,但我在C#中没找到对应的类型,这块儿应该不对,但是不是不应该报那个错误?
      

  3.   

    C++中的结构体,C#中,用一个IntPtr,就可以了吧;不过,怎么获得C++中的结构体数组,还没有搞定...
      

  4.   

    [DllImport("Dll.Dll")] 
                    public   static   extern   int   User_AddSingleText(int   iCardNum,   User_SingleText   pSingleText,   int   iProgramIndex); =>
    [DllImport("Dll.Dll")] 
    public static extern int User_AddSingleText(int iCardNum, ref User_SingleText pSingleText, int iProgramIndex); 
    或者
    [DllImport("Dll.Dll")] 
    public static extern int User_AddSingleText(int iCardNum, IntPtr pSingleText, int iProgramIndex); //usage
    User_SingleText   me_text=new   User_SingleText(); 
    me_text.chContent   =   "bbbbbbbbb"; 
    User_SingleText   me_text=new   User_SingleText(); 
    me_text.chContent   =   "bbbbbbbbb"; 
    User_PartInfo   me_PartInfo   =   new   User_PartInfo(); 
    me_PartInfo.iX   =   0; 
    me_PartInfo.iY   =   0; 
    me_PartInfo.iHeight   =   32; 
    me_PartInfo.iWidth   =   192; 
    me_PartInfo.iFrameMode   =   0; me_text.PartInfo   =   me_PartInfo; 
    User_MoveSet   me_MoveSet   =   new   User_MoveSet(); 
    me_MoveSet.iActionType   =   0; 
    me_MoveSet.iActionSpeed   =   5; 
    me_MoveSet.iHoldTime   =   20; 
    me_text.MoveSet   =   me_MoveSet; 
    User_FontSet   me_FontSet   =   new   User_FontSet(); me_text.FontInfo   =   me_FontSet; IntPtr pSingleText = Marshal.GlobalAloc(Marshal.SizeOf(me_text));
    Marshal.StructureToPoint(me_text, pSingleText, false);
    int   b=   User_AddSingleText(6,pSingleText,0);
    me_text = (User_SingleText)Marshal.PointToStructure(pSingleText, typeof(User_SingleText))
    Marshal.GlobalFree(pSingleText);
      

  5.   

    调用Marshal.GlobalAloc是不是需要增加引用?
      

  6.   

    引用改成了这个[DllImport("Dll.Dll")] 
    public static extern int User_AddSingleText(int iCardNum, ref User_SingleText pSingleText, int iProgramIndex); 

     结构中[MarshalAs(UnmanagedType.I4)] 
                    public   int   colorFont; 
    也改成了
    [MarshalAs(UnmanagedType.U4 )]
            public System.Drawing.Color  colorFont;
    调用的方法改成了int   b=   User_AddText(6,ref me_text, 1)
    ,提示出错“无法封送处理类型为“WindowsApplication1.User_Text”的字段“PartInfo”: 该字段的类型定义具有布局信息,但具有无效的托管/非托管类型组合或是不可封送的。”
      

  7.   

    调用Marshal.GlobalAloc是不是需要增加别的Using引用?
      

  8.   

    不用啦,能使用Marshal类就可以了
    COLORREF也不是个结构体,是个宏
    typedef DWORD COLORREF;
    就是说你用uint是最好的,但问题应该还不在这
    你能确定在VC中int是多少位?在C#中可是32位的,如果VC中是16位的话,那么你就要用short来代替了
    别的暂时还没看出来,
      

  9.   

    我调用那里面的public static extern bool User_OpenCom(int iComNum, int iBaudRate)方法没有问题可以把串口打开。就是调用带结构体的方法存在问题,
    8楼的IntPtr pSingleText = Marshal.GlobalAloc(Marshal.SizeOf(me_text));
    Marshal.StructureToPoint(me_text, pSingleText, false);
    int   b=   User_AddSingleText(6,pSingleText,0);
    me_text = (User_SingleText)Marshal.PointToStructure(pSingleText, typeof(User_SingleText))
    Marshal.GlobalFree(pSingleText);
    也试过,也有错误。