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.

191 lines
5.1 KiB

  1. #include "stdinc.h"
  2. PCWSTR g_pcwszManifestFilename = NULL;
  3. PCWSTR g_pcwszGuidToCreate = NULL;
  4. DWORD g_dwOperations = NULL;
  5. DWORD g_dwContexts = 0;
  6. DWORD g_dwCoInitContextForCreatedThread = 0;
  7. DWORD g_dwCoInitContextForMainThread = 0;
  8. #define OP_CREATE_ALTERNATE_THREAD (0x00000001)
  9. #define OP_ACTIVATE_BEFORE_CREATE_ON_CREATE_THREAD (0x00000002)
  10. DWORD WINAPI ThreadFunc(PVOID)
  11. {
  12. ACTCTXW Ctx = { sizeof(Ctx) };
  13. WCHAR wchPath[MAX_PATH];
  14. HANDLE hActCtx = INVALID_HANDLE_VALUE;
  15. ULONG_PTR ulpCookie = 0;
  16. CLSID clsidToCreate;
  17. HRESULT hr = S_OK;
  18. IUnknown* ppUnk = NULL;
  19. if (g_dwOperations & OP_CREATE_ALTERNATE_THREAD)
  20. {
  21. hr = CoInitializeEx(NULL, g_dwCoInitContextForCreatedThread);
  22. if (FAILED(hr))
  23. {
  24. wprintf(L"Failed to coinitialize on the alternate thread, error 0x%08lx\n", hr);
  25. return 0;
  26. }
  27. }
  28. if (g_pcwszManifestFilename == NULL)
  29. {
  30. GetModuleFileNameW(NULL, wchPath, MAX_PATH);
  31. wcscpy(wcsrchr(wchPath, L'.'), L".manifest");
  32. g_pcwszManifestFilename = wchPath;
  33. }
  34. if (g_pcwszGuidToCreate != NULL)
  35. {
  36. if (FAILED(CLSIDFromString(const_cast<LPOLESTR>(g_pcwszGuidToCreate), &clsidToCreate)))
  37. {
  38. wprintf(L"Failed converting guid string '%ls' to a real GUID",
  39. g_pcwszGuidToCreate);
  40. return 0;
  41. }
  42. }
  43. Ctx.lpSource = g_pcwszManifestFilename;
  44. hActCtx = CreateActCtxW(&Ctx);
  45. if (g_dwOperations & OP_ACTIVATE_BEFORE_CREATE_ON_CREATE_THREAD)
  46. {
  47. if (!ActivateActCtx(hActCtx, &ulpCookie))
  48. {
  49. wprintf(L"Failed activation context %p?\n", hActCtx);
  50. return 0;
  51. }
  52. }
  53. //
  54. // First pass, with context nonactive
  55. //
  56. hr = CoCreateInstance(
  57. clsidToCreate,
  58. NULL,
  59. g_dwContexts,
  60. IID_IUnknown,
  61. (PVOID*)&ppUnk);
  62. if (SUCCEEDED(hr) && (ppUnk != NULL))
  63. {
  64. wprintf(L"Created instance.\n");
  65. ppUnk->Release();
  66. ppUnk = NULL;
  67. }
  68. else
  69. {
  70. DWORD le = GetLastError();
  71. wprintf(L"Error creating instance - 0x%08lx - lasterror %ld\r\n", hr, le);
  72. }
  73. if (g_dwOperations & OP_ACTIVATE_BEFORE_CREATE_ON_CREATE_THREAD)
  74. {
  75. if (!DeactivateActCtx(0, ulpCookie))
  76. {
  77. wprintf(L"Failed deactivating context cookie %l\n", ulpCookie);
  78. return 0;
  79. }
  80. }
  81. if ( hActCtx != INVALID_HANDLE_VALUE )
  82. {
  83. ReleaseActCtx(hActCtx);
  84. }
  85. if (g_dwOperations & OP_CREATE_ALTERNATE_THREAD)
  86. {
  87. CoUninitialize();
  88. }
  89. return 0;
  90. }
  91. void
  92. PumpMessagesUntilSignalled(HANDLE hThing)
  93. {
  94. do
  95. {
  96. // Wait 20msec for a signal ... otherwise we'll spin
  97. MSG msg;
  98. DWORD dwWaitResult = WaitForSingleObject(hThing, 20);
  99. if (dwWaitResult == WAIT_OBJECT_0)
  100. break;
  101. // Pump all waiting messages
  102. while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  103. {
  104. TranslateMessage(&msg);
  105. DispatchMessage(&msg);
  106. }
  107. }
  108. while (true);
  109. }
  110. int __cdecl wmain(int argc, WCHAR** wargv)
  111. {
  112. HANDLE hCreatedThread;
  113. DWORD dwThreadIdent;
  114. HRESULT hr;
  115. for (int i = 1; i < argc; i++)
  116. {
  117. if (lstrcmpiW(wargv[i], L"-manifest") == 0)
  118. g_pcwszManifestFilename = wargv[++i];
  119. else if (lstrcmpiW(wargv[i], L"-activatebeforecreate") == 0)
  120. g_dwOperations |= OP_ACTIVATE_BEFORE_CREATE_ON_CREATE_THREAD;
  121. else if (lstrcmpiW(wargv[i], L"-createotherthread") == 0)
  122. g_dwOperations |= OP_CREATE_ALTERNATE_THREAD;
  123. else if (lstrcmpiW(wargv[i], L"-coinitparamformainthread") == 0)
  124. g_dwCoInitContextForMainThread = _wtoi(wargv[++i]);
  125. else if (lstrcmpiW(wargv[i], L"-coinitparamforcreatedthread") == 0)
  126. g_dwCoInitContextForCreatedThread = _wtoi(wargv[++i]);
  127. else if (lstrcmpiW(wargv[i], L"-clsctx") == 0)
  128. g_dwContexts = _wtoi(wargv[++i]);
  129. else if (lstrcmpiW(wargv[i], L"-clsidtocreate") == 0)
  130. g_pcwszGuidToCreate = wargv[++i];
  131. else {
  132. wprintf(L"Invalid parameter %ls\r\n", wargv[i]);
  133. return -1;
  134. }
  135. }
  136. if (g_pcwszGuidToCreate == NULL)
  137. {
  138. wprintf(L"Must at least use -clsidtocreate to give a clsid target\r\n");
  139. return -2;
  140. }
  141. hr = CoInitializeEx(NULL, g_dwCoInitContextForMainThread);
  142. if (FAILED(hr))
  143. {
  144. wprintf(L"Failed main CoInitializeEx, error 0x%08lx\n", hr);
  145. return -3;
  146. }
  147. //
  148. // Create that alternate thread to do the test on?
  149. //
  150. if (g_dwOperations & OP_CREATE_ALTERNATE_THREAD)
  151. {
  152. hCreatedThread = CreateThread(NULL, 0, ThreadFunc, NULL, 0, &dwThreadIdent);
  153. PumpMessagesUntilSignalled(hCreatedThread);
  154. CloseHandle(hCreatedThread);
  155. }
  156. else
  157. {
  158. ThreadFunc(NULL);
  159. }
  160. CoUninitialize();
  161. }