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.

189 lines
5.3 KiB

  1. #include "windows.h"
  2. #include "comdef.h"
  3. #include "comip.h"
  4. #include "stdio.h"
  5. class __declspec(uuid("8af0ddb0-39bf-4e8e-b459-3561ef382ab3")) COutOfProcFoo : public IUnknown
  6. {
  7. ULONG m_ulRefCount;
  8. public:
  9. COutOfProcFoo(void) : m_ulRefCount(1) { }
  10. STDMETHOD_(ULONG, AddRef)() { return m_ulRefCount++; }
  11. STDMETHOD_(ULONG, Release)() { if (--m_ulRefCount == 0) { delete this; return 0; } return m_ulRefCount; }
  12. STDMETHOD(QueryInterface)(REFIID riid, void** ppvObject)
  13. {
  14. if (ppvObject == NULL) return E_INVALIDARG;
  15. *ppvObject = NULL;
  16. if (riid != IID_IUnknown) return E_NOINTERFACE;
  17. *ppvObject = (IUnknown*)this;
  18. return this->AddRef(), S_OK;
  19. }
  20. };
  21. ULONG g_ulServerLocks = 0;
  22. class __declspec(uuid("bf944fe6-54a3-4d27-ad96-16a9d427c01b")) CFooFactory : public IClassFactory
  23. {
  24. public:
  25. ULONG m_ulRefCount;
  26. DWORD m_dwRegisteredServerItem;
  27. CFooFactory(void) : m_dwRegisteredServerItem(0), m_ulRefCount(1) { }
  28. STDMETHOD_(ULONG, AddRef)() { return m_ulRefCount++; }
  29. STDMETHOD_(ULONG, Release)() { if (--m_ulRefCount == 0) { delete this; return 0; } return m_ulRefCount; }
  30. STDMETHOD(QueryInterface)(REFIID riid, void** ppvObject)
  31. {
  32. if (ppvObject == NULL) return E_INVALIDARG;
  33. *ppvObject = NULL;
  34. if (riid == IID_IUnknown) *ppvObject = (IUnknown*)this;
  35. if (riid == IID_IClassFactory) *ppvObject = (IClassFactory*)this;
  36. return (*ppvObject != NULL) ? this->AddRef(), S_OK : E_NOINTERFACE;
  37. }
  38. STDMETHOD(CreateInstance)(IUnknown* punk, REFIID riid, void** ppUnk)
  39. {
  40. if (punk != NULL) return CLASS_E_NOAGGREGATION;
  41. COutOfProcFoo *pFoo = new COutOfProcFoo();
  42. HRESULT hr = pFoo->QueryInterface(riid, ppUnk);
  43. pFoo->Release();
  44. return hr;
  45. }
  46. STDMETHOD(LockServer)(BOOL fLock)
  47. {
  48. g_ulServerLocks += fLock ? 1 : -1;
  49. return S_OK;
  50. }
  51. };
  52. int __cdecl wmain(int argc, WCHAR** argv)
  53. {
  54. bool fRegister = false;
  55. bool fCleanup = false;
  56. bool fClient = false;
  57. PCWSTR pcwszMachineName = NULL;
  58. CoInitializeEx(NULL, COINIT_MULTITHREADED);
  59. for (int i = 1; i < argc; i++)
  60. {
  61. if (lstrcmpiW(argv[i], L"-register") == 0)
  62. fRegister = true;
  63. else if (lstrcmpiW(argv[i], L"-unregister") == 0)
  64. fCleanup = true;
  65. else if (lstrcmpiW(argv[i], L"-client") == 0)
  66. fClient = true;
  67. else if (lstrcmpiW(argv[i], L"-machine") == 0)
  68. pcwszMachineName = argv[++i];
  69. }
  70. if ((fRegister && fCleanup) || (fRegister && fClient) || (fClient && fCleanup))
  71. {
  72. wprintf(L"One at a time, please\n");
  73. return -1;
  74. }
  75. //
  76. // Add to the registry
  77. //
  78. if (fRegister)
  79. {
  80. HKEY hkClsidRoot;
  81. ULONG ulResult;
  82. WCHAR wchLocalPath[MAX_PATH];
  83. GetModuleFileNameW(NULL, wchLocalPath, MAX_PATH);
  84. ulResult = RegSetValueW(
  85. HKEY_CLASSES_ROOT,
  86. L"CLSID\\{8af0ddb0-39bf-4e8e-b459-3561ef382ab3}\\LocalServer32",
  87. REG_SZ,
  88. wchLocalPath,
  89. lstrlenW(wchLocalPath) * sizeof(WCHAR));
  90. if (ulResult != ERROR_SUCCESS)
  91. {
  92. wprintf(L"Issues creating localserver32 key default value. - whoops?\n");
  93. return -1;
  94. }
  95. }
  96. else if (fCleanup)
  97. {
  98. ULONG ulResult;
  99. ulResult = RegDeleteKeyW(HKEY_CLASSES_ROOT, L"CLSID\\{8af0ddb0-39bf-4e8e-b459-3561ef382ab3}\\LocalServer32");
  100. if (ulResult == ERROR_SUCCESS)
  101. {
  102. ulResult = RegDeleteKeyW(HKEY_CLASSES_ROOT, L"CLSID\\{8af0ddb0-39bf-4e8e-b459-3561ef382ab3}");
  103. }
  104. wprintf((ulResult == ERROR_SUCCESS) ? L"Successfully uninstalled." : L"Can't uninstall!");
  105. }
  106. else if (fClient)
  107. {
  108. IUnknown *punk;
  109. HRESULT hr;
  110. MULTI_QI mqi;
  111. COSERVERINFO serverInfo = { 0 };
  112. mqi.pIID = &IID_IUnknown;
  113. mqi.pItf = NULL;
  114. mqi.hr = 0;
  115. if (pcwszMachineName)
  116. serverInfo.pwszName = _wcsdup(pcwszMachineName);
  117. hr = CoCreateInstanceEx(
  118. __uuidof(COutOfProcFoo),
  119. NULL,
  120. CLSCTX_ALL,
  121. pcwszMachineName ? &serverInfo : NULL,
  122. 1,
  123. &mqi);
  124. if (FAILED(hr))
  125. {
  126. wprintf(L"Failed cocreate, error code 0x%08x\n", hr);
  127. }
  128. else
  129. {
  130. wprintf(L"Created fine.\n");
  131. mqi.pItf->Release();
  132. }
  133. if (serverInfo.pwszName)
  134. free(serverInfo.pwszName);
  135. }
  136. else
  137. {
  138. CFooFactory *pFactory = new CFooFactory();
  139. IUnknown *pUnk = NULL;
  140. if (FAILED(pFactory->QueryInterface(IID_IUnknown, (void**)&pUnk)))
  141. {
  142. pFactory->Release();
  143. return -1;
  144. }
  145. CoRegisterClassObject(
  146. __uuidof(COutOfProcFoo),
  147. pUnk,
  148. CLSCTX_SERVER,
  149. REGCLS_MULTIPLEUSE,
  150. &pFactory->m_dwRegisteredServerItem
  151. );
  152. pUnk->Release();
  153. while(true)
  154. {
  155. Sleep(500);
  156. }
  157. CoRevokeClassObject(pFactory->m_dwRegisteredServerItem);
  158. pFactory->Release();
  159. }
  160. CoUninitialize();
  161. }