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.

457 lines
12 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1995.
  5. //
  6. // File: plugdll.cxx
  7. //
  8. // Contents:
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. // History: 11-07-1996 JohannP (Johann Posch) Created
  15. //
  16. //----------------------------------------------------------------------------
  17. #include <urlint.h>
  18. #include <stdio.h>
  19. #include <sem.hxx>
  20. #include "urlcf.hxx"
  21. #include "selfreg.hxx"
  22. #define SZNAMESPACEROOT "PROTOCOLS\\Name-Space Handler\\"
  23. #define SZPROTOCOLROOT "PROTOCOLS\\Handler\\"
  24. #define SZCLASS "CLSID"
  25. // ==========================================================================================================
  26. //
  27. // THIS IS OUR CLSID
  28. //
  29. GUID CLSID_ResProtocol = {0x79eaca01, 0xbaf9, 0x11ce, {0x8c, 0x82, 0x00, 0xaa, 0x00, 0x4b, 0xa9, 0x0b}};
  30. //
  31. // ==========================================================================================================
  32. DECLARE_INFOLEVEL(UrlMk)
  33. DECLARE_INFOLEVEL(Trans)
  34. HINSTANCE g_hInst = NULL;
  35. // global variables
  36. CRefCount g_cRef(0); // global dll refcount
  37. #define DLL_NAME "b4hook.dll"
  38. STDAPI_(BOOL) TlsDllMain(HINSTANCE hDll, DWORD dwReason, LPVOID lpvReserved);
  39. #define HANDLER_HOOK SZNAMESPACEROOT"Search Hook"
  40. #define HANDLER_RES SZPROTOCOLROOT"search"
  41. #define PROTOCOL_RES_CLSID "{79eaca01-baf9-11ce-8c82-00aa004ba90b}"
  42. #define PROTOCOL_RES_CLSID_REGKEY "CLSID\\"PROTOCOL_RES_CLSID
  43. #define PROTOCOL_HOOK_DESCRIP "Search Hook: Asychronous Name-Space Handler"
  44. #define PROTOCOL_RES_DESCRIP "search: Asychronous Pluggable Protocol Handler"
  45. #define HANDLER_PROTOCOLS HANDLER_HOOK"\\Protocols"
  46. // protocols
  47. //***** PROTOCOL_RES ENTRIES *****
  48. const REGENTRY rgClassesRes[] =
  49. {
  50. STD_ENTRY(PROTOCOL_RES_CLSID_REGKEY, PROTOCOL_RES_DESCRIP),
  51. STD_ENTRY(PROTOCOL_RES_CLSID_REGKEY"\\InprocServer32", "%s"DLL_NAME),
  52. { KEYTYPE_STRING, PROTOCOL_RES_CLSID_REGKEY"\\InprocServer32", "ThreadingModel", REG_SZ, (BYTE*)"Apartment" },
  53. };
  54. const REGENTRY rgHandlerRes [] =
  55. {
  56. STD_ENTRY(HANDLER_RES , PROTOCOL_RES_DESCRIP ),
  57. { KEYTYPE_STRING, HANDLER_RES , "CLSID", REG_SZ, (BYTE*)PROTOCOL_RES_CLSID },
  58. STD_ENTRY(HANDLER_HOOK , PROTOCOL_HOOK_DESCRIP ),
  59. { KEYTYPE_STRING, HANDLER_HOOK , "CLSID", REG_SZ, (BYTE*)PROTOCOL_RES_CLSID },
  60. STD_ENTRY(HANDLER_PROTOCOLS , "" ),
  61. { KEYTYPE_STRING, HANDLER_PROTOCOLS , "http", REG_SZ, (BYTE*)"" }
  62. };
  63. const REGENTRYGROUP rgRegEntryGroups[] = {
  64. { HKEY_CLASSES_ROOT, rgClassesRes, ARRAYSIZE(rgClassesRes) },
  65. { HKEY_CLASSES_ROOT, rgHandlerRes , ARRAYSIZE(rgHandlerRes ) },
  66. { NULL, NULL, 0 } // terminator
  67. };
  68. //+---------------------------------------------------------------------------
  69. //
  70. // Function: DllAddRef
  71. //
  72. // Synopsis:
  73. //
  74. // Arguments: [void] --
  75. //
  76. // Returns:
  77. //
  78. // History: 11-07-96 JohannP (Johann Posch) Created
  79. //
  80. // Notes:
  81. //
  82. //----------------------------------------------------------------------------
  83. void DllAddRef(void)
  84. {
  85. g_cRef++;
  86. }
  87. //+---------------------------------------------------------------------------
  88. //
  89. // Function: DllRelease
  90. //
  91. // Synopsis:
  92. //
  93. // Arguments: [void] --
  94. //
  95. // Returns:
  96. //
  97. // History: 11-07-96 JohannP (Johann Posch) Created
  98. //
  99. // Notes:
  100. //
  101. //----------------------------------------------------------------------------
  102. void DllRelease(void)
  103. {
  104. UrlMkAssert((g_cRef > 0));
  105. if (g_cRef > 0)
  106. {
  107. g_cRef--;
  108. }
  109. }
  110. //+---------------------------------------------------------------------------
  111. //
  112. // Operator: new
  113. //
  114. // Synopsis:
  115. //
  116. // Arguments: [size] --
  117. //
  118. // Returns:
  119. //
  120. // History: 11-07-96 JohannP (Johann Posch) Created
  121. //
  122. // Notes: BUBUG: get and use IMalloc
  123. //
  124. //----------------------------------------------------------------------------
  125. void * _cdecl operator new(size_t size)
  126. {
  127. void * pBuffer;
  128. pBuffer = CoTaskMemAlloc(size);
  129. if (pBuffer)
  130. {
  131. memset(pBuffer,0, size);
  132. }
  133. return pBuffer;
  134. }
  135. //+---------------------------------------------------------------------------
  136. //
  137. // Operator: delete
  138. //
  139. // Synopsis:
  140. //
  141. // Arguments: [lpv] --
  142. //
  143. // Returns:
  144. //
  145. // History: 2-14-96 JohannP (Johann Posch) Created
  146. //
  147. // Notes: BUBUG: get and use IMalloc
  148. //
  149. //----------------------------------------------------------------------------
  150. void _cdecl operator delete(void *lpv)
  151. {
  152. UrlMkAssert((lpv != NULL));
  153. if (lpv == NULL)
  154. {
  155. return;
  156. }
  157. CoTaskMemFree(lpv);
  158. }
  159. //+-------------------------------------------------------------------------
  160. //
  161. // Function: DllGetClassObject
  162. //
  163. // Synopsis: Dll entry point
  164. //
  165. // Arguments: [clsid] - class id for new class
  166. // [iid] - interface required of class
  167. // [ppv] - where to put new interface
  168. //
  169. // Returns: S_OK - class object created successfully created.
  170. //
  171. // History: 11-07-96 JohannP (Johann Posch) Created
  172. //
  173. //--------------------------------------------------------------------------
  174. STDAPI DllGetClassObject(REFCLSID clsid, REFIID iid, void **ppv)
  175. {
  176. UrlMkDebugOut((DEB_URLMON, "API _IN DllGetClassObject\n"));
  177. HRESULT hr = E_FAIL;
  178. if (clsid == CLSID_ResProtocol)
  179. {
  180. /*
  181. IClassFactory *pCF = NULL;
  182. //hr = tsaMain.GetClassFactory(&pCF);
  183. if (hr == NOERROR)
  184. {
  185. UrlMkAssert((pCF != NULL));
  186. hr = pCF->QueryInterface(iid, ppv);
  187. UrlMkAssert((hr == NOERROR));
  188. pCF->Release();
  189. }
  190. */
  191. CUrlClsFact *pCF = NULL;
  192. hr = CUrlClsFact::Create(clsid, &pCF);
  193. if (hr == NOERROR)
  194. {
  195. UrlMkAssert((pCF != NULL));
  196. hr = pCF->QueryInterface(iid, ppv);
  197. pCF->Release();
  198. }
  199. }
  200. UrlMkDebugOut((DEB_URLMON, "API OUT DllGetClassObject (hr:%lx, ppv:%p)\n",hr,*ppv));
  201. return hr;
  202. }
  203. //+-------------------------------------------------------------------------
  204. //
  205. // Function: DllMain
  206. //
  207. // Synopsis:
  208. //
  209. // Arguments: [hDll] - a handle to the dll instance
  210. // [dwReason] - the reason LibMain was called
  211. // [lpvReserved] - NULL - called due to FreeLibrary
  212. // - non-NULL - called due to process exit
  213. //
  214. // Returns: TRUE on success, FALSE otherwise
  215. //
  216. // Notes:
  217. //
  218. // The officially approved DLL entrypoint name is DllMain. This
  219. // entry point will be called by the CRT Init function.
  220. //
  221. // History: 11-07-96 JohannP (Johann Posch) Created
  222. //
  223. //--------------------------------------------------------------------------
  224. BOOL WINAPI DllMain(HINSTANCE hInstance,DWORD dwReason,LPVOID lpvReserved)
  225. {
  226. BOOL fResult = TRUE;
  227. switch (dwReason)
  228. {
  229. case DLL_PROCESS_ATTACH:
  230. #if DBG==1
  231. {
  232. UrlMkInfoLevel = (DWORD) GetProfileIntA("UrlMon","UrlMk", (DEB_ERROR | DEB_WARN));
  233. TransInfoLevel = (DWORD) GetProfileIntA("UrlMon","Trans", (DEB_ERROR | DEB_WARN));
  234. }
  235. #endif //DBG==1
  236. g_hInst = hInstance;
  237. //tsaMain.InitApp(NULL);
  238. //fResult = TlsDllMain(hInstance, dwReason, lpvReserved);
  239. break;
  240. case DLL_PROCESS_DETACH:
  241. // Fall through
  242. case DLL_THREAD_ATTACH:
  243. case DLL_THREAD_DETACH:
  244. //fResult = TlsDllMain(hInstance, dwReason, lpvReserved);
  245. break;
  246. }
  247. return fResult;
  248. }
  249. //+---------------------------------------------------------------------------
  250. //
  251. // Function: DllCanUnloadNow
  252. //
  253. // Synopsis:
  254. //
  255. // Arguments: [void] --
  256. //
  257. // Returns:
  258. //
  259. // History: 11-07-96 JohannP (Johann Posch) Created
  260. //
  261. // Notes:
  262. //
  263. //----------------------------------------------------------------------------
  264. STDAPI DllCanUnloadNow(void)
  265. {
  266. return (g_cRef ? S_FALSE : S_OK);
  267. }
  268. //+---------------------------------------------------------------------------
  269. //
  270. // Function: DllRegisterServer
  271. //
  272. // Synopsis:
  273. //
  274. // Arguments: (none)
  275. //
  276. // Returns:
  277. //
  278. // History: 10-07-96 JohannP (Johann Posch) Created
  279. //
  280. // Notes:
  281. //
  282. //----------------------------------------------------------------------------
  283. STDAPI DllRegisterServer()
  284. {
  285. UrlMkDebugOut((DEB_URLMON, "API _IN DllRegisterServer\n"));
  286. HRESULT hr;
  287. hr = HrDllRegisterServer(rgRegEntryGroups, g_hInst, NULL /*pfnLoadString*/);
  288. UrlMkDebugOut((DEB_URLMON, "API OUT DllRegisterServer (hr:%lx)\n",hr));
  289. return hr;
  290. }
  291. //+---------------------------------------------------------------------------
  292. //
  293. // Function: DllUnregisterServer
  294. //
  295. // Synopsis:
  296. //
  297. // Arguments: (none)
  298. //
  299. // Returns:
  300. //
  301. // History: 11-07-96 JohannP (Johann Posch) Created
  302. //
  303. // Notes:
  304. //
  305. //----------------------------------------------------------------------------
  306. STDAPI DllUnregisterServer()
  307. {
  308. UrlMkDebugOut((DEB_URLMON, "API _IN DllUnregisterServer\n"));
  309. HRESULT hr;
  310. hr = HrDllUnregisterServer(rgRegEntryGroups, g_hInst, NULL /*pfnLoadString*/);
  311. UrlMkDebugOut((DEB_URLMON, "API OUT DllUnregisterServer (hr:%lx)\n",hr));
  312. return hr;
  313. }
  314. #if DBG==1
  315. #include <sem.hxx>
  316. CMutexSem mxs;
  317. void TransUrlSpy(int iOption, const char *pscFormat, ...)
  318. {
  319. static char szOutBuffer[2048];
  320. CLock lck(mxs);
  321. DWORD tid = GetCurrentThreadId();
  322. DWORD cbBufLen;
  323. sprintf(szOutBuffer,"%08x> ", tid );
  324. cbBufLen = strlen(szOutBuffer);
  325. va_list args;
  326. if (iOption & TransInfoLevel)
  327. {
  328. va_start(args, pscFormat);
  329. wvsprintf(szOutBuffer + cbBufLen, pscFormat, args);
  330. va_end(args);
  331. UrlSpySendEntry(szOutBuffer);
  332. }
  333. }
  334. void UrlMkUrlSpy(int iOption, const char *pscFormat, ...)
  335. {
  336. static char szOutBuffer[2048];
  337. CLock lck(mxs);
  338. DWORD tid = GetCurrentThreadId();
  339. DWORD cbBufLen;
  340. sprintf(szOutBuffer,"%08x> ", tid );
  341. cbBufLen = strlen(szOutBuffer);
  342. va_list args;
  343. if (iOption & UrlMkInfoLevel)
  344. {
  345. va_start(args, pscFormat);
  346. wvsprintf(szOutBuffer + cbBufLen, pscFormat, args);
  347. va_end(args);
  348. UrlSpySendEntry(szOutBuffer);
  349. }
  350. }
  351. void UrlSpy(int iOption, const char *pscFormat, ...)
  352. {
  353. static char szOutBuffer[2048];
  354. CLock lck(mxs);
  355. DWORD tid = GetCurrentThreadId();
  356. DWORD cbBufLen;
  357. //sprintf(szOutBuffer,"%08x.%08x> ", pid, tid );
  358. sprintf(szOutBuffer,"%08x> ", tid );
  359. cbBufLen = strlen(szOutBuffer);
  360. va_list args;
  361. //if ( (iOption & DEB_INVOKES) )
  362. {
  363. va_start(args, pscFormat);
  364. wvsprintf(szOutBuffer + cbBufLen, pscFormat, args);
  365. va_end(args);
  366. UrlSpySendEntry(szOutBuffer);
  367. }
  368. }
  369. IDebugOut *v_pDbgOut = NULL;
  370. void UrlSpySendEntry(LPSTR szOutBuffer)
  371. {
  372. if (v_pDbgOut)
  373. {
  374. v_pDbgOut->SendEntry(szOutBuffer);
  375. }
  376. {
  377. OutputDebugString(szOutBuffer);
  378. }
  379. }
  380. HRESULT RegisterDebugOut(IDebugOut *pDbgOut)
  381. {
  382. if (v_pDbgOut)
  383. {
  384. v_pDbgOut->Release();
  385. v_pDbgOut = NULL;
  386. }
  387. if (pDbgOut)
  388. {
  389. v_pDbgOut = pDbgOut;
  390. pDbgOut->AddRef();
  391. }
  392. return NOERROR;
  393. }
  394. #endif //DBG==1