summaryrefslogblamecommitdiffstats
path: root/private/ole32/com/dde/server/ddeutils.cxx
blob: e4043675968e95ffd270023f2f48b3147d78fe72 (plain) (tree)
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013




















































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                     
/****************************** Module Header ******************************\
* Module Name: ddeutils.c
*
* Purpose: Conatains all the utility routines
*
* Created: 1990
*
* Copyright (c) 1990, 1991  Microsoft Corporation
*
* History:
*   Raor, Srinik (../../1990)    Designed and coded
*
\***************************************************************************/
#include "ole2int.h"
#include <dde.h>
#include "srvr.h"
#include "ddesrvr.h"
#include "ddedebug.h"
ASSERTDATA


#define WM_NCMOUSEFIRST WM_NCMOUSEMOVE
#define WM_NCMOUSELAST WM_NCMBUTTONDBLCLK



#define KB_64   65536

extern ATOM    aTrue;
extern ATOM    aFalse;

extern ATOM    aStdCreateFromTemplate;
extern ATOM    aStdCreate;
extern ATOM    aStdOpen;
extern ATOM    aStdEdit;
extern ATOM    aStdShowItem;
extern ATOM    aStdClose;
extern ATOM    aStdExit;
extern ATOM    aStdDoVerbItem;


//ScanBoolArg: scans the argument which is not included in
//the quotes. These args could be only TRUE or FALSE for
//the time being. !!!The scanning routines should be
//merged and it should be generalized.

INTERNAL_(LPSTR)     ScanBoolArg
(
LPSTR   lpstr,
BOOL    FAR *lpflag
)
{


    LPSTR   lpbool;
    ATOM    aShow;
    char    ch;

    lpbool = lpstr;

    // !!! These routines does not take care of quoted quotes.

    while((ch = *lpstr) && (!(ch == ')' || ch == ',')))
	lpstr++;

    if(ch == NULL)
       return NULL;

    *lpstr++ = NULL;       // terminate the arg by null

    // if terminated by paren, then check for end of command
    // syntax.

    // Check for the end of the command string.
    if (ch == ')') {
	if (*lpstr++ != ']')
	    return NULL;

	if(*lpstr != NULL)
	    return NULL;	     //finally should be terminated by null.

    }

    aShow = GlobalFindAtomA (lpbool);
    if (aShow == aTrue)
	*lpflag = TRUE;

    else {
	if (aShow ==aFalse)
	    *lpflag = FALSE;
	else
	    return NULL;;
    }
    return lpstr;
}


//+---------------------------------------------------------------------------
//
//  Function:   CreateUnicodeFromAnsi
//
//  Synopsis:   Creates a UNICODE string from an ANSI string
//
//  Effects:    Makes a new UNICODE string from the given ANSI string.
//		The new UNICODE string is returned. Memory is allocated
//		using PrivMemAlloc
//
//  Arguments:  [lpAnsi] -- Ansi version of string
//
//  Requires:
//
//  Returns:	NULL if cannot create new string.
//
//  Signals:
//
//  Modifies:
//
//  Algorithm:
//
//  History:    6-07-94   kevinro Commented/cleaned
//
//  Notes:
//
//----------------------------------------------------------------------------
LPOLESTR CreateUnicodeFromAnsi( LPCSTR lpAnsi)
{
    WCHAR buf[MAX_PATH];
    ULONG ccbuf;
    LPOLESTR lpWideStr;

    if ((ccbuf=MultiByteToWideChar(CP_ACP,0,lpAnsi,-1,buf,MAX_PATH))
	 == FALSE)
    {
	intrAssert(!"Unable to convert characters");
	return NULL;
    }

    lpWideStr = (LPOLESTR) PrivMemAlloc(ccbuf * sizeof(WCHAR));

    if (lpWideStr != NULL)
    {
	memcpy(lpWideStr,buf,ccbuf*sizeof(WCHAR));
    }
    return(lpWideStr);
}
//+---------------------------------------------------------------------------
//
//  Function:   CreateAnsiFromUnicode
//
//  Synopsis:   Creates an Ansi string from a UNICODE string
//
//  Effects:    Makes a new ANSI string from the given UNICODE string.
//		The new string is returned. Memory is allocated
//		using PrivMemAlloc
//
//  Arguments:  [lpUnicode] -- Unicode version of string
//
//  Requires:
//
//  Returns:	NULL if cannot create new string.
//
//  Signals:
//
//  Modifies:
//
//  Algorithm:
//
//  History:    6-07-94   kevinro Commented/cleaned
//
//  Notes:
//
//----------------------------------------------------------------------------
LPSTR CreateAnsiFromUnicode( LPCOLESTR lpUnicode)
{
    char buf[MAX_PATH];
    ULONG ccbuf;
    LPSTR lpAnsiStr;

    ccbuf = WideCharToMultiByte(CP_ACP,
			    	0,
				lpUnicode,
				-1,
				buf,
				MAX_PATH,
				NULL,
				NULL);


    if (ccbuf == FALSE)
    {
	intrAssert(!"Unable to convert characters");
	return NULL;
    }

    lpAnsiStr = (LPSTR) PrivMemAlloc(ccbuf * sizeof(char));

    if (lpAnsiStr != NULL)
    {
	memcpy(lpAnsiStr,buf,ccbuf);
    }
    return(lpAnsiStr);
}

