summaryrefslogtreecommitdiffstats
path: root/private/oleutest/perform/cairole/ui/bm_link.cxx
blob: 4b5781db4d7b03a3fe9547881824ce98cb587bb0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
//+------------------------------------------------------------------------
//
//  Microsoft Windows
//  Copyright (C) Microsoft Corporation, 1993.
//
//  File:        bm_Link.cxx
//
//  Contents:    Profile methods which manipulate Links, i.e. interface IOleLink
//
//  Classes:    CIOLTest
//
//  Functions:    
//
//  History:    
//
//--------------------------------------------------------------------------

#include <headers.cxx>
#pragma hdrstop

#include "hlp_util.hxx"
#include "hlp_iocs.hxx"
#include "hlp_ias.hxx"
#include "hlp_app.hxx"
#include "hlp_site.hxx"
#include "hlp_doc.hxx"
#include "bm_link.hxx"


//**********************************************************************
//
// CIOLTest::Name, SetUp, Run, CleanUp
//
// Purpose:
//
//      These routines provide the implementation for the Name, Setup,
//      Run and CleanUp of the class CIOLTest. For details see the doc 
//      for driver what are these routines supposed to do.
//
// Parameters:
//
//      
// Return Value:
//
//      None
//
//
// Comments:
//      If STRESS is defined don't do anything with timer variable! We are
//      not  interested in time values.
//
//********************************************************************


TCHAR *CIOLTest::Name ()
{
    return TEXT("IOLTest");
}



SCODE CIOLTest::Setup (CTestInput *pInput)
{
    CTestBase::Setup(pInput);
    HRESULT sc;
    HRESULT hres;

#ifdef STRESS
    //If stress condition loop number of time = STRESSCOUNT
    m_ulIterations = STRESSCOUNT;
#else
    //    get iteration count
    m_ulIterations = pInput->GetIterations(Name());
#endif
    
#ifndef STRESS 
    //    initialize timing arrays
    INIT_LINKRESULTS(m_ulOleLinkSr32);
    INIT_LINKRESULTS(m_ulOleLinkOutl);

#endif

    sc = OleInitialize(NULL);
    if (FAILED(sc))
    {
    Log (TEXT("Setup - OleInitialize failed."), sc);
    return    sc;
    }

    hres = CLSIDFromString(L"Sr32test", &m_clsidSr32);
    Log (TEXT("CLSIDFromString returned ."), hres);
    if (hres != NOERROR)
        return E_FAIL;

    hres = CLSIDFromString(OutlineClassName, &m_clsidOutl);
    Log (TEXT("CLSIDFromString returned ."), hres);
    if (hres != NOERROR)
        return E_FAIL;

    //CreateLink Doc and Root Storage
    m_lpDoc = CSimpleDoc::Create();
        
    for (ULONG iIter=0; iIter<m_ulIterations; iIter++)
        {
        // CreateLink an instance of Site
        CSimpleSite *pObj  =  CSimpleSite::Create(m_lpDoc, iIter);
        if (pObj)
            m_pSite[iIter] = pObj;
        }
    
    return sc;
}


SCODE CIOLTest::Cleanup ()
{

    for (ULONG iIter=0; iIter<m_ulIterations; iIter++)
    {
        delete m_pSite[iIter]; 
    }


    OleUninitialize();
    return S_OK;
}


//**********************************************************************
//
// CIOLTest::Run
//
// Purpose:
//      This is the work horse routine which calls OLE apis.
//      The profile is done by creating moniker then calling OleCreateLink  on moniker.
//
// Parameters:
//
//      
// Return Value:
//
//      None
//
// Functions called:
//      CallOleCreate       defined below
//
//
// Comments:
//          Need to add more Server types including In-Proc servers.
//
//********************************************************************



