Revert some changes which don't have proper dependencies.
[mono-project.git] / mono / metadata / icall-windows.c
blob91fcaa33e15638f2df3d21ec8ad374014ced42ad
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 error_init (error);
80 return mono_string_new_handle (mono_domain_get (), "\r\n", error);
83 MonoBoolean
84 mono_icall_is_64bit_os (void)
86 #if SIZEOF_VOID_P == 8
87 return TRUE;
88 #else
89 gboolean isWow64Process = FALSE;
90 if (IsWow64Process (GetCurrentProcess (), &isWow64Process)) {
91 return (MonoBoolean)isWow64Process;
93 return FALSE;
94 #endif
97 MonoArray *
98 mono_icall_get_environment_variable_names (MonoError *error)
100 MonoArray *names;
101 MonoDomain *domain;
102 MonoString *str;
103 WCHAR* env_strings;
104 WCHAR* env_string;
105 WCHAR* equal_str;
106 int n = 0;
108 error_init (error);
109 env_strings = GetEnvironmentStrings();
111 if (env_strings) {
112 env_string = env_strings;
113 while (*env_string != '\0') {
114 /* weird case that MS seems to skip */
115 if (*env_string != '=')
116 n++;
117 while (*env_string != '\0')
118 env_string++;
119 env_string++;
123 domain = mono_domain_get ();
124 names = mono_array_new_checked (domain, mono_defaults.string_class, n, error);
125 return_val_if_nok (error, NULL);
127 if (env_strings) {
128 n = 0;
129 env_string = env_strings;
130 while (*env_string != '\0') {
131 /* weird case that MS seems to skip */
132 if (*env_string != '=') {
133 equal_str = wcschr(env_string, '=');
134 g_assert(equal_str);
135 str = mono_string_new_utf16_checked (domain, env_string, (gint32)(equal_str - env_string), error);
136 goto_if_nok (error, cleanup);
138 mono_array_setref_internal (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;
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 */