C++结构体:
typedef struct
{ LONG cfHeight; //字符高度
LONG cfWidth; //字符宽度
LONG cfWeight; //字符粗细
LONG cfSpacing; //字符间距
BOOL cfItalic; //斜体
BOOL cfUnderline; //下划线
BOOL cfStrikeOut; //删除线
BOOL bTransparent;   //透明
CString      str;
COLORREF cfBkColor; //背景色 
COLORREF cfTextColor; //前景色
TCHAR cfFaceName 32]; //字体名}OSDPARAM;
C++函数原形:
BOOL  WINAPI VCASetVidCapDateTimeOSDParam( OSDPARAM* pOSDParm );

[DllImport("Sa7134Capture.dll", EntryPoint = "VCASetVidCapDateTimeOSDParam")]
public static extern bool VCASetVidCapDateTimeOSDParam(ref OSDPARAM1 pOSDParm);
当结构体OSDPARAM1 只有一个变量时VCASetVidCapDateTimeOSDParam()能调用成功,C#声明的结构体如下:
  [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
   public   struct OSDPARAM1
        {
           public Int32 cfHeight;  //字符高度
        }
当结构体有2参数却不能成功,报一个C++的错误!!!!!!!!!!
  [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
 public   struct OSDPARAM1
        {
           public Int32 cfHeight;  //字符高度
           public Int32 cfWidth;   //字符宽度
        }
真是不明白啊!
不会是我引用的C++ DLL的结构体有问题吧????????????????
来帮帮偶吧
大虾~

解决方案 »

  1.   


    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public struct OSDPARAM
    {
        public int cfHeight;
        public int cfWidth;
        public int cfWeight;
        public int cfSpacing;
        public bool cfItalic;
        public bool cfUnderLine;
        public bool cfStrikeOut;
        public bool bTransparent;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = ??)]
        public string str;
        public int cfBkColor;
        public int cfTextColor;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string cfFaceName;
    }大体是这样的,我不清楚str的定义是如何的。
      

  2.   

    C++的函数里会改传进去的参数值吗?不改的话:
    public static extern bool VCASetVidCapDateTimeOSDParam(IntPtr pOSDParm); 
    改的话:
    public static extern bool VCASetVidCapDateTimeOSDParam(ref IntPtr pOSDParm); 参考Marshal.StructureToPtr