[netcore] Implement Thread.GetCurrentProcessorId (#18450)
[mono-project.git] / mono / metadata / icall-windows.c
blob93fed0d6622510d7e50a6cc782ff4962eea5ab91
1 /**
2 * \file
3 * Windows icall support.
5 * Copyright 2016 Microsoft
6 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
7 */
8 #include <config.h>
9 #include <glib.h>
11 #if defined(HOST_WIN32)
12 #include <winsock2.h>
13 #include <windows.h>
14 #include "mono/metadata/icall-windows-internals.h"
15 #include "mono/metadata/w32subset.h"
16 #if HAVE_API_SUPPORT_WIN32_SH_GET_FOLDER_PATH
17 #include <shlobj.h>
18 #endif
20 void
21 mono_icall_make_platform_path (gchar *path)
23 for (size_t i = strlen (path); i > 0; i--)
24 if (path [i-1] == '\\')
25 path [i-1] = '/';
28 const gchar *
29 mono_icall_get_file_path_prefix (const gchar *path)
31 if (*path == '/' && *(path + 1) == '/') {
32 return "file:";
33 } else {
34 return "file:///";
38 gpointer
39 mono_icall_module_get_hinstance (MonoImage *image)
41 if (image && m_image_is_module_handle (image))
42 return image->raw_data;
44 return (gpointer) (-1);
47 #if HAVE_API_SUPPORT_WIN32_GET_COMPUTER_NAME
48 // Support older UWP SDK?
49 WINBASEAPI
50 BOOL
51 WINAPI
52 GetComputerNameW (
53 PWSTR buffer,
54 PDWORD size
57 MonoStringHandle
58 mono_icall_get_machine_name (MonoError *error)
60 gunichar2 buf [MAX_COMPUTERNAME_LENGTH + 1];
61 DWORD len = G_N_ELEMENTS (buf);
63 if (GetComputerNameW (buf, &len))
64 return mono_string_new_utf16_handle (mono_domain_get (), buf, len, error);
65 return MONO_HANDLE_NEW (MonoString, NULL);
67 #endif
69 int
70 mono_icall_get_platform (void)
72 /* Win32NT */
73 return 2;
76 MonoStringHandle
77 mono_icall_get_new_line (MonoError *error)
79 return mono_string_new_handle (mono_domain_get (), "\r\n", error);
82 MonoBoolean
83 mono_icall_is_64bit_os (void)
85 #if SIZEOF_VOID_P == 8
86 return TRUE;
87 #else
88 gboolean isWow64Process = FALSE;
89 if (IsWow64Process (GetCurrentProcess (), &isWow64Process)) {
90 return (MonoBoolean)isWow64Process;
92 return FALSE;
93 #endif
96 MonoArrayHandle
97 mono_icall_get_environment_variable_names (MonoError *error)
99 MonoArrayHandle names;
100 MonoDomain *domain;
101 MonoStringHandle str;
102 WCHAR* env_strings;
103 WCHAR* env_string;
104 WCHAR* equal_str;
105 int n = 0;
107 env_strings = GetEnvironmentStrings();
109 if (env_strings) {
110 env_string = env_strings;
111 while (*env_string != '\0') {
112 /* weird case that MS seems to skip */
113 if (*env_string != '=')
114 n++;
115 while (*env_string != '\0')
116 env_string++;
117 env_string++;
121 domain = mono_domain_get ();
122 names = mono_array_new_handle (domain, mono_defaults.string_class, n, error);
123 return_val_if_nok (error, NULL_HANDLE_ARRAY);
125 if (env_strings) {
126 n = 0;
127 str = MONO_HANDLE_NEW (MonoString, NULL);
128 env_string = env_strings;
129 while (*env_string != '\0') {
130 /* weird case that MS seems to skip */
131 if (*env_string != '=') {
132 equal_str = wcschr(env_string, '=');
133 g_assert(equal_str);
134 MonoString *s = mono_string_new_utf16_checked (domain, env_string, (gint32)(equal_str - env_string), error);
135 goto_if_nok (error, cleanup);
136 MONO_HANDLE_ASSIGN_RAW (str, s);
138 mono_array_handle_setref (names, n, str);
139 n++;
141 while (*env_string != '\0')
142 env_string++;
143 env_string++;
148 cleanup:
149 if (env_strings)
150 FreeEnvironmentStrings (env_strings);
151 if (!is_ok (error))
152 return NULL_HANDLE_ARRAY;
153 return names;
156 #if HAVE_API_SUPPORT_WIN32_SH_GET_FOLDER_PATH
157 MonoStringHandle
158 mono_icall_get_windows_folder_path (int folder, MonoError *error)
160 error_init (error);
161 #ifndef CSIDL_FLAG_CREATE
162 #define CSIDL_FLAG_CREATE 0x8000
163 #endif
165 WCHAR path [MAX_PATH];
166 /* Create directory if no existing */
167 if (SUCCEEDED (SHGetFolderPathW (NULL, folder | CSIDL_FLAG_CREATE, NULL, 0, path))) {
168 int len = 0;
169 while (path [len])
170 ++ len;
171 return mono_string_new_utf16_handle (mono_domain_get (), path, len, error);
173 return mono_string_new_handle (mono_domain_get (), "", error);
175 #endif
177 #if HAVE_API_SUPPORT_WIN32_SEND_MESSAGE_TIMEOUT
178 ICALL_EXPORT void
179 ves_icall_System_Environment_BroadcastSettingChange (MonoError *error)
181 SendMessageTimeout (HWND_BROADCAST, WM_SETTINGCHANGE, (WPARAM)NULL, (LPARAM)L"Environment", SMTO_ABORTIFHUNG, 2000, 0);
183 #endif
185 #if HAVE_API_SUPPORT_WIN32_WAIT_FOR_INPUT_IDLE
186 gint32
187 mono_icall_wait_for_input_idle (gpointer handle, gint32 milliseconds)
189 return WaitForInputIdle (handle, milliseconds);
191 #endif
193 void
194 mono_icall_write_windows_debug_string (const gunichar2 *message)
196 OutputDebugString (message);
199 #endif /* HOST_WIN32 */