Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

568 lines
13 KiB

  1. #include "pch.hxx"
  2. #include "tbtest.hxx"
  3. #define OPEN 1
  4. #define CREATE 2
  5. extern "C" CLSID CLSID_ThumbnailUpdater;
  6. EXTERN_C const IID IID_IFlatStorage = { /* b29d6138-b92f-11d1-83ee-00c04fc2c6d4 */
  7. 0xb29d6138,
  8. 0xb92f,
  9. 0x11d1,
  10. {0x83, 0xee, 0x00, 0xc0, 0x4f, 0xc2, 0xc6, 0xd4}
  11. };
  12. void
  13. Call_CreateUpdater(REFIID riid, void** ppv)
  14. {
  15. HRESULT sc;
  16. sc = CoCreateInstance(CLSID_ThumbnailUpdater,
  17. NULL,
  18. CLSCTX_INPROC_SERVER,
  19. riid,
  20. ppv);
  21. if(FAILED(sc))
  22. {
  23. printf("CoCreateInstance of Thumbnaile Updater failed %x\n", sc);
  24. exit(0);
  25. }
  26. }
  27. HRESULT
  28. Call_IFilterStatus(IFilterStatus *pIFS,
  29. WCHAR * pwszFileName)
  30. {
  31. HRESULT sc;
  32. sc = pIFS->PreFilter(pwszFileName);
  33. if(FAILED(sc))
  34. {
  35. printf("PreFilter returned %x\n", sc);
  36. exit(0);
  37. }
  38. return S_OK;
  39. }
  40. HRESULT
  41. Call_CheckTime()
  42. {
  43. FILETIME mtime, ctime, atime;
  44. DWORD rc;
  45. HANDLE hFile;
  46. hFile = CreateFile( g_tszFileName,
  47. GENERIC_READ | GENERIC_WRITE,
  48. 0,
  49. NULL,
  50. OPEN_EXISTING,
  51. FILE_ATTRIBUTE_NORMAL,
  52. NULL);
  53. if(INVALID_HANDLE_VALUE == hFile)
  54. {
  55. printf("CheckTime File Open error %x\n", GetLastError());
  56. return S_OK;
  57. }
  58. if(!GetFileTime(hFile, &ctime, &atime, &mtime))
  59. {
  60. printf("GetFileTime Failed %x\n", GetLastError());
  61. exit(0);
  62. }
  63. CloseHandle(hFile);
  64. printf("file ctime=%x:%x, atime=%x:%x, mtime=%x:%x\n", ctime, atime, mtime);
  65. return S_OK;
  66. }
  67. IStorage *
  68. Call_CreateOplockStorageFile(DWORD mode)
  69. {
  70. IOplockStorage *pIOpStg;
  71. IStorage *pstg;
  72. HRESULT sc;
  73. Call_CreateUpdater(IID_IOplockStorage, (void**)&pIOpStg);
  74. switch(mode)
  75. {
  76. case CREATE:
  77. sc = pIOpStg->CreateStorageEx(
  78. g_tszFileName,
  79. STGM_CREATE | STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
  80. STGFMT_FILE,
  81. 0,
  82. IID_IFlatStorage,
  83. (void**)&pstg);
  84. break;
  85. case OPEN:
  86. sc = pIOpStg->OpenStorageEx(
  87. g_tszFileName,
  88. STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
  89. STGFMT_FILE,
  90. 0,
  91. IID_IFlatStorage,
  92. (void**)&pstg);
  93. break;
  94. default:
  95. printf("Bad file open mode\n");
  96. exit(0);
  97. break;
  98. }
  99. if(FAILED(sc))
  100. {
  101. printf("IOplockStorage::{Create/Open}StorageEx failed %x\n", sc);
  102. exit(0);
  103. }
  104. if(NULL != pIOpStg)
  105. pIOpStg->Release();
  106. return pstg;
  107. }
  108. IStorage *
  109. Call_CreateStorageFile(DWORD mode)
  110. {
  111. HRESULT hr;
  112. IStorage *pstg;
  113. DWORD stgfmt = STGFMT_FILE;
  114. if(g_AnyStorage)
  115. stgfmt = STGFMT_ANY;
  116. switch(mode)
  117. {
  118. case CREATE:
  119. hr = StgCreateStorageEx(
  120. g_tszFileName,
  121. STGM_CREATE | STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
  122. stgfmt,
  123. 0,0,0,
  124. IID_IFlatStorage,
  125. (void**)&pstg);
  126. break;
  127. case OPEN:
  128. hr = StgOpenStorageEx(
  129. g_tszFileName,
  130. STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
  131. stgfmt,
  132. 0,0,0,
  133. IID_IFlatStorage,
  134. (void**)&pstg);
  135. break;
  136. default:
  137. printf("Bad file open mode\n");
  138. exit(0);
  139. break;
  140. }
  141. if(FAILED(hr))
  142. {
  143. printf("Stg{Create/Open}StorageEx failed %x\n", hr);
  144. exit(0);
  145. }
  146. return pstg;
  147. }
  148. CLSID CLSID_JunkClassFile = { /* ce8103fd-905b-11d1-83eb-00c04fc2c6d4 */
  149. 0xce8103fd,
  150. 0x905b,
  151. 0x11d1,
  152. {0x83, 0xeb, 0x00, 0xc0, 0x4f, 0xc2, 0xc6, 0xd4}
  153. };
  154. void
  155. Call_SetClass(IStorage *pstg)
  156. {
  157. HRESULT sc;
  158. sc = pstg->SetClass(CLSID_JunkClassFile);
  159. if(FAILED(sc))
  160. {
  161. printf("SetClass Failed %x\n", sc);
  162. exit(0);
  163. }
  164. }
  165. void
  166. Call_Stat(
  167. IStorage *pstg,
  168. STATSTG *pstat)
  169. {
  170. HRESULT sc;
  171. sc = pstg->Stat(pstat, 0);
  172. if(FAILED(sc))
  173. {
  174. printf("Stat failed %x\n");
  175. exit(0);
  176. }
  177. printf("Pathname is: %ws\n", pstat->pwcsName);
  178. if( IsEqualGUID(pstat->clsid, CLSID_JunkClassFile) )
  179. printf("Class GUID is OK\n");
  180. else if( IsEqualGUID(pstat->clsid, CLSID_NULL) )
  181. printf("Class GUID is NULL_CLSID\n");
  182. else
  183. printf("Class GUID is not the JunkClassGuid!!\n");
  184. }
  185. void
  186. Call_QI(IUnknown *punk, REFIID riid, void **pv)
  187. {
  188. HRESULT hr;
  189. hr = punk->QueryInterface(riid, pv);
  190. if(FAILED(hr))
  191. {
  192. printf("QI failed %x\n", hr);
  193. exit(0);
  194. }
  195. }
  196. IStream *
  197. Call_OpenStream(IStorage *pstg, DWORD grfMode, int num)
  198. {
  199. WCHAR name[80];
  200. IStream *pstm;
  201. HRESULT hr;
  202. wsprintf(name, L"%02d", num);
  203. if(grfMode & STGM_CREATE)
  204. {
  205. hr = pstg->CreateStream(name, grfMode|STGM_SHARE_EXCLUSIVE, 0, 0, &pstm);
  206. }
  207. else
  208. {
  209. hr = pstg->OpenStream(name, 0, grfMode|STGM_SHARE_EXCLUSIVE, 0, &pstm);
  210. }
  211. if(FAILED(hr))
  212. {
  213. printf("CreateStream(%s) failed %x\n", name, hr);
  214. exit(0);
  215. }
  216. return pstm;
  217. }
  218. IStream *
  219. Call_OpenCONTENTSStream(IStorage *pstg)
  220. {
  221. IStream *pstm;
  222. HRESULT hr;
  223. hr = pstg->OpenStream(L"CONTENTS", 0,
  224. STGM_READ | STGM_SHARE_EXCLUSIVE,
  225. 0,
  226. &pstm);
  227. if(FAILED(hr) || NULL == pstm)
  228. {
  229. printf("OpenContentStream failed %x\n", hr);
  230. exit(0);
  231. }
  232. return pstm;
  233. }
  234. IPropertyStorage *
  235. Call_OpenPropStg(IStorage *pstg, DWORD mode, REFFMTID fmtid )
  236. {
  237. IPropertySetStorage *ppropsetstg = NULL;
  238. IPropertyStorage *ppropstg = NULL;
  239. HRESULT hr = S_OK;
  240. hr = pstg->QueryInterface( IID_IPropertySetStorage, (void**)&ppropsetstg );
  241. if( FAILED(hr) )
  242. {
  243. printf( "QI for IPropertySetStorage failed %x\n", hr );
  244. exit(0);
  245. }
  246. switch(mode)
  247. {
  248. case CREATE:
  249. hr = ppropsetstg->Create(fmtid, NULL,
  250. PROPSETFLAG_DEFAULT,
  251. STGM_CREATE | STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
  252. &ppropstg );
  253. break;
  254. case OPEN:
  255. hr = ppropsetstg->Open(fmtid,
  256. STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
  257. &ppropstg );
  258. break;
  259. default:
  260. printf("Bad file open mode\n");
  261. exit(0);
  262. break;
  263. }
  264. if(FAILED(hr))
  265. {
  266. printf("OpenPropStg failed %x\n", hr);
  267. exit(0);
  268. }
  269. if( NULL != ppropsetstg )
  270. ppropsetstg->Release();
  271. return ppropstg;
  272. }
  273. void
  274. Call_SuppressChanges(ITimeAndNoticeControl *ptnc)
  275. {
  276. HRESULT hr;
  277. hr = ptnc->SuppressChanges(0, 0);
  278. if(FAILED(hr))
  279. {
  280. printf("SuppressChanges failed %x\n", hr);
  281. exit(0);
  282. }
  283. printf("--------SuppressChanges called\n");
  284. }
  285. void
  286. TestPause(char * sz)
  287. {
  288. if(g_Pause)
  289. {
  290. printf("Before %s: press <return> to continue.", sz);
  291. getchar();
  292. }
  293. }
  294. EXTERN_C void
  295. __cdecl
  296. wmain(
  297. int argc,
  298. WCHAR **argv)
  299. {
  300. IStorage *pstg=NULL;
  301. ITimeAndNoticeControl *ptnc=NULL;
  302. IFilterStatus *pIFS=NULL;
  303. DWORD fOpenMode;
  304. STATSTG stat;
  305. LONG cRefs;
  306. CoInitialize(NULL);
  307. ParseArgs(argc, argv);
  308. if(g_CheckTime)
  309. Call_CheckTime();
  310. if(g_CheckIsStg)
  311. printf("StgIsStorageFile = %x\n", StgIsStorageFile(g_tszFileName));
  312. if(g_UseUpdater)
  313. {
  314. Call_CreateUpdater(IID_IFilterStatus, (void**)&pIFS);
  315. Call_IFilterStatus(pIFS, g_tszFileName);
  316. pIFS->Release();
  317. }
  318. else if(!g_NoOpenStg)
  319. {
  320. if(g_CreateStg)
  321. fOpenMode = CREATE;
  322. else
  323. fOpenMode = OPEN;
  324. if(g_OplockFile)
  325. pstg = Call_CreateOplockStorageFile(fOpenMode);
  326. else
  327. pstg = Call_CreateStorageFile(fOpenMode);
  328. if(g_AddRefStg)
  329. {
  330. pstg->AddRef();
  331. pstg->Release();
  332. }
  333. if(g_SuppressTime)
  334. {
  335. Call_QI(pstg, IID_ITimeAndNoticeControl, (void**)&ptnc);
  336. Call_SuppressChanges(ptnc);
  337. ptnc->Release();
  338. }
  339. if(g_SetClass)
  340. Call_SetClass(pstg);
  341. if(g_Stat)
  342. Call_Stat(pstg, &stat);
  343. TestPause("OpenStream");
  344. if(!g_NoOpenStm)
  345. {
  346. char readBuffer[1024];
  347. ULONG cb, cbXfred;
  348. IStream *pstm=NULL;
  349. IStream *pstmContents=NULL;
  350. IPropertyStorage *ppropstg=NULL;
  351. PROPSPEC propspec;
  352. PROPVARIANT propvar;
  353. HRESULT hr;
  354. if(g_CreateStm)
  355. {
  356. pstm = Call_OpenStream(pstg, STGM_CREATE|STGM_READWRITE, 1);
  357. ppropstg = Call_OpenPropStg(pstg, CREATE, FMTID_SummaryInformation);
  358. }
  359. else
  360. {
  361. pstm = Call_OpenStream(pstg, STGM_READWRITE, 1);
  362. pstmContents = Call_OpenCONTENTSStream(pstg);
  363. ppropstg = Call_OpenPropStg(pstg, OPEN, FMTID_SummaryInformation);
  364. }
  365. if(g_AddRefStm)
  366. {
  367. pstm->AddRef();
  368. pstm->Release();
  369. }
  370. if( g_ReleaseStg )
  371. {
  372. pstg->Release();
  373. pstg = NULL;
  374. }
  375. if(g_WriteStm)
  376. {
  377. TestPause("WriteStream");
  378. hr = pstm->Write((void*)"First ", 6, &cbXfred);
  379. if(FAILED(hr))
  380. {
  381. printf("First Write Failed with %x\n", hr);
  382. exit(0);
  383. }
  384. hr = pstm->Write((void*)"Second", 6, &cb);
  385. if(FAILED(hr))
  386. {
  387. printf("Second Write Failed with %x\n", hr);
  388. exit(0);
  389. }
  390. printf("Wrote %d bytes, in two pieces\n", cbXfred+cb);
  391. propspec.ulKind = PRSPEC_PROPID;
  392. propspec.propid = PIDSI_TITLE;
  393. propvar.vt = VT_LPSTR;
  394. propvar.pszVal = "My Title";
  395. hr = ppropstg->WriteMultiple( 1, &propspec, &propvar, PID_FIRST_USABLE );
  396. if(FAILED(hr))
  397. {
  398. printf( "First WriteMultiple failed with %x\n", hr );
  399. exit(0);
  400. }
  401. propspec.propid = PIDSI_COMMENTS;
  402. propvar.pszVal = "My Comments";
  403. hr = ppropstg->WriteMultiple( 1, &propspec, &propvar, PID_FIRST_USABLE );
  404. if(FAILED(hr))
  405. {
  406. printf( "Second WriteMultiple failed with %x\n", hr );
  407. exit(0);
  408. }
  409. printf("Wrote two properties\n");
  410. }
  411. if(g_ReadStm)
  412. {
  413. TestPause("ReadStream");
  414. hr = pstm->Read((void*)readBuffer, sizeof(readBuffer), &cbXfred);
  415. if(FAILED(hr))
  416. {
  417. printf("Read Failed with %x\n", hr);
  418. exit(0);
  419. }
  420. readBuffer[cbXfred] = '\0';
  421. printf("Read %d bytes: \"%s\"\n", cbXfred, readBuffer);
  422. propspec.ulKind = PRSPEC_PROPID;
  423. propspec.propid = PIDSI_TITLE;
  424. hr = ppropstg->ReadMultiple( 1, &propspec, &propvar );
  425. if(FAILED(hr))
  426. {
  427. printf("ReadMultiple failed with %x\n", hr );
  428. exit(0);
  429. }
  430. printf("ReadMultiple the title: \"%s\"\n", propvar.pszVal );
  431. PropVariantClear(&propvar);
  432. }
  433. if(NULL != pstm)
  434. {
  435. if( 0 != (cRefs = pstm->Release()))
  436. printf("Last release of the Stream and still %d References!!\n", cRefs);
  437. pstm = NULL;
  438. }
  439. if(NULL != pstmContents)
  440. {
  441. if(0 != (cRefs = pstmContents->Release()))
  442. printf("Last release of the PropertyStorage and still %d References!!\n", cRefs);
  443. pstmContents = NULL;
  444. }
  445. if(NULL != ppropstg)
  446. {
  447. if(0 != (cRefs = ppropstg->Release()))
  448. printf("Last release of the PropertyStorage and still %d References!!\n", cRefs);
  449. ppropstg = NULL;
  450. }
  451. }
  452. if(NULL != pstg)
  453. {
  454. TestPause("Last Release");
  455. if(0 != (cRefs = pstg->Release()))
  456. {
  457. printf("Last release of the Storage and still %d References!!\n", cRefs);
  458. }
  459. pstg = NULL;
  460. }
  461. }
  462. if(g_CheckTime)
  463. Call_CheckTime();
  464. CoUninitialize();
  465. exit(0);
  466. }