Page #32 (Managing the Lifetime of an Object)

< BACK  NEXT >
[oR]

Optimizations

In the previous section we saw that in order to use an interface, the client had to obtain the IGeneral pointer first. Once this pointer was obtained, the client had to call the method Probe to get an appropriate video interface pointer, as shown here:

 IGeneral* pGeneral = CreateInstance("vcr.dll");  ISVideo* pSVideo = (ISVideo*) pGeneral->Probe("svideo"); 

Clearly, the operations involve two round trips to the client. Each round trip implies a performance penalty. In our case, the performance degradation is not that significant as both the client and the server are running on the same machine and in the same process space. In the case where the server is running on a far away machine from the client or on a machine that is connected over a slow line, each round trip may take considerable time.

In some cases, round trips cannot be avoided and you have to live with it. But consider the case where the TV client knows that it will support only the S-Video signal. For such a case, it would make a great deal of sense to combine the above two operations into one and save one round trip, as illustrated below:

 ISVideo* pSVideo = (ISVideo*) CreateInstance("vcr.dll", "svideo"); 

If you recall, function CreateInstance invokes server function CreateVCR. We will need to extend CreateVCR to take another parameter, a parameter similar to the one that method Probe takes. As a matter of fact, CreateVCR can just turn around and invoke Probe to get the appropriate interface pointer. The code is shown below:

 IGeneral* _stdcall CreateVCR(char* pszType)  {   CVcr* pVcr = new CVcr;    IGeneral* pGeneral = pVcr->Probe(pszType);    if (NULL == pGeneral) {     delete pVcr;    }    return pGeneral;  } 

This is the only coding change that is required on the server side. On the client side, we will need to change CreateInstance method to take the new parameter, pszType.

 IGeneral* CreateInstance(char* pszDll, char* pszType)  {   typedef IGeneral* (_stdcall *CREATEVCRPROC)(char* pszType);    HINSTANCE h = LoadLibrary(pszDll);    CREATEVCRPROC proc = reinterpret_cast<CREATEVCRPROC>      (GetProcAddress(h, "CreateVCR"));    return (*proc)(pszType);  } 

With these two changes in place, not only did we improve the performance, we also simplified client-side logic:

 int main(int argc, char* argv[])  {   IGeneral* pGeneral = CreateInstance("vcr.dll", "svideo");    if (NULL == pGeneral) {     return 1;    }    UseSVideo(reinterpret_cast<ISVideo*>(pGeneral));    pGeneral->Delete();    return 0;  } 

Moreover, the logic doesn t preclude the possibility that you really wish to obtain IGeneral interface and probe for a specific interface:

 IGeneral* pGeneral= CreateInstance("vcr.dll", "general");  ISVideo* pSVideo = (ISVideo*) pGeneral->Probe("svideo"); 

Good job! Your TV-VCR system is now in place. You can turn the TV on, sit back, and enjoy the displayed video signals.

One day you come home, turn the TV on, and to your surprise, there is no display. What happened? You have no clue.


< BACK  NEXT >


COM+ Programming. A Practical Guide Using Visual C++ and ATL
COM+ Programming. A Practical Guide Using Visual C++ and ATL
ISBN: 130886742
EAN: N/A
Year: 2000
Pages: 129

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net