SCODE CIOLTest::Run ()
{
    CStopWatch  sw;
    BOOL        fRet;

    TCHAR       szTemp[MAX_PATH];
    OLECHAR     szSr2FileName[MAX_PATH];
    OLECHAR     szOutlFileName[MAX_PATH];


    GetCurrentDirectory (MAX_PATH, szTemp);
    swprintf(szSr2FileName,
#ifdef UNICODE
                L"%s\\foo.sr2",
#else
                L"%S\\foo.sr2",
#endif
                szTemp);
    swprintf(szOutlFileName,
#ifdef UNICODE
                L"%s\\foo.oln",
#else
                L"%S\\foo.oln",
#endif
                szTemp);

    fRet = CallOleLinkMethods(szSr2FileName, m_pSite, m_ulIterations, 
                                m_ulOleLinkSr32, L"OTS001", L"OTS002");
    fRet = CallOleLinkMethods(szOutlFileName, m_pSite, m_ulIterations, 
                                m_ulOleLinkOutl, L"Name1", L"Name2");

    return S_OK;
}                      



SCODE CIOLTest::Report (CTestOutput &output)
{

//Bail out immediately on STRESS because none of the following variables
//will have sane value
#ifdef STRESS
    return S_OK;
#endif

    output.WriteString (TEXT("*************************************************\n"));
    output.WriteSectionHeader (Name(), TEXT("IOleLink Methods"), *m_pInput);
    output.WriteString (TEXT("*************************************************\n"));

    output.WriteString (TEXT("\n"));
    WriteLinkOutput(output, TEXT(" Sr32test "), m_ulOleLinkSr32, m_ulIterations);
    output.WriteString (TEXT("\n"));

    output.WriteString (TEXT("\n"));
    output.WriteString (TEXT("******************************************\n"));
    WriteLinkOutput (output, TEXT(" Outline "), m_ulOleLinkOutl, m_ulIterations);
    output.WriteString (TEXT("\n"));

    return S_OK;
}
    
    


//**********************************************************************
//
// CallOleCreateLink
//
// Purpose:
//      Calls OleCreateLink to create the link and then destroys them.
//      
//
// Parameters:
//
//      
// Return Value:
//
//      None
//
// Functions called:
//      CreateFileMoniker      OLE2 api 
//      OleCreateLink          OLE2 api - Is profiled here
//
//
// Comments:
//          Need to add more Server types including In-Proc servers.
//
//********************************************************************



BOOL CallOleLinkMethods(LPCOLESTR lpFileName, CSimpleSite * pSite[], 
        ULONG ulIterations, LinkTimes IOLTime[],  LPCOLESTR lpNm1, LPCOLESTR lpNm2)
{
    CStopWatch  sw;
    HRESULT     hres;
    ULONG       iIter;
    LPMONIKER   pmk = NULL;
    BOOL        retVal = FALSE;
#ifdef STRESS
    LPOLELINK   pLink[STRESSCOUNT] = { NULL };
#else
    LPOLELINK   pLink[TEST_MAX_ITERATIONS] = { NULL };
#endif

    //
    //Create the Links and also cache link pointer for rest of the tests
    //this pointer is reqd for all the tests later
    //
    for ( iIter=0; iIter<ulIterations; iIter++)
        {
        HEAPVALIDATE() ;
        if (!pSite[iIter])
            goto error;
        hres = OleCreateLinkToFile(lpFileName,
                                    IID_IOleObject,
                                    OLERENDER_DRAW,
                                    NULL,
                                    &pSite[iIter]->m_OleClientSite,
                                    pSite[iIter]->m_lpObjStorage,
                                    (VOID FAR* FAR*)&pSite[iIter]->m_lpOleObject
                                    );
        if (hres != NOERROR)
            goto error;

        //Cache IOleLink pointer for rest of the tests that follow below
        hres = pSite[iIter]->m_lpOleObject->QueryInterface(IID_IOleLink, (LPVOID FAR*)&pLink[iIter]);
        if (hres != NOERROR)
            goto error;
        }

    //
    //Run those tests which deal with simple running
    //
    CallOleLinkRunMethods(IOLTime, pLink, ulIterations);

    //
    //Call those tests that deal with SourceDisplayName
    //
    CallOleLinkDisplayName(IOLTime, pLink, lpFileName,
                            lpNm1, lpNm2, ulIterations);


    retVal = TRUE;
error:
    if (hres != NOERROR)
        Log (TEXT("Routine CallOleCreateLink failed with hres = "), hres);

    //CleanUp before going to Next
    for (iIter=0; iIter<ulIterations; iIter++)
        {
        // Unload the object and release the Ole Object
        if (pLink[iIter])
            pLink[iIter]->Release();
        if (pSite[iIter])
            pSite[iIter]->UnloadOleObject();
        }

    return retVal;
}



