环境:VS 2005
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")]
        public static extern int SMGPSendSingle
        (
            int nNeedReport,
            int nMsgLevel,
            string sServiceID,
            int nMsgFormat,
            string sFeeType,
            string sFeeCode,
            string sFixedFee,
            string sValidTime,
            string sAtTime,
            string sSrcTermID,
            string sChargeTermID,
            string sDestTermID,
            int nMsgLen,
            string sMsgContent,
            StringBuilder sMsgID,
            out int pnErrorCode,
            string pMsgType,
            string pReserved,
            int pTP_pid,   
            int pTP_udhi,
            string pLinkID,
            string pMsgSrc,
            int pChargeUserType,
            int pChargeTermType,
            string pChargeTermPseudo,
            int pDestTermType,
            string pDestTermPseudo,
            int pPkTotal,
            int PkNumber,
            int pSubmitMsgType,
            int pSPDealResult,
            string pMServiceID
        );但是调用的时候就出现 "尝试读取或写入受保护的内存。这通常指示其他内存已损坏。",的错误,我把 string pMsgType, string pReserved, 改成StringBuilder还是出现同样错误,望高手指点一下,万分感谢

解决方案 »

  1.   

    C的声明没贴好,我重新贴一下,VC调用这个dll没有问题,这个dll也不是我写的,改动不了    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
    );
      

  2.   

    int* -〉IntPtr
    char* -> StringBulder
      

  3.   

    JasonHeung(拥有一切不过就这样笑着哭) 
    我马上试验,我先问下 char* 我都用 StringBulder 替换试验过,const char* 我用string,考虑到这个数据不会改变。const int* 用 int 应该也是可以的吧?也是不能改变的
      

  4.   

    如果参数是传进的话,用string是可以的,如果是传出的话,用StringBuilder。至于const int*,用"ref int"来替换。
      

  5.   

    我改成以下这种,还是要出现"尝试读取或写入受保护的内存。这通常指示其他内存已损坏。"这个错误,怎么办哦,一调用就出错,除了StringBuilder sMsgID, IntPtr pnErrorCode这两个需要传出的,其他都赋了初值,那位大侠帮忙看看啊        [DllImport("SmgpDll.dll")]
            public static extern int SMGPSendSingle
            (
                int nNeedReport,
                int nMsgLevel,
                string sServiceID,
                int nMsgFormat,
                string sFeeType,
                string sFeeCode,
                string sFixedFee,
                string sValidTime,
                string sAtTime,
                string sSrcTermID,
                string sChargeTermID,
                string sDestTermID,
                int nMsgLen,
                string sMsgContent,
                StringBuilder sMsgID,
                IntPtr 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
            );
      

  6.   

    int nMsgLen, 改成IntPtr
      

  7.   

    change
    StringBuilder sMsgID,
    IntPtr pnErrorCode,with
    [Out]StringBuilder sMsgID,
    ref int pnErrorCode,//Call
    StringBuilder sMsgID = new StringBuilder(256);//Init it 
      

  8.   

    int nMsgLen在C声明中也是一样的,既然C里面都是传值调用,用不用IntPtr应该都没有关系啊,自己顶一顶,麻烦大家再来看一看
      

  9.   

    愚翁 什么时候升成钻石?口水ing…………