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
|
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
WCHAR *Convert(char *psz)
{ int c;
WCHAR *pwsz = new WCHAR [ c = strlen(psz) + 1 ];
if (pwsz)
mbstowcs(pwsz, psz, c);
return pwsz;
}
#define CHECKNE(hr, stat, string, errRet) \
if (hr != stat) { iLine = __LINE__; pszErr = string; goto errRet; }
HRESULT TouchFile(WCHAR *pwszSource)
{
FILETIME ftc;
SYSTEMTIME st;
GetSystemTime(&st);
SystemTimeToFileTime(&st, &ftc);
return StgSetTimes(pwszSource, &ftc, NULL, NULL);
}
HRESULT CreateLink(WCHAR *pwszClient, WCHAR *pwszSource)
{
HRESULT hr;
IBindCtx *pbc=NULL;
ULONG ulEaten;
IMoniker *pmkSource=NULL;
IStorage *pstg=NULL;
IOleObject *poo=NULL;
IMoniker *pmkThis=NULL;
IMoniker *pmkLink=NULL;
IPersistStorage *pps=NULL;
char *pszErr;
int iLine=0;
hr = CreateFileMoniker(pwszClient, &pmkThis);
CHECKNE(hr, S_OK, "CreateFileMoniker", errRet);
hr = CreateItemMoniker(L"!", L"Link", &pmkLink);
CHECKNE(hr, S_OK, "CreateItemMoniker", errRet);
hr = StgCreateDocfile(pwszClient, STGM_CREATE | STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
0, &pstg);
CHECKNE(hr, S_OK, "StgCreateDocfile", errRet);
hr = CreateBindCtx(0, &pbc);
CHECKNE(hr, S_OK, "CreateBindCtx", errRet);
hr = MkParseDisplayName(pbc, pwszSource, &ulEaten, &pmkSource);
CHECKNE(hr, S_OK, "MkParseDisplayName", errRet);
hr = OleCreateLink(pmkSource, IID_IOleObject, OLERENDER_NONE, NULL, NULL, pstg, (void**)&poo);
CHECKNE(hr, S_OK, "OleCreateLink", errRet);
hr = poo->SetMoniker(OLEWHICHMK_CONTAINER, pmkThis);
CHECKNE(hr, S_OK, "SetMonikerOLEWHICHMK_CONTAINER", errRet);
hr = poo->SetMoniker(OLEWHICHMK_OBJREL, pmkLink);
CHECKNE(hr, S_OK, "SetMonikerOLEWHICHMK_OBJREL", errRet);
hr = poo->QueryInterface(IID_IPersistStorage, (void**)&pps);
CHECKNE(hr, S_OK, "QueryInterface(IID_IPersistStorage", errRet);
hr = OleSave(pps, pstg, TRUE);
CHECKNE(hr, S_OK, "OleSave", errRet);
errRet:
if (hr != S_OK)
printf("CreateLink at %s line %d with hr=%08x\n", pszErr, iLine, hr);
if (pbc) pbc->Release();
if (pmkSource) pmkSource->Release();
if (pstg) pstg->Release();
if (poo) poo->Release();
if (pmkThis) pmkThis->Release();
if (pmkLink) pmkLink->Release();
if (pps) pps->Release();
return hr;
}
HRESULT ResolveLink(WCHAR *pwszClient, WCHAR *pwszSource, BOOL fReSave)
{
HRESULT hr;
IStorage *pstg = NULL;
IOleLink *pol = NULL;
WCHAR *pwszSourceFound = NULL;
IPersistStorage *pps = NULL;
char *pszErr;
int iLine;
hr = StgOpenStorage(pwszClient, NULL, STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
NULL, 0, &pstg);
CHECKNE(hr, S_OK, "StgOpenStorage(pwszClient", errRet);
hr = OleLoad(pstg, IID_IOleLink, NULL, (void**)&pol);
CHECKNE(hr, S_OK, "OleLoad(pstg, IID_IOleLink", errRet);
hr = pol->BindToSource(0, NULL);
CHECKNE(hr, S_OK, "pol->BindToSource", errRet);
hr = pol->GetSourceDisplayName(&pwszSourceFound);
CHECKNE(hr, S_OK, "pol->GetSourceDisplayName", errRet);
if (wcsicmp(pwszSourceFound, pwszSource) != 0)
{
printf("SourceFound=%ls, SourceExpected=%ls\n", pwszSourceFound, pwszSource);
pszErr = "'path compare'";
hr = E_FAIL;
goto errRet;
}
if (fReSave)
{
hr = pol->QueryInterface(IID_IPersistStorage, (void**)&pps);
CHECKNE(hr, S_OK, "pol->QueryInterface(IID_IPersistStorage", errRet);
hr = OleSave(pps, pstg, TRUE);
CHECKNE(hr, S_OK, "OleSave(pps, pstg, TRUE)", errRet);
}
errRet:
if (hr != S_OK)
printf("ResolveLink at %s line %d with hr=%08x\n", pszErr, iLine, hr);
CoTaskMemFree(pwszSourceFound);
if (pol) pol->Release();
if (pstg) pstg->Release();
if (pps) pps->Release();
return hr;
}
int __cdecl main(int argc, char **argv)
{
char * pszExit = "TEST FAILED.";
int iExit = 1;
WCHAR *pwszClient = NULL;
WCHAR *pwszSource = NULL;
HRESULT hr=E_FAIL;
BOOL fArgsOk = FALSE;
HRESULT hrExpected=S_OK;
CoInitialize(NULL);
OleInitialize(NULL);
if (argc >= 3)
{
pwszClient = Convert(argv[2]);
}
if (argc >= 5)
{
pwszSource = Convert(argv[3]);
sscanf(argv[4], "%x", &hrExpected);
if (!pwszClient || !pwszSource)
{
pszExit = "Out of memory.";
goto errExit;
}
}
if (argc == 3)
{
if (stricmp(argv[1], "/t") == 0 || stricmp(argv[1], "-t") == 0)
{
// touch file creation date
hr = TouchFile(pwszClient);
fArgsOk = TRUE;
}
}
else
if (argc == 5)
{
if (stricmp(argv[1], "/c") == 0 || stricmp(argv[1], "-c") == 0)
{
// create link
hr = CreateLink(pwszClient, pwszSource);
fArgsOk = TRUE;
}
}
else
if (argc == 6)
{
if (stricmp(argv[1], "/r") == 0 || stricmp(argv[1], "-r") == 0)
{
// resolve link
hr = ResolveLink(pwszClient, pwszSource, argv[5][0] == 's');
fArgsOk = TRUE;
}
}
if (!fArgsOk)
{
pszExit = "Usage: /c client source expected_status -- create .lts file linked to source\n"
" /r client expected_source expected_status save/nosave -- resolve .lts file, checked linked to source\n"
" expected_source is ignored(but required) if not found during resolve.\n"
" /t source -- change the creation date\n";
}
else
{
if (hrExpected != hr)
{
printf("Expected status = %08X, actual status = %08X\n",
hrExpected,
hr);
}
else
{
pszExit = "TEST PASSED.";
iExit = 0;
}
}
errExit:
OleUninitialize();
CoUninitialize();
delete pwszClient;
delete pwszSource;
puts(pszExit);
return iExit;
}
|