改为 public struct GameInfoX { public StringBuilder gamename; public StringBuilder gameversion; } 还是一样
改为 public struct GameInfoX { public StringBuilder gamename; public StringBuilder gameversion; }GameInfoX g; g.gamename = new StringBuilder(256); g.gameversion= new StringBuilder(256); 分配好内存后再传进去
public struct GameInfoX
{
public StringBuilder gamename;
public StringBuilder gameversion;
}
还是一样
public struct GameInfoX
{
public StringBuilder gamename;
public StringBuilder gameversion;
}GameInfoX g;
g.gamename = new StringBuilder(256);
g.gameversion= new StringBuilder(256);
分配好内存后再传进去
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Runtime.InteropServices.MarshalDirectiveException: 方法的类型签名与 PInvoke 不兼容。源错误:
行 104: g.gameversion= new StringBuilder(256);
行 105:
行 106: g = add(3,5);
行 107: //Response.Write(contents);
行 108: Response.Write(g.gamename);
源文件: c:\inetpub\wwwroot\webapplication1\webform1.aspx.cs 行: 106 堆栈跟踪:
[MarshalDirectiveException: 方法的类型签名与 PInvoke 不兼容。]
WebApplication1.WebForm1.add(Int32 a, Int32 b) +0
WebApplication1.WebForm1.Button1_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\webapplication1\webform1.aspx.cs:106
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()
将指针转换成string
public struct GameInfoX
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=10)]
public char[] gamename; [MarshalAs(UnmanagedType.ByValArray, SizeConst=10)]
public char[] gameversion;
}