1
0

wxdebug.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  1. //------------------------------------------------------------------------------
  2. // File: WXDebug.cpp
  3. //
  4. // Desc: DirectShow base classes - implements ActiveX system debugging
  5. // facilities.
  6. //
  7. // Copyright (c) 1992-2001 Microsoft Corporation. All rights reserved.
  8. //------------------------------------------------------------------------------
  9. //#define _WINDLL
  10. #include <streams.h>
  11. #include <stdarg.h>
  12. #include <stdio.h>
  13. #include <dvdmedia.h>
  14. #ifdef DEBUG
  15. #ifdef UNICODE
  16. #ifndef _UNICODE
  17. #define _UNICODE
  18. #endif // _UNICODE
  19. #endif // UNICODE
  20. #endif // DEBUG
  21. #include <tchar.h>
  22. #include <strsafe.h>
  23. #ifdef DEBUG
  24. static void DisplayBITMAPINFO(const BITMAPINFOHEADER* pbmi);
  25. static void DisplayRECT(LPCTSTR szLabel, const RECT& rc);
  26. // The Win32 wsprintf() function writes a maximum of 1024 characters to it's output buffer.
  27. // See the documentation for wsprintf()'s lpOut parameter for more information.
  28. const INT iDEBUGINFO = 1024; // Used to format strings
  29. /* For every module and executable we store a debugging level for each of
  30. the five categories (eg LOG_ERROR and LOG_TIMING). This makes it easy
  31. to isolate and debug individual modules without seeing everybody elses
  32. spurious debug output. The keys are stored in the registry under the
  33. HKEY_LOCAL_MACHINE\SOFTWARE\Debug\<Module Name>\<KeyName> key values
  34. NOTE these must be in the same order as their enumeration definition */
  35. const LPCTSTR pKeyNames[] = {
  36. TEXT("TIMING"), // Timing and performance measurements
  37. TEXT("TRACE"), // General step point call tracing
  38. TEXT("MEMORY"), // Memory and object allocation/destruction
  39. TEXT("LOCKING"), // Locking/unlocking of critical sections
  40. TEXT("ERROR"), // Debug error notification
  41. TEXT("CUSTOM1"),
  42. TEXT("CUSTOM2"),
  43. TEXT("CUSTOM3"),
  44. TEXT("CUSTOM4"),
  45. TEXT("CUSTOM5")
  46. };
  47. const TCHAR CAutoTrace::_szEntering[] = TEXT("->: %s");
  48. const TCHAR CAutoTrace::_szLeaving[] = TEXT("<-: %s");
  49. const INT iMAXLEVELS = NUMELMS(pKeyNames); // Maximum debug categories
  50. HINSTANCE m_hInst; // Module instance handle
  51. TCHAR m_ModuleName[iDEBUGINFO]; // Cut down module name
  52. DWORD m_Levels[iMAXLEVELS]; // Debug level per category
  53. CRITICAL_SECTION m_CSDebug; // Controls access to list
  54. DWORD m_dwNextCookie; // Next active object ID
  55. ObjectDesc *pListHead = NULL; // First active object
  56. DWORD m_dwObjectCount; // Active object count
  57. BOOL m_bInit = FALSE; // Have we been initialised
  58. HANDLE m_hOutput = INVALID_HANDLE_VALUE; // Optional output written here
  59. DWORD dwWaitTimeout = INFINITE; // Default timeout value
  60. DWORD dwTimeOffset; // Time of first DbgLog call
  61. bool g_fUseKASSERT = false; // don't create messagebox
  62. bool g_fDbgInDllEntryPoint = false;
  63. bool g_fAutoRefreshLevels = false;
  64. LPCTSTR pBaseKey = TEXT("SOFTWARE\\Microsoft\\DirectShow\\Debug");
  65. LPCTSTR pGlobalKey = TEXT("GLOBAL");
  66. static CHAR *pUnknownName = "UNKNOWN";
  67. LPCTSTR TimeoutName = TEXT("TIMEOUT");
  68. /* This sets the instance handle that the debug library uses to find
  69. the module's file name from the Win32 GetModuleFileName function */
  70. void WINAPI DbgInitialise(HINSTANCE hInst)
  71. {
  72. InitializeCriticalSection(&m_CSDebug);
  73. m_bInit = TRUE;
  74. m_hInst = hInst;
  75. DbgInitModuleName();
  76. if (GetProfileInt(m_ModuleName, TEXT("BreakOnLoad"), 0))
  77. DebugBreak();
  78. DbgInitModuleSettings(false);
  79. DbgInitGlobalSettings(true);
  80. dwTimeOffset = timeGetTime();
  81. }
  82. /* This is called to clear up any resources the debug library uses - at the
  83. moment we delete our critical section and the object list. The values we
  84. retrieve from the registry are all done during initialisation but we don't
  85. go looking for update notifications while we are running, if the values
  86. are changed then the application has to be restarted to pick them up */
  87. void WINAPI DbgTerminate()
  88. {
  89. if (m_hOutput != INVALID_HANDLE_VALUE) {
  90. EXECUTE_ASSERT(CloseHandle(m_hOutput));
  91. m_hOutput = INVALID_HANDLE_VALUE;
  92. }
  93. DeleteCriticalSection(&m_CSDebug);
  94. m_bInit = FALSE;
  95. }
  96. /* This is called by DbgInitLogLevels to read the debug settings
  97. for each logging category for this module from the registry */
  98. void WINAPI DbgInitKeyLevels(HKEY hKey, bool fTakeMax)
  99. {
  100. LONG lReturn; // Create key return value
  101. LONG lKeyPos; // Current key category
  102. DWORD dwKeySize; // Size of the key value
  103. DWORD dwKeyType; // Receives it's type
  104. DWORD dwKeyValue; // This fields value
  105. /* Try and read a value for each key position in turn */
  106. for (lKeyPos = 0;lKeyPos < iMAXLEVELS;lKeyPos++) {
  107. dwKeySize = sizeof(DWORD);
  108. lReturn = RegQueryValueEx(
  109. hKey, // Handle to an open key
  110. pKeyNames[lKeyPos], // Subkey name derivation
  111. NULL, // Reserved field
  112. &dwKeyType, // Returns the field type
  113. (LPBYTE) &dwKeyValue, // Returns the field's value
  114. &dwKeySize ); // Number of bytes transferred
  115. /* If either the key was not available or it was not a DWORD value
  116. then we ensure only the high priority debug logging is output
  117. but we try and update the field to a zero filled DWORD value */
  118. if (lReturn != ERROR_SUCCESS || dwKeyType != REG_DWORD) {
  119. dwKeyValue = 0;
  120. lReturn = RegSetValueEx(
  121. hKey, // Handle of an open key
  122. pKeyNames[lKeyPos], // Address of subkey name
  123. (DWORD) 0, // Reserved field
  124. REG_DWORD, // Type of the key field
  125. (PBYTE) &dwKeyValue, // Value for the field
  126. sizeof(DWORD)); // Size of the field buffer
  127. if (lReturn != ERROR_SUCCESS) {
  128. DbgLog((LOG_ERROR,1,TEXT("Could not create subkey %s"),pKeyNames[lKeyPos]));
  129. dwKeyValue = 0;
  130. }
  131. }
  132. if(fTakeMax)
  133. {
  134. m_Levels[lKeyPos] = max(dwKeyValue,m_Levels[lKeyPos]);
  135. }
  136. else
  137. {
  138. if((m_Levels[lKeyPos] & LOG_FORCIBLY_SET) == 0) {
  139. m_Levels[lKeyPos] = dwKeyValue;
  140. }
  141. }
  142. }
  143. /* Read the timeout value for catching hangs */
  144. dwKeySize = sizeof(DWORD);
  145. lReturn = RegQueryValueEx(
  146. hKey, // Handle to an open key
  147. TimeoutName, // Subkey name derivation
  148. NULL, // Reserved field
  149. &dwKeyType, // Returns the field type
  150. (LPBYTE) &dwWaitTimeout, // Returns the field's value
  151. &dwKeySize ); // Number of bytes transferred
  152. /* If either the key was not available or it was not a DWORD value
  153. then we ensure only the high priority debug logging is output
  154. but we try and update the field to a zero filled DWORD value */
  155. if (lReturn != ERROR_SUCCESS || dwKeyType != REG_DWORD) {
  156. dwWaitTimeout = INFINITE;
  157. lReturn = RegSetValueEx(
  158. hKey, // Handle of an open key
  159. TimeoutName, // Address of subkey name
  160. (DWORD) 0, // Reserved field
  161. REG_DWORD, // Type of the key field
  162. (PBYTE) &dwWaitTimeout, // Value for the field
  163. sizeof(DWORD)); // Size of the field buffer
  164. if (lReturn != ERROR_SUCCESS) {
  165. DbgLog((LOG_ERROR,1,TEXT("Could not create subkey %s"),pKeyNames[lKeyPos]));
  166. dwWaitTimeout = INFINITE;
  167. }
  168. }
  169. }
  170. void WINAPI DbgOutString(LPCTSTR psz)
  171. {
  172. if (m_hOutput != INVALID_HANDLE_VALUE) {
  173. UINT cb = lstrlen(psz);
  174. DWORD dw;
  175. #ifdef UNICODE
  176. CHAR szDest[2048];
  177. WideCharToMultiByte(CP_ACP, 0, psz, -1, szDest, NUMELMS(szDest), 0, 0);
  178. WriteFile (m_hOutput, szDest, cb, &dw, NULL);
  179. #else
  180. WriteFile (m_hOutput, psz, cb, &dw, NULL);
  181. #endif
  182. } else {
  183. OutputDebugString (psz);
  184. }
  185. }
  186. HRESULT DbgUniqueProcessName(LPCTSTR inName, LPTSTR outName)
  187. {
  188. HRESULT hr = S_OK;
  189. const TCHAR *pIn = inName;
  190. int dotPos = -1;
  191. //scan the input and record the last '.' position
  192. while (*pIn && (pIn - inName) < MAX_PATH)
  193. {
  194. if ( TEXT('.') == *pIn )
  195. dotPos = (int)(pIn-inName);
  196. ++pIn;
  197. }
  198. if (*pIn) //input should be zero-terminated within MAX_PATH
  199. return E_INVALIDARG;
  200. DWORD dwProcessId = GetCurrentProcessId();
  201. if (dotPos < 0)
  202. {
  203. //no extension in the input, appending process id to the input
  204. hr = StringCchPrintf(outName, MAX_PATH, TEXT("%s_%d"), inName, dwProcessId);
  205. }
  206. else
  207. {
  208. TCHAR pathAndBasename[MAX_PATH] = {0};
  209. //there's an extension - zero-terminate the path and basename first by copying
  210. hr = StringCchCopyN(pathAndBasename, MAX_PATH, inName, (size_t)dotPos);
  211. //re-combine path, basename and extension with processId appended to a basename
  212. if (SUCCEEDED(hr))
  213. hr = StringCchPrintf(outName, MAX_PATH, TEXT("%s_%d%s"), pathAndBasename, dwProcessId, inName + dotPos);
  214. }
  215. return hr;
  216. }
  217. /* Called by DbgInitGlobalSettings to setup alternate logging destinations
  218. */
  219. void WINAPI DbgInitLogTo (
  220. HKEY hKey)
  221. {
  222. LONG lReturn;
  223. DWORD dwKeyType;
  224. DWORD dwKeySize;
  225. TCHAR szFile[MAX_PATH] = {0};
  226. static const TCHAR cszKey[] = TEXT("LogToFile");
  227. dwKeySize = MAX_PATH;
  228. lReturn = RegQueryValueEx(
  229. hKey, // Handle to an open key
  230. cszKey, // Subkey name derivation
  231. NULL, // Reserved field
  232. &dwKeyType, // Returns the field type
  233. (LPBYTE) szFile, // Returns the field's value
  234. &dwKeySize); // Number of bytes transferred
  235. // create an empty key if it does not already exist
  236. //
  237. if (lReturn != ERROR_SUCCESS || dwKeyType != REG_SZ)
  238. {
  239. dwKeySize = sizeof(TCHAR);
  240. lReturn = RegSetValueEx(
  241. hKey, // Handle of an open key
  242. cszKey, // Address of subkey name
  243. (DWORD) 0, // Reserved field
  244. REG_SZ, // Type of the key field
  245. (PBYTE)szFile, // Value for the field
  246. dwKeySize); // Size of the field buffer
  247. }
  248. // if an output-to was specified. try to open it.
  249. //
  250. if (m_hOutput != INVALID_HANDLE_VALUE) {
  251. EXECUTE_ASSERT(CloseHandle (m_hOutput));
  252. m_hOutput = INVALID_HANDLE_VALUE;
  253. }
  254. if (szFile[0] != 0)
  255. {
  256. if (!lstrcmpi(szFile, TEXT("Console"))) {
  257. m_hOutput = GetStdHandle (STD_OUTPUT_HANDLE);
  258. if (m_hOutput == INVALID_HANDLE_VALUE) {
  259. AllocConsole ();
  260. m_hOutput = GetStdHandle (STD_OUTPUT_HANDLE);
  261. }
  262. SetConsoleTitle (TEXT("ActiveX Debug Output"));
  263. } else if (szFile[0] &&
  264. lstrcmpi(szFile, TEXT("Debug")) &&
  265. lstrcmpi(szFile, TEXT("Debugger")) &&
  266. lstrcmpi(szFile, TEXT("Deb")))
  267. {
  268. m_hOutput = CreateFile(szFile, GENERIC_WRITE,
  269. FILE_SHARE_READ,
  270. NULL, OPEN_ALWAYS,
  271. FILE_ATTRIBUTE_NORMAL,
  272. NULL);
  273. if (INVALID_HANDLE_VALUE == m_hOutput &&
  274. GetLastError() == ERROR_SHARING_VIOLATION)
  275. {
  276. TCHAR uniqueName[MAX_PATH] = {0};
  277. if (SUCCEEDED(DbgUniqueProcessName(szFile, uniqueName)))
  278. {
  279. m_hOutput = CreateFile(uniqueName, GENERIC_WRITE,
  280. FILE_SHARE_READ,
  281. NULL, OPEN_ALWAYS,
  282. FILE_ATTRIBUTE_NORMAL,
  283. NULL);
  284. }
  285. }
  286. if (INVALID_HANDLE_VALUE != m_hOutput)
  287. {
  288. static const TCHAR cszBar[] = TEXT("\r\n\r\n=====DbgInitialize()=====\r\n\r\n");
  289. SetFilePointer (m_hOutput, 0, NULL, FILE_END);
  290. DbgOutString (cszBar);
  291. }
  292. }
  293. }
  294. }
  295. /* This is called by DbgInitLogLevels to read the global debug settings for
  296. each logging category for this module from the registry. Normally each
  297. module has it's own values set for it's different debug categories but
  298. setting the global SOFTWARE\Debug\Global applies them to ALL modules */
  299. void WINAPI DbgInitGlobalSettings(bool fTakeMax)
  300. {
  301. LONG lReturn; // Create key return value
  302. TCHAR szInfo[iDEBUGINFO]; // Constructs key names
  303. HKEY hGlobalKey; // Global override key
  304. /* Construct the global base key name */
  305. (void)StringCchPrintf(szInfo,NUMELMS(szInfo),TEXT("%s\\%s"),pBaseKey,pGlobalKey);
  306. /* Create or open the key for this module */
  307. lReturn = RegCreateKeyEx(HKEY_LOCAL_MACHINE, // Handle of an open key
  308. szInfo, // Address of subkey name
  309. (DWORD) 0, // Reserved value
  310. NULL, // Address of class name
  311. (DWORD) 0, // Special options flags
  312. GENERIC_READ | GENERIC_WRITE, // Desired security access
  313. NULL, // Key security descriptor
  314. &hGlobalKey, // Opened handle buffer
  315. NULL); // What really happened
  316. if (lReturn != ERROR_SUCCESS) {
  317. lReturn = RegCreateKeyEx(HKEY_LOCAL_MACHINE, // Handle of an open key
  318. szInfo, // Address of subkey name
  319. (DWORD) 0, // Reserved value
  320. NULL, // Address of class name
  321. (DWORD) 0, // Special options flags
  322. GENERIC_READ, // Desired security access
  323. NULL, // Key security descriptor
  324. &hGlobalKey, // Opened handle buffer
  325. NULL); // What really happened
  326. if (lReturn != ERROR_SUCCESS) {
  327. DbgLog((LOG_ERROR,1,TEXT("Could not access GLOBAL module key")));
  328. }
  329. return;
  330. }
  331. DbgInitKeyLevels(hGlobalKey, fTakeMax);
  332. RegCloseKey(hGlobalKey);
  333. }
  334. /* This sets the debugging log levels for the different categories. We start
  335. by opening (or creating if not already available) the SOFTWARE\Debug key
  336. that all these settings live under. We then look at the global values
  337. set under SOFTWARE\Debug\Global which apply on top of the individual
  338. module settings. We then load the individual module registry settings */
  339. void WINAPI DbgInitModuleSettings(bool fTakeMax)
  340. {
  341. LONG lReturn; // Create key return value
  342. TCHAR szInfo[iDEBUGINFO]; // Constructs key names
  343. HKEY hModuleKey; // Module key handle
  344. /* Construct the base key name */
  345. (void)StringCchPrintf(szInfo,NUMELMS(szInfo),TEXT("%s\\%s"),pBaseKey,m_ModuleName);
  346. /* Create or open the key for this module */
  347. lReturn = RegCreateKeyEx(HKEY_LOCAL_MACHINE, // Handle of an open key
  348. szInfo, // Address of subkey name
  349. (DWORD) 0, // Reserved value
  350. NULL, // Address of class name
  351. (DWORD) 0, // Special options flags
  352. GENERIC_READ | GENERIC_WRITE, // Desired security access
  353. NULL, // Key security descriptor
  354. &hModuleKey, // Opened handle buffer
  355. NULL); // What really happened
  356. if (lReturn != ERROR_SUCCESS) {
  357. lReturn = RegCreateKeyEx(HKEY_LOCAL_MACHINE, // Handle of an open key
  358. szInfo, // Address of subkey name
  359. (DWORD) 0, // Reserved value
  360. NULL, // Address of class name
  361. (DWORD) 0, // Special options flags
  362. GENERIC_READ, // Desired security access
  363. NULL, // Key security descriptor
  364. &hModuleKey, // Opened handle buffer
  365. NULL); // What really happened
  366. if (lReturn != ERROR_SUCCESS) {
  367. DbgLog((LOG_ERROR,1,TEXT("Could not access module key")));
  368. }
  369. return;
  370. }
  371. DbgInitLogTo(hModuleKey);
  372. DbgInitKeyLevels(hModuleKey, fTakeMax);
  373. RegCloseKey(hModuleKey);
  374. }
  375. /* Initialise the module file name */
  376. void WINAPI DbgInitModuleName()
  377. {
  378. TCHAR FullName[iDEBUGINFO]; // Load the full path and module name
  379. LPTSTR pName; // Searches from the end for a backslash
  380. GetModuleFileName(m_hInst,FullName,iDEBUGINFO);
  381. pName = _tcsrchr(FullName,'\\');
  382. if (pName == NULL) {
  383. pName = FullName;
  384. } else {
  385. pName++;
  386. }
  387. (void)StringCchCopy(m_ModuleName,NUMELMS(m_ModuleName), pName);
  388. }
  389. struct MsgBoxMsg
  390. {
  391. HWND hwnd;
  392. LPCTSTR szTitle;
  393. LPCTSTR szMessage;
  394. DWORD dwFlags;
  395. INT iResult;
  396. };
  397. //
  398. // create a thread to call MessageBox(). calling MessageBox() on
  399. // random threads at bad times can confuse the host (eg IE).
  400. //
  401. DWORD WINAPI MsgBoxThread(
  402. __inout LPVOID lpParameter // thread data
  403. )
  404. {
  405. MsgBoxMsg *pmsg = (MsgBoxMsg *)lpParameter;
  406. pmsg->iResult = MessageBox(
  407. pmsg->hwnd,
  408. pmsg->szTitle,
  409. pmsg->szMessage,
  410. pmsg->dwFlags);
  411. return 0;
  412. }
  413. INT MessageBoxOtherThread(
  414. HWND hwnd,
  415. LPCTSTR szTitle,
  416. LPCTSTR szMessage,
  417. DWORD dwFlags)
  418. {
  419. if(g_fDbgInDllEntryPoint)
  420. {
  421. // can't wait on another thread because we have the loader
  422. // lock held in the dll entry point.
  423. // This can crash sometimes so just skip it
  424. // return MessageBox(hwnd, szTitle, szMessage, dwFlags);
  425. return IDCANCEL;
  426. }
  427. else
  428. {
  429. MsgBoxMsg msg = {hwnd, szTitle, szMessage, dwFlags, 0};
  430. DWORD dwid;
  431. HANDLE hThread = CreateThread(
  432. 0, // security
  433. 0, // stack size
  434. MsgBoxThread,
  435. (void *)&msg, // arg
  436. 0, // flags
  437. &dwid);
  438. if(hThread)
  439. {
  440. WaitForSingleObject(hThread, INFINITE);
  441. CloseHandle(hThread);
  442. return msg.iResult;
  443. }
  444. // break into debugger on failure.
  445. return IDCANCEL;
  446. }
  447. }
  448. /* Displays a message box if the condition evaluated to FALSE */
  449. void WINAPI DbgAssert(LPCTSTR pCondition,LPCTSTR pFileName,INT iLine)
  450. {
  451. if(g_fUseKASSERT)
  452. {
  453. DbgKernelAssert(pCondition, pFileName, iLine);
  454. }
  455. else
  456. {
  457. TCHAR szInfo[iDEBUGINFO];
  458. (void)StringCchPrintf(szInfo, NUMELMS(szInfo),TEXT("%s \nAt line %d of %s\nContinue? (Cancel to debug)"),
  459. pCondition, iLine, pFileName);
  460. INT MsgId = MessageBoxOtherThread(NULL,szInfo,TEXT("ASSERT Failed"),
  461. MB_SYSTEMMODAL |
  462. MB_ICONHAND |
  463. MB_YESNOCANCEL |
  464. MB_SETFOREGROUND);
  465. switch (MsgId)
  466. {
  467. case IDNO: /* Kill the application */
  468. FatalAppExit(FALSE, TEXT("Application terminated"));
  469. break;
  470. case IDCANCEL: /* Break into the debugger */
  471. DebugBreak();
  472. break;
  473. case IDYES: /* Ignore assertion continue execution */
  474. break;
  475. }
  476. }
  477. }
  478. /* Displays a message box at a break point */
  479. void WINAPI DbgBreakPoint(LPCTSTR pCondition,LPCTSTR pFileName,INT iLine)
  480. {
  481. if(g_fUseKASSERT)
  482. {
  483. DbgKernelAssert(pCondition, pFileName, iLine);
  484. }
  485. else
  486. {
  487. TCHAR szInfo[iDEBUGINFO];
  488. (void)StringCchPrintf(szInfo, NUMELMS(szInfo),TEXT("%s \nAt line %d of %s\nContinue? (Cancel to debug)"),
  489. pCondition, iLine, pFileName);
  490. INT MsgId = MessageBoxOtherThread(NULL,szInfo,TEXT("Hard coded break point"),
  491. MB_SYSTEMMODAL |
  492. MB_ICONHAND |
  493. MB_YESNOCANCEL |
  494. MB_SETFOREGROUND);
  495. switch (MsgId)
  496. {
  497. case IDNO: /* Kill the application */
  498. FatalAppExit(FALSE, TEXT("Application terminated"));
  499. break;
  500. case IDCANCEL: /* Break into the debugger */
  501. DebugBreak();
  502. break;
  503. case IDYES: /* Ignore break point continue execution */
  504. break;
  505. }
  506. }
  507. }
  508. void WINAPI DbgBreakPoint(LPCTSTR pFileName,INT iLine,__format_string LPCTSTR szFormatString,...)
  509. {
  510. // A debug break point message can have at most 2000 characters if
  511. // ANSI or UNICODE characters are being used. A debug break point message
  512. // can have between 1000 and 2000 double byte characters in it. If a
  513. // particular message needs more characters, then the value of this constant
  514. // should be increased.
  515. const DWORD MAX_BREAK_POINT_MESSAGE_SIZE = 2000;
  516. TCHAR szBreakPointMessage[MAX_BREAK_POINT_MESSAGE_SIZE];
  517. va_list va;
  518. va_start( va, szFormatString );
  519. HRESULT hr = StringCchVPrintf( szBreakPointMessage, NUMELMS(szBreakPointMessage), szFormatString, va );
  520. va_end(va);
  521. if( FAILED(hr) ) {
  522. DbgBreak( "ERROR in DbgBreakPoint(). The variable length debug message could not be displayed because StringCchVPrintf() failed." );
  523. return;
  524. }
  525. ::DbgBreakPoint( szBreakPointMessage, pFileName, iLine );
  526. }
  527. /* When we initialised the library we stored in the m_Levels array the current
  528. debug output level for this module for each of the five categories. When
  529. some debug logging is sent to us it can be sent with a combination of the
  530. categories (if it is applicable to many for example) in which case we map
  531. the type's categories into their current debug levels and see if any of
  532. them can be accepted. The function looks at each bit position in turn from
  533. the input type field and then compares it's debug level with the modules.
  534. A level of 0 means that output is always sent to the debugger. This is
  535. due to producing output if the input level is <= m_Levels.
  536. */
  537. BOOL WINAPI DbgCheckModuleLevel(DWORD Type,DWORD Level)
  538. {
  539. if(g_fAutoRefreshLevels)
  540. {
  541. // re-read the registry every second. We cannot use RegNotify() to
  542. // notice registry changes because it's not available on win9x.
  543. static DWORD g_dwLastRefresh = 0;
  544. DWORD dwTime = timeGetTime();
  545. if(dwTime - g_dwLastRefresh > 1000) {
  546. g_dwLastRefresh = dwTime;
  547. // there's a race condition: multiple threads could update the
  548. // values. plus read and write not synchronized. no harm
  549. // though.
  550. DbgInitModuleSettings(false);
  551. }
  552. }
  553. DWORD Mask = 0x01;
  554. // If no valid bits are set return FALSE
  555. if ((Type & ((1<<iMAXLEVELS)-1))) {
  556. // speed up unconditional output.
  557. if (0==Level)
  558. return(TRUE);
  559. for (LONG lKeyPos = 0;lKeyPos < iMAXLEVELS;lKeyPos++) {
  560. if (Type & Mask) {
  561. if (Level <= (m_Levels[lKeyPos] & ~LOG_FORCIBLY_SET)) {
  562. return TRUE;
  563. }
  564. }
  565. Mask <<= 1;
  566. }
  567. }
  568. return FALSE;
  569. }
  570. /* Set debug levels to a given value */
  571. void WINAPI DbgSetModuleLevel(DWORD Type, DWORD Level)
  572. {
  573. DWORD Mask = 0x01;
  574. for (LONG lKeyPos = 0;lKeyPos < iMAXLEVELS;lKeyPos++) {
  575. if (Type & Mask) {
  576. m_Levels[lKeyPos] = Level | LOG_FORCIBLY_SET;
  577. }
  578. Mask <<= 1;
  579. }
  580. }
  581. /* whether to check registry values periodically. this isn't turned
  582. automatically because of the potential performance hit. */
  583. void WINAPI DbgSetAutoRefreshLevels(bool fAuto)
  584. {
  585. g_fAutoRefreshLevels = fAuto;
  586. }
  587. #ifdef UNICODE
  588. //
  589. // warning -- this function is implemented twice for ansi applications
  590. // linking to the unicode library
  591. //
  592. void WINAPI DbgLogInfo(DWORD Type,DWORD Level,__format_string LPCSTR pFormat,...)
  593. {
  594. /* Check the current level for this type combination */
  595. BOOL bAccept = DbgCheckModuleLevel(Type,Level);
  596. if (bAccept == FALSE) {
  597. return;
  598. }
  599. TCHAR szInfo[2000];
  600. /* Format the variable length parameter list */
  601. va_list va;
  602. va_start(va, pFormat);
  603. (void)StringCchPrintf(szInfo, NUMELMS(szInfo),
  604. TEXT("%s(tid %x) %8d : "),
  605. m_ModuleName,
  606. GetCurrentThreadId(), timeGetTime() - dwTimeOffset);
  607. CHAR szInfoA[2000];
  608. WideCharToMultiByte(CP_ACP, 0, szInfo, -1, szInfoA, NUMELMS(szInfoA), 0, 0);
  609. (void)StringCchVPrintfA(szInfoA + lstrlenA(szInfoA), NUMELMS(szInfoA) - lstrlenA(szInfoA), pFormat, va);
  610. (void)StringCchCatA(szInfoA, NUMELMS(szInfoA), "\r\n");
  611. WCHAR wszOutString[2000];
  612. MultiByteToWideChar(CP_ACP, 0, szInfoA, -1, wszOutString, NUMELMS(wszOutString));
  613. DbgOutString(wszOutString);
  614. va_end(va);
  615. }
  616. void WINAPI DbgAssert(LPCSTR pCondition,LPCSTR pFileName,INT iLine)
  617. {
  618. if(g_fUseKASSERT)
  619. {
  620. DbgKernelAssert(pCondition, pFileName, iLine);
  621. }
  622. else
  623. {
  624. TCHAR szInfo[iDEBUGINFO];
  625. (void)StringCchPrintf(szInfo, NUMELMS(szInfo), TEXT("%hs \nAt line %d of %hs\nContinue? (Cancel to debug)"),
  626. pCondition, iLine, pFileName);
  627. INT MsgId = MessageBoxOtherThread(NULL,szInfo,TEXT("ASSERT Failed"),
  628. MB_SYSTEMMODAL |
  629. MB_ICONHAND |
  630. MB_YESNOCANCEL |
  631. MB_SETFOREGROUND);
  632. switch (MsgId)
  633. {
  634. case IDNO: /* Kill the application */
  635. FatalAppExit(FALSE, TEXT("Application terminated"));
  636. break;
  637. case IDCANCEL: /* Break into the debugger */
  638. DebugBreak();
  639. break;
  640. case IDYES: /* Ignore assertion continue execution */
  641. break;
  642. }
  643. }
  644. }
  645. /* Displays a message box at a break point */
  646. void WINAPI DbgBreakPoint(LPCSTR pCondition,LPCSTR pFileName,INT iLine)
  647. {
  648. if(g_fUseKASSERT)
  649. {
  650. DbgKernelAssert(pCondition, pFileName, iLine);
  651. }
  652. else
  653. {
  654. TCHAR szInfo[iDEBUGINFO];
  655. (void)StringCchPrintf(szInfo, NUMELMS(szInfo),TEXT("%hs \nAt line %d of %hs\nContinue? (Cancel to debug)"),
  656. pCondition, iLine, pFileName);
  657. INT MsgId = MessageBoxOtherThread(NULL,szInfo,TEXT("Hard coded break point"),
  658. MB_SYSTEMMODAL |
  659. MB_ICONHAND |
  660. MB_YESNOCANCEL |
  661. MB_SETFOREGROUND);
  662. switch (MsgId)
  663. {
  664. case IDNO: /* Kill the application */
  665. FatalAppExit(FALSE, TEXT("Application terminated"));
  666. break;
  667. case IDCANCEL: /* Break into the debugger */
  668. DebugBreak();
  669. break;
  670. case IDYES: /* Ignore break point continue execution */
  671. break;
  672. }
  673. }
  674. }
  675. void WINAPI DbgKernelAssert(LPCSTR pCondition,LPCSTR pFileName,INT iLine)
  676. {
  677. DbgLog((LOG_ERROR,0,TEXT("Assertion FAILED (%hs) at line %d in file %hs"),
  678. pCondition, iLine, pFileName));
  679. DebugBreak();
  680. }
  681. #endif
  682. /* Print a formatted string to the debugger prefixed with this module's name
  683. Because the COMBASE classes are linked statically every module loaded will
  684. have their own copy of this code. It therefore helps if the module name is
  685. included on the output so that the offending code can be easily found */
  686. //
  687. // warning -- this function is implemented twice for ansi applications
  688. // linking to the unicode library
  689. //
  690. void WINAPI DbgLogInfo(DWORD Type,DWORD Level,LPCTSTR pFormat,...)
  691. {
  692. /* Check the current level for this type combination */
  693. BOOL bAccept = DbgCheckModuleLevel(Type,Level);
  694. if (bAccept == FALSE) {
  695. return;
  696. }
  697. TCHAR szInfo[2000];
  698. /* Format the variable length parameter list */
  699. va_list va;
  700. va_start(va, pFormat);
  701. (void)StringCchPrintf(szInfo, NUMELMS(szInfo),
  702. TEXT("%s(tid %x) %8d : "),
  703. m_ModuleName,
  704. GetCurrentThreadId(), timeGetTime() - dwTimeOffset);
  705. (void)StringCchVPrintf(szInfo + lstrlen(szInfo), NUMELMS(szInfo) - lstrlen(szInfo), pFormat, va);
  706. (void)StringCchCat(szInfo, NUMELMS(szInfo), TEXT("\r\n"));
  707. DbgOutString(szInfo);
  708. va_end(va);
  709. }
  710. /* If we are executing as a pure kernel filter we cannot display message
  711. boxes to the user, this provides an alternative which puts the error
  712. condition on the debugger output with a suitable eye catching message */
  713. void WINAPI DbgKernelAssert(LPCTSTR pCondition,LPCTSTR pFileName,INT iLine)
  714. {
  715. DbgLog((LOG_ERROR,0,TEXT("Assertion FAILED (%s) at line %d in file %s"),
  716. pCondition, iLine, pFileName));
  717. DebugBreak();
  718. }
  719. /* Each time we create an object derived from CBaseObject the constructor will
  720. call us to register the creation of the new object. We are passed a string
  721. description which we store away. We return a cookie that the constructor
  722. uses to identify the object when it is destroyed later on. We update the
  723. total number of active objects in the DLL mainly for debugging purposes */
  724. DWORD WINAPI DbgRegisterObjectCreation(LPCSTR szObjectName,
  725. LPCWSTR wszObjectName)
  726. {
  727. /* If this fires you have a mixed DEBUG/RETAIL build */
  728. ASSERT(!!szObjectName ^ !!wszObjectName);
  729. /* Create a place holder for this object description */
  730. ObjectDesc *pObject = new ObjectDesc;
  731. ASSERT(pObject);
  732. /* It is valid to pass a NULL object name */
  733. if (pObject == NULL) {
  734. return FALSE;
  735. }
  736. /* Check we have been initialised - we may not be initialised when we are
  737. being pulled in from an executable which has globally defined objects
  738. as they are created by the C++ run time before WinMain is called */
  739. if (m_bInit == FALSE) {
  740. DbgInitialise(GetModuleHandle(NULL));
  741. }
  742. /* Grab the list critical section */
  743. EnterCriticalSection(&m_CSDebug);
  744. /* If no name then default to UNKNOWN */
  745. if (!szObjectName && !wszObjectName) {
  746. szObjectName = pUnknownName;
  747. }
  748. /* Put the new description at the _head of the list */
  749. pObject->m_szName = szObjectName;
  750. pObject->m_wszName = wszObjectName;
  751. pObject->m_dwCookie = ++m_dwNextCookie;
  752. pObject->m_pNext = pListHead;
  753. pListHead = pObject;
  754. m_dwObjectCount++;
  755. DWORD ObjectCookie = pObject->m_dwCookie;
  756. ASSERT(ObjectCookie);
  757. if(wszObjectName) {
  758. DbgLog((LOG_MEMORY,2,TEXT("Object created %d (%ls) %d Active"),
  759. pObject->m_dwCookie, wszObjectName, m_dwObjectCount));
  760. } else {
  761. DbgLog((LOG_MEMORY,2,TEXT("Object created %d (%hs) %d Active"),
  762. pObject->m_dwCookie, szObjectName, m_dwObjectCount));
  763. }
  764. LeaveCriticalSection(&m_CSDebug);
  765. return ObjectCookie;
  766. }
  767. /* This is called by the CBaseObject destructor when an object is about to be
  768. destroyed, we are passed the cookie we returned during construction that
  769. identifies this object. We scan the object list for a matching cookie and
  770. remove the object if successful. We also update the active object count */
  771. BOOL WINAPI DbgRegisterObjectDestruction(DWORD dwCookie)
  772. {
  773. /* Grab the list critical section */
  774. EnterCriticalSection(&m_CSDebug);
  775. ObjectDesc *pObject = pListHead;
  776. ObjectDesc *pPrevious = NULL;
  777. /* Scan the object list looking for a cookie match */
  778. while (pObject) {
  779. if (pObject->m_dwCookie == dwCookie) {
  780. break;
  781. }
  782. pPrevious = pObject;
  783. pObject = pObject->m_pNext;
  784. }
  785. if (pObject == NULL) {
  786. DbgBreak("Apparently destroying a bogus object");
  787. LeaveCriticalSection(&m_CSDebug);
  788. return FALSE;
  789. }
  790. /* Is the object at the _head of the list */
  791. if (pPrevious == NULL) {
  792. pListHead = pObject->m_pNext;
  793. } else {
  794. pPrevious->m_pNext = pObject->m_pNext;
  795. }
  796. /* Delete the object and update the housekeeping information */
  797. m_dwObjectCount--;
  798. if(pObject->m_wszName) {
  799. DbgLog((LOG_MEMORY,2,TEXT("Object destroyed %d (%ls) %d Active"),
  800. pObject->m_dwCookie, pObject->m_wszName, m_dwObjectCount));
  801. } else {
  802. DbgLog((LOG_MEMORY,2,TEXT("Object destroyed %d (%hs) %d Active"),
  803. pObject->m_dwCookie, pObject->m_szName, m_dwObjectCount));
  804. }
  805. delete pObject;
  806. LeaveCriticalSection(&m_CSDebug);
  807. return TRUE;
  808. }
  809. /* This runs through the active object list displaying their details */
  810. void WINAPI DbgDumpObjectRegister()
  811. {
  812. TCHAR szInfo[iDEBUGINFO];
  813. /* Grab the list critical section */
  814. EnterCriticalSection(&m_CSDebug);
  815. ObjectDesc *pObject = pListHead;
  816. /* Scan the object list displaying the name and cookie */
  817. DbgLog((LOG_MEMORY,2,TEXT("")));
  818. DbgLog((LOG_MEMORY,2,TEXT(" ID Object Description")));
  819. DbgLog((LOG_MEMORY,2,TEXT("")));
  820. while (pObject) {
  821. if(pObject->m_wszName) {
  822. (void)StringCchPrintf(szInfo,NUMELMS(szInfo),TEXT("%5d (%p) %30ls"),pObject->m_dwCookie, &pObject, pObject->m_wszName);
  823. } else {
  824. (void)StringCchPrintf(szInfo,NUMELMS(szInfo),TEXT("%5d (%p) %30hs"),pObject->m_dwCookie, &pObject, pObject->m_szName);
  825. }
  826. DbgLog((LOG_MEMORY,2,szInfo));
  827. pObject = pObject->m_pNext;
  828. }
  829. (void)StringCchPrintf(szInfo,NUMELMS(szInfo),TEXT("Total object count %5d"),m_dwObjectCount);
  830. DbgLog((LOG_MEMORY,2,TEXT("")));
  831. DbgLog((LOG_MEMORY,1,szInfo));
  832. LeaveCriticalSection(&m_CSDebug);
  833. }
  834. /* Debug infinite wait stuff */
  835. DWORD WINAPI DbgWaitForSingleObject(HANDLE h)
  836. {
  837. DWORD dwWaitResult;
  838. do {
  839. dwWaitResult = WaitForSingleObject(h, dwWaitTimeout);
  840. ASSERT(dwWaitResult == WAIT_OBJECT_0);
  841. } while (dwWaitResult == WAIT_TIMEOUT);
  842. return dwWaitResult;
  843. }
  844. DWORD WINAPI DbgWaitForMultipleObjects(DWORD nCount,
  845. __in_ecount(nCount) CONST HANDLE *lpHandles,
  846. BOOL bWaitAll)
  847. {
  848. DWORD dwWaitResult;
  849. do {
  850. dwWaitResult = WaitForMultipleObjects(nCount,
  851. lpHandles,
  852. bWaitAll,
  853. dwWaitTimeout);
  854. ASSERT((DWORD)(dwWaitResult - WAIT_OBJECT_0) < MAXIMUM_WAIT_OBJECTS);
  855. } while (dwWaitResult == WAIT_TIMEOUT);
  856. return dwWaitResult;
  857. }
  858. void WINAPI DbgSetWaitTimeout(DWORD dwTimeout)
  859. {
  860. dwWaitTimeout = dwTimeout;
  861. }
  862. #endif /* DEBUG */
  863. #ifdef _OBJBASE_H_
  864. /* Stuff for printing out our GUID names */
  865. GUID_STRING_ENTRY g_GuidNames[] = {
  866. #define OUR_GUID_ENTRY(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
  867. { #name, { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } } },
  868. #include <uuids.h>
  869. };
  870. CGuidNameList GuidNames;
  871. int g_cGuidNames = sizeof(g_GuidNames) / sizeof(g_GuidNames[0]);
  872. char *CGuidNameList::operator [] (const GUID &guid)
  873. {
  874. for (int i = 0; i < g_cGuidNames; i++) {
  875. if (g_GuidNames[i].guid == guid) {
  876. return g_GuidNames[i].szName;
  877. }
  878. }
  879. if (guid == GUID_NULL) {
  880. return "GUID_NULL";
  881. }
  882. // !!! add something to print FOURCC guids?
  883. // shouldn't this print the hex CLSID?
  884. return "Unknown GUID Name";
  885. }
  886. #endif /* _OBJBASE_H_ */
  887. /* CDisp class - display our data types */
  888. // clashes with REFERENCE_TIME
  889. CDisp::CDisp(LONGLONG ll, int Format)
  890. {
  891. // note: this could be combined with CDisp(LONGLONG) by
  892. // introducing a default format of CDISP_REFTIME
  893. LARGE_INTEGER li;
  894. li.QuadPart = ll;
  895. switch (Format) {
  896. case CDISP_DEC:
  897. {
  898. TCHAR temp[20];
  899. int pos=20;
  900. temp[--pos] = 0;
  901. int digit;
  902. // always output at least one digit
  903. do {
  904. // Get the rightmost digit - we only need the low word
  905. digit = li.LowPart % 10;
  906. li.QuadPart /= 10;
  907. temp[--pos] = (TCHAR) digit+L'0';
  908. } while (li.QuadPart);
  909. (void)StringCchCopy(m_String, NUMELMS(m_String), temp+pos);
  910. break;
  911. }
  912. case CDISP_HEX:
  913. default:
  914. (void)StringCchPrintf(m_String, NUMELMS(m_String), TEXT("0x%X%8.8X"), li.HighPart, li.LowPart);
  915. }
  916. };
  917. CDisp::CDisp(REFCLSID clsid)
  918. {
  919. #ifdef UNICODE
  920. (void)StringFromGUID2(clsid, m_String, NUMELMS(m_String));
  921. #else
  922. WCHAR wszTemp[50];
  923. (void)StringFromGUID2(clsid, wszTemp, NUMELMS(wszTemp));
  924. (void)StringCchPrintf(m_String, NUMELMS(m_String), TEXT("%S"), wszTemp);
  925. #endif
  926. };
  927. #ifdef __STREAMS__
  928. /* Display stuff */
  929. CDisp::CDisp(CRefTime llTime)
  930. {
  931. LONGLONG llDiv;
  932. if (llTime < 0) {
  933. llTime = -llTime;
  934. (void)StringCchCopy(m_String, NUMELMS(m_String), TEXT("-"));
  935. }
  936. llDiv = (LONGLONG)24 * 3600 * 10000000;
  937. if (llTime >= llDiv) {
  938. (void)StringCchPrintf(m_String + lstrlen(m_String), NUMELMS(m_String) - lstrlen(m_String), TEXT("%d days "), (LONG)(llTime / llDiv));
  939. llTime = llTime % llDiv;
  940. }
  941. llDiv = (LONGLONG)3600 * 10000000;
  942. if (llTime >= llDiv) {
  943. (void)StringCchPrintf(m_String + lstrlen(m_String), NUMELMS(m_String) - lstrlen(m_String), TEXT("%d hrs "), (LONG)(llTime / llDiv));
  944. llTime = llTime % llDiv;
  945. }
  946. llDiv = (LONGLONG)60 * 10000000;
  947. if (llTime >= llDiv) {
  948. (void)StringCchPrintf(m_String + lstrlen(m_String), NUMELMS(m_String) - lstrlen(m_String), TEXT("%d mins "), (LONG)(llTime / llDiv));
  949. llTime = llTime % llDiv;
  950. }
  951. (void)StringCchPrintf(m_String + lstrlen(m_String), NUMELMS(m_String) - lstrlen(m_String), TEXT("%d.%3.3d sec"),
  952. (LONG)llTime / 10000000,
  953. (LONG)((llTime % 10000000) / 10000));
  954. };
  955. #endif // __STREAMS__
  956. /* Display pin */
  957. CDisp::CDisp(IPin *pPin)
  958. {
  959. PIN_INFO pi;
  960. TCHAR str[MAX_PIN_NAME];
  961. CLSID clsid;
  962. if (pPin) {
  963. pPin->QueryPinInfo(&pi);
  964. pi.pFilter->GetClassID(&clsid);
  965. QueryPinInfoReleaseFilter(pi);
  966. #ifndef UNICODE
  967. WideCharToMultiByte(GetACP(), 0, pi.achName, lstrlenW(pi.achName) + 1,
  968. str, MAX_PIN_NAME, NULL, NULL);
  969. #else
  970. (void)StringCchCopy(str, NUMELMS(str), pi.achName);
  971. #endif
  972. } else {
  973. (void)StringCchCopy(str, NUMELMS(str), TEXT("NULL IPin"));
  974. }
  975. m_pString = (PTCHAR) new TCHAR[lstrlen(str)+64];
  976. if (!m_pString) {
  977. return;
  978. }
  979. (void)StringCchPrintf(m_pString, lstrlen(str) + 64, TEXT("%hs(%s)"), GuidNames[clsid], str);
  980. }
  981. /* Display filter or pin */
  982. CDisp::CDisp(IUnknown *pUnk)
  983. {
  984. IBaseFilter *pf;
  985. HRESULT hr = pUnk->QueryInterface(IID_IBaseFilter, (void **)&pf);
  986. if(SUCCEEDED(hr))
  987. {
  988. FILTER_INFO fi;
  989. hr = pf->QueryFilterInfo(&fi);
  990. if(SUCCEEDED(hr))
  991. {
  992. QueryFilterInfoReleaseGraph(fi);
  993. size_t len = lstrlenW(fi.achName) + 1;
  994. m_pString = new TCHAR[len];
  995. if(m_pString)
  996. {
  997. #ifdef UNICODE
  998. (void)StringCchCopy(m_pString, len, fi.achName);
  999. #else
  1000. (void)StringCchPrintf(m_pString, len, "%S", fi.achName);
  1001. #endif
  1002. }
  1003. }
  1004. pf->Release();
  1005. return;
  1006. }
  1007. IPin *pp;
  1008. hr = pUnk->QueryInterface(IID_IPin, (void **)&pp);
  1009. if(SUCCEEDED(hr))
  1010. {
  1011. CDisp::CDisp(pp);
  1012. pp->Release();
  1013. return;
  1014. }
  1015. }
  1016. CDisp::~CDisp()
  1017. {
  1018. }
  1019. CDispBasic::~CDispBasic()
  1020. {
  1021. if (m_pString != m_String) {
  1022. delete [] m_pString;
  1023. }
  1024. }
  1025. CDisp::CDisp(double d)
  1026. {
  1027. (void)StringCchPrintf(m_String, NUMELMS(m_String), TEXT("%d.%03d"), (int) d, (int) ((d - (int) d) * 1000));
  1028. }
  1029. /* If built for debug this will display the media type details. We convert the
  1030. major and subtypes into strings and also ask the base classes for a string
  1031. description of the subtype, so MEDIASUBTYPE_RGB565 becomes RGB 565 16 bit
  1032. We also display the fields in the BITMAPINFOHEADER structure, this should
  1033. succeed as we do not accept input types unless the format is big enough */
  1034. #ifdef DEBUG
  1035. void WINAPI DisplayType(LPCTSTR label, const AM_MEDIA_TYPE *pmtIn)
  1036. {
  1037. /* Dump the GUID types and a short description */
  1038. DbgLog((LOG_TRACE,5,TEXT("")));
  1039. DbgLog((LOG_TRACE,2,TEXT("%s M type %hs S type %hs"), label,
  1040. GuidNames[pmtIn->majortype],
  1041. GuidNames[pmtIn->subtype]));
  1042. DbgLog((LOG_TRACE,5,TEXT("Subtype description %s"),GetSubtypeName(&pmtIn->subtype)));
  1043. /* Dump the generic media types */
  1044. if (pmtIn->bTemporalCompression) {
  1045. DbgLog((LOG_TRACE,5,TEXT("Temporally compressed")));
  1046. } else {
  1047. DbgLog((LOG_TRACE,5,TEXT("Not temporally compressed")));
  1048. }
  1049. if (pmtIn->bFixedSizeSamples) {
  1050. DbgLog((LOG_TRACE,5,TEXT("Sample size %d"),pmtIn->lSampleSize));
  1051. } else {
  1052. DbgLog((LOG_TRACE,5,TEXT("Variable size samples")));
  1053. }
  1054. if (pmtIn->formattype == FORMAT_VideoInfo) {
  1055. VIDEOINFOHEADER *pVideoInfo = (VIDEOINFOHEADER *)pmtIn->pbFormat;
  1056. DisplayRECT(TEXT("Source rectangle"),pVideoInfo->rcSource);
  1057. DisplayRECT(TEXT("Target rectangle"),pVideoInfo->rcTarget);
  1058. DisplayBITMAPINFO(HEADER(pmtIn->pbFormat));
  1059. } if (pmtIn->formattype == FORMAT_VideoInfo2) {
  1060. VIDEOINFOHEADER2 *pVideoInfo2 = (VIDEOINFOHEADER2 *)pmtIn->pbFormat;
  1061. DisplayRECT(TEXT("Source rectangle"),pVideoInfo2->rcSource);
  1062. DisplayRECT(TEXT("Target rectangle"),pVideoInfo2->rcTarget);
  1063. DbgLog((LOG_TRACE, 5, TEXT("Aspect Ratio: %d:%d"),
  1064. pVideoInfo2->dwPictAspectRatioX,
  1065. pVideoInfo2->dwPictAspectRatioY));
  1066. DisplayBITMAPINFO(&pVideoInfo2->bmiHeader);
  1067. } else if (pmtIn->majortype == MEDIATYPE_Audio) {
  1068. DbgLog((LOG_TRACE,2,TEXT(" Format type %hs"),
  1069. GuidNames[pmtIn->formattype]));
  1070. DbgLog((LOG_TRACE,2,TEXT(" Subtype %hs"),
  1071. GuidNames[pmtIn->subtype]));
  1072. if ((pmtIn->subtype != MEDIASUBTYPE_MPEG1Packet)
  1073. && (pmtIn->cbFormat >= sizeof(PCMWAVEFORMAT)))
  1074. {
  1075. /* Dump the contents of the WAVEFORMATEX type-specific format structure */
  1076. WAVEFORMATEX *pwfx = (WAVEFORMATEX *) pmtIn->pbFormat;
  1077. DbgLog((LOG_TRACE,2,TEXT("wFormatTag %u"), pwfx->wFormatTag));
  1078. DbgLog((LOG_TRACE,2,TEXT("nChannels %u"), pwfx->nChannels));
  1079. DbgLog((LOG_TRACE,2,TEXT("nSamplesPerSec %lu"), pwfx->nSamplesPerSec));
  1080. DbgLog((LOG_TRACE,2,TEXT("nAvgBytesPerSec %lu"), pwfx->nAvgBytesPerSec));
  1081. DbgLog((LOG_TRACE,2,TEXT("nBlockAlign %u"), pwfx->nBlockAlign));
  1082. DbgLog((LOG_TRACE,2,TEXT("wBitsPerSample %u"), pwfx->wBitsPerSample));
  1083. /* PCM uses a WAVEFORMAT and does not have the extra size field */
  1084. if (pmtIn->cbFormat >= sizeof(WAVEFORMATEX)) {
  1085. DbgLog((LOG_TRACE,2,TEXT("cbSize %u"), pwfx->cbSize));
  1086. }
  1087. } else {
  1088. }
  1089. } else {
  1090. DbgLog((LOG_TRACE,2,TEXT(" Format type %hs"),
  1091. GuidNames[pmtIn->formattype]));
  1092. }
  1093. }
  1094. void DisplayBITMAPINFO(const BITMAPINFOHEADER* pbmi)
  1095. {
  1096. DbgLog((LOG_TRACE,5,TEXT("Size of BITMAPINFO structure %d"),pbmi->biSize));
  1097. if (pbmi->biCompression < 256) {
  1098. DbgLog((LOG_TRACE,2,TEXT("%dx%dx%d bit (%d)"),
  1099. pbmi->biWidth, pbmi->biHeight,
  1100. pbmi->biBitCount, pbmi->biCompression));
  1101. } else {
  1102. DbgLog((LOG_TRACE,2,TEXT("%dx%dx%d bit '%4.4hs'"),
  1103. pbmi->biWidth, pbmi->biHeight,
  1104. pbmi->biBitCount, &pbmi->biCompression));
  1105. }
  1106. DbgLog((LOG_TRACE,2,TEXT("Image size %d"),pbmi->biSizeImage));
  1107. DbgLog((LOG_TRACE,5,TEXT("Planes %d"),pbmi->biPlanes));
  1108. DbgLog((LOG_TRACE,5,TEXT("X Pels per metre %d"),pbmi->biXPelsPerMeter));
  1109. DbgLog((LOG_TRACE,5,TEXT("Y Pels per metre %d"),pbmi->biYPelsPerMeter));
  1110. DbgLog((LOG_TRACE,5,TEXT("Colours used %d"),pbmi->biClrUsed));
  1111. }
  1112. void DisplayRECT(LPCTSTR szLabel, const RECT& rc)
  1113. {
  1114. DbgLog((LOG_TRACE,5,TEXT("%s (Left %d Top %d Right %d Bottom %d)"),
  1115. szLabel,
  1116. rc.left,
  1117. rc.top,
  1118. rc.right,
  1119. rc.bottom));
  1120. }
  1121. void WINAPI DumpGraph(IFilterGraph *pGraph, DWORD dwLevel)
  1122. {
  1123. if( !pGraph )
  1124. {
  1125. return;
  1126. }
  1127. IEnumFilters *pFilters;
  1128. DbgLog((LOG_TRACE,dwLevel,TEXT("DumpGraph [%x]"), pGraph));
  1129. if (FAILED(pGraph->EnumFilters(&pFilters))) {
  1130. DbgLog((LOG_TRACE,dwLevel,TEXT("EnumFilters failed!")));
  1131. }
  1132. IBaseFilter *pFilter;
  1133. ULONG n;
  1134. while (pFilters->Next(1, &pFilter, &n) == S_OK) {
  1135. FILTER_INFO info;
  1136. if (FAILED(pFilter->QueryFilterInfo(&info))) {
  1137. DbgLog((LOG_TRACE,dwLevel,TEXT(" Filter [%p] -- failed QueryFilterInfo"), pFilter));
  1138. } else {
  1139. QueryFilterInfoReleaseGraph(info);
  1140. // !!! should QueryVendorInfo here!
  1141. DbgLog((LOG_TRACE,dwLevel,TEXT(" Filter [%p] '%ls'"), pFilter, info.achName));
  1142. IEnumPins *pins;
  1143. if (FAILED(pFilter->EnumPins(&pins))) {
  1144. DbgLog((LOG_TRACE,dwLevel,TEXT("EnumPins failed!")));
  1145. } else {
  1146. IPin *pPin;
  1147. while (pins->Next(1, &pPin, &n) == S_OK) {
  1148. PIN_INFO pinInfo;
  1149. if (FAILED(pPin->QueryPinInfo(&pinInfo))) {
  1150. DbgLog((LOG_TRACE,dwLevel,TEXT(" Pin [%x] -- failed QueryPinInfo"), pPin));
  1151. } else {
  1152. QueryPinInfoReleaseFilter(pinInfo);
  1153. IPin *pPinConnected = NULL;
  1154. HRESULT hr = pPin->ConnectedTo(&pPinConnected);
  1155. if (pPinConnected) {
  1156. DbgLog((LOG_TRACE,dwLevel,TEXT(" Pin [%p] '%ls' [%sput]")
  1157. TEXT(" Connected to pin [%p]"),
  1158. pPin, pinInfo.achName,
  1159. pinInfo.dir == PINDIR_INPUT ? TEXT("In") : TEXT("Out"),
  1160. pPinConnected));
  1161. pPinConnected->Release();
  1162. // perhaps we should really dump the type both ways as a sanity
  1163. // check?
  1164. if (pinInfo.dir == PINDIR_OUTPUT) {
  1165. AM_MEDIA_TYPE mt;
  1166. hr = pPin->ConnectionMediaType(&mt);
  1167. if (SUCCEEDED(hr)) {
  1168. DisplayType(TEXT("Connection type"), &mt);
  1169. FreeMediaType(mt);
  1170. }
  1171. }
  1172. } else {
  1173. DbgLog((LOG_TRACE,dwLevel,
  1174. TEXT(" Pin [%x] '%ls' [%sput]"),
  1175. pPin, pinInfo.achName,
  1176. pinInfo.dir == PINDIR_INPUT ? TEXT("In") : TEXT("Out")));
  1177. }
  1178. }
  1179. pPin->Release();
  1180. }
  1181. pins->Release();
  1182. }
  1183. }
  1184. pFilter->Release();
  1185. }
  1186. pFilters->Release();
  1187. }
  1188. #endif