首页 新闻 会员 周边

会C# 调用 c dll的进

0
悬赏园豆:100 [已关闭问题]

C语言函数声明为:
bool bSendMsg(HANDLE hComm,char *szMsg,char *szPhNbr,char *szSCA,int iSMSFormat,char *szNextMsg,char *szErrInfo);

入口参数:hComm 串口句柄,szMsg 待发的短信内容,szSCA 短信中心号码,iSMSFormat 短信编码格式
出口参数:szNextMsg 超长部分的待发内容, sErrInfo 出错信息

其中szSCA是结构体
struct STRUCommInfo
{
      int iBaudRate;
      char szSCA[30];
};
我的c#声明为

[DllImport("SMSCom.dll", EntryPoint = "bSendMsg")]
public static extern bool bSendMsg(IntPtr hComm,string szMsg,string szPhNbr,string szSCA,int iSMSFormat,  StringBuilder szNextMsg, StringBuilder szErrInfo);
   


结构体为:
[StructLayout(LayoutKind.Sequential)]
    public struct STRUCommInfo
    {
      public  int iBaudRate;
      [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 30)]   
      public string szSCA;
    }

调用方法:

其中stru.szSCA通过上个函数调用,已返回 “8613800555500”
CallDll.bSendMsg(ptr, "在干嘛呢", "15215553319", stru.szSCA, 0, sd, sc);

然后报:尝试读取或写入受保护的内存错误,期待高手看下我的代码参数是否传错了

螺丝转弯的主页 螺丝转弯 | 初学一级 | 园豆:0
提问于:2009-12-23 11:03
< >
分享
其他回答(1)
0

bSendMsg中用到的变量被delete了,

 

把bSendMsg函数中所有变量,都定义成类的成员.(DLL中的变量和C#中的变量)

一滴血 | 园豆:1602 (小虾三级) | 2009-12-23 11:56
0

C不识别C#的StringBuilder

剧终 | 园豆:313 (菜鸟二级) | 2010-01-05 17:30
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册