kernel32.odin 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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_direcotry: 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_direcotry: cstring, 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="GetVersionExA") get_version :: proc(osvi: ^OS_Version_Info_Ex_A) ---;
  30. @(link_name="GetCurrentThreadId") get_current_thread_id :: proc() -> u32 ---;
  31. @(link_name="GetSystemTimeAsFileTime") get_system_time_as_file_time :: proc(system_time_as_file_time: ^Filetime) ---;
  32. @(link_name="FileTimeToLocalFileTime") file_time_to_local_file_time :: proc(file_time: ^Filetime, local_file_time: ^Filetime) -> Bool ---;
  33. @(link_name="FileTimeToSystemTime") file_time_to_system_time :: proc(file_time: ^Filetime, system_time: ^Systemtime) -> Bool ---;
  34. @(link_name="SystemTimeToFileTime") system_time_to_file_time :: proc(system_time: ^Systemtime, file_time: ^Filetime) -> Bool ---;
  35. @(link_name="GetStdHandle") get_std_handle :: proc(h: i32) -> Handle ---;
  36. @(link_name="CreateFileA")
  37. create_file_a :: proc(filename: cstring, desired_access, share_module: u32,
  38. security: rawptr,
  39. creation, flags_and_attribs: u32, template_file: Handle) -> Handle ---;
  40. @(link_name="CreateFileW")
  41. create_file_w :: proc(filename: Wstring, desired_access, share_module: u32,
  42. security: rawptr,
  43. creation, flags_and_attribs: u32, template_file: Handle) -> Handle ---;
  44. @(link_name="ReadFile") read_file :: proc(h: Handle, buf: rawptr, to_read: u32, bytes_read: ^i32, overlapped: rawptr) -> Bool ---;
  45. @(link_name="WriteFile") write_file :: proc(h: Handle, buf: rawptr, len: i32, written_result: ^i32, overlapped: rawptr) -> Bool ---;
  46. @(link_name="GetFileSizeEx") get_file_size_ex :: proc(file_handle: Handle, file_size: ^i64) -> Bool ---;
  47. @(link_name="GetFileInformationByHandle") get_file_information_by_handle :: proc(file_handle: Handle, file_info: ^By_Handle_File_Information) -> Bool ---;
  48. @(link_name="CreateDirectoryA") create_directory_a :: proc(path: cstring, security_attributes: ^Security_Attributes) -> Bool ---;
  49. @(link_name="CreateDirectoryW") create_directory_w :: proc(path: Wstring, security_attributes: ^Security_Attributes) -> Bool ---;
  50. @(link_name="GetFileType") get_file_type :: proc(file_handle: Handle) -> u32 ---;
  51. @(link_name="SetFilePointer") set_file_pointer :: proc(file_handle: Handle, distance_to_move: i32, distance_to_move_high: ^i32, move_method: u32) -> u32 ---;
  52. @(link_name="SetHandleInformation") set_handle_information :: proc(obj: Handle, mask, flags: u32) -> Bool ---;
  53. @(link_name="FindFirstFileA") find_first_file_a :: proc(file_name: cstring, data: ^Find_Data_A) -> Handle ---;
  54. @(link_name="FindNextFileA") find_next_file_a :: proc(file: Handle, data: ^Find_Data_A) -> Bool ---;
  55. @(link_name="FindFirstFileW") find_first_file_w :: proc(file_name: Wstring, data: ^Find_Data_W) -> Handle ---;
  56. @(link_name="FindNextFileW") find_next_file_w :: proc(file: Handle, data: ^Find_Data_W) -> Bool ---;
  57. @(link_name="FindClose") find_close :: proc(file: Handle) -> Bool ---;
  58. @(link_name="MoveFileExA") move_file_ex_a :: proc(existing, new: cstring, flags: u32) -> Bool ---;
  59. @(link_name="DeleteFileA") delete_file_a :: proc(file_name: cstring) -> Bool ---;
  60. @(link_name="CopyFileA") copy_file_a :: proc(existing, new: cstring, fail_if_exists: Bool) -> Bool ---;
  61. @(link_name="MoveFileExW") move_file_ex_w :: proc(existing, new: Wstring, flags: u32) -> Bool ---;
  62. @(link_name="DeleteFileW") delete_file_w :: proc(file_name: Wstring) -> Bool ---;
  63. @(link_name="CopyFileW") copy_file_w :: proc(existing, new: Wstring, fail_if_exists: Bool) -> Bool ---;
  64. @(link_name="HeapAlloc") heap_alloc :: proc(h: Handle, flags: u32, bytes: int) -> rawptr ---;
  65. @(link_name="HeapReAlloc") heap_realloc :: proc(h: Handle, flags: u32, memory: rawptr, bytes: int) -> rawptr ---;
  66. @(link_name="HeapFree") heap_free :: proc(h: Handle, flags: u32, memory: rawptr) -> Bool ---;
  67. @(link_name="GetProcessHeap") get_process_heap :: proc() -> Handle ---;
  68. @(link_name="LocalAlloc") local_alloc :: proc(flags: u32, bytes: int) -> rawptr ---;
  69. @(link_name="LocalReAlloc") local_realloc :: proc(mem: rawptr, bytes: int, flags: uint) -> rawptr ---;
  70. @(link_name="LocalFree") local_free :: proc(mem: rawptr) -> rawptr ---;
  71. @(link_name="FindFirstChangeNotificationA") find_first_change_notification_a :: proc(path: cstring, watch_subtree: Bool, filter: u32) -> Handle ---;
  72. @(link_name="FindNextChangeNotification") find_next_change_notification :: proc(h: Handle) -> Bool ---;
  73. @(link_name="FindCloseChangeNotification") find_close_change_notification :: proc(h: Handle) -> Bool ---;
  74. @(link_name="ReadDirectoryChangesW") read_directory_changes_w :: proc(dir: Handle, buf: rawptr, buf_length: u32,
  75. watch_subtree: Bool, notify_filter: u32,
  76. bytes_returned: ^u32, overlapped: ^Overlapped,
  77. completion: rawptr) -> Bool ---;
  78. @(link_name="WideCharToMultiByte") wide_char_to_multi_byte :: proc(code_page: u32, flags: u32,
  79. wchar_str: Wstring, wchar: i32,
  80. multi_str: cstring, multi: i32,
  81. default_char: cstring, used_default_char: ^Bool) -> i32 ---;
  82. @(link_name="MultiByteToWideChar") multi_byte_to_wide_char :: proc(code_page: u32, flags: u32,
  83. mb_str: cstring, mb: i32,
  84. wc_str: Wstring, wc: i32) -> i32 ---;
  85. @(link_name="CreateSemaphoreA") create_semaphore_a :: proc(attributes: ^Security_Attributes, initial_count, maximum_count: i32, name: cstring) -> Handle ---;
  86. @(link_name="CreateSemaphoreW") create_semaphore_w :: proc(attributes: ^Security_Attributes, initial_count, maximum_count: i32, name: cstring) -> Handle ---;
  87. @(link_name="ReleaseSemaphore") release_semaphore :: proc(semaphore: Handle, release_count: i32, previous_count: ^i32) -> Bool ---;
  88. @(link_name="WaitForSingleObject") wait_for_single_object :: proc(handle: Handle, milliseconds: u32) -> u32 ---;
  89. }
  90. // @(default_calling_convention = "c")
  91. foreign kernel32 {
  92. @(link_name="GetLastError") get_last_error :: proc() -> i32 ---;
  93. @(link_name="CloseHandle") close_handle :: proc(h: Handle) -> i32 ---;
  94. @(link_name="GetFileAttributesA") get_file_attributes_a :: proc(filename: cstring) -> u32 ---;
  95. @(link_name="GetFileAttributesW") get_file_attributes_w :: proc(filename: Wstring) -> u32 ---;
  96. @(link_name="GetFileAttributesExA") get_file_attributes_ex_a :: proc(filename: cstring, info_level_id: GET_FILEEX_INFO_LEVELS, file_info: ^File_Attribute_Data) -> Bool ---;
  97. @(link_name="GetFileAttributesExW") get_file_attributes_ex_w :: proc(filename: Wstring, info_level_id: GET_FILEEX_INFO_LEVELS, file_info: ^File_Attribute_Data) -> Bool ---;
  98. @(link_name="CompareFileTime") compare_file_time :: proc(a, b: ^Filetime) -> i32 ---;
  99. }
  100. @(default_calling_convention = "c")
  101. foreign kernel32 {
  102. @(link_name="InterlockedCompareExchange") interlocked_compare_exchange :: proc(dst: ^i32, exchange, comparand: i32) -> i32 ---;
  103. @(link_name="InterlockedExchange") interlocked_exchange :: proc(dst: ^i32, desired: i32) -> i32 ---;
  104. @(link_name="InterlockedExchangeAdd") interlocked_exchange_add :: proc(dst: ^i32, desired: i32) -> i32 ---;
  105. @(link_name="InterlockedAnd") interlocked_and :: proc(dst: ^i32, desired: i32) -> i32 ---;
  106. @(link_name="InterlockedOr") interlocked_or :: proc(dst: ^i32, desired: i32) -> i32 ---;
  107. @(link_name="InterlockedCompareExchange64") interlocked_compare_exchange64 :: proc(dst: ^i64, exchange, comparand: i64) -> i64 ---;
  108. @(link_name="InterlockedExchange64") interlocked_exchange64 :: proc(dst: ^i64, desired: i64) -> i64 ---;
  109. @(link_name="InterlockedExchangeAdd64") interlocked_exchange_add64 :: proc(dst: ^i64, desired: i64) -> i64 ---;
  110. @(link_name="InterlockedAnd64") interlocked_and64 :: proc(dst: ^i64, desired: i64) -> i64 ---;
  111. @(link_name="InterlockedOr64") interlocked_or64 :: proc(dst: ^i64, desired: i64) -> i64 ---;
  112. }
  113. @(default_calling_convention = "std")
  114. foreign kernel32 {
  115. @(link_name="_mm_pause") mm_pause :: proc() ---;
  116. @(link_name="ReadWriteBarrier") read_write_barrier :: proc() ---;
  117. @(link_name="WriteBarrier") write_barrier :: proc() ---;
  118. @(link_name="ReadBarrier") read_barrier :: proc() ---;
  119. @(link_name="CreateThread")
  120. create_thread :: proc(thread_attributes: ^Security_Attributes, stack_size: int, start_routine: rawptr,
  121. parameter: rawptr, creation_flags: u32, thread_id: ^u32) -> Handle ---;
  122. @(link_name="ResumeThread") resume_thread :: proc(thread: Handle) -> u32 ---;
  123. @(link_name="GetThreadPriority") get_thread_priority :: proc(thread: Handle) -> i32 ---;
  124. @(link_name="SetThreadPriority") set_thread_priority :: proc(thread: Handle, priority: i32) -> Bool ---;
  125. @(link_name="GetExitCodeThread") get_exit_code_thread :: proc(thread: Handle, exit_code: ^u32) -> Bool ---;
  126. @(link_name="TerminateThread") terminate_thread :: proc(thread: Handle, exit_code: u32) -> Bool ---;
  127. @(link_name="InitializeCriticalSection") initialize_critical_section :: proc(critical_section: ^Critical_Section) ---;
  128. @(link_name="InitializeCriticalSectionAndSpinCount") initialize_critical_section_and_spin_count :: proc(critical_section: ^Critical_Section, spin_count: u32) ---;
  129. @(link_name="DeleteCriticalSection") delete_critical_section :: proc(critical_section: ^Critical_Section) ---;
  130. @(link_name="SetCriticalSectionSpinCount") set_critical_section_spin_count :: proc(critical_section: ^Critical_Section, spin_count: u32) -> u32 ---;
  131. @(link_name="TryEnterCriticalSection") try_enter_critical_section :: proc(critical_section: ^Critical_Section) -> Bool ---;
  132. @(link_name="EnterCriticalSection") enter_critical_section :: proc(critical_section: ^Critical_Section) ---;
  133. @(link_name="LeaveCriticalSection") leave_critical_section :: proc(critical_section: ^Critical_Section) ---;
  134. @(link_name="CreateEventA") create_event_a :: proc(event_attributes: ^Security_Attributes, manual_reset, initial_state: Bool, name: cstring) -> Handle ---;
  135. @(link_name="CreateEventW") create_event_w :: proc(event_attributes: ^Security_Attributes, manual_reset, initial_state: Bool, name: Wstring) -> Handle ---;
  136. @(link_name="PulseEvent") pulse_event :: proc(event: Handle) -> Bool ---;
  137. @(link_name="SetEvent") set_event :: proc(event: Handle) -> Bool ---;
  138. @(link_name="ResetEvent") reset_event :: proc(event: Handle) -> Bool ---;
  139. @(link_name="LoadLibraryA") load_library_a :: proc(c_str: cstring) -> Hmodule ---;
  140. @(link_name="LoadLibraryW") load_library_w :: proc(c_str: Wstring) -> Hmodule ---;
  141. @(link_name="FreeLibrary") free_library :: proc(h: Hmodule) -> Bool ---;
  142. @(link_name="GetProcAddress") get_proc_address :: proc(h: Hmodule, c_str: cstring) -> rawptr ---;
  143. }
  144. Memory_Basic_Information :: struct {
  145. base_address: rawptr,
  146. allocation_base: rawptr,
  147. allocation_protect: u32,
  148. region_size: uint,
  149. state: u32,
  150. protect: u32,
  151. type: u32,
  152. }
  153. @(default_calling_convention = "std")
  154. foreign kernel32 {
  155. @(link_name="VirtualAlloc") virtual_alloc :: proc(address: rawptr, size: uint, allocation_type: u32, protect: u32) -> rawptr ---
  156. @(link_name="VirtualAllocEx") virtual_alloc_ex :: proc(process: Handle, address: rawptr, size: uint, allocation_type: u32, protect: u32) -> rawptr ---
  157. @(link_name="VirtualFree") virtual_free :: proc(address: rawptr, size: uint, free_type: u32) -> Bool ---
  158. @(link_name="VirtualLock") virtual_lock :: proc(address: rawptr, size: uint) -> Bool ---
  159. @(link_name="VirtualProtect") virtual_protect :: proc(address: rawptr, size: uint, new_protect: u32, old_protect: ^u32) -> Bool ---
  160. @(link_name="VirtualQuery") virtual_query :: proc(address: rawptr, buffer: ^Memory_Basic_Information, length: uint) -> uint ---
  161. }
  162. MEM_COMMIT :: 0x00001000;
  163. MEM_RESERVE :: 0x00002000;
  164. MEM_DECOMMIT :: 0x00004000;
  165. MEM_RELEASE :: 0x00008000;
  166. MEM_RESET :: 0x00080000;
  167. MEM_RESET_UNDO :: 0x01000000;
  168. MEM_LARGE_PAGES :: 0x20000000;
  169. MEM_PHYSICAL :: 0x00400000;
  170. MEM_TOP_DOWN :: 0x00100000;
  171. MEM_WRITE_WATCH :: 0x00200000;
  172. PAGE_NOACCESS :: 0x01;
  173. PAGE_READONLY :: 0x02;
  174. PAGE_READWRITE :: 0x04;
  175. PAGE_WRITECOPY :: 0x08;
  176. PAGE_EXECUTE :: 0x10;
  177. PAGE_EXECUTE_READ :: 0x20;
  178. PAGE_EXECUTE_READWRITE :: 0x40;
  179. PAGE_EXECUTE_WRITECOPY :: 0x80;