public static object CreateInstanceWithLicenseKey(Guid clsid, string hostName, UserIdentity identity, string licenseKey)
{
ServerInfo serverInfo = new ServerInfo();
COSERVERINFO coserverInfo = serverInfo.Allocate(hostName, identity);
object instance = null;
IClassFactory2 factory = null;
try
{
// check whether connecting locally or remotely.
uint clsctx = CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER;
if (hostName != null && hostName.Length > 0)
{
clsctx = CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER;
}
// get the class factory.
object unknown = null;
CoGetClassObject(
clsid,
clsctx,
ref coserverInfo,
typeof(IClassFactory2).GUID,
out unknown);
// SetProxySecurity(unknown, coserverInfo.pAuthInfo);
factory = (IClassFactory2)unknown;
// check for valid factory.
if (factory == null)
{
throw ServiceResultException.Create(StatusCodes.BadCommunicationError, "Could not load IClassFactory2 for COM server '{0}' on host '{1}'.", clsid, hostName);
}
// SetProxySecurity(factory, coserverInfo.pAuthInfo);
// create instance.
factory.CreateInstanceLic(
null,
null,
IID_IUnknown,
licenseKey,
out instance);
// SetProxySecurity(instance, coserverInfo.pAuthInfo);
}
finally
{
serverInfo.Deallocate();
ComUtils.ReleaseServer(factory);
}
return instance;
}
#endif