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 /* You must free() this yourself. */
63 const F_CHAR
*default_image_path(void)
65 F_CHAR full_path
[MAX_UNICODE_PATH
];
67 F_CHAR path_temp
[MAX_UNICODE_PATH
];
69 if(!GetModuleFileName(NULL
, full_path
, MAX_UNICODE_PATH
))
70 fatal_error("GetModuleFileName() failed", 0);
72 if((ptr
= wcsrchr(full_path
, '.')))
75 snwprintf(path_temp
, sizeof(path_temp
)-1, L
"%s.image", full_path
);
76 path_temp
[sizeof(path_temp
) - 1] = 0;
78 return safe_strdup(path_temp
);
81 /* You must free() this yourself. */
82 const F_CHAR
*vm_executable_path(void)
84 F_CHAR full_path
[MAX_UNICODE_PATH
];
85 if(!GetModuleFileName(NULL
, full_path
, MAX_UNICODE_PATH
))
86 fatal_error("GetModuleFileName() failed", 0);
87 return safe_strdup(full_path
);
90 void primitive_existsp(void)
92 BY_HANDLE_FILE_INFORMATION bhfi
;
94 F_CHAR
*path
= unbox_u16_string();
95 HANDLE h
= CreateFileW(path
,
100 FILE_FLAG_BACKUP_SEMANTICS
,
103 if(h
== INVALID_HANDLE_VALUE
)
105 // FindFirstFile is the only call that can stat c:\pagefile.sys
109 if(INVALID_HANDLE_VALUE
== (h
= FindFirstFile(path
, &st
)))
119 box_boolean(GetFileInformationByHandle(h
, &bhfi
));
123 F_SEGMENT
*alloc_segment(CELL size
)
128 if((mem
= (char *)VirtualAlloc(NULL
, getpagesize() * 2 + size
,
129 MEM_COMMIT
, PAGE_EXECUTE_READWRITE
)) == 0)
132 if (!VirtualProtect(mem
, getpagesize(), PAGE_NOACCESS
, &ignore
))
133 fatal_error("Cannot allocate low guard page", (CELL
)mem
);
135 if (!VirtualProtect(mem
+ size
+ getpagesize(),
136 getpagesize(), PAGE_NOACCESS
, &ignore
))
137 fatal_error("Cannot allocate high guard page", (CELL
)mem
);
139 F_SEGMENT
*block
= safe_malloc(sizeof(F_SEGMENT
));
141 block
->start
= (CELL
)mem
+ getpagesize();
143 block
->end
= block
->start
+ size
;
148 void dealloc_segment(F_SEGMENT
*block
)
152 if(!VirtualFree((void*)(block
->start
- si
.dwPageSize
), 0, MEM_RELEASE
))
153 fatal_error("dealloc_segment failed",0);
157 long getpagesize(void)
159 static long g_pagesize
= 0;
162 SYSTEM_INFO system_info
;
163 GetSystemInfo (&system_info
);
164 g_pagesize
= system_info
.dwPageSize
;
169 void sleep_micros(u64 usec
)
171 Sleep((DWORD
)(usec
/ 1000));