Unable to pass object ... for input
Visual Studio 2008 (C #) created Interop for my COM objects. The main objects that I use are as follows: OPCHDAServerClass
, IOPCHDAItems
and OPCHDAItem
. Code:
OPCHDAServerClass server = new OPCHDAServerClass();
server.Connect("OPC.PHDServerHDA.1");
OPCHDAItem item = server.OPCHDAItems.AddItem("MyItem",1);
On the third line, the method AddItem
should return OPCHDAItem
. Defining interactions for AddItem
:
[DispId(1610743813)]
OPCHDAItem AddItem(string ItemID, int ClientHandle);
The exception I am getting:
Unable to pass object of type 'OPCHDAServerClass' for input 'IOPCHDAItems'.
I don't understand why I am getting this error message. server.OPCHDAItems
implements IOPCHDAItems
. I don't know why server ( OPCHDAServerClass
) is being added to IOPCHDAItems
?
I did some initial prototyping in python which worked fine so I know COM components are functional. This is the python code:
server = win32com.client.dynamic.Dispatch("Uniformance.OPCHDA.Automation.1")
server.Connect("OPC.PHDServerHDA.1")
item = server.OPCHDAItems.AddItem("MyItem", 1)
Has anyone seen a similar issue and knows how it works?
source to share
It looks like the declared property type is OPCHDAItems
not IOPCHDAItems
- it OPCHDAServerClass
. C # is a statically typed language - it will not render COM interfaces unless explicitly specified, and it will not be used for IDispatch unless told. Rephrase like this:
server.Connect("MyServerName");
OPCHDAItem item = (server.OPCHDAItems as IOPCHDAItems).AddItem("MyItem",1);
EDIT: first try:
IOPCHDAItems Items = server.OPCHDAItems;
Still the same error? What about
IOPCHDAItems Items = server.OPCHDAItems as IOPCHDAItems;
source to share