Service Host (Windows Embedded CE 6.0)
The Core Connectivity service host is an out-of-proc COM server that acts as a broker for all Core Connectivity services. Service host services can be part of the basic services, or add-on services discovered at run-time. Kernel services should always be hosted by the service host; application services may be hosted by the service host, or directly by a client application.
The service host separates instances of each active service, providing each service with the equivalent of an exclusive environment. In this way, you can run multiple instances of the same service within the same host framework.
The service host:
Loads a service.
Caches a copy of the service.
Manages the service lifetime within the service host.
Because the service host caches active services, an active service continues running even if a client application terminates abnormally.
A new instance of the client program can reconnect to the existing service with a simple request, providing a more robust environment. Because the service is already created, the service host can simply return the cached instance when the client reconnects.
The service host lifetime is governed by the clients using the service host.
Clients access individual services through ICcSvcHost::GetService. This API co-creates the service, initializes the service with the target device pointer, and returns the ICcService pointer back to the client. With this interface, clients can query ICcBootstrapfor a download service or ICcTransport for a transport service.
After a client finishes working with the service, the client releases the service pointer by calling ICcService::UnlockService.
It is important to unlock the service when no longer needed. Unlockinginvalidates the cache in the service host, which ensures that the next time a client calls ICcSvcHost::GetService, the service is created as a new service.
ICcService::UnlockService only needs to be called once during the lifetime of a service object.
The following code example shows how a client might call the service host to get a service and then release the service. The client does not explicitly call ICcSvcHost::LockService; instead, the service host locks the service when it caches a copy.
HRESULT hr = S_OK; CComPtr<ICcService> piService = NULL; CComPtr<ICcSvcHost> piSvcHost = NULL; BSTR bstrDeviceId = SysAllocString(L"My PC");// target device ID BSTR bstrSCatId = SysAllocString(<service category id>); // all service category ids are defined in CcService.h in corecon\sdk\inc folder. // // Call the service host // hr = piSvcHost.CoCreateInstance(__uuidof(SvcHost)); // // Get the desired service // if (SUCCEEDED(hr) && piSvcHost) hr = piSvcHost-GetService(bstrDeviceId,bstrSCatId,&piService); // // use ICcService methods // if (SUCCEEDED(hr) && piService) piService->UnlockService();