Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Bizarre COM interop problem (using OPC automation wrapper)

4,876 views
Skip to first unread message

Dave

unread,
May 1, 2002, 6:18:35 PM5/1/02
to
I think this problem is related to the fact that .NET uses zero-based
arrays/collections while OPC uses one-based collections. But what a bizarre
manifestation of the problem huh?

If anyone has ideas, please respond.

"Dave" <no...@nowhere.com> wrote in message
news:eoGZqnV8BHA.1292@tkmsftngp07...
> See the attached screenshot for what is happening.
>
> Basically, the "groups" variable should have a value of an object of
> OPCGroups but it appears to have a value of OPCServer. I don't understand
> why. server.OPCGroups returns an OPCGroups object but in the debug window
> it shows it as an OPCServerClass object..
>
> Can anyone see anything wrong with that code? Why in the world is it
doing
> that?
>
> The equivalent code works fine in VB6. Is this some weird bug in COM
> interop?
>
>
>


NETMaster

unread,
May 2, 2002, 3:24:45 AM5/2/02
to
Yes, legacy OPC-Automation uses 1-based arrays!
You have to rewrite the OPC-Automation wrapper,
or (better) switch to the OPC custom-interface:

read
http://www.codeproject.com/useritems/opcdotnet.asp

(updated download: http://www.viscomvisual.com/dotnet/ )

also visit:
http://www.opcconnect.com/tooltech.shtml


--
NETMaster (Thomas Scheidegger)
http://www.cetus-links.org/oo_csharp.html

"Dave" <no...@nowhere.com> wrote in message news:eWoEy3V8BHA.1380@tkmsftngp03...

Dave

unread,
May 2, 2002, 1:35:59 PM5/2/02
to
I've tried the viscom stuff. It seems to work but there is a lot of code
and I don't have time right now to write my own, even using that as a
starting point. That's why I was hoping to get the Automation wrapper
working.

So what we're saying is, COM Interop doesn't really work 100% of the time?
I thought the /sysarray switch on tlbimp was supposed to compensate for
non-zero based Automation arrays?

This statement from http://www.opcconnect.com/tooltech.shtml just isn't true
then:

"If you're happy using the OPC Automation interfaces, then a reference to
the Automation server or DLL may be added directly to your .NET project.
Coding with C# or your favorite .NET language is then straightforward."


"NETMaster" <spam.ne...@swissonline.ch> wrote in message
news:#pGUwpa8BHA.2152@tkmsftngp02...

NETMaster

unread,
May 3, 2002, 8:36:40 AM5/3/02
to
For your 'bizzare' problem:

first AFAIK console apps should have [MTAThread] for Main().

I guess you have the good old collection / CreateWrapperOfType problem:

this quick & dirty hack works for me:

// ===============================================================
server = new OPCServerClass();
server.Connect( "?????????????", null );

// this cast fails:
// OPCGroupsClass ogrps = (OPCGroupsClass) server.OPCGroups;

object tmpg = server.OPCGroups;
OPCGroupsClass ogrps = (OPCGroupsClass) Marshal.CreateWrapperOfType( tmpg, typeof(OPCGroupsClass) );
tmpg = null;

int ct = ogrps.Count;
OPCGroupClass og = (OPCGroupClass) ogrps.Add( "MyNewGroup" );
ct = ogrps.Count;
int ur = og.UpdateRate;

Marshal.ReleaseComObject( og );
og = null;

ogrps.RemoveAll();
Marshal.ReleaseComObject( ogrps );
ogrps = null;

server.Disconnect();
Marshal.ReleaseComObject( server );
server = null;
// ===============================================================

--
NETMaster (Thomas Scheidegger)
http://www.cetus-links.org/oo_csharp.html


"Dave" <no...@nowhere.com> wrote in message news:#7CIi#f8BHA.2540@tkmsftngp04...

Dave

unread,
May 3, 2002, 5:23:57 PM5/3/02
to
Your code works great! Thanks so much for your help.

What is this "collection / CreateWrapperOfType problem" you speak of? Do
you have any links to information about it?

NETMaster

unread,
May 4, 2002, 5:50:39 AM5/4/02
to
Nothing besides online help, my own experience and
Google groups:
http://groups.google.com/groups?q=CreateWrapperOfType&scoring=d

--
NETMaster (Thomas Scheidegger)
http://www.cetus-links.org/oo_csharp.html


"Dave" <no...@nowhere.com> wrote in message news:uqycliu8BHA.2244@tkmsftngp03...

Andy DeMaurice [MS]

unread,
Jun 5, 2002, 2:42:01 PM6/5/02
to
The problem with get_OPCGroups doesn't have anything to do with the one or
zero based array problem.

The opcdaauto.dll behaves different from what its type library says. The
type library indicates that there are three different coclasses: the
server, the groups, and the group. It indicates that when you call
get_OPCGroups you get a Groups object.

You don't.

The server object and the Groups object are one and the same. When you
call get_OPCGroups you just get a different interface pointer to the same
object (the server object). This does not pose a problem with classic COM
clients (VB6, C++) but it causes a big problem with .NET.

NET checks the COM identity rule of the interface pointer that it gets
back from get_OPCGroups, and realizes that it is the same object as the
server object. Since it already has a RCW for the server object, it just
uses the same wrapper for the new pointer.


Andy DeMaurice
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.

NETMaster

unread,
Jun 6, 2002, 4:46:56 AM6/6/02
to
Thanks for your clarification...
just to be sure, so the usage of
OPCGroupsClass ogrps = (OPCGroupsClass) Marshal.CreateWrapperOfType
is a correct workaround?


--
NETMaster (Thomas Scheidegger)
http://www.cetus-links.org/oo_csharp.html


"Andy DeMaurice [MS]" <andr...@online.microsoft.com> wrote in message news:H512aCMDCHA.1908@cpmsftngxa07...

joh....@gmail.com

unread,
Feb 9, 2015, 4:15:27 AM2/9/15
to
I have had the same problem, while devloping with .net2. After switching to .net4 it worksy very well. I can not explain it :(

dianmuham...@gmail.com

unread,
Sep 8, 2015, 10:29:38 AM9/8/15
to
hello i'm just a users MS.Net (newbie), and then i also still develop the OPC, did you help to explain about Classic COM Clients different with COM .Net? why?.
if i see the envrnmt from .NET very complete.

Regards
Dian Muhammad Adam
0 new messages