//ScannumArg: Checks for the syntax of num arg in Execute and if
//the arg is syntactically correct, returns the ptr to the
//beginning of the next arg and also, returns the number
//Does not take care of the last num arg in the list.

INTERNAL_(LPSTR)       ScanNumArg
(
LPSTR   lpstr,
LPINT   lpnum
)
{

    WORD    val = 0;
    char    ch;

    while((ch = *lpstr++) && (ch != ',')) {
	if (ch < '0' || ch >'9')
	    return NULL;
	val += val * 10 + (ch - '0');

    }

    if(!ch)
       return NULL;

    *lpnum = val;
    return lpstr;
}




//ScanArg: Checks for the syntax of arg in Execute and if
//the arg is syntactically correct, returns the ptr to the
//beginning of the next arg or to the end of the excute string.

INTERNAL_(LPSTR)      ScanArg
(
LPSTR   lpstr
)
{


    // !!! These routines does not take care of quoted quotes.

    // first char should be quote.

    if (*(lpstr-1) != '\"')
	return NULL;

    while(*lpstr && *lpstr != '\"')
	lpstr++;

    if(*lpstr == NULL)
       return NULL;

    *lpstr++ = NULL;       // terminate the arg by null

    if(!(*lpstr == ',' || *lpstr == ')'))
	return NULL;


    if(*lpstr++ == ','){

	if(*lpstr == '\"')
	    return ++lpstr;
	// If it is not quote, leave the ptr on the first char
	return lpstr;
    }

    // terminated by paren
    // already skiped right paren

    // Check for the end of the command string.
    if (*lpstr++ != ']')
	return NULL;

    if(*lpstr != NULL)
	return NULL;    	 //finally should be terminated by null.

    return lpstr;
}

// ScanCommand: scanns the command string for the syntax
// correctness. If syntactically correct, returns the ptr
// to the first arg or to the end of the string.

INTERNAL_(WORD)   ScanCommand
(
LPSTR       lpstr,
WORD        wType,
LPSTR FAR * lplpnextcmd,
ATOM FAR *  lpAtom
)
{
    // !!! These routines does not take care of quoted quotes.
    // and not taking care of blanks arround the operators

    // !!! We are not allowing blanks after operators.
    // Should be allright! since this is arestricted syntax.

    char    ch;
    LPSTR   lptemp = lpstr;


    while(*lpstr && (!(*lpstr == '(' || *lpstr == ']')))
	lpstr++;

    if(*lpstr == NULL)
       return NULL;

    ch = *lpstr;
    *lpstr++ = NULL;       // set the end of command

    *lpAtom = GlobalFindAtomA (lptemp);

    if (!IsOleCommand (*lpAtom, wType))
	return NON_OLE_COMMAND;

    if (ch == '(') {
	ch = *lpstr++;

	if (ch == ')') {
	     if (*lpstr++ != ']')
		return NULL;
	}
	else {
	    if (ch != '\"')
		return NULL;
	}

	*lplpnextcmd = lpstr;
	return OLE_COMMAND;
    }

    // terminated by ']'

    if (*(*lplpnextcmd = lpstr)) // if no nul termination, then it is error.
	return NULL;

    return OLE_COMMAND;
}


