Opc.Ua.Com.ComUtils.CreateInstance2 C# (CSharp) Method

CreateInstance2() public static method

Creates an instance of a COM server using the specified license key.
public static CreateInstance2 ( System.Guid clsid, string hostName, UserIdentity identity ) : object
clsid System.Guid
hostName string
identity UserIdentity
return object
		public static object CreateInstance2(Guid clsid, string hostName, UserIdentity identity)
		{
            // validate the host name before proceeding (exception thrown if host is not valid).
            bool isLocalHost = IsLocalHost(hostName);

            // allocate the connection info.
			ServerInfo    serverInfo   = new ServerInfo();
			COSERVERINFO  coserverInfo = serverInfo.Allocate(hostName, identity);
			object        instance     = null; 
			IClassFactory factory      = null; 
			
			try
			{
                // create the factory.
                object unknown = null;

				CoGetClassObject(
					clsid,
					(isLocalHost)?CLSCTX_LOCAL_SERVER:CLSCTX_REMOTE_SERVER,
					ref coserverInfo,
					IID_IUnknown,
					out unknown);

                // SetProxySecurity(unknown, coserverInfo.pAuthInfo);
                
                factory = (IClassFactory)unknown;

                // check for valid factory.
                if (factory == null)
                {
                    throw ServiceResultException.Create(StatusCodes.BadCommunicationError, "Could not load IClassFactory for COM server '{0}' on host '{1}'.", clsid, hostName);
                }

                // SetProxySecurity(factory, coserverInfo.pAuthInfo);

			    factory.CreateInstance(null, IID_IUnknown, out instance);

                // SetProxySecurity(instance, coserverInfo.pAuthInfo);
			}
			finally
			{
				serverInfo.Deallocate();
			}

            return instance;
		}