summaryrefslogtreecommitdiffstats
path: root/private/ole32/ole232/base/ole2.cpp
blob: 9efcc70f64bdf1deca2773a472a3907b12e6fc1b (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

//+-------------------------------------------------------------------------
//
//  Microsoft Windows
//  Copyright (C) Microsoft Corporation, 1992 - 1993.
//
//  File:       ole2.cpp
//
//  Contents:   LibMain and initialization routines
//
//  Classes:
//
//  Functions:  LibMain
//              OleInitialize
//              OleInitializeWOW
//              OleInitializeEx
//              OleUnitialize
//              OleBuildVersion - !WIN32
//
//
//  History:    dd-mmm-yy Author    Comment
//              16-Feb-94 AlexT     alias OleBuildVersion, remove OleGetMalloc
//                                  remove DisableThreadLibaryCalls
//              11-Jan-94 alexgo    added VDATEHEAP macros to every function
//              10-Dec-93 alexgo    added support for LEDebugOut
//              06-Dec-93 ChrisWe   remove declaration of ClipboardInitialize()
//                      and ClipboardUninitialize(), which are declared in
//                      clipbrd.h; include that instead
//              15-Mar-94 KevinRo   Added OleInitializeWOW();
//
//--------------------------------------------------------------------------


#include <le2int.h>
#include <clipbrd.h>
#include <dragopt.h>
#include <drag.h>

#pragma SEG(ole)

#include <olerem.h>
#include <ole2ver.h>
#include <thunkapi.hxx>
#include <ddesrvr.h>
#include <perfmnce.hxx>
#include <olesem.hxx>

//
// DECLARE_INFOLEVEL is a macro used with cairo-style debugging output.
// it creates a global variable LEInfoLevel which contains bits flags
// of the various debugging output that should be sent to the debugger.
//
// Note that info level may be set within the debugger once ole232.dll
// has loaded.
//
// Currently LEInfoLevel defaults to DEB_WARN | DEB_ERROR
//
DECLARE_INFOLEVEL(LE);
DECLARE_INFOLEVEL(Ref);
DECLARE_INFOLEVEL(DD);
DECLARE_INFOLEVEL(VDATE);

NAME_SEG(Ole2Main)
// these are globals

HMODULE         g_hmodOLE2 = NULL;
HINSTANCE	g_hinst = NULL;
ULONG		g_cOleProcessInits = 0;

CLIPFORMAT      g_cfObjectLink = NULL;
CLIPFORMAT      g_cfOwnerLink = NULL;
CLIPFORMAT      g_cfNative = NULL;
CLIPFORMAT      g_cfLink = NULL;
CLIPFORMAT      g_cfBinary = NULL;
CLIPFORMAT      g_cfFileName = NULL;
CLIPFORMAT      g_cfFileNameW = NULL;
CLIPFORMAT      g_cfNetworkName = NULL;
CLIPFORMAT      g_cfDataObject = NULL;
CLIPFORMAT      g_cfEmbeddedObject = NULL;
CLIPFORMAT      g_cfEmbedSource = NULL;
CLIPFORMAT      g_cfCustomLinkSource = NULL;
CLIPFORMAT      g_cfLinkSource = NULL;
CLIPFORMAT      g_cfLinkSrcDescriptor = NULL;
CLIPFORMAT      g_cfObjectDescriptor = NULL;
CLIPFORMAT      g_cfOleDraw = NULL;
CLIPFORMAT      g_cfPBrush = NULL;
CLIPFORMAT      g_cfMSDraw = NULL;
CLIPFORMAT      g_cfOlePrivateData = NULL;
CLIPFORMAT      g_cfScreenPicture = NULL;

ATOM            g_aDropTarget = NULL;

ASSERTDATA

ASSERTOUTDATA

// more globals

extern UINT     uOmPostWmCommand;
extern UINT	uOleMessage;
extern COleStaticMutexSem g_mxsSingleThreadOle;


// this dummy function is used to avoid a copy of the environment variables.
// NOTE: the moniker and dde code still use the windows heap.

extern "C" void _setenvp(void) {
        VDATEHEAP();
 }


#ifdef _CHICAGO_
// Private Chicago Defines
//
//  The Chicago Shell will dynamically load the OLE32.DLL to improve
//  bootup start time.  When an application calls CoInitialize, post
//  a message to the shell to inform it to load OLE32.DLL if it hasn't
//  already.     The Shell will never unload OLE32.DLL.
//
//  We are using an undocumented Shell interface.
//
BOOL    gfShellInitialized = FALSE;

#define WM_SHELLNOTIFY           0x0034
#define SHELLNOTIFY_OLELOADED    0x0002

extern "C" HWND WINAPI GetShellWindow(void);
#endif  // _CHICAGO_

//+---------------------------------------------------------------------------
//
//  Function:   OleInitializeWOW
//  Synopsis:   Entry point to initialize the 16-bit WOW thunk layer.
//
//  Effects:    This routine is called when OLE32 is loaded by a VDM.
//              It serves two functions: It lets OLE know that it is
//              running in a VDM, and it passes in the address to a set
//              of functions that are called by the thunk layer. This
//              allows normal 32-bit processes to avoid loading the WOW
//              DLL since the thunk layer references it.
//
//  Arguments:  [vlpmalloc] -- 16:16 pointer to the 16 bit allocator.
//              [lpthk] -- Flat pointer to the OleThunkWOW virtual
//                         interface. This is NOT an OLE/IUnknown style
//                         interface.
//  Requires:
//
//  Returns:
//
//  Signals:
//
//  Modifies:
//
//  Algorithm:
//
//  History:    3-15-94   kevinro   Created
//
//  Notes:
//
//----------------------------------------------------------------------------
STDAPI OleInitializeWOW( LPMALLOC vlpmalloc, LPOLETHUNKWOW lpthk )
{
    OLETRACEIN((API_OleInitializeWOW, PARAMFMT("vlpmalloc= %x, lpthk= %p"),
                                                                                vlpmalloc, lpthk));

    SetOleThunkWowPtr(lpthk);

    HRESULT hr;

    hr = OleInitializeEx( NULL, COINIT_APARTMENTTHREADED );

    OLETRACEOUT((API_OleInitializeWOW, hr));

    return hr;
}


//+-------------------------------------------------------------------------
//
//  Function:   OleInitialize
//
//  Synopsis:   Initializes OLE in single threaded mode
//
//  Effects:
//
//  Arguments:  [pMalloc]       -- the memory allocator to use
//
//  Requires:
//
//  Returns:    HRESULT
//
//  Signals:
//
//  Modifies:
//
//  Algorithm:
//
//  History:    dd-mmm-yy Author    Comment
//              06-Dec-93 alexgo    32bit port
//
//  Notes:
//
//--------------------------------------------------------------------------

STDAPI OleInitialize(void * pMalloc)
{
    OLETRACEIN((API_OleInitialize, PARAMFMT("pMalloc= %p"), pMalloc));

    VDATEHEAP();

    HRESULT hr;

    hr = OleInitializeEx( pMalloc, COINIT_APARTMENTTHREADED );

    OLETRACEOUT((API_OleInitialize, hr));

    return hr;
}

//+-------------------------------------------------------------------------
//
//  Function:   OleInitializeEx
//
//  Synopsis:   Initializes ole
//
//  Effects:
//
//  Arguments:  [pMalloc]       -- the task memory allocator to use
//              [flags]         -- single or multi-threaded
//
//  Requires:
//
//  Returns:    HRESULT
//
//  Signals:
//
//  Modifies:
//
//  Algorithm:
//
//  History:    dd-mmm-yy Author    Comment
//              06-Dec-93 alexgo    32bit port
//              24-May-94 AlexT     Propagate CoInitializeEx's return code
//              21-Jul-94 AlexT     Allow nested OleInit/Uninit calls
//              24-Aug-94 AlexT     Return S_OK for first success and S_FALSE
//                                  thereafter (unless an allocator was
//                                  passed in)
//
//  Notes:      This routine may be called multiple times per apartment
//
//--------------------------------------------------------------------------
#pragma SEG(OleInitialize)
STDAPI OleInitializeEx(LPVOID pMalloc, ULONG ulFlags)
{
    OLETRACEIN((API_OleInitialize, PARAMFMT("pMalloc= %p, ulFlags= %x"), pMalloc, ulFlags));
    VDATEHEAP();

    HRESULT hr;
#if DBG==1
    HRESULT hrCoInit = S_OK;
#endif
    DWORD cThreadOleInits;

    StartPerfCounter(CoInitialize);
    hr = CoInitializeEx(pMalloc, ulFlags);
    EndPerfCounter(CoInitialize);

    if (SUCCEEDED(hr))
    {
	Assert (g_hmodOLE2);
#if DBG==1
	hrCoInit = hr;
#endif

	COleTls tls;
	cThreadOleInits = ++ tls->cOleInits;

	do
	{
	    // We only want to do the below initialization once per apartment
	    if (cThreadOleInits > 1)
	    {
		// We've already been this way before, just return
		Assert(SUCCEEDED(hr) && "Bad OleInitializeEx logic");
		break;
	    }

	    // single thread registration of DDE and clipboard formats.
	    // Only do this once per process.

	    COleStaticLock lck(g_mxsSingleThreadOle);

	    if (++g_cOleProcessInits != 1)
	    {
		// already done the per-process initialization
		break;
	    }

	    if (FALSE==DDELibMain (g_hmodOLE2, (WORD)0, (WORD)0, NULL))
	    {
		Assert (!"DDELibMain failed()");

		hr = E_OUTOFMEMORY;
		break;
	    }

	    // Only need to do the initialization once so check the global
	    // that gets assigned last.

	    if( !g_aDropTarget )
	    {
#ifndef _CHICAGO_
	    // on NT3.51, clipboard formats are pre-registered for us by user32.
	    // We know they are going to be sequential.  This gives us a
	    // good performance improvement (since the clipboard formats never
	    // change.

	    // BUGBUG(alexgo); Chicago needs to get this behaviour too

	    g_cfObjectLink = RegisterClipboardFormat(OLESTR("ObjectLink"));

            g_cfOwnerLink = g_cfObjectLink + 1;
	    Assert(g_cfOwnerLink == RegisterClipboardFormat(OLESTR("OwnerLink")));

            g_cfNative = g_cfObjectLink + 2;
            Assert(g_cfNative == RegisterClipboardFormat(OLESTR("Native")));

            g_cfBinary = g_cfObjectLink + 3;
            Assert(g_cfBinary == RegisterClipboardFormat(OLESTR("Binary")));

            g_cfFileName = g_cfObjectLink + 4;
	    Assert(g_cfFileName == RegisterClipboardFormat(OLESTR("FileName")));

            g_cfFileNameW = g_cfObjectLink + 5;
            Assert(g_cfFileNameW ==
                    RegisterClipboardFormat(OLESTR("FileNameW")));

            g_cfNetworkName = g_cfObjectLink + 6;
            Assert(g_cfNetworkName  ==
                    RegisterClipboardFormat(OLESTR("NetworkName")));

            g_cfDataObject = g_cfObjectLink + 7;
            Assert(g_cfDataObject ==
                    RegisterClipboardFormat(OLESTR("DataObject")));

            g_cfEmbeddedObject = g_cfObjectLink + 8;
            Assert(g_cfEmbeddedObject ==
                    RegisterClipboardFormat(OLESTR("Embedded Object")));

            g_cfEmbedSource = g_cfObjectLink + 9;
            Assert(g_cfEmbedSource ==
                    RegisterClipboardFormat(OLESTR("Embed Source")));

            g_cfCustomLinkSource = g_cfObjectLink + 10;
            Assert(g_cfCustomLinkSource  ==
                    RegisterClipboardFormat(OLESTR("Custom Link Source")));

            g_cfLinkSource = g_cfObjectLink + 11;
            Assert(g_cfLinkSource ==
                    RegisterClipboardFormat(OLESTR("Link Source")));

            g_cfObjectDescriptor = g_cfObjectLink + 12;
            Assert(g_cfObjectDescriptor ==
                    RegisterClipboardFormat(OLESTR("Object Descriptor")));

            g_cfLinkSrcDescriptor = g_cfObjectLink + 13;
            Assert(g_cfLinkSrcDescriptor ==
                    RegisterClipboardFormat(OLESTR("Link Source Descriptor")));

            g_cfOleDraw = g_cfObjectLink + 14;
            Assert(g_cfOleDraw == RegisterClipboardFormat(OLESTR("OleDraw")));

            g_cfPBrush = g_cfObjectLink + 15;
            Assert(g_cfPBrush == RegisterClipboardFormat(OLESTR("PBrush")));

            g_cfMSDraw = g_cfObjectLink + 16;
            Assert(g_cfMSDraw == RegisterClipboardFormat(OLESTR("MSDraw")));

            g_cfOlePrivateData = g_cfObjectLink + 17;
            Assert(g_cfOlePrivateData ==
                    RegisterClipboardFormat(OLESTR("Ole Private Data")));

            g_cfScreenPicture = g_cfObjectLink + 18;
            Assert(g_cfScreenPicture  ==
                RegisterClipboardFormat(OLESTR("Screen Picture")));

            g_aDropTarget = GlobalAddAtom(OLE_DROP_TARGET_PROP);
            AssertSz(g_aDropTarget, "Couldn't add drop target atom\n");
	    }

	    // Used in Inplace editing
	    uOmPostWmCommand = RegisterWindowMessage(OLESTR("OM_POST_WM_COMMAND"));
	    uOleMessage	     = RegisterWindowMessage(OLESTR("OLE_MESSAHE"));

#else  //  !_CHICAGO_

	    g_cfObjectLink	  = SSRegisterClipboardFormatA("ObjectLink");
            g_cfOwnerLink         = SSRegisterClipboardFormatA("OwnerLink");
            g_cfNative            = SSRegisterClipboardFormatA("Native");
            g_cfBinary            = SSRegisterClipboardFormatA("Binary");
            g_cfFileName          = SSRegisterClipboardFormatA("FileName");
            g_cfFileNameW         = SSRegisterClipboardFormatA("FileNameW");
            g_cfNetworkName       = SSRegisterClipboardFormatA("NetworkName");
            g_cfDataObject        = SSRegisterClipboardFormatA("DataObject");
            g_cfEmbeddedObject    = SSRegisterClipboardFormatA("Embedded Object");
            g_cfEmbedSource       = SSRegisterClipboardFormatA("Embed Source");
            g_cfCustomLinkSource  = SSRegisterClipboardFormatA("Custom Link Source");
            g_cfLinkSource        = SSRegisterClipboardFormatA("Link Source");
            g_cfObjectDescriptor  = SSRegisterClipboardFormatA("Object Descriptor");
            g_cfLinkSrcDescriptor = SSRegisterClipboardFormatA("Link Source Descriptor");
            g_cfOleDraw           = SSRegisterClipboardFormatA("OleDraw");
            g_cfPBrush            = SSRegisterClipboardFormatA("PBrush");
            g_cfMSDraw            = SSRegisterClipboardFormatA("MSDraw");
            g_cfOlePrivateData    = SSRegisterClipboardFormatA("Ole Private Data");
            g_cfScreenPicture     = SSRegisterClipboardFormatA("Screen Picture");
            g_aDropTarget         = GlobalAddAtomA(OLE_DROP_TARGET_PROPA);
            AssertSz(g_aDropTarget, "Couldn't add drop target atom\n");
	    }

	    // Used in Inplace editing
	    uOmPostWmCommand = RegisterWindowMessageA("OM_POST_WM_COMMAND");
	    uOleMessage	     = RegisterWindowMessageA("OLE_MESSAHE");

#endif // !_CHICAGO_

	} while (FALSE); // end of do


	if (FAILED(hr))
	{
	    // clean up and break out
	    DDEWEP (WEP_FREE_DLL);

	    tls->cOleInits--;
	    CoUninitialize();
	}
	else
	{
#if defined(_CHICAGO_)
	    if (!gfShellInitialized && CoGetCurrentProcess() == 1)
	    {
		//  The Chicago Shell will dynamically load the OLE32.DLL to improve
		//  bootup start time.	    When an application calls CoInitialize, post
		//  a message to the shell to inform it to load OLE32.DLL if it hasn't
		//  already.   The Shell will never unload OLE32.DLL.
		//
		//  We are using an undocumented Shell interface.
		//
		//  We do this last so that we dont take a task switch while in
		//  CoInitialize.
#if DBG==1
		if (RegQueryValueEx(HKEY_CURRENT_USER,
                            L"Software\\Microsoft\\OLE2\\NoShellNotify",
                            NULL,  // reserved
                            NULL,  // lpdwType
                            NULL,  // lpbData
                            NULL) != ERROR_SUCCESS) // lpcbData
#endif
		{
		    HWND hwndShell = GetShellWindow();
		    if (hwndShell)
		    {
			PostMessage(hwndShell,WM_SHELLNOTIFY,
                                 SHELLNOTIFY_OLELOADED,0L);
		    }
		}

	    }

	    gfShellInitialized = TRUE;
#endif  // _CHICAGO_

	    Assert(SUCCEEDED(hr) && "Bad OleInitializeEx logic");

	    //	If we're overriding the allocator, we return whatever
	    //	CoInitializeEx returned

	    if (NULL != pMalloc)
	    {
		Assert(hr == hrCoInit && "Bad OleInit logic");
	    }
	    else if (1 == cThreadOleInits)
	    {
		//  First successful call to OleInitializeEx - S_OK
		hr = S_OK;
	    }
	    else
	    {
		//  Second or greater succesful call to OleInitializeEx - S_FALSE
		hr = S_FALSE;
	    }
	}
    }

    OLETRACEOUT((API_OleInitialize, hr));
    return hr;
}