//MakeDataAtom: Creates a data atom from the item string
//and the item data otions.

INTERNAL_(ATOM)  MakeDataAtom
(
ATOM    aItem,
int     options
)
{
    WCHAR    buf[MAX_STR];

    if (options == OLE_CHANGED)
	return DuplicateAtom (aItem);

    if (!aItem)
	buf[0] = NULL;
    else
	GlobalGetAtomName (aItem, buf, MAX_STR);

    if (options == OLE_CLOSED)
	lstrcatW (buf, OLESTR("/Close"));
    else {
	if (options == OLE_SAVED)
	   lstrcatW (buf, OLESTR("/Save"));
	else
	    AssertSz (0, "Bad option\n");
    }

    Puts ("MakeDataAtom "); Puts(buf); Putn();
    if (buf[0])
	return wGlobalAddAtom (buf);
    else
	return NULL;
}

//DuplicateAtom: Duplicates an atom
INTERNAL_(ATOM)  DuplicateAtom
(
ATOM    atom
)
{
    WCHAR buf[MAX_STR];

    if (!atom)
	return NULL;

    GlobalGetAtomName (atom, buf, MAX_STR);
    return wGlobalAddAtom (buf);
}

// MakeGlobal: makes global out of strings.
// works only for << 64k

INTERNAL_(HANDLE)   MakeGlobal
(
LPSTR   lpstr
)
{

    int     len = 0;
    HANDLE  hdata  = NULL;
    LPSTR   lpdata = NULL;

    len = strlen (lpstr) + 1;

    hdata = GlobalAlloc (GMEM_MOVEABLE | GMEM_DDESHARE, len);

    if (hdata == NULL || (lpdata = (LPSTR) GlobalLock (hdata)) == NULL)
	goto errRtn;


    memcpy(lpdata, lpstr, (DWORD)len);
    GlobalUnlock (hdata);
    return hdata;

errRtn:
    Assert (0);
    if (lpdata)
	GlobalUnlock (hdata);


    if (hdata)
	GlobalFree (hdata);

     return NULL;

}


INTERNAL_(BOOL) CLSIDFromAtom(ATOM aClass, LPCLSID lpclsid)
{
    WCHAR szProgID[MAX_STR];
    if (!ISATOM (aClass))
	return FALSE;
    WORD cb=GlobalGetAtomName (aClass, szProgID, MAX_STR);
    Assert (cb>0 && cb < (MAX_STR - 1));

    return CLSIDFromProgID(szProgID, lpclsid) == S_OK;
}

