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.

243 lines
4.8 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. Utils95.c
  5. Abstract:
  6. Utilities for Win95 OLEDS support in NWAPI32
  7. Author:
  8. Felix Wong [t-felixw] 23-Sept-1996
  9. --*/
  10. #include <procs.h>
  11. #include <nw95.h>
  12. #define NULL_TERMINATED 0
  13. //
  14. // define error mapping structure
  15. //
  16. typedef struct _NWSTATUS_TO_NTSTATUS {
  17. NTSTATUS NtStatus;
  18. NW_STATUS NwStatus;
  19. } NWSTATUS_TO_NTSTATUS, *LPNWSTATUS_TO_NTSTATUS;
  20. //
  21. // Error mappings for bindery errors
  22. //
  23. NWSTATUS_TO_NTSTATUS MapNtErrors[] =
  24. {
  25. {STATUS_ACCESS_DENIED, NO_OBJECT_READ_PRIVILEGE},
  26. {STATUS_NO_MORE_ENTRIES, UNKNOWN_FILE_SERVER},
  27. {STATUS_NO_MORE_ENTRIES, NO_SUCH_OBJECT},
  28. {STATUS_INVALID_PARAMETER, NO_SUCH_PROPERTY},
  29. {STATUS_UNSUCCESSFUL, INVALID_CONNECTION},
  30. {STATUS_INSUFF_SERVER_RESOURCES, SERVER_OUT_OF_MEMORY},
  31. {STATUS_NO_SUCH_DEVICE, VOLUME_DOES_NOT_EXIST},
  32. {STATUS_INVALID_HANDLE, BAD_DIRECTORY_HANDLE},
  33. {STATUS_OBJECT_PATH_NOT_FOUND, INVALID_PATH},
  34. { 0, 0 }
  35. } ;
  36. NTSTATUS
  37. MapNwToNtStatus(
  38. const NW_STATUS nwstatus
  39. )
  40. {
  41. LPNWSTATUS_TO_NTSTATUS pErrorMap ;
  42. if (nwstatus == SUCCESSFUL)
  43. return STATUS_SUCCESS;
  44. pErrorMap = MapNtErrors ;
  45. while (pErrorMap->NwStatus)
  46. {
  47. if (pErrorMap->NwStatus == nwstatus)
  48. return (pErrorMap->NtStatus) ;
  49. pErrorMap++ ;
  50. }
  51. return STATUS_UNSUCCESSFUL;
  52. }
  53. int
  54. UnicodeToAnsiString(
  55. LPWSTR pUnicode,
  56. LPSTR pAnsi,
  57. DWORD StringLength
  58. )
  59. {
  60. LPSTR pTempBuf = NULL;
  61. INT rc = 0;
  62. if( StringLength == NULL_TERMINATED ) {
  63. //
  64. // StringLength is just the
  65. // number of characters in the string
  66. //
  67. StringLength = wcslen( pUnicode );
  68. }
  69. //
  70. // WideCharToMultiByte doesn't NULL terminate if we're copying
  71. // just part of the string, so terminate here.
  72. //
  73. pUnicode[StringLength] = 0;
  74. //
  75. // Include one for the NULL
  76. //
  77. StringLength++;
  78. //
  79. // Unfortunately, WideCharToMultiByte doesn't do conversion in place,
  80. // so allocate a temporary buffer, which we can then copy:
  81. //
  82. if( pAnsi == (LPSTR)pUnicode )
  83. {
  84. #ifdef DBCS
  85. pTempBuf = (LPSTR)LocalAlloc( LPTR, StringLength * 2 );
  86. #else
  87. pTempBuf = (LPSTR)LocalAlloc( LPTR, StringLength );
  88. #endif
  89. pAnsi = pTempBuf;
  90. }
  91. if( pAnsi )
  92. {
  93. rc = WideCharToMultiByte( CP_ACP,
  94. 0,
  95. pUnicode,
  96. StringLength,
  97. pAnsi,
  98. #ifdef DBCS
  99. StringLength*2,
  100. #else
  101. StringLength,
  102. #endif
  103. NULL,
  104. NULL );
  105. }
  106. /* If pTempBuf is non-null, we must copy the resulting string
  107. * so that it looks as if we did it in place:
  108. */
  109. if( pTempBuf && ( rc > 0 ) )
  110. {
  111. pAnsi = (LPSTR)pUnicode;
  112. strcpy( pAnsi, pTempBuf );
  113. LocalFree( pTempBuf );
  114. }
  115. return rc;
  116. }
  117. LPSTR
  118. AllocateAnsiString(
  119. LPWSTR pPrinterName
  120. )
  121. {
  122. LPSTR pAnsiString;
  123. if (!pPrinterName)
  124. return NULL;
  125. pAnsiString = (LPSTR)LocalAlloc(LPTR, wcslen(pPrinterName)*sizeof(CHAR) +
  126. sizeof(CHAR));
  127. if (pAnsiString)
  128. UnicodeToAnsiString(pPrinterName, pAnsiString, NULL_TERMINATED);
  129. return pAnsiString;
  130. }
  131. void
  132. FreeAnsiString(
  133. LPSTR pAnsiString
  134. )
  135. {
  136. if (!pAnsiString)
  137. return;
  138. LocalFree(pAnsiString);
  139. return;
  140. }
  141. int
  142. AnsiToUnicodeString(
  143. LPSTR pAnsi,
  144. LPWSTR pUnicode,
  145. DWORD StringLength
  146. )
  147. {
  148. int iReturn;
  149. if( StringLength == NULL_TERMINATED )
  150. StringLength = strlen( pAnsi );
  151. iReturn = MultiByteToWideChar(CP_ACP,
  152. MB_PRECOMPOSED,
  153. pAnsi,
  154. StringLength + 1,
  155. pUnicode,
  156. StringLength + 1 );
  157. //
  158. // Ensure NULL termination.
  159. //
  160. pUnicode[StringLength] = 0;
  161. return iReturn;
  162. }
  163. LPWSTR
  164. AllocateUnicodeString(
  165. LPSTR pAnsiString
  166. )
  167. {
  168. LPWSTR pUnicodeString = NULL;
  169. if (!pAnsiString)
  170. return NULL;
  171. pUnicodeString = (LPWSTR)LocalAlloc(
  172. LPTR,
  173. strlen(pAnsiString)*sizeof(WCHAR) +sizeof(WCHAR)
  174. );
  175. if (pUnicodeString) {
  176. AnsiToUnicodeString(
  177. pAnsiString,
  178. pUnicodeString,
  179. NULL_TERMINATED
  180. );
  181. }
  182. return pUnicodeString;
  183. }
  184. void
  185. FreeUnicodeString(
  186. LPWSTR pUnicodeString
  187. )
  188. {
  189. if (!pUnicodeString)
  190. return;
  191. LocalFree(pUnicodeString);
  192. return;
  193. }