public OpcServer CreateFromClsId(Guid clsid) { var obj = ComUtils.CreateInstance(clsid, null, null); if (obj != null) { return(new OpcServer(obj)); } return(null); }
public static OpcServerFactory Connect() { var obj = ComUtils.CreateInstance(OpcServerListGuid, null, null); if (obj != null) { return(new OpcServerFactory(obj)); } throw new Exception(); //TODO: xxxx }
/// <summary> /// Connects to OPCEnum on the specified machine. /// </summary> public void Connect(string host, UserIdentity identity) { // disconnect from current server. Disconnect(); // create in the instance. object unknown = null; try { unknown = ComUtils.CreateInstance(OPCEnumCLSID, host, identity); } catch (Exception e) { throw ServiceResultException.Create(StatusCodes.BadCommunicationError, e, "Could not connect to OPCEnum server."); } m_server = unknown as IOPCServerList2; if (m_server == null) { ComUtils.ReleaseServer(unknown); StringBuilder error = new StringBuilder(); error.Append("Server does not support IOPCServerList2. "); error.Append("The OPC proxy/stubs may not be installed properly or the client or server machine. "); error.Append("The also could be a problem with DCOM security configuration."); throw ServiceResultException.Create(StatusCodes.BadCommunicationError, error.ToString()); } m_host = host; if (String.IsNullOrEmpty(m_host)) { m_host = "localhost"; } }
/// <summary> /// Connects to the specified COM server server. /// </summary> public object CreateServer(Uri uri, UserIdentity identity) { // parse path to find prog id and clsid. string progID = uri.LocalPath; string clsid = null; while (progID.StartsWith("/")) { progID = progID.Substring(1); } int index = progID.IndexOf('/'); if (index >= 0) { clsid = progID.Substring(index + 1); progID = progID.Substring(0, index); } // look up prog id if clsid not specified in the uri. Guid guid = Guid.Empty; if (String.IsNullOrEmpty(clsid)) { // connect to enumerator. Connect(uri.Host, identity); // use OpcEnum to lookup the prog id. guid = CLSIDFromProgID(progID); // check if prog id is actually a clsid string. if (guid == Guid.Empty) { clsid = progID; } } // convert CLSID to a GUID. if (guid == Guid.Empty) { try { guid = new Guid(clsid); } catch (Exception e) { throw ServiceResultException.Create(StatusCodes.BadCommunicationError, e, "COM server URI does not contain a valid CLSID or ProgID."); } } // use normal activation. return(ComUtils.CreateInstance(guid, uri.Host, identity)); #if COM_IMPERSONATION_SUPPORT // set the current thread token. IPrincipal existingPrincipal = Thread.CurrentPrincipal; WindowsPrincipal principal = ComUtils.GetPrincipalFromUserIdentity(identity); try { if (principal != null) { Thread.CurrentPrincipal = principal; } // activate with a license key if one provided. if (identity != null && !String.IsNullOrEmpty(identity.LicenseKey)) { return(ComUtils.CreateInstanceWithLicenseKey(guid, uri.Host, identity, identity.LicenseKey)); } // use normal activation. return(ComUtils.CreateInstance(guid, uri.Host, identity)); } finally { Thread.CurrentPrincipal = existingPrincipal; } #endif }