Values from the CLSCTX enumeration are used in activation calls (CoCreateInstance, CoCreateInstanceEx, CoGetClassObject, and so on) to indicate the preferred execution contexts—in-process, local, or remote—in which an object is to be run. They are also used in calls to CoRegisterClassObject to indicate the set of execution contexts in which a class object is to be made available for requests to construct instances (IClassFactory::CreateInstance).
To indicate that more than one context is acceptable, you can combine multiple values with Boolean ORs. The contexts are tried in the order in which they are listed.
The following table shows how other functions and methods that call CoGetClassObject use the CLSCTX values.
|
Function called
|
Context flag used
|
| OleLoad | CLSCTX_INPROC_HANDLER | CLSCTX_INPROC_SERVER Putting an OLE object into the loaded state requires in-process access; it doesn't matter whether all of the object's function is presently available. |
| IRunnableObject::Run | CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER Running an OLE object requires connecting to the full code of the object wherever it is located. |
| CoUnMarshalInterface | CLSCTX_INPROC_HANDLER Unmarshaling needs the form of the class designed for remote access. |
| IMoniker::BindToObject | For a file moniker created through a call to CreateFileMoniker. In this case, uses CLSCTX_SERVER internally to create the instance after calling GetClassFile to determine the class to be instantiated. |
The CLSCTX_REMOTE_SERVER value is added to the CLSCTX enumeration for distributed COM. The CLSCTX_SERVER and CLSCTX_ALL constants are further updated to include the CLSCTX_REMOTE_SERVER value.
Given a set of CLSCTX flags, dwClsCtx, the execution context to be used depends on the availability of registered class codes and other parameters according to the following algorithm.
The first part of the processing determines whether CLSCTX_REMOTE SERVER should be specified as follows.
-
If the call specifies one of the following:
-
An explicit COSERVERINFO structure indicating a machine different from the current machine.
-
No explicit COSERVERINFO structure specified but the specified class is registered with either the RemoteServerName or the ActivateAtStorage named-value.
In either case, CLSCTX_REMOTE_SERVER is implied and is added to dwClsCtx. The second case allows applications written prior to the release of distributed COM to be the configuration of classes for remote activation to be used by client applications available prior to DCOM and the CLSCTX_REMOTE_SERVER flag. The cases in which there would be no explicit COSERVERINFO structure are as follows:
-
The value is specified as NULL.
-
It is not one of the function parameters, as would be the case in calls to CoCreateInstance or CoGetClassObject in existing applications.
-
If the explicit COSERVERINFO parameter indicates the current computer, CLSCTX_REMOTE_SERVER is removed (if present) from dwClsCtx.
The rest of the processing proceeds by looking at the value(s) of dwClsCtx in the following sequence:
-
If dwClsCtx includes CLSCTX_REMOTE_SERVER and no COSERVERINFO parameter is specified and if the activation request indicates a persistent state from which to initialize the object (with CoGetInstanceFromFile, CoGetInstanceFromIStorage, or, for a file moniker, in a call to IMoniker::BindToObject) and the class has an ActivateAtStorage subkey or no class registry information whatsoever, the request to activate and initialize is forwarded to the computer where the persistent state resides. (Refer to the remote activation functions listed in the See Also section for details.)
-
If dwClsCtx includes CLSCTX_INPROC_SERVER, the class code in the DLL found under the class's InprocServer32 key is used if this key exists. The class code will run within the same process as the caller.
-
If dwClsCtx includes CLSCTX_INPROC_HANDLER, the class code in the DLL found under the class's InprocHandler32 key is used if this key exists. The class code will run within the same process as the caller.
-
If dwClsCtx includes CLSCTX_LOCAL_SERVER, the class code in the Win32 service found under the class's LocalService key is used if this key exists. If no Win32 service is specified but an EXE is specified under that same key, the class code associated with that EXE is used. The class code (in either case) will be run in a separate service process on the same machine as the caller.
-
If dwClsCtx is set to CLSCTX_REMOTE_SERVER and an additional COSERVERINFO parameter to the function specifies a particular remote computer, a request to activate is forwarded to this remote computer with dwClsCtx modified to be CLSCTX_LOCAL_SERVER. The class code will run in its own process on this specific computer, which must be different from that of the caller.
-
Finally, if dwClsCtx includes CLSCTX_REMOTE_SERVER and no COSERVERINFO parameter is specified and if a computer name is given under the class's RemoteServerName named-value, the request to activate is forwarded to this remote computer with dwClsCtx modified to be CLSCTX_LOCAL_SERVER. The class code will run in its own process on this specific computer, which must be different from that of the caller.
CLSCTX_ACTIVATE_32_BIT_SERVER and CLSCTX_ACTIVATE_64_BIT_SERVER
The 64-bit versions of Windows XP and Windows Server 2003 introduce two new CLSCTX flags: CLSCTX_ACTIVATE_32_BIT_SERVER and CLSCTX_ACTIVATE_64_BIT_SERVER. On a 64-bit computer, a 32-bit and 64-bit version of the same COM server may coexist. When a client requests an activation of an out-of-process server, these CLSCTX flags allow the client to specify a 32-bit or a 64-bit version of the server.
Usually, a client will not care whether it uses a 32-bit or a 64-bit version of the server. However, if the server itself loads an additional in-process server, then it and the in-process server must both be either 32-bit or 64-bit. For example, suppose that the client wants to use a server "A", which in turn loads an in-process server "B". If only a 32-bit version of server "B" is available, then the client must specify the 32-bit version of server "A". If only a 64-bit version of server "B" is available, then the client must specify the 64-bit version of server "A".
A server can specify its own architecture preference via the PreferredServerBitness registry key, but the client's preference, specified via a CLSCTX_ACTIVATE_32_BIT_SERVER or CLSCTX_ACTIVATE_64_BIT_SERVER flag, will override the server's preference. If the client does not specify a preference, then the server's preference will be used.
If neither the client nor the server specifies a preference, then:
-
If the computer that hosts the server is running Windows XP or Windows Server 2003 without Service Pack 1 (SP1) or later installed, then COM will prefer a 64-bit version of the server if available; otherwise it will activate a 32-bit version of the server.
-
If the computer that hosts the server is running Windows Server 2003 with SP1 or later installed, then COM will try to match the server architecture to the client architecture. In other words, for a 32-bit client, COM will activate a 32-bit server if available; otherwise it will activate a 64-bit version of the server. For a 64-bit client, COM will activate a 64-bit server if available; otherwise it will activate a 32-bit server.
The following table shows the results of the various combinations of client architectures and client settings and server architectures and server settings:
| | 32-bit client, no flag | 64-bit client, no flag | 32-bit client, 32-bit flag1 | 32-bit client, 64-bit flag2 | 64-bit client, 32-bit flag1 | 64-bit client, 64-bit flag2 |
| 32-bit server, match client registry value3 | 32-bit server | F8 | 32-bit server | F8 | 32-bit server | F8 |
| 32-bit server, 32-bit registry value4 | 32-bit server | 32-bit server | 32-bit server | F8 | 32-bit server | F8 |
| 32-bit server, 64-bit registry value5 | F8 | F8 | 32-bit server | F8 | 32-bit server | F8 |
| 32-bit server, no registry value6 | 64/329 | 64/329 | 32-bit server | F8 | 32-bit server | F8 |
| 32-bit server, no registry value, Windows Server 2003 SP17 | 32-bit server | 64/329 | 32-bit server | F8 | 32-bit server | F8 |
| 64-bit server, match client registry value3 | F8 | 64-bit server | F8 | 64-bit server | F8 | 64-bit server |
| 64-bit server, 32-bit registry value4 | F8 | F8 | F8 | 64-bit server | F8 | 64-bit server |
| 64-bit server, 64-bit registry value5 | 64-bit server | 64-bit server | F8 | 64-bit server | F8 | 64-bit server |
| 64-bit server, no registry value6 | 64-bit server | 64-bit server | F8 | 64-bit server | F8 | 64-bit server |
| 64-bit server, no registry value, Windows Server 2003 SP17 | 32/6410 | 64-bit server | F8 | 64-bit server | F8 | 64-bit server |
1 CLSCTX_ACTIVATE_32_BIT_SERVER.
2 CLSCTX_ACTIVATE_64_BIT_SERVER.
3 PreferredServerBitness registry value = 1. See PreferredServerBitness.
4 PreferredServerBitness registry value = 2.
5 PreferredServerBitness registry value = 3.
6 PreferredServerBitness registry value is missing and Windows Server XP or Windows Server 2003 without SP 1 or later is installed.
7 PreferredServerBitness registry value is missing and Windows Server 2003 SP 1 or later is installed.
8 Fails with CO_CLASSNOTREG error.
9 A 64-bit server is activated if it exists; otherwise a 32-bit server is activated.
10 A 32-bit server is activated if it exists; otherwise a 64-bit server is activated.
If a CLSCTX enumeration has both the CLSCTX_ACTIVATE_32_BIT_SERVER and CLSCTX_ACTIVATE_64_BIT_SERVER flags set, then it is invalid and the activation will return E_INVALIDARG.
The flags CLSCTX_ACTIVATE_32_BIT_SERVER and CLSCTX_ACTIVATE_64_BIT_SERVER flow across computer boundaries. If the computer that hosts the server is running the 64-bit version of Windows XP or Windows Server 2003, then it will honor these flags; otherwise it will ignore them.