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.

118 lines
3.4 KiB

  1. //--------------------------------------------------------------------
  2. // utils - implementation
  3. // Copyright (C) Microsoft Corporation, 2001
  4. //
  5. // Created by: Duncan Bryce (duncanb), 11-11-2001
  6. //
  7. // Various utility functions
  8. #include "pch.h"
  9. //--------------------------------------------------------------------------------
  10. //
  11. // File Manipulation Utility Functions
  12. //
  13. //--------------------------------------------------------------------------------
  14. //---------------------------------------------------------------------------------
  15. HRESULT MyMapFile(LPWSTR wszFileName, LPBYTE *ppbFile, DWORD *pcbFile)
  16. {
  17. BYTE *pbFile = NULL;
  18. HANDLE hFile = NULL;
  19. HRESULT hr;
  20. HANDLE hFileMapping = NULL;
  21. LARGE_INTEGER liFileSize;
  22. hFile = CreateFile(wszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  23. if (INVALID_HANDLE_VALUE == hFile) {
  24. _JumpLastError(hr, error, "CreateFile");
  25. }
  26. if (!GetFileSizeEx(hFile, &liFileSize)) {
  27. _JumpLastError(hr, error, "GetFileSizeEx");
  28. }
  29. if (0 != liFileSize.HighPart) {
  30. hr = HRESULT_FROM_WIN32(ERROR_NO_SYSTEM_RESOURCES);
  31. _JumpError(hr, error, "MyMapFile: file too large");
  32. }
  33. hFileMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
  34. if (NULL == hFileMapping) {
  35. _JumpLastError(hr, error, "CreateFileMapping");
  36. }
  37. pbFile = (BYTE *)MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, liFileSize.LowPart);
  38. if (NULL == pbFile) {
  39. _JumpLastError(hr, error, "MapViewOfFile");
  40. }
  41. // Success! Assign out params:
  42. *ppbFile = pbFile;
  43. *pcbFile = liFileSize.LowPart;
  44. hr = S_OK;
  45. error:
  46. if(hFile) {
  47. CloseHandle(hFile);
  48. }
  49. if(hFileMapping) {
  50. CloseHandle(hFileMapping);
  51. }
  52. return hr;
  53. }
  54. //---------------------------------------------------------------------------------
  55. HRESULT MyUnmapFile(LPCVOID pvBaseAddress) {
  56. HRESULT hr;
  57. if (!UnmapViewOfFile(pvBaseAddress)) {
  58. _JumpLastError(hr, error, "UnmapViewOfFile");
  59. }
  60. hr = S_OK;
  61. error:
  62. return hr;
  63. }
  64. //--------------------------------------------------------------------------------
  65. //
  66. // String Manipulation Utility Functions
  67. //
  68. //--------------------------------------------------------------------------------
  69. //--------------------------------------------------------------------------
  70. void InitKeysvcUnicodeString(PKEYSVC_UNICODE_STRING pUnicodeString, LPCWSTR wszString) {
  71. pUnicodeString->Length = wcslen(wszString) * sizeof(WCHAR);
  72. pUnicodeString->MaximumLength = pUnicodeString->Length + sizeof(WCHAR);
  73. pUnicodeString->Buffer = (USHORT *)wszString;
  74. }
  75. //--------------------------------------------------------------------------
  76. LPSTR MBFromWide(LPCWSTR wsz) {
  77. LPSTR sz = NULL;
  78. DWORD cb = 0;
  79. _MyAssert(NULL != wsz);
  80. if(NULL == wsz) {
  81. SetLastError(ERROR_INVALID_PARAMETER);
  82. return(NULL);
  83. }
  84. if( (cb = WideCharToMultiByte(0, 0, wsz, -1, NULL, 0, NULL, NULL)) == 0 ||
  85. (sz = (char *) LocalAlloc(LPTR, cb)) == NULL ||
  86. (cb = WideCharToMultiByte(0, 0, wsz, -1, sz, cb, NULL, NULL)) == 0 ) {
  87. if(GetLastError() == ERROR_SUCCESS)
  88. SetLastError(ERROR_OUTOFMEMORY);
  89. return(NULL);
  90. }
  91. return(sz);
  92. }