// CLSIDFromAtomWithTreatAs
//
// Input: *paClass
// Output: *pclsid == corresponding CLSID, taking into account TreatAs and
//                     AutoConvert
//		   *paClass == atom correpsonding to *pclsid
//
#pragma SEG(CLSIDFromAtomWithTreatAs)
INTERNAL CLSIDFromAtomWithTreatAs
	(ATOM FAR* 	paClass,
	LPCLSID 	pclsid,
	CNVTYP FAR* pcnvtyp)
{
    HRESULT hr;


    intrDebugOut((DEB_ITRACE,
		  "%p _IN CLSIDFromAtomWithTreatAs(paClass=%x,"
		  "pclsid=%x,pcnvtyp=%x)\n",0,
		  paClass,pclsid,pcnvtyp));

    LPOLESTR szProgID = NULL;
    CLSID clsidNew;

    if (!CLSIDFromAtom (*paClass, pclsid))
    {
	hr = S_FALSE;
	goto exitRtn;
    }

    DEBUG_GUIDSTR(clsidStr,pclsid);

    intrDebugOut((DEB_ITRACE,"Guid %ws",clsidStr));
    if (CoGetTreatAsClass (*pclsid, &clsidNew) == NOERROR)
    {
	DEBUG_GUIDSTR(newStr,pclsid);

	intrDebugOut((DEB_ITRACE," cnvtypTreatAs %ws\n",newStr));
    	if (pcnvtyp)
    		*pcnvtyp = cnvtypTreatAs;
    }
    else if (OleGetAutoConvert (*pclsid, &clsidNew) == NOERROR)
    {
	DEBUG_GUIDSTR(newStr,pclsid);
	intrDebugOut((DEB_ITRACE," cnvtypConvertTo %ws\n",newStr));
    	if (pcnvtyp)
    		*pcnvtyp = cnvtypConvertTo;
    }
    else	
    {
	intrDebugOut((DEB_ITRACE," no conversion\n"));
    	if (pcnvtyp)
    		*pcnvtyp = cnvtypNone;
    	clsidNew = *pclsid; // no translation
    }

    hr = ProgIDFromCLSID(clsidNew, &szProgID);
    if (FAILED(hr))
    {
	intrDebugOut((DEB_ITRACE,"  ProgIDFromCLSID failed\n"));
	goto exitRtn;
    }

    intrDebugOut((DEB_ITRACE,"ProgIDFromCLSID returns %ws\n",szProgID));
    *paClass = GlobalAddAtom (szProgID);
    *pclsid  = clsidNew;
    CoTaskMemFree(szProgID);

exitRtn:
    intrDebugOut((DEB_ITRACE,
		  "%p OUT CLSIDFromAtomWithTreatAs returns %x\n",
		  0,hr));

    return hr;
}


INTERNAL_(BOOL)  PostMessageToClientWithReply
(
HWND    hWnd,
UINT    wMsg,
WPARAM  wParam,  // posting window
LPARAM  lParam,
UINT    wReplyMsg
)
{
    MSG 		msg;

    if (!IsWindowValid (hWnd))
    {
	AssertSz(FALSE, "Client's window is missing");
	return FALSE;
    }

    if (!IsWindowValid ((HWND)wParam))
    {
	AssertSz (0, "Posting window is invalid");
	return FALSE;
    }

    // Post message to client failed. Treat it as if we got the reply.
    if (!PostMessageToClient (hWnd, wMsg, wParam, lParam))
	return FALSE;

    return NOERROR == wTimedGetMessage (&msg, (HWND)wParam, WM_DDE_TERMINATE,
					WM_DDE_TERMINATE);
}



INTERNAL_(BOOL)  PostMessageToClient
(
    HWND    hWnd,
    UINT    wMsg,
    WPARAM  wParam,
    LPARAM  lParam
)
{
	UINT c=0;

	while (c < 10)
	{
	    if (!IsWindowValid (hWnd)) {
    	    Warn ("Client's window is missing");
        	return FALSE;
	    }
		Puts ("Posting"); Puth(wMsg); Puts("to"); Puth(hWnd); Putn();
    	if (PostMessage (hWnd, wMsg, wParam, lParam))
			return TRUE; // success
		else
		{
			Yield();
			c++; // try again
		}
	}
	return FALSE;
}


INTERNAL_(BOOL)     IsWindowValid
    (HWND    hwnd)
{
    HTASK   htask;

    if (!IsWindow (hwnd))
	return FALSE;

    htask  = GetWindowThreadProcessId(hwnd, NULL);

#ifndef WIN32
    if (IsTask(htask))
#endif
	return TRUE;

    return FALSE;
}



INTERNAL_(BOOL)  UtilQueryProtocol
(
ATOM    aClass,
LPOLESTR   lpprotocol
)
{
    HKEY    hKey;
    WCHAR    key[MAX_STR];
    WCHAR    cclass[MAX_STR];

    if (!aClass)
	return FALSE;

    if (!GlobalGetAtomName (aClass, cclass, MAX_STR))
	return FALSE;

    lstrcpyW (key, cclass);
    lstrcatW (key, OLESTR("\\protocol\\"));
    lstrcatW (key, lpprotocol);
    lstrcatW (key, OLESTR("\\server"));
    if (RegOpenKey (HKEY_CLASSES_ROOT, key, &hKey) != ERROR_SUCCESS)
	return FALSE;
    RegCloseKey (hKey);
    return TRUE;
}



