C#调用C++DLL的问题

来源:百度知道 编辑:UC知道 时间:2024/06/06 04:33:46
函数名称:bool WINAPI SendSMS(char *Phone, char *Sms,
int uPhoneLen, int uSmsLen);

入口参数: Phone 保存电话号码的缓冲指针

Sms 保存短信内容的缓冲指针
uPhoneLen 电话号码的长度
uSmsLen 短信的长度
函数返回: 如果要发送的短信格式正确,返回真
功能描述:
发送短信。
调用后,返回真,仅代表发送命令完成,但不代表已经正确发送短信。必须要等待发送短信结果的回调函数通知,才能确保短信发送成功。
如果发送的短信号码过多(超过5个号码)或内容过长(超过140个字节),则返回假。
C++类的定义是这样的,在C#应该如何调用
[DllImport("sample.dll", EntryPoint ="SendSMS",SetLastError=true,CharSet=CharSet.Unicode, ExactSpelling=true,CallingConvention=CallingConvention.StdCall)]
public static extern bool SendSMS(
/*[in]*/ char[] PhoneNos,
/*[in]*/ char[] Sms,
/*[in]*/ int Plen,
/*[in]*/ int Slen);

这样总是报“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”
试过换成string还是不行,能详细说说我这个C++函数C#应该怎么调用吗?

[DllImport("xxx.dll", EntryPoint = "xxxxxx", CallingConvention = CallingConvention.Winapi)]
public static unsafe extern long xxxxxx(string cszVTAppSvrAddr, long nPort, ref IntPtr out_phvt);
我用的是这样
然后配置能使用不安全的代码

[DllImport(@"sample.dll", EntryPoint ="SendSMS",CharSet=CharSet.Unicode, ExactSpelling=true,CallingConvention=CallingConvention.StdCall)]
public static extern bool SendSMS( String PhoneNos, String Sms, int Plen, int Slen);
或者是:
public static extern Boolean SendSMS( String PhoneNos, String Sms, int Plen, int Slen);
你都试试吧 不行我也没辙了,找C++高手吧.