Win32ApiSharedEmulation.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "il2cpp-config.h"
  2. #if IL2CPP_TARGET_WINRT || IL2CPP_TARGET_XBOXONE
  3. #if WINDOWS_SDK_BUILD_VERSION < 16299
  4. #include "os/Win32/WindowsHeaders.h"
  5. #include "Win32ApiSharedEmulation.h"
  6. #include <io.h>
  7. #include <windows.networking.connectivity.h>
  8. #include <windows.storage.h>
  9. using namespace il2cpp::winrt;
  10. using namespace Microsoft::WRL;
  11. using namespace Microsoft::WRL::Wrappers;
  12. using namespace ABI::Windows::Foundation;
  13. using namespace ABI::Windows::Foundation::Collections;
  14. using namespace ABI::Windows::Networking;
  15. using namespace ABI::Windows::Networking::Connectivity;
  16. using namespace ABI::Windows::Storage;
  17. extern "C"
  18. {
  19. BOOL WINAPI GetComputerNameW(LPWSTR lpBuffer, LPDWORD nSize)
  20. {
  21. #define ERROR_CHECK(hr) do { if (FAILED(hr)) { SetLastError(WIN32_FROM_HRESULT(hr)); return FALSE; } } while (false)
  22. ComPtr<INetworkInformationStatics> info;
  23. auto hr = RoGetActivationFactory(HStringReference(RuntimeClass_Windows_Networking_Connectivity_NetworkInformation).Get(), __uuidof(info), &info);
  24. ERROR_CHECK(hr);
  25. ComPtr<IVectorView<HostName*> > names;
  26. hr = info->GetHostNames(&names);
  27. ERROR_CHECK(hr);
  28. unsigned int size;
  29. hr = names->get_Size(&size);
  30. if (FAILED(hr) || !size)
  31. {
  32. SetLastError(WIN32_FROM_HRESULT(hr));
  33. return FALSE;
  34. }
  35. ComPtr<IHostName> name;
  36. hr = names->GetAt(0, &name);
  37. ERROR_CHECK(hr);
  38. HString displayName;
  39. hr = name->get_DisplayName(displayName.GetAddressOf());
  40. ERROR_CHECK(hr);
  41. #undef ERROR_CHECK
  42. unsigned int sourceLength;
  43. auto sourceBuffer = displayName.GetRawBuffer(&sourceLength);
  44. // NetBIOS caps at 15 characters, not including the null terminator
  45. DWORD finalSize = sourceLength > 15 ? 15 : sourceLength;
  46. // Cap at the first period
  47. for (DWORD i = 0; i < finalSize; ++i)
  48. if (sourceBuffer[i] == '.')
  49. finalSize = i;
  50. // Error and return the size if the buffer is not large enough
  51. if (finalSize + 1 > *nSize)
  52. {
  53. SetLastError(ERROR_BUFFER_OVERFLOW);
  54. *nSize = finalSize + 1;
  55. return FALSE;
  56. }
  57. if (lpBuffer != nullptr)
  58. {
  59. memset(lpBuffer, 0, *nSize);
  60. // Copy the characters and make them uppercase
  61. for (DWORD i = 0; i < finalSize; ++i)
  62. lpBuffer[i] = toupper(sourceBuffer[i]);
  63. *nSize = finalSize;
  64. return TRUE;
  65. }
  66. *nSize = finalSize;
  67. return FALSE;
  68. }
  69. } // extern "C"
  70. #endif // WINDOWS_SDK_BUILD_VERSION < 16299
  71. #if WINDOWS_SDK_BUILD_VERSION < 15063
  72. #include "os/Win32/WindowsHeaders.h"
  73. #include "Win32ApiSharedEmulation.h"
  74. extern "C"
  75. {
  76. DWORD WINAPI GetNetworkParams(PFIXED_INFO pFixedInfo, PULONG pOutBufLen)
  77. {
  78. if (*pOutBufLen < sizeof(FIXED_INFO))
  79. {
  80. *pOutBufLen = sizeof(FIXED_INFO);
  81. return ERROR_BUFFER_OVERFLOW;
  82. }
  83. memset(pFixedInfo, 0, sizeof(FIXED_INFO));
  84. return ERROR_NOT_SUPPORTED;
  85. }
  86. } // extern "C"
  87. #endif // WINDOWS_SDK_BUILD_VERSION < 15063
  88. #if IL2CPP_TARGET_WINRT
  89. #include "Win32ApiSharedEmulation.h"
  90. extern "C"
  91. {
  92. // Provide a dummy GetIfEntry for WinRT. This is used by the class library
  93. // code to implement GetAllNetworkInterfaces(). It looks like the values
  94. // returned though are never actually used. So this dummy implementation seems
  95. // to be enough for the class library code to work in WinRT.
  96. DWORD WINAPI GetIfEntry(PMIB_IFROW pIfRow)
  97. {
  98. memset(pIfRow, 0, sizeof(MIB_IFROW));
  99. return NO_ERROR;
  100. }
  101. } // extern "C"
  102. #endif // IL2CPP_TARGET_WINRT
  103. #endif // IL2CPP_TARGET_WINRT || IL2CPP_TARGET_XBOXONE