BOOL CallOleLinkRunMethods(LinkTimes IOLTime[], LPOLELINK pLink[], ULONG ulIterations)
{
    HRESULT     hres;
    CStopWatch  sw;
    ULONG       iIter;
    BOOL        retVal = FALSE;
    LPBINDCTX   pBindCtx = NULL;

    //
    //1. get the first estimates using NULL BindCtx
    //
    for ( iIter=0; iIter< ulIterations; iIter++) 
        {
        sw.Reset();
        hres = pLink[iIter]->BindToSource(NULL /* !BIND_EVEN_IF_CLASSDIF */,
                                   NULL /*Bind Ctx*/);
        GetTimerVal(IOLTime[iIter].ulBindToSourceNull);
        LOGRESULTS (TEXT("IOL:BindToSource "), hres);
        } //End Bind To Source with Null BindCtx

    //Unbind links to start next estimates. Which also BindToSource with Non 
    //NULL BindCtx
    for ( iIter=0; iIter< ulIterations; iIter++)
        {
        hres = pLink[iIter]->UnbindSource();
        } 

    //
    //2. Following tests are to be done with BindContext that we get here
    //
    hres = CreateBindCtx(NULL, &pBindCtx);
    if (hres != NOERROR)
        goto error;
    //Now get the Estimates when Binding with same BindContext
    //
    for ( iIter=0; iIter< ulIterations; iIter++) 
        {
        sw.Reset();
        hres = pLink[iIter]->BindToSource(NULL /* !BIND_EVEN_IF_CLASSDIF */,
                                   pBindCtx /*Bind Ctx*/);
        GetTimerVal(IOLTime[iIter].ulBindToSourceBindCtx);
        LOGRESULTS (TEXT("IOL:BindToSource  "), hres);
        } //End Bind To Source with BindCtx


    //
    //3. Get Estimates for IOL:BindIfRunning
    //
    for ( iIter=0; iIter<ulIterations; iIter++) 
        {
        sw.Reset();
        hres = pLink[iIter]->BindIfRunning();
        GetTimerVal(IOLTime[iIter].ulBindIfRunning);
        LOGRESULTS (TEXT("IOL:BindIfRunning "), hres);
        } //End BindIfRunning, when actually running.


    //
    //4. Get Estimates for IOL:UnbindSource    
    //
    for ( iIter=0; iIter<ulIterations; iIter++) 
        {
        sw.Reset();
        hres = pLink[iIter]->UnbindSource();
        GetTimerVal(IOLTime[iIter].ulUnbindSource);
        LOGRESULTS (TEXT("IOL:UnbindSource "), hres);
        sw.Reset();
        hres = pLink[iIter]->UnbindSource();
        GetTimerVal(IOLTime[iIter].ulUnbindSource2);
        LOGRESULTS (TEXT("IOL:UnbindSource2 "), hres);
        } 
    //
    //5. Get Estimates for IOL:BindIfRunning
    //
    for ( iIter=0; iIter<ulIterations; iIter++) 
        {
        sw.Reset();
        hres = pLink[iIter]->BindIfRunning();
        GetTimerVal(IOLTime[iIter].ulBindIfRunning2);
        LOGRESULTS (TEXT("IOL:BindIfRunning2 "), hres);
        } //End BindIfRunning, when not running.
    //
    //6. Get Estimates for IOL:UnbindSource    
    //
    for ( iIter=0; iIter<ulIterations; iIter++) 
        {
        sw.Reset();
        hres = pLink[iIter]->UnbindSource();
        GetTimerVal(IOLTime[iIter].ulUnbindSource3);
        LOGRESULTS (TEXT("IOL:UnbindSource3 "), hres);
        } 

    retVal = TRUE;

error:
    if (pBindCtx)
        pBindCtx->Release();
    return hres;
}
    



