Edit: I know that C# equivalent of For Each server As String In servers
is foreach(var item in servers)
but since GetOPCServers
returns object, it should be cast to iterable type.
I am developing an application using a COM library. Since 64bit causes problems, my target CPU x86.
All methods works as expected, except GetOPCServers()
.
Although Visual Basic code using same dll does not cause problem, C# throws System.InvalidCastException
saying that:
A first chance exception of type 'System.InvalidCastException'
occurred in System.Core.dll
Additional information:
'System.String[*]' türündeki nesne 'System.String[]' türüne atılamadı.
Apologizes for the error message, since my OS in Turkish.
The object in type
System.String[*]
can not be assigned toSystem.String[]
What is diffence between System.String[*]
and System.String[]
?
The VB code using same dll runs without exception
Public Class OpcInfo
Dim servers As VariantType
Dim server As OPCAutomation.OPCServer = New OPCAutomation.OPCServer()
Function GetServers()
Dim servers As Object
Dim _servers As New List(Of String)
servers = server.GetOPCServers()
For Each server As String In servers
_servers.Add(server)
Next
Return _servers
End Function
End Class
But the the C# code also uses same dll throws exception
static void Main(string[] args)
{
var opc_servers = new OPCAutomation.OPCServer().GetOPCServers();
foreach (var item in (string[])opc_servers)
{
Console.WriteLine(item);
}
}
More interestingly, I able to view the data in Watch/Immediate windows:
The error code -2147467262
corresponds to FFFFFFFF80004002
and the explanation according to https://technet.microsoft.com/en-us/library/bb632794.aspx
E_NOINTERFACE
FFFFFFFF80004002
-2147467262
No such interface supported
Thanks everyone.
Casting inside the loop does not make the trick.
as @pikoh stated the answer on MS Word Automation in C# made the trick:
And also
var opc_servers = ...
did not work. Must beobject opc_servers = ...