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.