//**********************************************************************
//
// CallOleLinkDisplayName
//
// Purpose:
//      Calls 
//      
//
// Parameters:
//
//      
// Return Value:
//
//      None
//
// Functions called:
//      OleLoad               OLE2 api 
//      OleSave               OLE2 api
//
//
// Comments:
//          
//
//********************************************************************

BOOL CallOleLinkDisplayName(LinkTimes IOLTime[], LPOLELINK pLink[],
                            LPCOLESTR lpFileName, LPCOLESTR lpNm1, 
                            LPCOLESTR lpNm2, ULONG ulIterations)
{
    CStopWatch  sw;
    HRESULT     hres;
    HRESULT     hres2;
    ULONG       iIter;
    BOOL        retVal = FALSE;
    OLECHAR     szMkName1[256];
    OLECHAR     szMkName2[256];
    LPBINDCTX   pBindCtx = NULL;

    //
    //Set the display name to something known and then call IOL:SetDisplayName
    //Then update the link so that rest of its info is now updated by OLE.
    //

    // This code used to grab the moniker from the link, compose with an item
    // moniker and then the displayname from the composite moniker.  This
    // works, although it's rather convoluted, the first time but then the
    // code would end up doing it a second time and the first item moniker
    // name would still be on the end and it would fail.  Just creating the
    // new display name ourselves seems much easier anyway.
    //
    //First get the Moniker Name by following routine 
    //hres = GetLinkCompositeName(pLink[0], lpNm1, &lpMkName1);
    //if (hres != NOERROR)
    //    goto error;

    swprintf(szMkName1, L"%s!%s", lpFileName, lpNm1);

    for (iIter=0; iIter<ulIterations; iIter++)
        {
        sw.Read();
        hres = pLink[iIter]->SetSourceDisplayName(szMkName1);
        hres2 = pLink[iIter]->Update(NULL);
        GetTimerVal(IOLTime[iIter].ulUpdateNull);
        LOGRESULTS (TEXT("IOL:SetSourceDisplayName "), hres);
        LOGRESULTS (TEXT("IOL:Update "), hres2);

        } //End SetSourceDisplayName and Update


    //
    //Repeate the iteration when we have BindContext available. Check the 
    //results.
    //
    //hres = GetLinkCompositeName(pLink[0], lpNm2, &lpMkName2);
    //if (hres != NOERROR)
    //    goto error;

    swprintf(szMkName2, L"%s!%s", lpFileName, lpNm2);

    hres = CreateBindCtx(NULL, &pBindCtx);
    if (hres != NOERROR)
        goto error;

    for (iIter=0; iIter<ulIterations; iIter++)
        {
        sw.Read();
        hres = pLink[iIter]->SetSourceDisplayName(szMkName2);
        hres2 = pLink[iIter]->Update(pBindCtx);
        GetTimerVal(IOLTime[iIter].ulUpdateBindCtx);
        LOGRESULTS (TEXT("IOL:SetSourceDisplayName "), hres);
        LOGRESULTS (TEXT("IOL:Update "), hres2);

        } //End SetSourceDisplayName and Update


    retVal = TRUE;

error:
    if (pBindCtx)
        pBindCtx->Release();

return retVal;
}

