关于C#调用C写的Dll的问题

mast 2007-12-19 11:04:49
C API声明如下:
int WINAPI SMGPSendSingle(
int nNeedReport,
int nMsgLevel,
const char* sServiceID,
int nMsgFormat,
const char* sFeeType,
const char* sFeeCode,
const char* sFixedFee,
const char* sValidTime,
const char* sAtTime,
const char* sSrcTermID,
const char* sChargeTermID,
const char* sDestTermID,
int nMsgLen,
const char* sMsgContent,
char* sMsgID,
int* pnErrorCode,
char* pMsgType = NULL,
char* pReserved = NULL,
const int* pTP_pid = NULL,
const int* pTP_udhi = NULL,
const char* pLinkID = NULL,
const char* pMsgSrc = NULL,
const int* pChargeUserType = NULL,
const int* pChargeTermType = NULL,
const char* pChargeTermPseudo =NULL,
const int* pDestTermType = NULL,
const char* pDestTermPseudo = NULL,
const int* pPkTotal = NULL,
const int* PkNumber = NULL,
const int* pSubmitMsgType = NULL,
const int* pSPDealResult = NULL,
const char* pMServiceID = NULL
);

我用C#写的调用代码如下:
[DllImport("SmgpDll.dll", EntryPoint = "SMGPSendSingle")]
private static extern int SMGPSendSingle(
int nNeedReport,int nMsgLevel,string sServiceID,int nMsgFormat,string sFeeType,string sFeeCode, //[7]
string sFixedFee,string sValidTime,string sAtTime,string sSrcTermID,string sChargeTermID, //[22]
string sDestTermID,int nMsgLen,string sMsgContent,
[Out]StringBuilder sMsgID, ref int pnErrorCode, StringBuilder pMsgType, StringBuilder pReserved, ref int pTP_pid,
ref int pTP_udhi, string pLinkID, string pMsgSrc, ref int pChargeUserType,
ref int pChargeTermType, string pChargeTermPseudo, ref int pDestTermType, string pDestTermPseudo,
ref int pPkTotal, ref int PkNumber, ref int pSubmitMsgType, ref int pSPDealResult, string pMServiceID);

每次调用就报"尝试读取或写入受保护的内存。这通常指示其他内存已损坏。"
请高手解答
...全文
80 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhongwanli 2007-12-19
  • 打赏
  • 举报
回复
使用.NET 非安全代码。
mast 2007-12-19
  • 打赏
  • 举报
回复
char* 都申请了足够的空间
cpio 2007-12-19
  • 打赏
  • 举报
回复

像那些char*的,要先申请足够的空间

比如字符串,如果DLL里面需要50长度,那么传进去的字符串要先设置至少50长度
mast 2007-12-19
  • 打赏
  • 举报
回复
已解决
mast 2007-12-19
  • 打赏
  • 举报
回复
问题已解决,谢谢各位.
Knight94 2007-12-19
  • 打赏
  • 举报
回复
to char* 都申请了足够的空间

如果是传出参数,用StringBuilder来定义参数。
不过调用的时候需要初始化空间,例如:
StringBuilder sBuffer= new StringBuilder(256);//init buffer

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