0

We have an integration with a third party software that involves passing XML data back and forth. We have to use a DLL import for this process, as they don't have a .NET library or API. Our project has been working fine, targeting the .NET 4.0 framework, for years now.

Due to another component update, we're forced to update our .NET framework. After doing so, this block of code that has been working for years is all of a sudden throwing

AccessViolationException

We rolled our project back to .NET 4.0 and it worked again, so that is the common change we have narrowed it down to.

This is the syntax provided for the third party integration.

Syntax (c/c++)

int VprtXML(tVprtConn lConnHdl, 
            void **pXmlContextPtr, 
            char *XmlBuf, 
            char *XmlResult, 
            long *pResultSize);

Here's the relevant pieces of code that handle this.

        private UInt32 connectionHandle;
        private UInt32 contextHandle = 0;
        byte[] inXMLArray;
        byte[] outXMLArray;
        char[] nulls = { '\0' };

        [DllImport("VprtEI.dll", EntryPoint = "VprtXML",ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        static extern int VprtXML(
             UInt32 pConnHdl,
             UInt32* pContextHdl,
             [MarshalAs(UnmanagedType.LPArray)] byte[] inParm,
             [MarshalAs(UnmanagedType.LPArray)] byte[] outParm,
             UInt32* pResultSize);

        private void InitArray(byte[] arr)
        {
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                arr[i] = 0;
            }
        }


        public bool DoCalc(String inXML, out String outXML)
        {
            int rc;
            ASCIIEncoding ascii = new ASCIIEncoding();
            int nullChar;

            UInt32 resultSize = (uint)(outXMLArray.GetLength(0)) - 1024;

            InitArray(inXMLArray);
            InitArray(outXMLArray);

            ascii.GetBytes(inXML).CopyTo(inXMLArray, 0);

            fixed (uint* pCtxHdl = &(this.contextHandle))
            {
                rc = VprtXML(this.connectionHandle, pCtxHdl,
                                            inXMLArray, outXMLArray,
                                            &resultSize);
            }

            outXML = ascii.GetString(outXMLArray).TrimEnd(nulls);
            nullChar = outXML.IndexOf('\0');
            if (nullChar > 0)
                outXML = outXML.Substring(0, nullChar);

            return true;
        }

The call to VprtXML is what is throwing the AccessViolationException now when it wasn't prior to updating our .NET framework. Any help with figuring out what we need to fix would be appreciated.

  • Did you inadvertently move from x86 to x64 or vice versa in your project settings during the upgrade? –  Jul 12 '19 at 23:22
  • There are many reason why it could fail. First of all your DllImport declaration doesn't seem perfect. It heavily depends how C/C++ code is behaving with what you pass to it. Are all parameters in? are some out? if it's crashing in the C/C++ code, just debug it. Are you running x86 or x64? etc. – Simon Mourier Jul 13 '19 at 06:02
  • @MickyD No, didn't chagne the bitness of the application during the change. Only the .NET framework, and I can go back and downgrade back to .NET 4.0 and this code works fine again. – CodeMonkey05 Jul 13 '19 at 20:01
  • @SimonMourier Can't debug into the C++ code, as I don't have access to it. Only provided with the DLLs and a very limited documentation on the function headers. – CodeMonkey05 Jul 13 '19 at 20:01
  • Difficult to say w/o more information. You should contact the vendor – Simon Mourier Jul 13 '19 at 22:49

0 Answers0