INTERNAL_(BOOL)  IsOleCommand
(
ATOM    aCmd,
WORD    wType
)
{
    if (wType == WT_SRVR) {
	if ((aCmd == aStdCreateFromTemplate)
		|| (aCmd == aStdCreate)
		|| (aCmd == aStdOpen)
		|| (aCmd == aStdEdit)
		|| (aCmd == aStdShowItem)
		|| (aCmd == aStdClose)
		|| (aCmd == aStdExit))
	    return TRUE;
    }
    else {
	if ((aCmd == aStdClose)
		|| (aCmd == aStdDoVerbItem)
		|| (aCmd == aStdShowItem))
	    return TRUE;
    }

    return FALSE;
}
INTERNAL wFileBind
	(LPOLESTR szFile,
	LPUNKNOWN FAR* ppUnk)
{
	HRESULT hresult = NOERROR;
	LPBC pbc = NULL;
	LPMONIKER pmk = NULL;
	*ppUnk = NULL;
	ErrRtnH (CreateBindCtx (0, &pbc));
	ErrRtnH (CreateFileMoniker (szFile, &pmk));
	ErrRtnH (pmk->BindToObject (pbc, NULL, IID_IUnknown, (LPLPVOID) ppUnk));
  errRtn:
//	AssertOutPtrIface(hresult, *ppUnk);
	if (pbc)
		pbc->Release();
	if (pmk)
		pmk->Release();
	return hresult;
}

// SynchronousPostMessage
//
// Post a message and wait for the ack.
// (jasonful)
//
INTERNAL SynchronousPostMessage
    (HWND   hWndTo,  // also who you expect the reply from
    UINT    wMsg,
    WPARAM  wParam,
    LPARAM  lParam)
{
#ifdef _MAC
#else

    HRESULT hresult = NOERROR;

    static unsigned iCounter;


    HWND hWndFrom = (HWND) wParam;



    RetZ (IsWindowValid(hWndFrom));
    RetZ (IsWindowValid(hWndTo));

    Assert (wMsg != WM_DDE_INITIATE);  // can't check for positive ack.

    RetZS (PostMessage (hWndTo, wMsg, wParam, lParam), RPC_E_SERVER_DIED);

    MSG msg;
    RetErr (wTimedGetMessage (&msg, hWndFrom, WM_DDE_ACK, WM_DDE_ACK));
    Assert (msg.message == WM_DDE_ACK);
    if (!( GET_WM_DDE_ACK_STATUS(msg.wParam,msg.lParam) & POSITIVE_ACK))
	hresult = ResultFromScode (RPC_E_DDE_NACK);
    if (msg.hwnd != hWndFrom)
	hresult = ResultFromScode (RPC_E_DDE_UNEXP_MSG);



    return hresult;
#endif _MAC
}


INTERNAL wFileIsRunning
    (LPOLESTR szFile)
{
    LPMONIKER pmk = NULL;
    LPBINDCTX pbc=NULL;
    HRESULT hresult;

    RetErr (CreateBindCtx (0, &pbc));
    ErrRtnH (CreateFileMoniker (szFile, &pmk));
    hresult = pmk->IsRunning (pbc, NULL, NULL);
  errRtn:
    if (pbc)
	pbc->Release();
    if (pmk)
	pmk->Release();
    return hresult;
}




