/source/WindowsAPICodePack/DirectX/DirectX/CommonUtils.cpp

https://github.com/aybe/Windows-API-Code-Pack-1.1 · C++ · 104 lines · 70 code · 21 blank · 13 comment · 8 complexity · 3928e2e9a7b1187971b836365c49557a MD5 · raw file

  1. //Copyright (c) Microsoft Corporation. All rights reserved.
  2. #include "StdAfx.h"
  3. #include "CommonUtils.h"
  4. #include "AllTypes.h"
  5. #include <msclr/lock.h>
  6. using namespace msclr;
  7. using namespace System::IO;
  8. using namespace System::Reflection;
  9. using namespace System::Globalization;
  10. using namespace Microsoft::WindowsAPICodePack::DirectX::Utilities;
  11. using namespace Microsoft::WindowsAPICodePack::DirectX::Graphics;
  12. using namespace Microsoft::WindowsAPICodePack::DirectX::Direct3D;
  13. GUID CommonUtils::GetGuid(System::Type^ type)
  14. {
  15. return Utilities::Convert::GUIDFromSystemGuid(GetManagedGuid(type));
  16. }
  17. Guid CommonUtils::GetManagedGuid(Type^ type)
  18. {
  19. lock l(guidDictionaryLock);
  20. System::Guid guid;
  21. if (!GuidsDictionary->TryGetValue(type, guid))
  22. {
  23. PropertyInfo^ prop = type->GetProperty(
  24. "Guid",
  25. BindingFlags::Static | BindingFlags::NonPublic | BindingFlags::DeclaredOnly,
  26. nullptr,
  27. System::Guid::typeid,
  28. gcnew array<Type^>(0),
  29. nullptr);
  30. if (prop == nullptr)
  31. {
  32. throw gcnew ArgumentException(
  33. String::Format(CultureInfo::CurrentCulture,
  34. "Type argument \"{0}\" is not a valid DirectX wrapper type", type->Name),
  35. "type");
  36. }
  37. guid = safe_cast<System::Guid>(prop->GetValue(nullptr, nullptr));
  38. GuidsDictionary->Add(type, guid);
  39. }
  40. return guid;
  41. }
  42. HINSTANCE CommonUtils::LoadDll(String^ libraryName)
  43. {
  44. HINSTANCE softwareModule = NULL;
  45. String^ fullPath = Path::GetFullPath(libraryName);
  46. if (!File::Exists(fullPath) && !Path::IsPathRooted(libraryName))
  47. {
  48. fullPath = Path::Combine(System::Environment::GetFolderPath(
  49. Environment::SpecialFolder::System),
  50. libraryName);
  51. }
  52. if (!File::Exists(fullPath))
  53. {
  54. throw gcnew FileNotFoundException(
  55. String::Format(CultureInfo::CurrentCulture,
  56. "Unable to find library \"{0}\".", libraryName));
  57. }
  58. pin_ptr<const wchar_t> libName = PtrToStringChars(fullPath);
  59. softwareModule = LoadLibrary(libName);
  60. if (softwareModule == NULL)
  61. {
  62. throw gcnew System::ComponentModel::Win32Exception(Marshal::GetLastWin32Error());
  63. }
  64. return softwareModule;
  65. }
  66. array<unsigned char>^ CommonUtils::ReadStream(Stream^ stream)
  67. {
  68. // REVIEW: technically, no dispose of BinaryReader is necessary,
  69. // as this code doesn't own the Stream passed in. In fact, disposing
  70. // it could hose the caller, who may expect the Stream to last
  71. // longer. On the other hand, it means that the BinaryReader will
  72. // wind up in the finalizer queue, causing GC overhead that's unnecessary.
  73. // If the contract for this method can be "disposes the passed-in
  74. // stream", it can be more efficient.
  75. //
  76. // Basically, this code is an accident waiting to happen. At
  77. // the very least, should leave a comment in to explain that the code
  78. // intentionally fails to dispose the IDisposable object (contrary to
  79. // .NET guidelines).
  80. BinaryReader^ reader = gcnew BinaryReader(stream);
  81. array<unsigned char>^ data = reader->ReadBytes( (int)stream->Length);
  82. return data;
  83. }