HRESULT GetLinkCompositeName(LPOLELINK lpLink, LPCOLESTR lpItem,  LPOLESTR FAR* lpComposeName)
{
    HRESULT     hres;
    LPMONIKER   lpLinkMon = NULL;
    LPMONIKER   lpItemMk = NULL;
    LPMONIKER   lpCompose = NULL;
    //SInce NULL BindContext not allowed any more
    LPBINDCTX   pBindCtx = NULL;

    //Get the source moniker of the link
    hres = lpLink->GetSourceMoniker(&lpLinkMon);
    if (hres != NOERROR)
        goto error;
    //Create item moniker from String Item
    hres = CreateItemMoniker(L"!", lpItem, &lpItemMk);
    if (hres != NOERROR)
        goto error;

    //Ask moniker to compose itself with another one in the end to get Composite
    //moniker.
    hres = lpLinkMon->ComposeWith(lpItemMk, FALSE, &lpCompose);
    if (hres != NOERROR)
        goto error;

    hres = CreateBindCtx(NULL, &pBindCtx);
    if (hres != NOERROR)
        goto error;
    //Get the display Name of the moniker
    hres = lpCompose->GetDisplayName(pBindCtx /*BindCtx*/, NULL /*pmkToLeft*/, 
                                    lpComposeName);
    if (hres != NOERROR)
        goto error;

error:
    if (lpLinkMon)
        lpLinkMon->Release();
    if (lpItemMk)
        lpItemMk->Release();
    if (lpCompose)
        lpCompose->Release();
    if (pBindCtx)
        pBindCtx->Release();
        

    return hres;
}

void WriteLinkOutput(CTestOutput &output, LPTSTR lpstr, LinkTimes *lnkTimes, ULONG ulIterations)
{
    UINT iIter;

    output.WriteString (TEXT("Name"));
    output.WriteString (lpszTab);
    output.WriteString (TEXT("BindToSource (NULL)"));
    output.WriteString (lpszTab);
    output.WriteString (TEXT("BindToSource"));
    output.WriteString (lpszTab);
    output.WriteString (TEXT("BindIfRunning"));
    output.WriteString (lpszTab);
    output.WriteString (TEXT("UnBindSource"));
    output.WriteString (lpszTab);
    output.WriteString (TEXT("UnBindSource2"));
    output.WriteString (lpszTab);
    output.WriteString (TEXT("BindIfRunning2"));
    output.WriteString (lpszTab);
    output.WriteString (TEXT("UnBindSource3"));
    output.WriteString (lpszTab);
    output.WriteString (TEXT("Update(NULL)"));
    output.WriteString (lpszTab);
    output.WriteString (TEXT("Update"));
    output.WriteString (TEXT("\n"));

    for (iIter = 0; iIter < ulIterations; iIter++)
        {
        output.WriteString (lpstr);
        output.WriteLong (lnkTimes[iIter].ulBindToSourceNull);
        output.WriteString (lpszTab);
        output.WriteString (lpszTab);
        output.WriteLong (lnkTimes[iIter].ulBindToSourceBindCtx);
        output.WriteString (lpszTab);
        output.WriteString (lpszTab);
        output.WriteLong (lnkTimes[iIter].ulBindIfRunning);
        output.WriteString (lpszTab);
        output.WriteString (lpszTab);
        output.WriteLong (lnkTimes[iIter].ulUnbindSource);
        output.WriteString (lpszTab);
        output.WriteString (lpszTab);
        output.WriteLong (lnkTimes[iIter].ulUnbindSource2);
        output.WriteString (lpszTab);
        output.WriteString (lpszTab);
        output.WriteLong (lnkTimes[iIter].ulBindIfRunning2);
        output.WriteString (lpszTab);
        output.WriteString (lpszTab);
        output.WriteLong (lnkTimes[iIter].ulUnbindSource3);
        output.WriteString (lpszTab);
        output.WriteString (lpszTab);
        output.WriteLong (lnkTimes[iIter].ulUpdateNull);
        output.WriteString (lpszTab);
        output.WriteString (lpszTab);
        output.WriteLong (lnkTimes[iIter].ulUpdateBindCtx);
        output.WriteString (TEXT("\n"));
        }

    }