首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#调用VC的DLL涉及到结构参数,解决方案

2012-01-19 
C#调用VC的DLL涉及到结构参数,急啊这是VC DLL中定义的结构typedef struct{char*strFontNameintiFontSize

C#调用VC的DLL涉及到结构参数,急啊
这是VC DLL中定义的结构
typedef struct
{
char*strFontName;  
intiFontSize; 
BOOLbFontBold; 
BOOLbFontItaic; 
BOOLbFontUnderline; 
COLORREFcolorFont; 
intiAlignStyle;  

}User_FontSet;

typedef struct
{
intiX; 
intiY; 
intiWidth; 
intiHeight;  
intiFrameMode; 
COLORREFFrameColor; 
}User_PartInfo;

typedef struct _User_MoveSet
{
intiActionType;  
intiActionSpeed;  
BOOLbClear; 
intiHoldTime; 
}User_MoveSet;


typedef struct _User_SingleText
{
char *chContent;  
User_PartInfo PartInfo; 
COLORREFBkColor; 
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);//这行出错




[解决办法]
要仔细的调试,亦能解决,主要注意类型所占的字节数要一一对应
[解决办法]
[DllImport("Dll.Dll")] 
public static extern int User_AddSingleText(int iCardNum, User_SingleText

pSingleText, int iProgramIndex); 

=>

C# code
[DllImport("Dll.Dll")] public static extern int User_AddSingleText(int iCardNum, ref User_SingleText pSingleText, int iProgramIndex);
[解决办法]
不用啦,能使用Marshal类就可以了
COLORREF也不是个结构体,是个宏
typedef DWORD COLORREF;
就是说你用uint是最好的,但问题应该还不在这
你能确定在VC中int是多少位?在C#中可是32位的,如果VC中是16位的话,那么你就要用short来代替了
别的暂时还没看出来,

热点排行