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.

116 lines
2.8 KiB

  1. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2. Microsoft Windows, Copyright (C) Microsoft Corporation, 2000
  3. File: Debug.cpp
  4. Content: Implementation of debugging facilities.
  5. History: 11-15-99 dsie created
  6. ------------------------------------------------------------------------------*/
  7. #include "StdAfx.h"
  8. #include "CAPICOM.h"
  9. #include "Debug.h"
  10. #ifdef _DEBUG
  11. #define CAPICOM_DUMP_DIR_ENV_VAR "CAPICOM_DUMP_DIR"
  12. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  13. Function : DumpToFile
  14. Synopsis : Dump data to file for debug analysis.
  15. Parameter: char * szFileName - File name (just the file name without any
  16. directory path).
  17. BYTE * pbData - Pointer to data.
  18. DWORD cbData - Size of data.
  19. Remark : No action is taken if the environment variable, CAPICOM_DUMP_DIR,
  20. is not defined. If defined, the value should be the directory
  21. where the file would be created (i.e. C:\Test).
  22. ------------------------------------------------------------------------------*/
  23. void DumpToFile (char * szFileName, BYTE * pbData, DWORD cbData)
  24. {
  25. DWORD dwSize = 0;
  26. char * szPath = NULL;
  27. HANDLE hFile = NULL;
  28. //
  29. // No dump, if CAPICOM_DUMP_DIR environment is not found.
  30. //
  31. if (0 == (dwSize = ::GetEnvironmentVariableA(CAPICOM_DUMP_DIR_ENV_VAR, NULL, 0)))
  32. {
  33. goto CommonExit;
  34. }
  35. //
  36. // Allocate memory for the entire path (dir + filename).
  37. //
  38. if (!(szPath = (char *) ::CoTaskMemAlloc(dwSize + ::strlen(szFileName) + 1)))
  39. {
  40. goto CommonExit;
  41. }
  42. //
  43. // Get the dir.
  44. //
  45. if (dwSize != ::GetEnvironmentVariableA(CAPICOM_DUMP_DIR_ENV_VAR, szPath, dwSize) + 1)
  46. {
  47. goto CommonExit;
  48. }
  49. //
  50. // Append \ if not the last char.
  51. //
  52. if (szPath[dwSize - 1] != '\\')
  53. {
  54. ::strcat(szPath, "\\");
  55. }
  56. //
  57. // Form full path.
  58. //
  59. ::strcat(szPath, szFileName);
  60. //
  61. // Open the file.
  62. //
  63. if (hFile = ::CreateFileA(szPath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL))
  64. {
  65. DWORD cbWritten = 0;
  66. ::WriteFile(hFile, // handle to file
  67. pbData, // data buffer
  68. cbData, // number of bytes to write
  69. &cbWritten, // number of bytes written
  70. NULL); // overlapped buffer
  71. ATLASSERT(cbData == cbWritten);
  72. }
  73. CommonExit:
  74. //
  75. // Free resource.
  76. //
  77. if (hFile)
  78. {
  79. ::CloseHandle(hFile);
  80. }
  81. if (szPath)
  82. {
  83. ::CoTaskMemFree(szPath);
  84. }
  85. return;
  86. }
  87. #endif // _DEBUG