//+---------------------------------------------------------------------------
//
//  Function:   IsFile
//
//  Synopsis:   Given a handle to an atom, determine if it is a file
//
//  Effects:    Attempts to get the files attributes. If there are no
//		attributes, then the file doesn't exist.
//
//  Arguments:  [a] -- Atom for filename
//
//  Requires:
//
//  Returns:
//
//  Signals:
//
//  Modifies:
//
//  Algorithm:
//
//  History:    5-03-94   kevinro Commented/cleaned
//
//  Notes:
//
//----------------------------------------------------------------------------
INTERNAL_ (BOOL) IsFile
    (ATOM a,	BOOL FAR* pfUnsavedDoc)
{
	LPMONIKER pmk = NULL;
	LPBC pbc = NULL;
	LPRUNNINGOBJECTTABLE pROT=NULL;

	WCHAR szFile [MAX_STR];
	if (0==GlobalGetAtomName (a, szFile, MAX_STR))
	    return FALSE;

	DWORD dwAttribs = GetFileAttributes(szFile);

	 /* flags prevent sharing violation*/
	if (dwAttribs != 0xFFFFFFFF)
	{
		if (pfUnsavedDoc)
			*pfUnsavedDoc = FALSE;
		return TRUE;
	}
	// This will deal with unsaved documents in the ROT.
	// We do NOT want to call pmk->IsRunning because if a 2.0 client called
	// DdeIsRunning, we do not want call it here, because then we get stuck
	// in an infinite loop.  We only care about true 2.0 running objects.


	//
	// BUGBUG: KevinRo There is a function (GetPathFromRot) that could replace
	// the following code sequence.
	//

	BOOL f= NOERROR==CreateBindCtx (0, &pbc) &&
			NOERROR==CreateFileMoniker (szFile, &pmk) &&
			NOERROR==pbc->GetRunningObjectTable (&pROT) &&
			NOERROR==pROT->IsRunning (pmk) ;
	if (pROT)
		pROT->Release();
	if (pmk)
		pmk->Release();
	if (pbc)
		pbc->Release();
	if (pfUnsavedDoc)
		*pfUnsavedDoc = TRUE;
	return f;


}

// wCompatibleClasses
//
// Determine if class "aClient" is Auto-Converted to class "aSrvr" or
// Treated-As class "aSrvr".
// (Does not check if aClient==aSrvr)
//
#pragma SEG(wCompatibleClasses)
INTERNAL wCompatibleClasses
	(ATOM aClient,
	ATOM aSrvr)
{
	CLSID clsidClient, clsidSrvr, clsidTo;
	HRESULT hresult;
	RetZS (CLSIDFromAtom (aClient, &clsidClient), S_FALSE);
	RetZS (CLSIDFromAtom (aSrvr,   &clsidSrvr  ), S_FALSE);
	if (NOERROR==OleGetAutoConvert (clsidClient, &clsidTo)
		&& clsidTo == clsidSrvr)
	{
		// aClient is Auto-Converted to aSrvr
		return NOERROR;
	}
	hresult = CoGetTreatAsClass(clsidClient, &clsidTo);
	if (hresult != NOERROR)
	{
	    intrDebugOut((DEB_IERROR,
	    		  "wCompatibleClasses CoGetTreatAs returns %x\n",
			  hresult));
	    return(hresult);
	}

	if (clsidTo == clsidSrvr)
	{
		// aClient is Treated-As aSrvr
		return NOERROR;
	}
	return ResultFromScode (S_FALSE); // not compatible
}



