kernel32.odin 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // +build windows
  2. package win32
  3. foreign import "system:kernel32.lib"
  4. @(default_calling_convention = "std")
  5. foreign kernel32 {
  6. @(link_name="CreateProcessA") create_process_a :: proc(application_name, command_line: cstring,
  7. process_attributes, thread_attributes: ^Security_Attributes,
  8. inherit_handle: Bool, creation_flags: u32, environment: rawptr,
  9. current_directory: cstring, startup_info: ^Startup_Info,
  10. process_information: ^Process_Information) -> Bool ---
  11. @(link_name="CreateProcessW") create_process_w :: proc(application_name, command_line: Wstring,
  12. process_attributes, thread_attributes: ^Security_Attributes,
  13. inherit_handle: Bool, creation_flags: u32, environment: rawptr,
  14. current_directory: Wstring, startup_info: ^Startup_Info,
  15. process_information: ^Process_Information) -> Bool ---
  16. @(link_name="GetExitCodeProcess") get_exit_code_process :: proc(process: Handle, exit: ^u32) -> Bool ---
  17. @(link_name="ExitProcess") exit_process :: proc(exit_code: u32) ---
  18. @(link_name="GetModuleHandleA") get_module_handle_a :: proc(module_name: cstring) -> Hmodule ---
  19. @(link_name="GetModuleHandleW") get_module_handle_w :: proc(module_name: Wstring) -> Hmodule ---
  20. @(link_name="GetModuleFileNameA") get_module_file_name_a :: proc(module: Hmodule, filename: cstring, size: u32) -> u32 ---
  21. @(link_name="GetModuleFileNameW") get_module_file_name_w :: proc(module: Hmodule, filename: Wstring, size: u32) -> u32 ---
  22. @(link_name="Sleep") sleep :: proc(ms: u32) ---
  23. @(link_name="QueryPerformanceFrequency") query_performance_frequency :: proc(result: ^i64) -> i32 ---
  24. @(link_name="QueryPerformanceCounter") query_performance_counter :: proc(result: ^i64) -> i32 ---
  25. @(link_name="OutputDebugStringA") output_debug_string_a :: proc(c_str: cstring) ---
  26. @(link_name="GetCommandLineA") get_command_line_a :: proc() -> cstring ---
  27. @(link_name="GetCommandLineW") get_command_line_w :: proc() -> Wstring ---
  28. @(link_name="GetSystemMetrics") get_system_metrics :: proc(index: i32) -> i32 ---
  29. @(link_name="GetSystemInfo") get_system_info :: proc(info: ^System_Info) ---
  30. @(link_name="GetVersionExA") get_version :: proc(osvi: ^OS_Version_Info_Ex_A) ---
  31. @(link_name="GetCurrentThreadId") get_current_thread_id :: proc() -> u32 ---
  32. // NOTE(tetra): Not thread safe with SetCurrentDirectory and GetFullPathName;
  33. // The current directory is stored as a global variable in the process.
  34. @(link_name="GetCurrentDirectoryW") get_current_directory_w :: proc(len: u32, buf: Wstring) -> u32 ---
  35. @(link_name="SetCurrentDirectoryW") set_current_directory_w :: proc(buf: Wstring) -> u32 ---
  36. @(link_name="GetSystemTimeAsFileTime") get_system_time_as_file_time :: proc(system_time_as_file_time: ^Filetime) ---
  37. @(link_name="FileTimeToLocalFileTime") file_time_to_local_file_time :: proc(file_time: ^Filetime, local_file_time: ^Filetime) -> Bool ---
  38. @(link_name="FileTimeToSystemTime") file_time_to_system_time :: proc(file_time: ^Filetime, system_time: ^Systemtime) -> Bool ---
  39. @(link_name="SystemTimeToFileTime") system_time_to_file_time :: proc(system_time: ^Systemtime, file_time: ^Filetime) -> Bool ---
  40. @(link_name="GetStdHandle") get_std_handle :: proc(h: i32) -> Handle ---
  41. @(link_name="CreateFileA")
  42. create_file_a :: proc(filename: cstring, desired_access, share_module: u32,
  43. security: rawptr,
  44. creation, flags_and_attribs: u32, template_file: Handle) -> Handle ---
  45. @(link_name="CreateFileW")
  46. create_file_w :: proc(filename: Wstring, desired_access, share_module: u32,
  47. security: rawptr,
  48. creation, flags_and_attribs: u32, template_file: Handle) -> Handle ---
  49. @(link_name="ReadFile") read_file :: proc(h: Handle, buf: rawptr, to_read: u32, bytes_read: ^i32, overlapped: rawptr) -> Bool ---
  50. @(link_name="WriteFile") write_file :: proc(h: Handle, buf: rawptr, len: i32, written_result: ^i32, overlapped: rawptr) -> Bool ---
  51. @(link_name="GetFileSizeEx") get_file_size_ex :: proc(file_handle: Handle, file_size: ^i64) -> Bool ---
  52. @(link_name="GetFileInformationByHandle") get_file_information_by_handle :: proc(file_handle: Handle, file_info: ^By_Handle_File_Information) -> Bool ---
  53. @(link_name="CreateDirectoryA") create_directory_a :: proc(path: cstring, security_attributes: ^Security_Attributes) -> Bool ---
  54. @(link_name="CreateDirectoryW") create_directory_w :: proc(path: Wstring, security_attributes: ^Security_Attributes) -> Bool ---
  55. @(link_name="GetFileType") get_file_type :: proc(file_handle: Handle) -> u32 ---
  56. @(link_name="SetFilePointer") set_file_pointer :: proc(file_handle: Handle, distance_to_move: i32, distance_to_move_high: ^i32, move_method: u32) -> u32 ---
  57. @(link_name="SetHandleInformation") set_handle_information :: proc(obj: Handle, mask, flags: u32) -> Bool ---
  58. @(link_name="FindFirstFileA") find_first_file_a :: proc(file_name: cstring, data: ^Find_Data_A) -> Handle ---
  59. @(link_name="FindNextFileA") find_next_file_a :: proc(file: Handle, data: ^Find_Data_A) -> Bool ---
  60. @(link_name="FindFirstFileW") find_first_file_w :: proc(file_name: Wstring, data: ^Find_Data_W) -> Handle ---
  61. @(link_name="FindNextFileW") find_next_file_w :: proc(file: Handle, data: ^Find_Data_W) -> Bool ---
  62. @(link_name="FindClose") find_close :: proc(file: Handle) -> Bool ---
  63. @(link_name="MoveFileExA") move_file_ex_a :: proc(existing, new: cstring, flags: u32) -> Bool ---
  64. @(link_name="DeleteFileA") delete_file_a :: proc(file_name: cstring) -> Bool ---
  65. @(link_name="CopyFileA") copy_file_a :: proc(existing, new: cstring, fail_if_exists: Bool) -> Bool ---
  66. @(link_name="MoveFileExW") move_file_ex_w :: proc(existing, new: Wstring, flags: u32) -> Bool ---
  67. @(link_name="DeleteFileW") delete_file_w :: proc(file_name: Wstring) -> Bool ---
  68. @(link_name="CopyFileW") copy_file_w :: proc(existing, new: Wstring, fail_if_exists: Bool) -> Bool ---
  69. @(link_name="HeapAlloc") heap_alloc :: proc(h: Handle, flags: u32, bytes: int) -> rawptr ---
  70. @(link_name="HeapReAlloc") heap_realloc :: proc(h: Handle, flags: u32, memory: rawptr, bytes: int) -> rawptr ---
  71. @(link_name="HeapFree") heap_free :: proc(h: Handle, flags: u32, memory: rawptr) -> Bool ---
  72. @(link_name="GetProcessHeap") get_process_heap :: proc() -> Handle ---
  73. @(link_name="LocalAlloc") local_alloc :: proc(flags: u32, bytes: int) -> rawptr ---
  74. @(link_name="LocalReAlloc") local_realloc :: proc(mem: rawptr, bytes: int, flags: uint) -> rawptr ---
  75. @(link_name="LocalFree") local_free :: proc(mem: rawptr) -> rawptr ---
  76. @(link_name="FindFirstChangeNotificationA") find_first_change_notification_a :: proc(path: cstring, watch_subtree: Bool, filter: u32) -> Handle ---
  77. @(link_name="FindNextChangeNotification") find_next_change_notification :: proc(h: Handle) -> Bool ---
  78. @(link_name="FindCloseChangeNotification") find_close_change_notification :: proc(h: Handle) -> Bool ---
  79. @(link_name="ReadDirectoryChangesW") read_directory_changes_w :: proc(dir: Handle, buf: rawptr, buf_length: u32,
  80. watch_subtree: Bool, notify_filter: u32,
  81. bytes_returned: ^u32, overlapped: ^Overlapped,
  82. completion: rawptr) -> Bool ---
  83. @(link_name="GetOverlappedResult") get_overlapped_result :: proc(file: Handle, overlapped: ^Overlapped, number_of_bytes_transferred: ^u32, wait: Bool) -> Bool ---
  84. @(link_name="WideCharToMultiByte") wide_char_to_multi_byte :: proc(code_page: u32, flags: u32,
  85. wchar_str: Wstring, wchar: i32,
  86. multi_str: cstring, multi: i32,
  87. default_char: cstring, used_default_char: ^Bool) -> i32 ---
  88. @(link_name="MultiByteToWideChar") multi_byte_to_wide_char :: proc(code_page: u32, flags: u32,
  89. mb_str: cstring, mb: i32,
  90. wc_str: Wstring, wc: i32) -> i32 ---
  91. @(link_name="CreateSemaphoreA") create_semaphore_a :: proc(attributes: ^Security_Attributes, initial_count, maximum_count: i32, name: cstring) -> Handle ---
  92. @(link_name="CreateSemaphoreW") create_semaphore_w :: proc(attributes: ^Security_Attributes, initial_count, maximum_count: i32, name: cstring) -> Handle ---
  93. @(link_name="ReleaseSemaphore") release_semaphore :: proc(semaphore: Handle, release_count: i32, previous_count: ^i32) -> Bool ---
  94. @(link_name="WaitForSingleObject") wait_for_single_object :: proc(handle: Handle, milliseconds: u32) -> u32 ---
  95. }
  96. // @(default_calling_convention = "c")
  97. foreign kernel32 {
  98. @(link_name="GetLastError") get_last_error :: proc() -> i32 ---
  99. @(link_name="CloseHandle") close_handle :: proc(h: Handle) -> i32 ---
  100. @(link_name="GetFileAttributesA") get_file_attributes_a :: proc(filename: cstring) -> u32 ---
  101. @(link_name="GetFileAttributesW") get_file_attributes_w :: proc(filename: Wstring) -> u32 ---
  102. @(link_name="GetFileAttributesExA") get_file_attributes_ex_a :: proc(filename: cstring, info_level_id: GET_FILEEX_INFO_LEVELS, file_info: ^File_Attribute_Data) -> Bool ---
  103. @(link_name="GetFileAttributesExW") get_file_attributes_ex_w :: proc(filename: Wstring, info_level_id: GET_FILEEX_INFO_LEVELS, file_info: ^File_Attribute_Data) -> Bool ---
  104. @(link_name="CompareFileTime") compare_file_time :: proc(a, b: ^Filetime) -> i32 ---
  105. }
  106. @(default_calling_convention = "c")
  107. foreign kernel32 {
  108. @(link_name="InterlockedCompareExchange") interlocked_compare_exchange :: proc(dst: ^i32, exchange, comparand: i32) -> i32 ---
  109. @(link_name="InterlockedExchange") interlocked_exchange :: proc(dst: ^i32, desired: i32) -> i32 ---
  110. @(link_name="InterlockedExchangeAdd") interlocked_exchange_add :: proc(dst: ^i32, desired: i32) -> i32 ---
  111. @(link_name="InterlockedAnd") interlocked_and :: proc(dst: ^i32, desired: i32) -> i32 ---
  112. @(link_name="InterlockedOr") interlocked_or :: proc(dst: ^i32, desired: i32) -> i32 ---
  113. @(link_name="InterlockedCompareExchange64") interlocked_compare_exchange64 :: proc(dst: ^i64, exchange, comparand: i64) -> i64 ---
  114. @(link_name="InterlockedExchange64") interlocked_exchange64 :: proc(dst: ^i64, desired: i64) -> i64 ---
  115. @(link_name="InterlockedExchangeAdd64") interlocked_exchange_add64 :: proc(dst: ^i64, desired: i64) -> i64 ---
  116. @(link_name="InterlockedAnd64") interlocked_and64 :: proc(dst: ^i64, desired: i64) -> i64 ---
  117. @(link_name="InterlockedOr64") interlocked_or64 :: proc(dst: ^i64, desired: i64) -> i64 ---
  118. }
  119. @(default_calling_convention = "std")
  120. foreign kernel32 {
  121. @(link_name="_mm_pause") mm_pause :: proc() ---
  122. @(link_name="ReadWriteBarrier") read_write_barrier :: proc() ---
  123. @(link_name="WriteBarrier") write_barrier :: proc() ---
  124. @(link_name="ReadBarrier") read_barrier :: proc() ---
  125. @(link_name="CreateThread")
  126. create_thread :: proc(thread_attributes: ^Security_Attributes, stack_size: uint, start_routine: proc "stdcall" (rawptr) -> u32,
  127. parameter: rawptr, creation_flags: u32, thread_id: ^u32) -> Handle ---
  128. @(link_name="ResumeThread") resume_thread :: proc(thread: Handle) -> u32 ---
  129. @(link_name="GetThreadPriority") get_thread_priority :: proc(thread: Handle) -> i32 ---
  130. @(link_name="SetThreadPriority") set_thread_priority :: proc(thread: Handle, priority: i32) -> Bool ---
  131. @(link_name="GetExitCodeThread") get_exit_code_thread :: proc(thread: Handle, exit_code: ^u32) -> Bool ---
  132. @(link_name="TerminateThread") terminate_thread :: proc(thread: Handle, exit_code: u32) -> Bool ---
  133. @(link_name="InitializeCriticalSection") initialize_critical_section :: proc(critical_section: ^Critical_Section) ---
  134. @(link_name="InitializeCriticalSectionAndSpinCount") initialize_critical_section_and_spin_count :: proc(critical_section: ^Critical_Section, spin_count: u32) -> b32 ---
  135. @(link_name="DeleteCriticalSection") delete_critical_section :: proc(critical_section: ^Critical_Section) ---
  136. @(link_name="SetCriticalSectionSpinCount") set_critical_section_spin_count :: proc(critical_section: ^Critical_Section, spin_count: u32) -> u32 ---
  137. @(link_name="TryEnterCriticalSection") try_enter_critical_section :: proc(critical_section: ^Critical_Section) -> b8 ---
  138. @(link_name="EnterCriticalSection") enter_critical_section :: proc(critical_section: ^Critical_Section) ---
  139. @(link_name="LeaveCriticalSection") leave_critical_section :: proc(critical_section: ^Critical_Section) ---
  140. @(link_name="CreateEventA") create_event_a :: proc(event_attributes: ^Security_Attributes, manual_reset, initial_state: Bool, name: cstring) -> Handle ---
  141. @(link_name="CreateEventW") create_event_w :: proc(event_attributes: ^Security_Attributes, manual_reset, initial_state: Bool, name: Wstring) -> Handle ---
  142. @(link_name="PulseEvent") pulse_event :: proc(event: Handle) -> Bool ---
  143. @(link_name="SetEvent") set_event :: proc(event: Handle) -> Bool ---
  144. @(link_name="ResetEvent") reset_event :: proc(event: Handle) -> Bool ---
  145. @(link_name="LoadLibraryA") load_library_a :: proc(c_str: cstring) -> Hmodule ---
  146. @(link_name="LoadLibraryW") load_library_w :: proc(c_str: Wstring) -> Hmodule ---
  147. @(link_name="FreeLibrary") free_library :: proc(h: Hmodule) -> Bool ---
  148. @(link_name="GetProcAddress") get_proc_address :: proc(h: Hmodule, c_str: cstring) -> rawptr ---
  149. @(link_name="GetFullPathNameA") get_full_path_name_a :: proc(filename: cstring, buffer_length: u32, buffer: cstring, file_part: ^Wstring) -> u32 ---
  150. @(link_name="GetFullPathNameW") get_full_path_name_w :: proc(filename: Wstring, buffer_length: u32, buffer: Wstring, file_part: ^Wstring) -> u32 ---
  151. @(link_name="GetLongPathNameA") get_long_path_name_a :: proc(short, long: cstring, len: u32) -> u32 ---
  152. @(link_name="GetLongPathNameW") get_long_path_name_w :: proc(short, long: Wstring, len: u32) -> u32 ---
  153. @(link_name="GetShortPathNameA") get_short_path_name_a :: proc(long, short: cstring, len: u32) -> u32 ---
  154. @(link_name="GetShortPathNameW") get_short_path_name_w :: proc(long, short: Wstring, len: u32) -> u32 ---
  155. @(link_name="GetCurrentDirectoryA") get_current_directory_a :: proc(buffer_length: u32, buffer: cstring) -> u32 ---
  156. }
  157. Memory_Basic_Information :: struct {
  158. base_address: rawptr,
  159. allocation_base: rawptr,
  160. allocation_protect: u32,
  161. region_size: uint,
  162. state: u32,
  163. protect: u32,
  164. type: u32,
  165. }
  166. @(default_calling_convention = "std")
  167. foreign kernel32 {
  168. @(link_name="VirtualAlloc") virtual_alloc :: proc(address: rawptr, size: uint, allocation_type: u32, protect: u32) -> rawptr ---
  169. @(link_name="VirtualAllocEx") virtual_alloc_ex :: proc(process: Handle, address: rawptr, size: uint, allocation_type: u32, protect: u32) -> rawptr ---
  170. @(link_name="VirtualFree") virtual_free :: proc(address: rawptr, size: uint, free_type: u32) -> Bool ---
  171. @(link_name="VirtualLock") virtual_lock :: proc(address: rawptr, size: uint) -> Bool ---
  172. @(link_name="VirtualProtect") virtual_protect :: proc(address: rawptr, size: uint, new_protect: u32, old_protect: ^u32) -> Bool ---
  173. @(link_name="VirtualQuery") virtual_query :: proc(address: rawptr, buffer: ^Memory_Basic_Information, length: uint) -> uint ---
  174. }
  175. MEM_COMMIT :: 0x00001000
  176. MEM_RESERVE :: 0x00002000
  177. MEM_DECOMMIT :: 0x00004000
  178. MEM_RELEASE :: 0x00008000
  179. MEM_RESET :: 0x00080000
  180. MEM_RESET_UNDO :: 0x01000000
  181. MEM_LARGE_PAGES :: 0x20000000
  182. MEM_PHYSICAL :: 0x00400000
  183. MEM_TOP_DOWN :: 0x00100000
  184. MEM_WRITE_WATCH :: 0x00200000
  185. PAGE_NOACCESS :: 0x01
  186. PAGE_READONLY :: 0x02
  187. PAGE_READWRITE :: 0x04
  188. PAGE_WRITECOPY :: 0x08
  189. PAGE_EXECUTE :: 0x10
  190. PAGE_EXECUTE_READ :: 0x20
  191. PAGE_EXECUTE_READWRITE :: 0x40
  192. PAGE_EXECUTE_WRITECOPY :: 0x80