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

OPC Automation Connection problems with C#

2,012 views
Skip to first unread message

Stephen Kew

unread,
Dec 5, 2002, 4:11:49 AM12/5/02
to
Hi,

I am trying to create an OPC Client application in C# using the OPC
Automation wrapper. What I did was to add a reference to the
Automation wapper COM component (from Visual Studio.net) and then I
started using it. It seemed to work ok, and I am able to use it to
browse for OPC Servers over a network, or locally and I am able to
connect to local servers. However as soon as I try to connect to a
remote server, I get an exception thrown of type
System.Runtime.InteropService.COMException with an HRESULT of
0x80040202.

I know that it is not my access rights to the remote servers because I
have checked that as I was originally getting an "Access Denied"
Exception, however as soon as I fixed that problem I got this one. The
exception is thrown when I try to call the Connect function on the
OPCServer object. like this:

OPCAutomation.OPCServer server = new OPCAutomation.OPCServerClass();

server.Connect(m_progid, m_server); // exception thrown here

The prog id and server name, I am getting from the OPC Server Browser.

The funny thing is that the connection is made and the server starts.
I know this because when I use the Matrikon OPC simulation server, the
spash screen is shown and the little system tray icon starts up as
well. However the exception is still thrown.

I have done some reading around, and came across the following
Knowledge base article from Microsoft:
http://support.microsoft.com/default.aspx?scid=kb;en-us;309336

which discusses a problem with the interop assemblies that are created
by TlbImp. It says that the access permissions given to the event sink
helper classes is too strong, and the work around is to edit the IL
for the interop assembly to make the classes public. Apparently it is
a known bug in the RTM version of TlbImp. However I have made these
changes and it still does not help.

I would appreciate any help that anyone could give, as I really don't
want to have to spend the time to code a wrapper DLL for the Custom
interfaces.

Many Thanks in advance.

Stephen Kew

Furnace Control Group / Software Development
Measurement And Control Division
Mintek

Thomas Scheidegger [MVP] NETMaster

unread,
Dec 5, 2002, 6:18:09 AM12/5/02
to
> I know that it is not my access rights to the remote servers because I
> have checked that as I was originally getting an "Access Denied"

I guess it could still be a DCOM security configuration problem.

Did you test remote connectivity with a legacy OPC client?

Depending on DCOM configuration, a client has to call:
CoInitializeSecurity
and this is done with PInvoke in .NET ...
NOTE:
If there is any [STAThread] or [MTAThread] attribute
applied to your main/Main function, do REMOVE it and write:

// !no [stathread] here!
Main()
{
System.Threading.Thread.CurrentThread.ApartmentState = ApartmentState.STA; // or MTA
hr = CoInitializeSecurity(...

http://groups.google.com/groups?q=dotnet+CoInitializeSecurity

> spend time to code a wrapper DLL for the Custom interfaces.

maybe check first:
http://www.opcconnect.com/dotnet.php
or
http://www.codeproject.com/useritems/opcdotnet.asp

--
Thomas Scheidegger - MVP .NET - 'NETMaster'
http://www.cetus-links.org/oo_dotnet.html - http://dnetmaster.net/


Stephen Kew

unread,
Dec 6, 2002, 1:42:44 AM12/6/02
to
Hi Thomas,

Many thanks for your advice. It turns out that that was exactly the
problem. I removed the [STAThread] attribute, and then manually set
the Threading, called CoInitializeSecurity, and now it works
perfectly.

Thanks again.

Stephen Kew

Furnace Control Group / Software Development
Measurement And Control Division
Mintek


"Thomas Scheidegger [MVP] NETMaster" <spam.ne...@swissonline.ch> wrote in message news:<OW5f7$EnCHA.2308@TK2MSFTNGP10>...

0 new messages