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.

339 lines
6.2 KiB

  1. #include "nc.h"
  2. #pragma hdrstop
  3. typedef struct _STRING_TABLE {
  4. DWORD ResourceId;
  5. BOOL UseTitle;
  6. LPWSTR String;
  7. } STRING_TABLE, *PSTRING_TABLE;
  8. static STRING_TABLE StringTable[] =
  9. {
  10. { IDS_TITLE, FALSE, NULL },
  11. { IDS_ERR_TITLE, TRUE, NULL },
  12. { IDS_WRN_TITLE, TRUE, NULL },
  13. { IDS_BAD_SERVER, FALSE, NULL },
  14. { IDS_MISSING_INFO, FALSE, NULL },
  15. { IDS_MISSING_ACCNT, FALSE, NULL },
  16. { IDS_MISSING_BILLING, FALSE, NULL },
  17. { IDS_CC_AMEX, FALSE, NULL },
  18. { IDS_CC_DINERS, FALSE, NULL },
  19. { IDS_CC_DISCOVER, FALSE, NULL },
  20. { IDS_CC_MASTERCARD, FALSE, NULL },
  21. { IDS_CC_VISA, FALSE, NULL },
  22. { IDS_BAD_ISP, FALSE, NULL }
  23. };
  24. #define CountStringTable (sizeof(StringTable)/sizeof(STRING_TABLE))
  25. VOID
  26. InitializeStringTable(
  27. VOID
  28. )
  29. {
  30. DWORD i;
  31. WCHAR Buffer[512];
  32. for (i=0; i<CountStringTable; i++) {
  33. if (LoadString(
  34. MyhInstance,
  35. StringTable[i].ResourceId,
  36. Buffer,
  37. sizeof(Buffer)/sizeof(WCHAR)
  38. )) {
  39. StringTable[i].String = (LPWSTR) MemAlloc( StringSize( Buffer ) + 256 );
  40. if (!StringTable[i].String) {
  41. StringTable[i].String = L"";
  42. } else {
  43. if (StringTable[i].UseTitle) {
  44. swprintf( StringTable[i].String, Buffer, StringTable[0].String );
  45. } else {
  46. wcscpy( StringTable[i].String, Buffer );
  47. }
  48. }
  49. } else {
  50. StringTable[i].String = L"";
  51. }
  52. }
  53. }
  54. LPWSTR
  55. GetString(
  56. DWORD ResourceId
  57. )
  58. {
  59. DWORD i;
  60. for (i=0; i<CountStringTable; i++) {
  61. if (StringTable[i].ResourceId == ResourceId) {
  62. return StringTable[i].String;
  63. }
  64. }
  65. return NULL;
  66. }
  67. int
  68. PopUpMsg(
  69. HWND hwnd,
  70. DWORD ResourceId,
  71. BOOL Error,
  72. DWORD Type
  73. )
  74. {
  75. return MessageBox(
  76. hwnd,
  77. GetString( ResourceId ),
  78. GetString( Error ? IDS_ERR_TITLE : IDS_WRN_TITLE ),
  79. MB_SETFOREGROUND | (Error ? MB_ICONEXCLAMATION : MB_ICONINFORMATION) | (Type == 0 ? MB_OK : Type)
  80. );
  81. }
  82. int
  83. PopUpMsgString(
  84. HWND hwnd,
  85. LPSTR String,
  86. BOOL Error,
  87. DWORD Type
  88. )
  89. {
  90. int Rslt = 0;
  91. LPWSTR StringW = AnsiStringToUnicodeString( String );
  92. if (!StringW) {
  93. return 0;
  94. }
  95. Rslt = MessageBox(
  96. hwnd,
  97. StringW,
  98. GetString( Error ? IDS_ERR_TITLE : IDS_WRN_TITLE ),
  99. MB_SETFOREGROUND | (Error ? MB_ICONEXCLAMATION : MB_ICONINFORMATION) | (Type == 0 ? MB_OK : Type)
  100. );
  101. MemFree( StringW );
  102. return Rslt;
  103. }
  104. LPTSTR
  105. StringDup(
  106. LPTSTR String
  107. )
  108. {
  109. LPTSTR NewString;
  110. if (!String) {
  111. return NULL;
  112. }
  113. NewString = (LPTSTR) MemAlloc( (_tcslen( String ) + 1) * sizeof(TCHAR) );
  114. if (!NewString) {
  115. return NULL;
  116. }
  117. _tcscpy( NewString, String );
  118. return NewString;
  119. }
  120. LPSTR
  121. UnicodeStringToAnsiString(
  122. LPWSTR UnicodeString
  123. )
  124. {
  125. DWORD Count;
  126. LPSTR AnsiString;
  127. //
  128. // first see how big the buffer needs to be
  129. //
  130. Count = WideCharToMultiByte(
  131. CP_ACP,
  132. 0,
  133. UnicodeString,
  134. -1,
  135. NULL,
  136. 0,
  137. NULL,
  138. NULL
  139. );
  140. //
  141. // i guess the input string is empty
  142. //
  143. if (!Count) {
  144. return NULL;
  145. }
  146. //
  147. // allocate a buffer for the unicode string
  148. //
  149. Count += 1;
  150. AnsiString = (LPSTR) MemAlloc( Count );
  151. if (!AnsiString) {
  152. return NULL;
  153. }
  154. //
  155. // convert the string
  156. //
  157. Count = WideCharToMultiByte(
  158. CP_ACP,
  159. 0,
  160. UnicodeString,
  161. -1,
  162. AnsiString,
  163. Count,
  164. NULL,
  165. NULL
  166. );
  167. //
  168. // the conversion failed
  169. //
  170. if (!Count) {
  171. MemFree( AnsiString );
  172. return NULL;
  173. }
  174. return AnsiString;
  175. }
  176. LPWSTR
  177. AnsiStringToUnicodeString(
  178. LPSTR AnsiString
  179. )
  180. {
  181. DWORD Count;
  182. LPWSTR UnicodeString;
  183. //
  184. // first see how big the buffer needs to be
  185. //
  186. Count = MultiByteToWideChar(
  187. CP_ACP,
  188. MB_PRECOMPOSED,
  189. AnsiString,
  190. -1,
  191. NULL,
  192. 0
  193. );
  194. //
  195. // i guess the input string is empty
  196. //
  197. if (!Count) {
  198. return NULL;
  199. }
  200. //
  201. // allocate a buffer for the unicode string
  202. //
  203. Count += 1;
  204. UnicodeString = (LPWSTR) MemAlloc( Count * sizeof(UNICODE_NULL) );
  205. if (!UnicodeString) {
  206. return NULL;
  207. }
  208. //
  209. // convert the string
  210. //
  211. Count = MultiByteToWideChar(
  212. CP_ACP,
  213. MB_PRECOMPOSED,
  214. AnsiString,
  215. -1,
  216. UnicodeString,
  217. Count
  218. );
  219. //
  220. // the conversion failed
  221. //
  222. if (!Count) {
  223. MemFree( UnicodeString );
  224. return NULL;
  225. }
  226. return UnicodeString;
  227. }
  228. void
  229. dprintf(
  230. LPTSTR Format,
  231. ...
  232. )
  233. /*++
  234. Routine Description:
  235. Prints a debug string
  236. Arguments:
  237. format - printf() format string
  238. ... - Variable data
  239. Return Value:
  240. None.
  241. --*/
  242. {
  243. TCHAR buf[1024];
  244. DWORD len;
  245. static TCHAR AppName[16];
  246. va_list arg_ptr;
  247. SYSTEMTIME CurrentTime;
  248. if (AppName[0] == 0) {
  249. if (GetModuleFileName( NULL, buf, sizeof(buf) )) {
  250. _tsplitpath( buf, NULL, NULL, AppName, NULL );
  251. }
  252. }
  253. va_start(arg_ptr, Format);
  254. GetLocalTime( &CurrentTime );
  255. _stprintf( buf, TEXT("%x %02d:%02d:%02d.%03d %s: "),
  256. GetCurrentThreadId(),
  257. CurrentTime.wHour,
  258. CurrentTime.wMinute,
  259. CurrentTime.wSecond,
  260. CurrentTime.wMilliseconds,
  261. AppName[0] ? AppName : TEXT("")
  262. );
  263. len = _tcslen( buf );
  264. _vsntprintf(&buf[len], sizeof(buf)-len, Format, arg_ptr);
  265. len = _tcslen( buf );
  266. if (buf[len-1] != TEXT('\n')) {
  267. buf[len] = TEXT('\r');
  268. buf[len+1] = TEXT('\n');
  269. buf[len+2] = 0;
  270. }
  271. OutputDebugString( buf );
  272. }