Since 1.7.7.x version of SDKs you can create objects in external processes. This is useful to increase a stability of your application.
For example, when you work with flash items in Character Generator the items might be memory-consuming (a well-known problem of SWF animation). And if overall memory usage is higher than system limits, the whole application might hang or crash. To avoid this problem, there is a way to create an object (in this example, Character Generator) in an external process. So if something goes wrong, it is only the external process that experiences problems and you can delete the process without any harm to your main application. If necessary, you can create a new instance of the object in a new external process. For instance, to restore the graphics overlay.
All the process is operated by IMFCreator interface of MFFactory object both in MFormats and MPlatform SDKs.
Set the CPU priority of new MServer processes
On Windows, configure mserver.priority on the application's MFFactory before creating external SDK objects. In C#, MFormats exposes MFORMATSLib.MFFactoryClass, and MPlatform exposes MPLATFORMLib.MFFactoryClass. Both expose PropsSet for this setting.
The supported values are normal, above_normal, high and realtime, compared case-insensitively. The initial default request is realtime; loaded factory configuration can override it. Empty or unsupported values passed directly to PropsSet("mserver.priority", value) return E_INVALIDARG.
After creating the factory, set the desired value before calling MFExternalCreate:
m_objMFFactory.PropsSet("mserver.priority", "normal");
This changes the setting used for subsequent MServer.exe launches from that application. Already running MServers, including reused shared external processes, retain their priority. PropsSet does not automatically save this setting to the registry. Apply the desired value during application startup.
To obtain Realtime priority:
- Close the application.
- Right-click its executable or shortcut and select Run as administrator.
- Use the default request or explicitly set
mserver.prioritytorealtime, then create new external SDK objects. - In Task Manager, find the returned process ID on the Details tab and check its priority.
The application must have the Windows SeIncreaseBasePriorityPrivilege privilege (Increase scheduling priority). Windows may assign High instead of Realtime when that privilege is unavailable. If an elevated application still receives High, check the applicable local or domain policy. The SDK does not elevate the application automatically.
The SDK checks the actual priority after a successful launch. A Realtime-to-High fallback is logged at INFO level; other differences between requested and actual priority are logged at WARN level.
Create an object in an external process
For this, you should create an MFFactory object and use MFExternalCreate method. You need a GUID of an object you'd like to create. You can find GUIDs of objects in IDL files included in /Include folders of your SDK's directory. For example, GUID of Character Generator is "0F56D2E7-033C-4A05-BCDA-DF58C9BBF06F".
The Character Generator connection example below uses MPlatform (MPLATFORMLib).
MFFactoryClass m_objMFFactory = new MFFactoryClass();
m_objMFFactory.PropsSet("mserver.priority", "normal");
uint m_dwProcessID; // Process ID for a new process
Guid gCG_CLSID = new Guid( "{0F56D2E7-033C-4A05-BCDA-DF58C9BBF06F}" ); // GUID of a target type
object pCG_Unk; // output object itself
m_objMFFactory.MFExternalCreate(gCG_CLSID, out pCG_Unk, out m_dwProcessID, "");
It returns you an object that you should cast to a required interface or class before use it further:
IMLCharGen m_pCharGen = (IMLCharGen)pCG_Unk; // cast the result object to a required interface
And the next step is to use the object:
mySourceObject.PluginsAdd(m_pCharGen, 0);
Manage the object from the main application
Once created, you can get a state of the object from your main code. To get info whether the object is alive, you should use MFExternalIsAliveByPID method. The method returns you whether the external object is alive (returns 1) or not (returns 0). You should use the process ID you've received the moment you created an object:
int bAlive = 0; m_objMFFactory.MFExternalIsAliveByPID(m_dwProcessID, out bAlive );
You can destroy the external process if something is wrong with it with MFExternalDestroy method. For this method, you need an object that you've created with the MFExternalCreate:
m_objMFFactory.MFExternalDestroy(pCG_Unk);
Still have questions?
Tell us what didn’t work or what you’d like clarified.