2019-07-01 17:45:55 +02:00
|
|
|
module os
|
|
|
|
|
2019-07-16 20:00:41 +02:00
|
|
|
#flag -lws2_32
|
|
|
|
#include <winsock2.h>
|
|
|
|
|
2019-07-16 01:57:03 +02:00
|
|
|
const (
|
2019-07-16 01:59:28 +02:00
|
|
|
PathSeparator = '\\'
|
2019-07-16 01:57:03 +02:00
|
|
|
)
|
|
|
|
|
2019-07-01 17:45:55 +02:00
|
|
|
// Ref - https://docs.microsoft.com/en-us/windows/desktop/winprog/windows-data-types
|
|
|
|
// A handle to an object.
|
2019-07-14 22:59:25 +02:00
|
|
|
type HANDLE voidptr
|
2019-07-01 17:45:55 +02:00
|
|
|
|
2019-07-14 22:59:25 +02:00
|
|
|
// Ref - https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle?view=vs-2019
|
|
|
|
// get_file_handle retrieves the operating-system file handle that is associated with the specified file descriptor.
|
2019-07-01 17:45:55 +02:00
|
|
|
pub fn get_file_handle(path string) HANDLE {
|
2019-07-01 21:31:36 +02:00
|
|
|
mode := 'rb'
|
2019-07-24 00:24:34 +02:00
|
|
|
_fd := C.fopen(path.str, mode.str)
|
2019-07-14 22:59:25 +02:00
|
|
|
if _fd == 0 {
|
|
|
|
return HANDLE(INVALID_HANDLE_VALUE)
|
2019-07-01 17:45:55 +02:00
|
|
|
}
|
2019-07-14 22:59:25 +02:00
|
|
|
_handle := C._get_osfhandle(C._fileno(_fd)) // CreateFile? - hah, no -_-
|
2019-07-01 17:45:55 +02:00
|
|
|
return _handle
|
|
|
|
}
|
2019-07-14 22:59:25 +02:00
|
|
|
|
|
|
|
// Ref - https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulefilenamea
|
|
|
|
// get_module_filename retrieves the fully qualified path for the file that contains the specified module.
|
|
|
|
// The module must have been loaded by the current process.
|
|
|
|
pub fn get_module_filename(handle HANDLE) ?string {
|
2019-07-24 00:24:34 +02:00
|
|
|
mut sz := int(1024) // Optimized length
|
|
|
|
mut buf := [byte(0); sz] // Not work for GetModuleFileNameW :(
|
2019-07-14 22:59:25 +02:00
|
|
|
for {
|
|
|
|
status := C.GetModuleFileName(handle, &buf, sz)
|
|
|
|
switch status {
|
|
|
|
case SUCCESS:
|
2019-07-24 00:24:34 +02:00
|
|
|
_filename := tos(buf.data, sz)
|
2019-07-14 22:59:25 +02:00
|
|
|
return _filename
|
2019-07-24 00:24:34 +02:00
|
|
|
case ERROR_INSUFFICIENT_BUFFER:
|
|
|
|
sz += 1024 // increment buffer cluster by 1024
|
|
|
|
buf = [byte(0); sz] // clear buffer
|
2019-07-14 22:59:25 +02:00
|
|
|
default:
|
|
|
|
// Must handled with GetLastError and converted by FormatMessage
|
|
|
|
return error('Cannot get file name from handle.')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-15 19:30:40 +02:00
|
|
|
|
|
|
|
// Ref - https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-formatmessagea#parameters
|
|
|
|
const (
|
|
|
|
FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100
|
|
|
|
FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000
|
|
|
|
FORMAT_MESSAGE_FROM_HMODULE = 0x00000800
|
|
|
|
FORMAT_MESSAGE_FROM_STRING = 0x00000400
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200
|
|
|
|
)
|
|
|
|
|
|
|
|
// Ref - winnt.h
|
|
|
|
const (
|
|
|
|
SUBLANG_NEUTRAL = 0x00
|
|
|
|
SUBLANG_DEFAULT = 0x01
|
|
|
|
LANG_NEUTRAL = (SUBLANG_NEUTRAL)
|
|
|
|
)
|
|
|
|
|
2019-07-16 20:38:19 +02:00
|
|
|
// Ref - https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes--12000-15999-
|
|
|
|
const (
|
|
|
|
MAX_ERROR_CODE = 15841 // ERROR_API_UNAVAILABLE
|
|
|
|
)
|
|
|
|
|
2019-07-18 20:21:48 +02:00
|
|
|
// ptr_win_get_error_msg return string (voidptr)
|
|
|
|
// representation of error, only for windows.
|
|
|
|
fn ptr_win_get_error_msg(code u32) voidptr {
|
2019-07-15 19:30:40 +02:00
|
|
|
mut buf := voidptr(0)
|
2019-07-16 20:38:19 +02:00
|
|
|
// Check for code overflow
|
|
|
|
if code > u32(MAX_ERROR_CODE) {
|
|
|
|
return buf
|
|
|
|
}
|
2019-07-15 19:30:40 +02:00
|
|
|
C.FormatMessage(
|
|
|
|
FORMAT_MESSAGE_ALLOCATE_BUFFER
|
|
|
|
| FORMAT_MESSAGE_FROM_SYSTEM
|
|
|
|
| FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
0, code, C.MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), &buf, 0, 0)
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
2019-07-18 20:21:48 +02:00
|
|
|
// get_error_msg return error code representation in string.
|
|
|
|
pub fn get_error_msg(code int) string {
|
|
|
|
if code < 0 { // skip negative
|
2019-07-16 20:38:19 +02:00
|
|
|
return ''
|
|
|
|
}
|
2019-07-18 20:21:48 +02:00
|
|
|
_ptr_text := ptr_win_get_error_msg(u32(code))
|
|
|
|
if _ptr_text == 0 { // compare with null
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
return tos(_ptr_text, C.strlen(_ptr_text))
|
|
|
|
}
|