Windows Mobile 6.5
A version of this page is also available for

This function creates a new object and initializes it from a file using IPersistFile::Load.

HRESULT CoGetInstanceFromFile(
  COSERVERINFO* pServerInfo,
  CLSID* pclsid,
  IUnknown* punkOuter,
  DWORD dwClsCtx,
  DWORD grfMode,
  OLECHAR* szName,
  ULONG cmq,
  MULTI_QI* rgmqResults


[in] Pointer to a COSERVERINFO structure that specifies the machine on which to instantiate the object and the authentication setting to be used.

May be NULL, in which case the object is instantiated (1) on the current machine, (2) at the machine specified under the RemoteServerName named-value for the class in the registry, or (3) at the machine where the szName file resides if the ActivateAtStorage named-value is specified for the class in the registry or there is no local registry information.


[in] Pointer to the class of the object to create. May be NULL, in which case there is a call to GetClassFile, using szName as its parameter to get the class of the object to be instantiated.


[in] When non-NULL, indicates the instance is being created as part of an aggregate, and punkOuter is to be used as the pointer to the new instance's controlling IUnknown.

Aggregation is currently not supported cross-process or cross-machine. When instantiating an object out of process, CLASS_E_NOAGGREGATION will be returned if punkOuter is non-NULL.


[in] Values taken from the CLSCTX enumeration.


[in] Flags specifying how the file is to be opened. Values are taken from the STGM enumeration.


[in] File to initialize the object with using IPersistFile::Load. May not be NULL.


[in] Number of MULTI_QI structures in rgmqResults. Must be greater than zero.


[in] Array of MULTI_QI structures. Each structure has three members: the identifier for a requested interface (pIID), the location to return the interface pointer (pItf) and the return value of the call to QueryInterface (hr).

This function supports the standard return value E_INVALIDARG, as well as the following:


Indicates success.


At least one, but not all of the interfaces requested in the rgmqResults array were successfully retrieved. The hr field of each of the MULTI_QI structures in rgmqResults indicates with S_OK or E_NOINTERFACE whether or not the specific interface was returned.


None of the interfaces requested in the rgmqResults array were successfully retrieved.

CoGetInstanceFromFile creates a new object and initializes it from a file using IPersistFile::Load. The result of this function is similar to creating an instance with a call to CoCreateInstanceEx, followed by an initializing call to IPersistFile::Load, with the following important distinctions:

  • Fewer network round trips are required by this function when instantiating an object on a remote machine.
  • In the case where dwClsCtx is set to CLSCTX_REMOTE_SERVER and pServerInfo is NULL, if the class is registered with the ActivateAtStorage sub-key or has no associated registry information, this function will instantiate an object on the machine where szName resides, providing the least possible network traffic.
    For example, if szName specified "\\myserver\users\johndo\file", the object would be instantiated on the "myserver" machine, and the object would access the file directly.

To determine whether the platform supports this function, see Determining Supported COM APIs.

Windows Embedded CEWindows CE 3.0 and later
Windows MobileWindows Mobile Version 5.0 and later

Community Additions