int WINAPI SubmitAExEx(
                      unsigned char OrgTON, unsigned char OrgNPI, LPCSTR OrgAddr,
      unsigned char DestTON, unsigned char DestNPI, LPCSTR DestAddr, 
      unsigned char PRI, unsigned char RD, unsigned char RP, 
      unsigned char SRR, unsigned char MR, unsigned char DCS, 
      unsigned char PID, LPCSTR Schedule, LPCSTR Expire, 
      unsigned long Default_ID, unsigned char UDHI, unsigned long UDLen,
      LPCSTR UserData, LPCSTR ServiceSubType, 
      unsigned char* byMsgID, unsigned char byPKTotal, 
      unsigned char byPKNumber, unsigned char byMsglevel, 
                      unsigned char byFeeUserType, LPCSTR sSPID, LPCSTR sFeeType, 
                      LPCSTR sFeeAddr, LPCSTR sFeeCode,unsigned char byUserNum, 
                      LPCSTR sDestAddrs,unsigned long* SM_ID, unsigned char* FCS
                      )
以上为C++ 中的DLL里的原形函数,其中unsigned char* byMsgID,unsigned long* SM_ID,unsigned char* FCS均为输出参数。
我用C#调用之后的写法        public static extern int SubmitAExEx(
                                             char OrgTON, char OrgNPI, string OrgAddr,
                                             char DestTON, char DestNPI, string DestAddr,
                                             char PRI, char RD, char RP,
                                             char SRR, char MR, char DCS,
                                             char PID, string Schedule, string Expire,
                                             long Default_ID, char UDHI, long UDLen,
                                             string UserData, string ServiceSubType,
                                             out StringBuilder byMsgID, char byPKTotal,
                                             char byPKNumber, char byMsglevel,
                                             char byFeeUserType, string sSPID, string sFeeType,
                                             string sFeeAddr, string sFeeCode, char byUserNum,
                                             string sDestAddrs,out StringBuilder SM_ID,out StringBuilder FCS
                                             );
运行时,为什么会报这样的错误  {尝试读取或写入受保护的内存。这通常指示其他内存已损坏。}
请高人帮忙,所有分都加给你!拜你为师!谢谢!

解决方案 »

  1.   

    建议更换一下类型:
    unsigned char-〉byte
    unsigned long-〉UInt32unsigned char* ->char[]
    unsigned long*->UInt32[]关于unsigned char* byMsgID,unsigned long* SM_ID,unsigned char* FCS的变量可以尝试:
    char[] FCS = new char[256];
    char[] byMsgID = new char[256];UInt32[] SM_ID = new UInt32[256];或者:
    IntPtr ptrFCS = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(char)) * 256);
    IntPtr ptrMsgID = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(char)) * 256);IntPtr ptrSM_ID = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(UInt32)) * 256);不过SubmitAExEx方法中对应的参数类型也需要修改为IntPtr;读取返回值也需要下面的方式:
    char[] FCS = new char[256];
    Marshal.Copy(ptrFCS, FCS,0,256);
      

  2.   

    运行时,为什么会报这样的错误  {尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
    out StringBuilder SM_ID,out StringBuilder FCS看能不能使用MARSH的非托管类型属性来标记!内存使用非法。 
      

  3.   

    类似的问题如果在自己的身上找不到原因时可以考虑下调用的dll文件本身是否有问题。
      

  4.   

    还是没有解决!楼上有人说是DLL的问题!可是我调用前几个函数的时候!都是没有问题的!就是这个函数的参数超多!其他的函数就两到三个函数!
    还是继续请兄弟们求助!谢谢!
      

  5.   

    以前调C++的 DLL常出现这种问题,大部分是由于C++回调C#中的一个方法,而这个方法已经被垃圾资源回收掉!
    看看封送处理的资料,注意看看各回调函数!
      

  6.   

    还是参数类型转换的问题,参考一下下面的
    vb                                       c++                                         C#
    handle void* System.IntPtr 32 位 
    byte unsigned char System.Byte 8 位 
    short short System.Int16 16 位 
    word unsigned short System.UInt16 16 位 
    int int System.Int32 32 位 
    uint unsigned int System.UInt32 32 位 
    long long System.Int32 32 位 
    bool long System.Int32 32 位 
    dword unsigned long System.UInt32 32 位 
    ulong unsigned long System.UInt32 32 位 
    char char System.Char 用 ANSI 修饰。 
    lpstr char* System.String 或 System.StringBuilder 用 ANSI 修饰。 
    lpcstr const char* System.String 或 System.StringBuilder 用 ANSI 修饰。 
    lpwstr wchar_t* System.String 或 System.StringBuilder 用 Unicode 修饰。 
    lpcwstr const wchar_t* System.String 或 System.StringBuilder 用 Unicode 修饰。 
    float float System.Single 32 位 
    double double System.Double 64 位 
      

  7.   

    晕死,拷过来格式变了,不易看清楚,还是看我blog吧
    http://blog.163.com/tellyes_software@126/blog/static/30734086200882352618167/edit/?mode=prev