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.

445 lines
9.8 KiB

  1. #define INIT_MY_GUIDS
  2. #include <ole2ver.h>
  3. #include "edataobj.h"
  4. // Count of the number of objects and number of locks.
  5. ULONG g_cObj=0;
  6. ULONG g_cLock=0;
  7. //Make window handle global so other code can cause a shutdown
  8. HWND g_hWnd=NULL;
  9. HINSTANCE g_hInst=NULL;
  10. /*
  11. * WinMain
  12. *
  13. * Purpose:
  14. * Main entry point of application.
  15. */
  16. int PASCAL
  17. WinMain(
  18. HINSTANCE hInst,
  19. HINSTANCE hInstPrev,
  20. LPSTR pszCmdLine,
  21. int nCmdShow)
  22. {
  23. MSG msg;
  24. PAPPVARS pAV;
  25. #ifndef WIN32
  26. int cMsg = 96;
  27. while (!SetMessageQueue(cMsg) && (cMsg -= 9))
  28. ;
  29. #endif
  30. g_hInst=hInst;
  31. pAV=new CAppVars(hInst, hInstPrev, pszCmdLine, nCmdShow);
  32. if (NULL==pAV)
  33. return -1;
  34. if (pAV->FInit())
  35. {
  36. while (GetMessage(&msg, NULL, 0,0 ))
  37. {
  38. TranslateMessage(&msg);
  39. DispatchMessage(&msg);
  40. }
  41. }
  42. delete pAV;
  43. return msg.wParam;
  44. }
  45. LRESULT WINAPI
  46. DataObjectWndProc(
  47. HWND hWnd,
  48. UINT iMsg,
  49. WPARAM wParam,
  50. LPARAM lParam)
  51. {
  52. switch (iMsg)
  53. {
  54. case WM_DESTROY:
  55. PostQuitMessage(0);
  56. break;
  57. default:
  58. return (DefWindowProc(hWnd, iMsg, wParam, lParam));
  59. }
  60. return 0L;
  61. }
  62. void PASCAL
  63. ObjectDestroyed(void)
  64. {
  65. g_cObj--;
  66. //No more objects and no locks, shut the app down.
  67. if (0L==g_cObj && 0L==g_cLock && IsWindow(g_hWnd))
  68. PostMessage(g_hWnd, WM_CLOSE, 0, 0L);
  69. return;
  70. }
  71. CAppVars::CAppVars(
  72. HINSTANCE hInst,
  73. HINSTANCE hInstPrev,
  74. LPSTR pszCmdLine,
  75. UINT nCmdShow)
  76. {
  77. m_hInst =hInst;
  78. m_hInstPrev =hInstPrev;
  79. m_pszCmdLine=pszCmdLine;
  80. m_nCmdShow = nCmdShow;
  81. m_hWnd=NULL;
  82. #if 0
  83. for (i=0; i < DOSIZE_CSIZES; i++)
  84. {
  85. m_rgdwRegCO[i]=0;
  86. m_rgpIClassFactory[i]=NULL;
  87. }
  88. #else
  89. m_dwRegCO = 0;
  90. m_pIClassFactory = NULL;
  91. #endif
  92. m_fInitialized=FALSE;
  93. return;
  94. }
  95. CAppVars::~CAppVars(void)
  96. {
  97. #if 0
  98. UINT i;
  99. //Revoke and destroy the class factories of all sizes
  100. for (i=0; i < DOSIZE_CSIZES; i++)
  101. {
  102. if (0L!=m_rgdwRegCO[i])
  103. CoRevokeClassObject(m_rgdwRegCO[i]);
  104. if (NULL!=m_rgpIClassFactory[i])
  105. m_rgpIClassFactory[i]->Release();
  106. }
  107. #else
  108. if (0L != m_dwRegCO)
  109. CoRevokeClassObject(m_dwRegCO);
  110. if (NULL != m_pIClassFactory)
  111. m_pIClassFactory->Release();
  112. #endif
  113. if (m_fInitialized)
  114. CoUninitialize();
  115. return;
  116. }
  117. /*
  118. * CAppVars::FInit
  119. *
  120. * Purpose:
  121. * Initializes an CAppVars object by registering window classes,
  122. * etc... If this function fails the caller should guarantee
  123. * that the destructor is called.
  124. *
  125. * Return Value:
  126. * BOOL TRUE if successful, FALSE otherwise.
  127. */
  128. BOOL
  129. CAppVars::FInit(void)
  130. {
  131. WNDCLASS wc;
  132. HRESULT hr;
  133. DWORD dwVer;
  134. #ifdef WIN32
  135. static TCHAR szClass[] = TEXT("IdataSvr32");
  136. #else
  137. static TCHAR szClass[] = TEXT("IdataSvr16");
  138. #endif
  139. //Check command line for -Embedding
  140. if (lstrcmpiA(m_pszCmdLine, "-Embedding"))
  141. return FALSE;
  142. dwVer=CoBuildVersion();
  143. if (rmm!=HIWORD(dwVer))
  144. return FALSE;
  145. if (FAILED(CoInitialize(NULL)))
  146. return FALSE;
  147. m_fInitialized=TRUE;
  148. if (!m_hInstPrev)
  149. {
  150. wc.style = CS_HREDRAW | CS_VREDRAW;
  151. wc.lpfnWndProc = DataObjectWndProc;
  152. wc.cbClsExtra = 0;
  153. wc.cbWndExtra = 0;
  154. wc.hInstance = m_hInst;
  155. wc.hIcon = NULL;
  156. wc.hCursor = NULL;
  157. wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  158. wc.lpszMenuName = NULL;
  159. wc.lpszClassName = szClass;
  160. if (!RegisterClass(&wc))
  161. return FALSE;
  162. }
  163. m_hWnd=CreateWindow(szClass,
  164. szClass,
  165. WS_OVERLAPPEDWINDOW,
  166. 135, 135, 350, 250,
  167. NULL, NULL, m_hInst, NULL);
  168. if (NULL==m_hWnd)
  169. return FALSE;
  170. g_hWnd=m_hWnd;
  171. //ShowWindow(m_hWnd, m_nCmdShow);
  172. //UpdateWindow(m_hWnd);
  173. #if 0
  174. /*
  175. * This code supplies three different classes, one for each type
  176. * of data object that handles a different size of data. All the
  177. * class factories share the same implementation, but their
  178. * instantiations differ by the type passed in the constructor.
  179. * When the class factories create objects, they pass that size
  180. * to the CDataObject contstructor as well.
  181. */
  182. UINT i;
  183. HRESULT hr2, hr3;
  184. for (i=0; i < DOSIZE_CSIZES; i++)
  185. {
  186. m_rgpIClassFactory[i]=new CDataObjectClassFactory(i);
  187. if (NULL==m_rgpIClassFactory[i])
  188. return FALSE;
  189. m_rgpIClassFactory[i]->AddRef();
  190. }
  191. hr=CoRegisterClassObject(CLSID_DataObjectSmall
  192. , m_rgpIClassFactory[0], CLSCTX_LOCAL_SERVER
  193. , REGCLS_MULTIPLEUSE, &m_rgdwRegCO[0]);
  194. hr2=CoRegisterClassObject(CLSID_DataObjectMedium
  195. , m_rgpIClassFactory[1], CLSCTX_LOCAL_SERVER
  196. , REGCLS_MULTIPLEUSE, &m_rgdwRegCO[1]);
  197. hr3=CoRegisterClassObject(CLSID_DataObjectLarge
  198. , m_rgpIClassFactory[2], CLSCTX_LOCAL_SERVER
  199. , REGCLS_MULTIPLEUSE, &m_rgdwRegCO[2]);
  200. if (FAILED(hr) || FAILED(hr2) || FAILED(hr3))
  201. return FALSE;
  202. #else
  203. m_pIClassFactory = new CDataObjectClassFactory();
  204. if (NULL == m_pIClassFactory)
  205. return FALSE;
  206. m_pIClassFactory->AddRef();
  207. #ifdef WIN32
  208. hr = CoRegisterClassObject( CLSID_DataObjectTest32,
  209. m_pIClassFactory,
  210. CLSCTX_LOCAL_SERVER,
  211. REGCLS_MULTIPLEUSE,
  212. &m_dwRegCO );
  213. #else
  214. hr = CoRegisterClassObject( CLSID_DataObjectTest16,
  215. m_pIClassFactory,
  216. CLSCTX_LOCAL_SERVER,
  217. REGCLS_MULTIPLEUSE,
  218. &m_dwRegCO );
  219. #endif // WIN32
  220. if (FAILED(hr))
  221. return FALSE;
  222. #endif
  223. return TRUE;
  224. }
  225. /*
  226. * CDataObjectClassFactory::CDataObjectClassFactory
  227. * CDataObjectClassFactory::~CDataObjectClassFactory
  228. *
  229. * Constructor Parameters:
  230. * iSize UINT specifying the data size for this class.
  231. */
  232. CDataObjectClassFactory::CDataObjectClassFactory()
  233. {
  234. m_cRef=0L;
  235. return;
  236. }
  237. CDataObjectClassFactory::~CDataObjectClassFactory(void)
  238. {
  239. return;
  240. }
  241. STDMETHODIMP
  242. CDataObjectClassFactory::QueryInterface(
  243. REFIID riid,
  244. PPVOID ppv)
  245. {
  246. *ppv=NULL;
  247. //Any interface on this object is the object pointer.
  248. #ifdef ORIGINAL_CODE_LOOKS_WRONG
  249. if (IID_IUnknown==riid || IID_IClassFactory==riid)
  250. #else
  251. if (IsEqualIID(IID_IUnknown, riid)|| IsEqualIID(IID_IClassFactory, riid))
  252. #endif
  253. *ppv = this;
  254. if (NULL!=*ppv)
  255. {
  256. ((LPUNKNOWN)*ppv)->AddRef();
  257. return NOERROR;
  258. }
  259. return ResultFromScode(E_NOINTERFACE);
  260. }
  261. STDMETHODIMP_(ULONG)
  262. CDataObjectClassFactory::AddRef(void)
  263. {
  264. return ++m_cRef;
  265. }
  266. STDMETHODIMP_(ULONG)
  267. CDataObjectClassFactory::Release(void)
  268. {
  269. ULONG cRefT;
  270. cRefT=--m_cRef;
  271. if (0L==m_cRef)
  272. delete this;
  273. return cRefT;
  274. }
  275. /*
  276. * CDataObjectClassFactory::CreateInstance
  277. *
  278. * Purpose:
  279. * Instantiates a CDataObject object that supports the IDataObject
  280. * and IUnknown interfaces. If the caller asks for a different
  281. * interface than these two then we fail.
  282. *
  283. * Parameters:
  284. * pUnkOuter LPUNKNOWN to the controlling IUnknown if we are
  285. * being used in an aggregation.
  286. * riid REFIID identifying the interface the caller
  287. * desires to have for the new object.
  288. * ppvObj PPVOID in which to store the desired interface
  289. * pointer for the new object.
  290. *
  291. * Return Value:
  292. * HRESULT NOERROR if successful, otherwise contains
  293. * E_NOINTERFACE if we cannot support the
  294. * requested interface.
  295. */
  296. STDMETHODIMP
  297. CDataObjectClassFactory::CreateInstance(
  298. LPUNKNOWN pUnkOuter,
  299. REFIID riid,
  300. PPVOID ppvObj)
  301. {
  302. PCDataObject pObj;
  303. HRESULT hr;
  304. *ppvObj=NULL;
  305. hr=ResultFromScode(E_OUTOFMEMORY);
  306. #ifdef ORIGINAL_CODE_LOOKS_WRONG
  307. if (NULL!=pUnkOuter && IID_IUnknown!=riid)
  308. #else
  309. if (NULL!=pUnkOuter && (! IsEqualIID(IID_IUnknown, riid) ) )
  310. #endif
  311. return ResultFromScode(E_NOINTERFACE);
  312. //Create the object telling it the data size to work with
  313. pObj=new CDataObject(pUnkOuter, ObjectDestroyed);
  314. if (NULL==pObj)
  315. return hr;
  316. if (pObj->FInit())
  317. hr=pObj->QueryInterface(riid, ppvObj);
  318. g_cObj++;
  319. if (FAILED(hr))
  320. {
  321. delete pObj;
  322. ObjectDestroyed(); //Decrements g_cObj
  323. }
  324. return hr;
  325. }
  326. /*
  327. * CDataObjectClassFactory::LockServer
  328. *
  329. * Purpose:
  330. * Increments or decrements the lock count of the serving
  331. * IClassFactory object. When the number of locks goes to
  332. * zero and the number of objects is zero, we shut down the
  333. * application.
  334. *
  335. * Parameters:
  336. * fLock BOOL specifying whether to increment or
  337. * decrement the lock count.
  338. *
  339. * Return Value:
  340. * HRESULT NOERROR always.
  341. */
  342. STDMETHODIMP
  343. CDataObjectClassFactory::LockServer(
  344. BOOL fLock)
  345. {
  346. if (fLock)
  347. g_cLock++;
  348. else
  349. {
  350. g_cLock--;
  351. //No more objects and no locks, shut the app down.
  352. if (0L==g_cObj && 0L==g_cLock && IsWindow(g_hWnd))
  353. PostMessage(g_hWnd, WM_CLOSE, 0, 0L);
  354. }
  355. return NOERROR;
  356. }
  357.