DbgFunc.c
上传用户:chzmdj
上传日期:2007-01-22
资源大小:135k
文件大小:3k
源码类别:

源码/资料

开发平台:

C/C++

  1. /**************************************************************************
  2.     DbgFunc.C
  3.     Use to write debug message to a file.
  4.     Because some code in this dll is system code, it can not be debug.
  5.     so I write some message to file.  Use this message to debug dll.
  6.     (C) 1996.11 Inventec (TianJin) Co., Ltd.
  7.     Author: Gang Yan
  8.     Comments:  1. 97.5.30 is version 1.0.
  9. ***************************************************************************/
  10. #include "windows.h"
  11. #include "stdio.h"
  12. #include "stdarg.h"
  13. #include "string.h"
  14. #include "DbgFunc.h"
  15. //#define _WIN16_DEBUG_
  16. #define _WIN32_DEBUG_
  17. extern BOOL bDebugBegin;
  18. #ifdef _WIN32_DEBUG_
  19. void WriteWin32File(char* szBuff);
  20. #endif
  21. // Because thunk program can not debug.
  22. // So this function use to write debug message to file.
  23. // And use for debug version.
  24. void DbgPrintf(LPSTR fmt, ...)
  25. {
  26.     va_list marker;
  27.     char szBuf[4096];
  28. int  len;
  29. #ifdef _WIN16_DEBUG_
  30.     FILE* fp;
  31. #endif
  32.     va_start(marker, fmt);
  33.     wvsprintf(szBuf, fmt, marker);
  34.     va_end(marker);
  35. len = strlen(szBuf);
  36. szBuf[len] = 0x0d;
  37. szBuf[len + 1] = 0x0a;
  38. szBuf[len + 2] = 0;
  39.     
  40.     if (bDebugBegin)
  41.     {
  42.      // Delete old debug file.
  43.      bDebugBegin = FALSE;
  44. #ifdef _WIN16_DEBUG_
  45.      fp = fopen(_DEBUG_FILE_NAME_, "w");
  46.      fprintf(fp, "Debug Message:n");
  47.      fclose(fp);
  48. #endif
  49. #ifdef _WIN32_DEBUG_
  50. WriteWin32File("Debug Message:n");
  51. #endif
  52.     }
  53. #ifdef _WIN32_DEBUG_
  54. WriteWin32File(szBuf);
  55. #endif
  56. #ifdef _WIN16_DEBUG_
  57. fp = fopen(_DEBUG_FILE_NAME_, "a");
  58. fprintf(fp, "%s", szBuf);
  59. fclose(fp); 
  60. #endif
  61. }
  62. void DbgLPCSTR(LPSTR szPreWrite, LPSTR szBuff, int cbLen, BOOL bReturn)
  63. {
  64. char temp[4096];
  65. FILE*  fp;
  66. strncpy(temp, szBuff, cbLen);
  67. temp[cbLen] = 0x00;
  68. fp = fopen(_DEBUG_FILE_NAME_, "a");
  69. fprintf(fp, "%s%s", szPreWrite, temp);
  70. if (bReturn)
  71. fprintf(fp, "n");
  72. fclose(fp); 
  73. }
  74. #ifdef _WIN32_DEBUG_
  75. void WriteWin32File(char* szBuff)
  76. {
  77. HANDLE hDbgFile;
  78. DWORD dwWritedNum;
  79. hDbgFile = CreateFile(_DEBUG_FILE_NAME_, // pointer to name of the file 
  80.   GENERIC_WRITE, // access (read-write) mode 
  81.   FILE_SHARE_WRITE, // share mode 
  82.   NULL, // pointer to security descriptor 
  83.   CREATE_NEW, // how to create 
  84.   FILE_ATTRIBUTE_NORMAL, // file attributes 
  85.   NULL  // handle to file with attributes to copy  
  86.  );
  87. SetEndOfFile(hDbgFile);
  88. WriteFile(hDbgFile, // handle to file to write to 
  89.   szBuff, // pointer to data to write to file 
  90.   strlen(szBuff), // number of bytes to write 
  91.   &dwWritedNum, // pointer to number of bytes written 
  92.   NULL   // pointer to structure needed for overlapped I/O
  93.  );
  94. CloseHandle(hDbgFile);
  95. }
  96. void DbgPrintfToICE(LPTSTR fmt, ...)
  97. {
  98.     va_list marker;
  99.     TCHAR szBuf[1024];
  100.     va_start(marker, fmt);
  101.     wvsprintf(szBuf, fmt, marker);
  102.     va_end(marker);
  103.     OutputDebugString(szBuf);
  104.     OutputDebugString(TEXT("rn"));
  105. }
  106. #endif