这是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);//这行出错
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);//这行出错
解决方案 »
- C# 关于字符串数组的赋值与使用,给ID001 - ID100 的string 数组(或者Dictionary)赋值?
- 如何使用clickonce发布附加的文件?
- 关于DataSet.WriteXml保存路径的问题
- 以文件的默认格式打开文件
- 子父窗体打开问题
- 请问C#的类库中有鼠标自动移动相关的类吗
- 帮我写一个正则表达式
- 急问!请问如何允许TreeView滚动条可以滚动,但checkedbox不可以编辑?
- 关于跨Form调用子程的问题!
- 谁能给我讲解一下 type [,]和type [][]的区别? 275分!
- 各位大侠帮下忙 关于条码软件barTender 调用的
- 用 ICSharpCode.SharpZipLib.dll 解压缩时,次级文件夹没解压出来!!
跟楼主一样,讨厌封装不完整的API:初看你:
public unsafe static extern int User_AddSingleText(int iCardNum, User_SingleText * pSingleText, int iProgramIndex);
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);
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”: 该字段的类型定义具有布局信息,但具有无效的托管/非托管类型组合或是不可封送的。”
COLORREF也不是个结构体,是个宏
typedef DWORD COLORREF;
就是说你用uint是最好的,但问题应该还不在这
你能确定在VC中int是多少位?在C#中可是32位的,如果VC中是16位的话,那么你就要用short来代替了
别的暂时还没看出来,
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);
也试过,也有错误。