I have written a custom OPC classic client in .Net C#. When subscribing multiple items, I get the following error below. I can use other third-party OPC explorer apps to successfully view the servers available and read items. I put try/catch blocks in my own code to attempt to catch it and display additional errors, but nothing is caught in the main code. I think is getting returned straight from the Quick OPC API. FYI, I have run this code on other machines and successfully subscribed to data there, so I do believe it is something unique to this machine. However, any information that can be inferred from that error message that could help me narrow down the problem would be greatly appreciated.
Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at QueryMultipleInterfaces(IUnknown* pUnknown, UInt32 cMQIs, tagMULTI_QI* pMQIs)
at VaryingQueryMultipleInterfaces(IUnknown* pUnknown, UInt32 cMQIs, UInt32 cComSupports, CComSupport** pComSupports, tagMULTI_QI* pMQIs, CComSupport** pComSupportChosen)
at COCKMachine.ConnectServerList(COCKMachine* )
at COCKMachine.ExecuteConnect(COCKMachine* )
at CMachine.Connect(CMachine* )
at CConsumer.RequestMachine(CConsumer* , CMachineClient* pMachineClient, CMachine** pMachine, Boolean AutoDisconnect)
at CClient.ConnectMyMachine(CClient* )
at CClient.ExecuteConnect(CClient* )
at COCKClientBase.ExecuteConnect(COCKClientBase* )
at CAbstractClient.HandleConnectRequest(CAbstractClient* )
at COCKClient.ReceiveAndProcessRequests(COCKClient* )
at COCKClient.ExecuteWork(COCKClient* )
at CAbstractClientWorkerThread.execute(CAbstractClientWorkerThread* )
at CBasicWorkerThread.perform(CBasicWorkerThread* )
at CBasicWorkerThread.process(CBasicWorkerThread* )
at CBasicWorkerThread.threadProc(Void* lpParameter)
THis is my code for adding event handler and subscribing:
static void GetData_DA()
{
// Dictionary<System.Guid, OpcLabs.EasyOpc.ServerElement> d = new Dictionary<System.Guid, OpcLabs.EasyOpc.ServerElement>();
EasyDAItemChangedEventHandler eventHandler = new EasyDAItemChangedEventHandler(client_ItemChanged_DA);
try
{
for (int j = 0; j < addressArray.Count; j++)
{
clients_DA[j].ItemChanged += eventHandler;
}
Console.WriteLine("Subscribing item...");
int k = 0;
for (int j = 0; j < addressArray.Count; j++)
{
var argumentArray = new DAItemGroupArguments[(Int16)chanCountArray[j]];
string address = (string)addressArray[j];
string hostname = address.Split(",".ToCharArray())[0];
string servername = address.Split(",".ToCharArray())[1];
for (int i = 0; i < (Int16)chanCountArray[j]; i++)
{
int requestedUpdateRate = Convert.ToInt32(scanRateCopy * 1000);
//Console.WriteLine(labelListCopy.GetValue(k).ToString());
argumentArray[i] = new DAItemGroupArguments(hostname.Trim(), servername.Trim(), labelListCopy.GetValue(k).ToString(), requestedUpdateRate, k);
k++;
}
Console.WriteLine("HERE1");
int[] error = clients_DA[j].SubscribeMultipleItems(argumentArray);
Console.WriteLine("HERE2 " + error[0].ToString());
}
}
catch (Exception ex)
{
Console.WriteLine("GetData_DA error : " + ex.ToString());
}
}
static void client_ItemChanged_DA(object sender, EasyDAItemChangedEventArgs e)
{
if (e.Succeeded)
Console.WriteLine("{0}: {1}", e.Arguments.ItemDescriptor.ItemId, e.Vtq);
else
{
Console.WriteLine("{0} *** Failure: {1}",
e.Arguments.ItemDescriptor.ItemId, e.ErrorMessageBrief);
}
}