6#if defined(DART_HOST_OS_WINDOWS)
24static constexpr int64_t kFileTimeEpoch = 116444736000000000LL;
39 FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
40 nullptr, code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
41 buffer, buffer_length,
nullptr);
42 if (message_size == 0) {
47 _snwprintf(
buffer, buffer_length,
L"OS Error %d", code);
50 buffer[buffer_length - 1] = 0;
54 const int64_t kTimeScaler = 10000;
56 t.t_ = millis * kTimeScaler + kFileTimeEpoch;
64void OSError::Reload() {
68void OSError::SetCodeAndMessage(SubSystem sub_system,
int code) {
69 set_sub_system(sub_system);
72 const int kMaxMessageLength = 256;
73 wchar_t message[kMaxMessageLength];
79char* StringUtils::ConsoleStringToUtf8(
char* str,
81 intptr_t* result_len) {
82 int wide_len = MultiByteToWideChar(CP_ACP, 0, str, len,
nullptr, 0);
86 MultiByteToWideChar(CP_ACP, 0, str, len, wide, wide_len);
87 char*
utf8 = StringUtilsWin::WideToUtf8(wide, wide_len, result_len);
91char* StringUtils::Utf8ToConsoleString(
char* utf8,
93 intptr_t* result_len) {
95 wchar_t* wide = StringUtilsWin::Utf8ToWide(utf8, len, &wide_len);
96 int system_len = WideCharToMultiByte(CP_ACP, 0, wide, wide_len,
nullptr, 0,
101 if (ansi ==
nullptr) {
104 WideCharToMultiByte(CP_ACP, 0, wide, wide_len, ansi, system_len,
nullptr,
106 if (result_len !=
nullptr) {
107 *result_len = system_len;
112char* StringUtilsWin::WideToUtf8(
wchar_t* wide,
114 intptr_t* result_len) {
118 WideCharToMultiByte(CP_UTF8, 0, wide, len,
nullptr, 0,
nullptr,
nullptr);
121 WideCharToMultiByte(CP_UTF8, 0, wide, len, utf8, utf8_len,
nullptr,
nullptr);
122 if (result_len !=
nullptr) {
123 *result_len = utf8_len;
128wchar_t* StringUtilsWin::Utf8ToWide(
char* utf8,
130 intptr_t* result_len) {
133 int wide_len = MultiByteToWideChar(CP_UTF8, 0, utf8, len,
nullptr, 0);
137 MultiByteToWideChar(CP_UTF8, 0, utf8, len, wide, wide_len);
138 if (result_len !=
nullptr) {
139 *result_len = wide_len;
144const char* StringUtils::Utf8ToConsoleString(
const char* utf8,
146 intptr_t* result_len) {
147 return const_cast<const char*
>(StringUtils::Utf8ToConsoleString(
148 const_cast<char*
>(utf8), len, result_len));
151const char* StringUtils::ConsoleStringToUtf8(
const char* str,
153 intptr_t* result_len) {
154 return const_cast<const char*
>(StringUtils::ConsoleStringToUtf8(
155 const_cast<char*
>(str), len, result_len));
158const char* StringUtilsWin::WideToUtf8(
const wchar_t* wide,
160 intptr_t* result_len) {
161 return const_cast<const char*
>(
162 StringUtilsWin::WideToUtf8(
const_cast<wchar_t*
>(wide), len, result_len));
165const wchar_t* StringUtilsWin::Utf8ToWide(
const char* utf8,
167 intptr_t* result_len) {
168 return const_cast<const wchar_t*
>(
169 StringUtilsWin::Utf8ToWide(
const_cast<char*
>(utf8), len, result_len));
172bool ShellUtils::GetUtf8Argv(
int argc,
char** argv) {
173 wchar_t* command_line = GetCommandLineW();
175 wchar_t** unicode_argv = CommandLineToArgvW(command_line, &unicode_argc);
176 if (unicode_argv ==
nullptr) {
180 ASSERT(argc == unicode_argc);
181 if (argc < unicode_argc) {
184 for (
int i = 0; i < unicode_argc; i++) {
185 wchar_t* arg = unicode_argv[i];
187 WideCharToMultiByte(CP_UTF8, 0, arg, -1,
nullptr, 0,
nullptr,
nullptr);
188 char* utf8_arg =
reinterpret_cast<char*
>(
malloc(arg_len));
189 WideCharToMultiByte(CP_UTF8, 0, arg, -1, utf8_arg, arg_len,
nullptr,
193 LocalFree(unicode_argv);
197static int64_t GetCurrentTimeMicros() {
198 const int64_t kTimeScaler = 10;
201 GetSystemTimeAsFileTime(&time.ft_);
202 return (time.t_ - kFileTimeEpoch) / kTimeScaler;
205static int64_t qpc_ticks_per_second = 0;
207void TimerUtils::InitOnce() {
208 LARGE_INTEGER ticks_per_sec;
209 if (!QueryPerformanceFrequency(&ticks_per_sec)) {
210 qpc_ticks_per_second = 0;
212 qpc_ticks_per_second =
static_cast<int64_t
>(ticks_per_sec.QuadPart);
216int64_t TimerUtils::GetCurrentMonotonicMillis() {
217 return GetCurrentMonotonicMicros() / 1000;
220int64_t TimerUtils::GetCurrentMonotonicMicros() {
221 if (qpc_ticks_per_second == 0) {
223 return GetCurrentTimeMicros();
227 QueryPerformanceCounter(&now);
228 int64_t qpc_value =
static_cast<int64_t
>(now.QuadPart);
230 int64_t seconds = qpc_value / qpc_ticks_per_second;
231 int64_t leftover_ticks = qpc_value - (seconds * qpc_ticks_per_second);
237void TimerUtils::Sleep(int64_t millis) {
242 int wide_len = MultiByteToWideChar(CP_UTF8, 0, path, -1,
nullptr, 0);
243 auto result = std::make_unique<wchar_t[]>(wide_len);
244 MultiByteToWideChar(CP_UTF8, 0, path, -1,
result.get(), wide_len);
static void PrintErr(const char *format,...) PRINTF_ATTRIBUTE(1
DART_EXPORT uint8_t * Dart_ScopeAllocate(intptr_t size)
static const uint8_t buffer[]
static constexpr int kMicrosecondsPerSecond
std::unique_ptr< wchar_t[]> Utf8ToWideChar(const char *path)
FILETIME GetFiletimeFromMillis(int64_t millis)
void FormatMessageIntoBuffer(DWORD code, wchar_t *buffer, int buffer_length)
void * malloc(size_t size)
struct _FILETIME FILETIME
WINBASEAPI _Check_return_ _Post_equals_last_error_ DWORD WINAPI GetLastError(VOID)