Source code of Windows XP (NT5)
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.

163 lines
3.8 KiB

  1. /*++
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. fmt.c
  5. Abstract:
  6. Example of a test code using FormatMessage() api.
  7. Author:
  8. Vladimir Z. Vulovic (vladimv) 06 - November - 1992
  9. Environment:
  10. User Mode - Win32
  11. Revision History:
  12. 06-Nov-1992 vladimv
  13. Created
  14. --*/
  15. #include "atclient.h"
  16. #include <stdlib.h> // exit()
  17. #include <stdio.h> // printf
  18. #include <wchar.h> // wcslen
  19. // #include <tstring.h> // wcslen
  20. #include <apperr.h> // APE_AT_USAGE
  21. #include <apperr2.h> // APE2_GEN_MONDAY + APE2_*
  22. #include <lmapibuf.h> // NetApiBufferFree
  23. BOOL
  24. DecimalStringToDword(
  25. IN CHAR * pDecimalString,
  26. OUT PDWORD pNumber
  27. )
  28. /*++
  29. This routine converts a string into a DWORD if it possibly can.
  30. The conversion is successful if string is decimal numeric and
  31. does not lead to an overflow.
  32. pDecimalString ptr to decimal string
  33. pNumber ptr to number
  34. FALSE invalid number
  35. TRUE valid number
  36. --*/
  37. {
  38. DWORD Value;
  39. DWORD OldValue;
  40. DWORD digit;
  41. if ( pDecimalString == NULL || *pDecimalString == 0) {
  42. return( FALSE);
  43. }
  44. Value = 0;
  45. while ( (digit = *pDecimalString++) != 0) {
  46. if ( digit < L'0' || digit > L'9') {
  47. return( FALSE); // not a decimal string
  48. }
  49. OldValue = Value;
  50. Value = digit - L'0' + 10 * Value;
  51. if ( Value < OldValue) {
  52. return( FALSE); // overflow
  53. }
  54. }
  55. *pNumber = Value;
  56. return( TRUE);
  57. }
  58. VOID __cdecl
  59. main(
  60. int argc,
  61. CHAR ** argv
  62. )
  63. {
  64. LPVOID lpSource;
  65. DWORD length;
  66. WCHAR buffer[ 256];
  67. BOOL success;
  68. DWORD number;
  69. WCHAR * strings[ 1];
  70. if ( argc != 2) {
  71. printf( "%s", "Usage: fmt error_number\n");
  72. exit( -1);
  73. }
  74. success = DecimalStringToDword( argv[ 1], &number);
  75. if ( success != TRUE) {
  76. printf( "Usage: fmt decimal_string_for_error_number\n");
  77. exit( -1);
  78. }
  79. lpSource = LoadLibrary( L"netmsg.dll");
  80. if ( lpSource == NULL) {
  81. printf( " LoadLibrary() fails with winError = %d\n", GetLastError());
  82. exit( -1);
  83. }
  84. length = FormatMessage(
  85. FORMAT_MESSAGE_FROM_HMODULE | // dwFlags
  86. FORMAT_MESSAGE_ARGUMENT_ARRAY,
  87. lpSource,
  88. number, // MessageId
  89. 0, // dwLanguageId
  90. buffer, // lpBuffer
  91. sizeof( buffer), // nSize
  92. NULL
  93. );
  94. if ( length == 0) {
  95. printf( " FormatMessage() fails with winError = %d\n", GetLastError());
  96. }
  97. printf( "%s", buffer);
  98. if ( number != APE2_GEN_DEFAULT_NO) {
  99. goto exit;
  100. }
  101. strings[ 0] = buffer;
  102. printf( "\nSpecial case of APE_GEN_DEFAULT_NO inserted in APE_OkToProceed\n");
  103. length = FormatMessage(
  104. FORMAT_MESSAGE_FROM_HMODULE | // dwFlags
  105. FORMAT_MESSAGE_ARGUMENT_ARRAY,
  106. lpSource,
  107. APE_OkToProceed, // MessageId
  108. 0, // dwLanguageId
  109. buffer, // lpBuffer
  110. sizeof( buffer), // nSize
  111. (va_list *)strings // lpArguments
  112. );
  113. if ( length == 0) {
  114. printf( " FormatMessage() fails with winError = %d\n", GetLastError());
  115. }
  116. printf( "%s", buffer);
  117. exit:
  118. success = FreeLibrary( lpSource);
  119. if ( success != TRUE) {
  120. printf( " FreeLibrary() fails with winError = %d\n", GetLastError());
  121. }
  122. }