// wCreateStgAroundNative
//
// Build an OLE2 storage around 1.0 native data by putting it in
// stream "\1Ole10Native" and creating valid CompObj and OLE streams.
// Return the IStorage and the ILockBytes it is built on.
//
INTERNAL wCreateStgAroundNative
	(HANDLE hNative,
	ATOM	aClassOld,
	ATOM	aClassNew,
	CNVTYP	cnvtyp,
	ATOM	aItem,
	LPSTORAGE FAR* ppstg,
	LPLOCKBYTES FAR* pplkbyt)
{
	HRESULT   hresult;
	LPSTORAGE pstg = NULL;
	LPLOCKBYTES plkbyt = NULL;
	LPOLESTR szUserType = NULL;
	WCHAR szClassOld [256];
	CLSID     clsid;
	ATOM	aClass;
	*ppstg = NULL;

	intrDebugOut((DEB_ITRACE,
	    	      "%p wCreateStgAroundNative(hNative=%x,aClassOld=%x"
		      ",aClassNew=%x cnvtyp=%x,aItem=%x)\n",
		      0,hNative,aClassOld,aClassNew,cnvtyp,aItem));

	// Create temporary docfile on our ILockBytes
	ErrRtnH (CreateILockBytesOnHGlobal (NULL,/*fDeleteOnRelease*/TRUE,&plkbyt));

	Assert (plkbyt);

	ErrRtnH (StgCreateDocfileOnILockBytes (plkbyt, grfCreateStg, 0, &pstg));

	RetZ (pstg);
	Assert (NOERROR==StgIsStorageILockBytes(plkbyt));

	aClass = (cnvtyp == cnvtypConvertTo)?aClassNew:aClassOld;

	if (CLSIDFromAtom (aClass,(LPCLSID)&clsid) == FALSE)
	{
	    hresult = REGDB_E_CLASSNOTREG;
	    goto errRtn;
	}

	ErrRtnH (WriteClassStg (pstg, clsid));

	// The UserType always corresponds to the clsid.
	ErrRtnH (OleRegGetUserType (clsid, USERCLASSTYPE_FULL, &szUserType));

	// The format is always the 1.0 format (classname/progid)
	ErrZS (GlobalGetAtomName (aClassOld, szClassOld, 256), E_UNEXPECTED);

	ErrRtnH (WriteFmtUserTypeStg (pstg, RegisterClipboardFormat(szClassOld),
						 			szUserType));


	if (cnvtyp == cnvtypConvertTo)
	{
		// SetConvertStg also writes a complete default Ole Stream
		ErrRtnH (SetConvertStg (pstg, TRUE));
	}
	else
	{
		ErrRtnH (WriteOleStg (pstg, NULL, (CLIPFORMAT)0, NULL));
	}
	ErrRtnH (StSave10NativeData (pstg, hNative, FALSE));
	if (aItem)
	{
		ErrRtnH (StSave10ItemName (pstg, wAtomNameA (aItem)));
	}
	*ppstg = pstg;
	*pplkbyt = plkbyt;
	return NOERROR;

  errRtn:
	if (pstg)
		pstg->Release();
	if (plkbyt)
		plkbyt->Release();
	CoTaskMemFree(szUserType);
	return hresult;
}


#ifdef _DEBUG


INTERNAL_ (BOOL) IsAtom (ATOM a)
{
    WCHAR sz[256]= {0};
    if (a < 0xc000)
	return FALSE;
    WORD cb=GlobalGetAtomName (a, sz, 256);
    Assert (lstrlenW(sz) == (int) cb);
    return cb>0 && cb < MAX_STR;
}


#include <limits.h>
#undef GlobalFree




INTERNAL_(HANDLE) wGlobalFree (HANDLE h)
{
    LPVOID p;
    Assert ((GlobalFlags(h) & GMEM_LOCKCOUNT)==0);
    if (!(p=GlobalLock(h)))
    {
	Puts ("Cannot free handle");
	Puth (h);
	Putn();
	AssertSz(0, "Invalid Handle\r\n");
    }
    Assert (!IsBadReadPtr (p, (UINT) min (UINT_MAX, GlobalSize(h))));
    Assert (GlobalUnlock(h)==0);
    Verify (!GlobalFree (h));
    Puts ("FREEING ");
    Puth (h);
    Putn ();
    return NULL; // success
}



#undef GlobalDeleteAtom

INTERNAL_(ATOM) wGlobalDeleteAtom (ATOM a)
{
    WCHAR sz[256];
    Assert (0 != GlobalGetAtomName (a, sz, 256));
    Assert (0==GlobalDeleteAtom (a));
    return (ATOM)0;
}

INTERNAL_(int) wCountChildren
    (HWND h)
{
    int c = 0;
    HWND hwndChild = GetWindow (h, GW_CHILD);
    while (hwndChild)
    {
	c++;
	hwndChild = GetWindow (hwndChild, GW_HWNDNEXT);
    }
    return c;
}


#endif // _DEBUG