一VC++6.0写的OCX,实现方法:
TransProc(LPCTSTR  msg, LPCTSTR  type, LPCTSTR  amount, LPCTSTR  sbuf)在C#中调用参数声明为string(C#引用进来时将方法参数处理为string,无法使用其它类型),调用时出现以下错误:System.AccessViolationException: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。这个OCX无法改,是别人写好的,请高手不吝赐教。谢谢。

解决方案 »

  1.   

    这种函数定义,应该是C++的才对:
    TransProc(LPCTSTR  msg, LPCTSTR  type, LPCTSTR  amount, LPCTSTR  sbuf)你这个异常是在什么地方出错的,因为如果你传入的string应该是要初始化的
      

  2.   

    这个一个OCX的一个方法,
    我是调用这个方法时出错的,string我初始化了,采用静态的不行,用动态的也不行。
    但这个OCX中的另个方法我调用是没问题的。
    bool PosProcess(string strMsg,string strBusinessType,string strMoney,string strSelfInfo)
            {
                bool boolReturn = false;            try
                {
                    string strReturn = "联机交易成功完成.";                                Int16 intReturn = this.axPayocxMain.InitPara(ClassSys_CurrentLogin.Com_PortBank);                if (intReturn < 0)
                    {
                        strReturn = "初始化错误.";
                        MessageBox.Show(strReturn, "销售", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return boolReturn;
                    }                intReturn = axPayocxMain.OpenCOM();                if (intReturn != 0)
                    {
                        strReturn = "通讯端口打开错误,请检查设备是否正常连接计算机.";
                        MessageBox.Show(strReturn, "销售", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return boolReturn;
                    }                strSMsg = strMsg;
                    strSType = strBusinessType;
                    strSMoney = strMoney;
                    strSSelfInfo = strSelfInfo;             /********************/这里调用出错  intReturn = axPayocxMain.TransProc(strSMsg, strSType, strSMoney, strSSelfInfo);
                    
                    string strBack = axPayocxMain.GetValue(1);  //获取应答码                if (intReturn == 0 & strBack.Equals("00"))
                    {
                        if (strBusinessType.Equals("02"))
                        {
                            strBankAccount = axPayocxMain.GetValue(2);
                        }                    boolReturn = true;
                    }
                    else
                    {
                        switch (strBack)
                        {
                            case "19":
                                strReturn = "OK.";
                                break;
                        }
                    }                axPayocxMain.CloseCOM();
                    MessageBox.Show(strReturn, "销售", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch(System.Exception error)
                {
                    MessageBox.Show("POS通信出错.", "销售", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }            return boolReturn;
            }
      

  3.   

    http://www.it130.net/Csharp/NET-OCX-113833.htm以前有个帖子, 貌似解决了..
    问题已经解决了,现总结如下:
    1、使用微软的OCX(如:MSCHART)没有问题,但自定义的OCX控件有问题,由于是第三方提供,无法再进一步分析原因;
    2、解决方法:使用编码实现创建控件,在窗体构造函数中调用该代码。
    一定要在构造函数中创建,在其它地方(如:OnLoad)创建都会出问题。