3 F_STRING *get_error_message(void)
5 DWORD id = GetLastError();
6 F_CHAR *msg = error_message(id);
7 F_STRING *string = from_u16_string(msg);
12 /* You must LocalFree() the return value! */
13 F_CHAR *error_message(DWORD id)
18 DWORD ret = FormatMessage(
19 FORMAT_MESSAGE_ALLOCATE_BUFFER |
20 FORMAT_MESSAGE_FROM_SYSTEM,
23 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
24 (LPTSTR)(void *) &buffer,
27 return error_message(GetLastError());
29 /* strip whitespace from end */
30 index = wcslen(buffer) - 1;
31 while(index >= 0 && isspace(buffer[index]))
41 hFactorDll = GetModuleHandle(FACTOR_DLL);
43 fatal_error("GetModuleHandle(\"" FACTOR_DLL_NAME "\") failed", 0);
46 void ffi_dlopen(F_DLL *dll)
48 dll->dll = LoadLibraryEx(alien_offset(dll->path), NULL, 0);
51 void *ffi_dlsym(F_DLL *dll, F_SYMBOL *symbol)
53 return GetProcAddress(dll ? (HMODULE)dll->dll : hFactorDll, symbol);
56 void ffi_dlclose(F_DLL *dll)
58 FreeLibrary((HMODULE)dll->dll);
62 bool windows_stat(F_CHAR *path)
64 BY_HANDLE_FILE_INFORMATION bhfi;
65 HANDLE h = CreateFileW(path,
70 FILE_FLAG_BACKUP_SEMANTICS,
73 if(h == INVALID_HANDLE_VALUE)
75 // FindFirstFile is the only call that can stat c:\pagefile.sys
79 if(INVALID_HANDLE_VALUE == (h = FindFirstFile(path, &st)))
85 ret = GetFileInformationByHandle(h, &bhfi);
90 void windows_image_path(F_CHAR *full_path, F_CHAR *temp_path, unsigned int length)
92 snwprintf(temp_path, length-1, L"%s.image", full_path);
93 temp_path[sizeof(temp_path) - 1] = 0;
96 /* You must free() this yourself. */
97 const F_CHAR *default_image_path(void)
99 F_CHAR full_path[MAX_UNICODE_PATH];
101 F_CHAR temp_path[MAX_UNICODE_PATH];
103 if(!GetModuleFileName(NULL, full_path, MAX_UNICODE_PATH))
104 fatal_error("GetModuleFileName() failed", 0);
106 if((ptr = wcsrchr(full_path, '.')))
109 snwprintf(temp_path, sizeof(temp_path)-1, L"%s.image", full_path);
110 temp_path[sizeof(temp_path) - 1] = 0;
112 if(!windows_stat(temp_path)) {
113 unsigned int len = wcslen(full_path);
114 F_CHAR magic[] = L"-console";
115 unsigned int magic_len = wcslen(magic);
117 if(!wcsncmp(full_path + len - magic_len, magic, MIN(len, magic_len)))
118 full_path[len - magic_len] = 0;
119 snwprintf(temp_path, sizeof(temp_path)-1, L"%s.image", full_path);
120 temp_path[sizeof(temp_path) - 1] = 0;
123 return safe_strdup(temp_path);
126 /* You must free() this yourself. */
127 const F_CHAR *vm_executable_path(void)
129 F_CHAR full_path[MAX_UNICODE_PATH];
130 if(!GetModuleFileName(NULL, full_path, MAX_UNICODE_PATH))
131 fatal_error("GetModuleFileName() failed", 0);
132 return safe_strdup(full_path);
136 void primitive_existsp(void)
139 F_CHAR *path = unbox_u16_string();
140 box_boolean(windows_stat(path));
143 F_SEGMENT *alloc_segment(CELL size)
148 if((mem = (char *)VirtualAlloc(NULL, getpagesize() * 2 + size,
149 MEM_COMMIT, PAGE_EXECUTE_READWRITE)) == 0)
152 if (!VirtualProtect(mem, getpagesize(), PAGE_NOACCESS, &ignore))
153 fatal_error("Cannot allocate low guard page", (CELL)mem);
155 if (!VirtualProtect(mem + size + getpagesize(),
156 getpagesize(), PAGE_NOACCESS, &ignore))
157 fatal_error("Cannot allocate high guard page", (CELL)mem);
159 F_SEGMENT *block = safe_malloc(sizeof(F_SEGMENT));
161 block->start = (CELL)mem + getpagesize();
163 block->end = block->start + size;
168 void dealloc_segment(F_SEGMENT *block)
172 if(!VirtualFree((void*)(block->start - si.dwPageSize), 0, MEM_RELEASE))
173 fatal_error("dealloc_segment failed",0);
177 long getpagesize(void)
179 static long g_pagesize = 0;
182 SYSTEM_INFO system_info;
183 GetSystemInfo (&system_info);
184 g_pagesize = system_info.dwPageSize;
189 void sleep_micros(u64 usec)
191 Sleep((DWORD)(usec / 1000));