//+-------------------------------------------------------------------------
//
//  Function:   OleUnitialize
//
//  Synopsis:   Unitializes OLE, releasing any grabbed resources
//
//  Effects:
//
//  Arguments:  void
//
//  Requires:
//
//  Returns:    void
//
//  Signals:
//
//  Modifies:
//
//  Algorithm:
//
//  History:    dd-mmm-yy Author    Comment
//              06-Dec-93 alexgo    32bit port
//              21-Jul-94 AlexT     Allow nested OleInit/Uninit calls
//
//  Notes:
//
//--------------------------------------------------------------------------
#pragma SEG(OleUninitialize)
STDAPI_(void) OleUninitialize(void)
{
    OLETRACEIN((API_OleUninitialize, NOPARAM));

    VDATEHEAP();

    COleTls tls(TRUE);

    if (tls.IsNULL() || 0 == tls->cOleInits)
    {
        LEDebugOut((DEB_ERROR,
                    "(0 == thread inits) Unbalanced call to OleUninitialize\n"));
        goto errRtn;
    }

    if (0 == -- tls->cOleInits)
    {
	// This thread has called OleUninitialize for the last time. Check if
	// we need to do per process uninit now.

	ClipboardUninitialize(); // Must be first thing
	DestroyCommonDdeWindow();

	COleStaticLock lck(g_mxsSingleThreadOle);

	if (--g_cOleProcessInits == 0)
	{

	    DragDropProcessUninitialize();

	    // after this point, the uninit should not fail (because we don't
	    // have code to redo the init).
	    DDEWEP (WEP_FREE_DLL);

#if DBG==1
	    // check for unreleased globals
	    UtGlobalFlushTracking();
#endif
	}
    }

    //  We call CoInitialize each time we call OleInitialize, so here we
    //  balance that call
    CoUninitialize();

errRtn:
    OLETRACEOUTEX((API_OleUninitialize, NORETURN));

    return;
}