winlean.nim 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module implements a small wrapper for some needed Win API procedures,
  10. ## so that the Nim compiler does not depend on the huge Windows module.
  11. {.deadCodeElim: on.} # dce option deprecated
  12. import dynlib
  13. {.passC: "-DWIN32_LEAN_AND_MEAN".}
  14. const
  15. useWinUnicode* = not defined(useWinAnsi)
  16. when useWinUnicode:
  17. type WinChar* = Utf16Char
  18. else:
  19. type WinChar* = char
  20. type
  21. Handle* = int
  22. LONG* = int32
  23. ULONG* = int32
  24. PULONG* = ptr int
  25. WINBOOL* = int32
  26. DWORD* = int32
  27. PDWORD* = ptr DWORD
  28. LPINT* = ptr int32
  29. ULONG_PTR* = uint
  30. PULONG_PTR* = ptr uint
  31. HDC* = Handle
  32. HGLRC* = Handle
  33. SECURITY_ATTRIBUTES* {.final, pure.} = object
  34. nLength*: int32
  35. lpSecurityDescriptor*: pointer
  36. bInheritHandle*: WINBOOL
  37. STARTUPINFO* {.final, pure.} = object
  38. cb*: int32
  39. lpReserved*: cstring
  40. lpDesktop*: cstring
  41. lpTitle*: cstring
  42. dwX*: int32
  43. dwY*: int32
  44. dwXSize*: int32
  45. dwYSize*: int32
  46. dwXCountChars*: int32
  47. dwYCountChars*: int32
  48. dwFillAttribute*: int32
  49. dwFlags*: int32
  50. wShowWindow*: int16
  51. cbReserved2*: int16
  52. lpReserved2*: pointer
  53. hStdInput*: Handle
  54. hStdOutput*: Handle
  55. hStdError*: Handle
  56. PROCESS_INFORMATION* {.final, pure.} = object
  57. hProcess*: Handle
  58. hThread*: Handle
  59. dwProcessId*: int32
  60. dwThreadId*: int32
  61. FILETIME* {.final, pure.} = object ## CANNOT BE int64 BECAUSE OF ALIGNMENT
  62. dwLowDateTime*: DWORD
  63. dwHighDateTime*: DWORD
  64. BY_HANDLE_FILE_INFORMATION* {.final, pure.} = object
  65. dwFileAttributes*: DWORD
  66. ftCreationTime*: FILETIME
  67. ftLastAccessTime*: FILETIME
  68. ftLastWriteTime*: FILETIME
  69. dwVolumeSerialNumber*: DWORD
  70. nFileSizeHigh*: DWORD
  71. nFileSizeLow*: DWORD
  72. nNumberOfLinks*: DWORD
  73. nFileIndexHigh*: DWORD
  74. nFileIndexLow*: DWORD
  75. OSVERSIONINFO* {.final, pure.} = object
  76. dwOSVersionInfoSize*: DWORD
  77. dwMajorVersion*: DWORD
  78. dwMinorVersion*: DWORD
  79. dwBuildNumber*: DWORD
  80. dwPlatformId*: DWORD
  81. szCSDVersion*: array[0..127, WinChar]
  82. const
  83. STARTF_USESHOWWINDOW* = 1'i32
  84. STARTF_USESTDHANDLES* = 256'i32
  85. HIGH_PRIORITY_CLASS* = 128'i32
  86. IDLE_PRIORITY_CLASS* = 64'i32
  87. NORMAL_PRIORITY_CLASS* = 32'i32
  88. REALTIME_PRIORITY_CLASS* = 256'i32
  89. WAIT_OBJECT_0* = 0'i32
  90. WAIT_TIMEOUT* = 0x00000102'i32
  91. WAIT_FAILED* = 0xFFFFFFFF'i32
  92. INFINITE* = -1'i32
  93. STILL_ACTIVE* = 0x00000103'i32
  94. STD_INPUT_HANDLE* = -10'i32
  95. STD_OUTPUT_HANDLE* = -11'i32
  96. STD_ERROR_HANDLE* = -12'i32
  97. DETACHED_PROCESS* = 8'i32
  98. SW_SHOWNORMAL* = 1'i32
  99. INVALID_HANDLE_VALUE* = Handle(-1)
  100. CREATE_UNICODE_ENVIRONMENT* = 1024'i32
  101. PIPE_ACCESS_DUPLEX* = 0x00000003'i32
  102. PIPE_ACCESS_INBOUND* = 1'i32
  103. PIPE_ACCESS_OUTBOUND* = 2'i32
  104. PIPE_NOWAIT* = 0x00000001'i32
  105. SYNCHRONIZE* = 0x00100000'i32
  106. CREATE_NO_WINDOW* = 0x08000000'i32
  107. when useWinUnicode:
  108. proc getVersionExW*(lpVersionInfo: ptr OSVERSIONINFO): WINBOOL {.stdcall, dynlib: "kernel32", importc: "GetVersionExW".}
  109. else:
  110. proc getVersionExA*(lpVersionInfo: ptr OSVERSIONINFO): WINBOOL {.stdcall, dynlib: "kernel32", importc: "GetVersionExA".}
  111. proc getVersion*(): DWORD {.stdcall, dynlib: "kernel32", importc: "GetVersion".}
  112. proc closeHandle*(hObject: Handle): WINBOOL {.stdcall, dynlib: "kernel32",
  113. importc: "CloseHandle".}
  114. proc readFile*(hFile: Handle, Buffer: pointer, nNumberOfBytesToRead: int32,
  115. lpNumberOfBytesRead: ptr int32, lpOverlapped: pointer): WINBOOL{.
  116. stdcall, dynlib: "kernel32", importc: "ReadFile".}
  117. proc writeFile*(hFile: Handle, Buffer: pointer, nNumberOfBytesToWrite: int32,
  118. lpNumberOfBytesWritten: ptr int32,
  119. lpOverlapped: pointer): WINBOOL{.
  120. stdcall, dynlib: "kernel32", importc: "WriteFile".}
  121. proc createPipe*(hReadPipe, hWritePipe: var Handle,
  122. lpPipeAttributes: var SECURITY_ATTRIBUTES,
  123. nSize: int32): WINBOOL{.
  124. stdcall, dynlib: "kernel32", importc: "CreatePipe".}
  125. proc createNamedPipe*(lpName: WideCString,
  126. dwOpenMode, dwPipeMode, nMaxInstances, nOutBufferSize,
  127. nInBufferSize, nDefaultTimeOut: int32,
  128. lpSecurityAttributes: ptr SECURITY_ATTRIBUTES): Handle {.
  129. stdcall, dynlib: "kernel32", importc: "CreateNamedPipeW".}
  130. proc peekNamedPipe*(hNamedPipe: Handle, lpBuffer: pointer=nil,
  131. nBufferSize: int32 = 0,
  132. lpBytesRead: ptr int32 = nil,
  133. lpTotalBytesAvail: ptr int32 = nil,
  134. lpBytesLeftThisMessage: ptr int32 = nil): bool {.
  135. stdcall, dynlib: "kernel32", importc: "PeekNamedPipe".}
  136. when useWinUnicode:
  137. proc createProcessW*(lpApplicationName, lpCommandLine: WideCString,
  138. lpProcessAttributes: ptr SECURITY_ATTRIBUTES,
  139. lpThreadAttributes: ptr SECURITY_ATTRIBUTES,
  140. bInheritHandles: WINBOOL, dwCreationFlags: int32,
  141. lpEnvironment, lpCurrentDirectory: WideCString,
  142. lpStartupInfo: var STARTUPINFO,
  143. lpProcessInformation: var PROCESS_INFORMATION): WINBOOL{.
  144. stdcall, dynlib: "kernel32", importc: "CreateProcessW".}
  145. else:
  146. proc createProcessA*(lpApplicationName, lpCommandLine: cstring,
  147. lpProcessAttributes: ptr SECURITY_ATTRIBUTES,
  148. lpThreadAttributes: ptr SECURITY_ATTRIBUTES,
  149. bInheritHandles: WINBOOL, dwCreationFlags: int32,
  150. lpEnvironment: pointer, lpCurrentDirectory: cstring,
  151. lpStartupInfo: var STARTUPINFO,
  152. lpProcessInformation: var PROCESS_INFORMATION): WINBOOL{.
  153. stdcall, dynlib: "kernel32", importc: "CreateProcessA".}
  154. proc suspendThread*(hThread: Handle): int32 {.stdcall, dynlib: "kernel32",
  155. importc: "SuspendThread".}
  156. proc resumeThread*(hThread: Handle): int32 {.stdcall, dynlib: "kernel32",
  157. importc: "ResumeThread".}
  158. proc waitForSingleObject*(hHandle: Handle, dwMilliseconds: int32): int32 {.
  159. stdcall, dynlib: "kernel32", importc: "WaitForSingleObject".}
  160. proc terminateProcess*(hProcess: Handle, uExitCode: int): WINBOOL {.stdcall,
  161. dynlib: "kernel32", importc: "TerminateProcess".}
  162. proc getExitCodeProcess*(hProcess: Handle, lpExitCode: var int32): WINBOOL {.
  163. stdcall, dynlib: "kernel32", importc: "GetExitCodeProcess".}
  164. proc getStdHandle*(nStdHandle: int32): Handle {.stdcall, dynlib: "kernel32",
  165. importc: "GetStdHandle".}
  166. proc setStdHandle*(nStdHandle: int32, hHandle: Handle): WINBOOL {.stdcall,
  167. dynlib: "kernel32", importc: "SetStdHandle".}
  168. proc flushFileBuffers*(hFile: Handle): WINBOOL {.stdcall, dynlib: "kernel32",
  169. importc: "FlushFileBuffers".}
  170. proc getLastError*(): int32 {.importc: "GetLastError",
  171. stdcall, dynlib: "kernel32".}
  172. proc setLastError*(error: int32) {.importc: "SetLastError",
  173. stdcall, dynlib: "kernel32".}
  174. when useWinUnicode:
  175. proc formatMessageW*(dwFlags: int32, lpSource: pointer,
  176. dwMessageId, dwLanguageId: int32,
  177. lpBuffer: pointer, nSize: int32,
  178. Arguments: pointer): int32 {.
  179. importc: "FormatMessageW", stdcall, dynlib: "kernel32".}
  180. else:
  181. proc formatMessageA*(dwFlags: int32, lpSource: pointer,
  182. dwMessageId, dwLanguageId: int32,
  183. lpBuffer: pointer, nSize: int32,
  184. Arguments: pointer): int32 {.
  185. importc: "FormatMessageA", stdcall, dynlib: "kernel32".}
  186. proc localFree*(p: pointer) {.
  187. importc: "LocalFree", stdcall, dynlib: "kernel32".}
  188. when useWinUnicode:
  189. proc getCurrentDirectoryW*(nBufferLength: int32,
  190. lpBuffer: WideCString): int32 {.
  191. importc: "GetCurrentDirectoryW", dynlib: "kernel32", stdcall.}
  192. proc setCurrentDirectoryW*(lpPathName: WideCString): int32 {.
  193. importc: "SetCurrentDirectoryW", dynlib: "kernel32", stdcall.}
  194. proc createDirectoryW*(pathName: WideCString, security: pointer=nil): int32 {.
  195. importc: "CreateDirectoryW", dynlib: "kernel32", stdcall.}
  196. proc removeDirectoryW*(lpPathName: WideCString): int32 {.
  197. importc: "RemoveDirectoryW", dynlib: "kernel32", stdcall.}
  198. proc setEnvironmentVariableW*(lpName, lpValue: WideCString): int32 {.
  199. stdcall, dynlib: "kernel32", importc: "SetEnvironmentVariableW".}
  200. proc getModuleFileNameW*(handle: Handle, buf: WideCString,
  201. size: int32): int32 {.importc: "GetModuleFileNameW",
  202. dynlib: "kernel32", stdcall.}
  203. else:
  204. proc getCurrentDirectoryA*(nBufferLength: int32, lpBuffer: cstring): int32 {.
  205. importc: "GetCurrentDirectoryA", dynlib: "kernel32", stdcall.}
  206. proc setCurrentDirectoryA*(lpPathName: cstring): int32 {.
  207. importc: "SetCurrentDirectoryA", dynlib: "kernel32", stdcall.}
  208. proc createDirectoryA*(pathName: cstring, security: pointer=nil): int32 {.
  209. importc: "CreateDirectoryA", dynlib: "kernel32", stdcall.}
  210. proc removeDirectoryA*(lpPathName: cstring): int32 {.
  211. importc: "RemoveDirectoryA", dynlib: "kernel32", stdcall.}
  212. proc setEnvironmentVariableA*(lpName, lpValue: cstring): int32 {.
  213. stdcall, dynlib: "kernel32", importc: "SetEnvironmentVariableA".}
  214. proc getModuleFileNameA*(handle: Handle, buf: cstring, size: int32): int32 {.
  215. importc: "GetModuleFileNameA", dynlib: "kernel32", stdcall.}
  216. when useWinUnicode:
  217. proc createSymbolicLinkW*(lpSymlinkFileName, lpTargetFileName: WideCString,
  218. flags: DWORD): int32 {.
  219. importc:"CreateSymbolicLinkW", dynlib: "kernel32", stdcall.}
  220. proc createHardLinkW*(lpFileName, lpExistingFileName: WideCString,
  221. security: pointer=nil): int32 {.
  222. importc:"CreateHardLinkW", dynlib: "kernel32", stdcall.}
  223. else:
  224. proc createSymbolicLinkA*(lpSymlinkFileName, lpTargetFileName: cstring,
  225. flags: DWORD): int32 {.
  226. importc:"CreateSymbolicLinkA", dynlib: "kernel32", stdcall.}
  227. proc createHardLinkA*(lpFileName, lpExistingFileName: cstring,
  228. security: pointer=nil): int32 {.
  229. importc:"CreateHardLinkA", dynlib: "kernel32", stdcall.}
  230. const
  231. FILE_ATTRIBUTE_READONLY* = 0x00000001'i32
  232. FILE_ATTRIBUTE_HIDDEN* = 0x00000002'i32
  233. FILE_ATTRIBUTE_SYSTEM* = 0x00000004'i32
  234. FILE_ATTRIBUTE_DIRECTORY* = 0x00000010'i32
  235. FILE_ATTRIBUTE_ARCHIVE* = 0x00000020'i32
  236. FILE_ATTRIBUTE_DEVICE* = 0x00000040'i32
  237. FILE_ATTRIBUTE_NORMAL* = 0x00000080'i32
  238. FILE_ATTRIBUTE_TEMPORARY* = 0x00000100'i32
  239. FILE_ATTRIBUTE_SPARSE_FILE* = 0x00000200'i32
  240. FILE_ATTRIBUTE_REPARSE_POINT* = 0x00000400'i32
  241. FILE_ATTRIBUTE_COMPRESSED* = 0x00000800'i32
  242. FILE_ATTRIBUTE_OFFLINE* = 0x00001000'i32
  243. FILE_ATTRIBUTE_NOT_CONTENT_INDEXED* = 0x00002000'i32
  244. FILE_FLAG_FIRST_PIPE_INSTANCE* = 0x00080000'i32
  245. FILE_FLAG_OPEN_NO_RECALL* = 0x00100000'i32
  246. FILE_FLAG_OPEN_REPARSE_POINT* = 0x00200000'i32
  247. FILE_FLAG_POSIX_SEMANTICS* = 0x01000000'i32
  248. FILE_FLAG_BACKUP_SEMANTICS* = 0x02000000'i32
  249. FILE_FLAG_DELETE_ON_CLOSE* = 0x04000000'i32
  250. FILE_FLAG_SEQUENTIAL_SCAN* = 0x08000000'i32
  251. FILE_FLAG_RANDOM_ACCESS* = 0x10000000'i32
  252. FILE_FLAG_NO_BUFFERING* = 0x20000000'i32
  253. FILE_FLAG_OVERLAPPED* = 0x40000000'i32
  254. FILE_FLAG_WRITE_THROUGH* = 0x80000000'i32
  255. MAX_PATH* = 260
  256. MOVEFILE_COPY_ALLOWED* = 0x2'i32
  257. MOVEFILE_CREATE_HARDLINK* = 0x10'i32
  258. MOVEFILE_DELAY_UNTIL_REBOOT* = 0x4'i32
  259. MOVEFILE_FAIL_IF_NOT_TRACKABLE* = 0x20'i32
  260. MOVEFILE_REPLACE_EXISTING* = 0x1'i32
  261. MOVEFILE_WRITE_THROUGH* = 0x8'i32
  262. type
  263. WIN32_FIND_DATA* {.pure.} = object
  264. dwFileAttributes*: int32
  265. ftCreationTime*: FILETIME
  266. ftLastAccessTime*: FILETIME
  267. ftLastWriteTime*: FILETIME
  268. nFileSizeHigh*: int32
  269. nFileSizeLow*: int32
  270. dwReserved0: int32
  271. dwReserved1: int32
  272. cFileName*: array[0..(MAX_PATH) - 1, WinChar]
  273. cAlternateFileName*: array[0..13, WinChar]
  274. when useWinUnicode:
  275. proc findFirstFileW*(lpFileName: WideCString,
  276. lpFindFileData: var WIN32_FIND_DATA): Handle {.
  277. stdcall, dynlib: "kernel32", importc: "FindFirstFileW".}
  278. proc findNextFileW*(hFindFile: Handle,
  279. lpFindFileData: var WIN32_FIND_DATA): int32 {.
  280. stdcall, dynlib: "kernel32", importc: "FindNextFileW".}
  281. else:
  282. proc findFirstFileA*(lpFileName: cstring,
  283. lpFindFileData: var WIN32_FIND_DATA): Handle {.
  284. stdcall, dynlib: "kernel32", importc: "FindFirstFileA".}
  285. proc findNextFileA*(hFindFile: Handle,
  286. lpFindFileData: var WIN32_FIND_DATA): int32 {.
  287. stdcall, dynlib: "kernel32", importc: "FindNextFileA".}
  288. proc findClose*(hFindFile: Handle) {.stdcall, dynlib: "kernel32",
  289. importc: "FindClose".}
  290. when useWinUnicode:
  291. proc getFullPathNameW*(lpFileName: WideCString, nBufferLength: int32,
  292. lpBuffer: WideCString,
  293. lpFilePart: var WideCString): int32 {.
  294. stdcall, dynlib: "kernel32",
  295. importc: "GetFullPathNameW".}
  296. proc getFileAttributesW*(lpFileName: WideCString): int32 {.
  297. stdcall, dynlib: "kernel32",
  298. importc: "GetFileAttributesW".}
  299. proc setFileAttributesW*(lpFileName: WideCString,
  300. dwFileAttributes: int32): WINBOOL {.
  301. stdcall, dynlib: "kernel32", importc: "SetFileAttributesW".}
  302. proc copyFileW*(lpExistingFileName, lpNewFileName: WideCString,
  303. bFailIfExists: WINBOOL): WINBOOL {.
  304. importc: "CopyFileW", stdcall, dynlib: "kernel32".}
  305. proc moveFileW*(lpExistingFileName, lpNewFileName: WideCString): WINBOOL {.
  306. importc: "MoveFileW", stdcall, dynlib: "kernel32".}
  307. proc moveFileExW*(lpExistingFileName, lpNewFileName: WideCString,
  308. flags: DWORD): WINBOOL {.
  309. importc: "MoveFileExW", stdcall, dynlib: "kernel32".}
  310. proc getEnvironmentStringsW*(): WideCString {.
  311. stdcall, dynlib: "kernel32", importc: "GetEnvironmentStringsW".}
  312. proc freeEnvironmentStringsW*(para1: WideCString): int32 {.
  313. stdcall, dynlib: "kernel32", importc: "FreeEnvironmentStringsW".}
  314. proc getCommandLineW*(): WideCString {.importc: "GetCommandLineW",
  315. stdcall, dynlib: "kernel32".}
  316. else:
  317. proc getFullPathNameA*(lpFileName: cstring, nBufferLength: int32,
  318. lpBuffer: cstring, lpFilePart: var cstring): int32 {.
  319. stdcall, dynlib: "kernel32",
  320. importc: "GetFullPathNameA".}
  321. proc getFileAttributesA*(lpFileName: cstring): int32 {.
  322. stdcall, dynlib: "kernel32",
  323. importc: "GetFileAttributesA".}
  324. proc setFileAttributesA*(lpFileName: cstring,
  325. dwFileAttributes: int32): WINBOOL {.
  326. stdcall, dynlib: "kernel32", importc: "SetFileAttributesA".}
  327. proc copyFileA*(lpExistingFileName, lpNewFileName: cstring,
  328. bFailIfExists: cint): cint {.
  329. importc: "CopyFileA", stdcall, dynlib: "kernel32".}
  330. proc moveFileA*(lpExistingFileName, lpNewFileName: cstring): WINBOOL {.
  331. importc: "MoveFileA", stdcall, dynlib: "kernel32".}
  332. proc moveFileExA*(lpExistingFileName, lpNewFileName: cstring,
  333. flags: DWORD): WINBOOL {.
  334. importc: "MoveFileExA", stdcall, dynlib: "kernel32".}
  335. proc getEnvironmentStringsA*(): cstring {.
  336. stdcall, dynlib: "kernel32", importc: "GetEnvironmentStringsA".}
  337. proc freeEnvironmentStringsA*(para1: cstring): int32 {.
  338. stdcall, dynlib: "kernel32", importc: "FreeEnvironmentStringsA".}
  339. proc getCommandLineA*(): cstring {.
  340. importc: "GetCommandLineA", stdcall, dynlib: "kernel32".}
  341. proc rdFileTime*(f: FILETIME): int64 =
  342. result = ze64(f.dwLowDateTime) or (ze64(f.dwHighDateTime) shl 32)
  343. proc rdFileSize*(f: WIN32_FIND_DATA): int64 =
  344. result = ze64(f.nFileSizeLow) or (ze64(f.nFileSizeHigh) shl 32)
  345. proc getSystemTimeAsFileTime*(lpSystemTimeAsFileTime: var FILETIME) {.
  346. importc: "GetSystemTimeAsFileTime", dynlib: "kernel32", stdcall.}
  347. proc sleep*(dwMilliseconds: int32){.stdcall, dynlib: "kernel32",
  348. importc: "Sleep".}
  349. when useWinUnicode:
  350. proc shellExecuteW*(HWND: Handle, lpOperation, lpFile,
  351. lpParameters, lpDirectory: WideCString,
  352. nShowCmd: int32): Handle{.
  353. stdcall, dynlib: "shell32.dll", importc: "ShellExecuteW".}
  354. else:
  355. proc shellExecuteA*(HWND: Handle, lpOperation, lpFile,
  356. lpParameters, lpDirectory: cstring,
  357. nShowCmd: int32): Handle{.
  358. stdcall, dynlib: "shell32.dll", importc: "ShellExecuteA".}
  359. proc getFileInformationByHandle*(hFile: Handle,
  360. lpFileInformation: ptr BY_HANDLE_FILE_INFORMATION): WINBOOL{.
  361. stdcall, dynlib: "kernel32", importc: "GetFileInformationByHandle".}
  362. const
  363. WSADESCRIPTION_LEN* = 256
  364. WSASYS_STATUS_LEN* = 128
  365. FD_SETSIZE* = 64
  366. MSG_PEEK* = 2
  367. INADDR_ANY* = 0'u32
  368. INADDR_LOOPBACK* = 0x7F000001
  369. INADDR_BROADCAST* = -1
  370. INADDR_NONE* = -1
  371. ws2dll = "Ws2_32.dll"
  372. proc wsaGetLastError*(): cint {.importc: "WSAGetLastError", dynlib: ws2dll.}
  373. type
  374. SocketHandle* = distinct int
  375. type
  376. WSAData* {.importc: "WSADATA", header: "winsock2.h".} = object
  377. wVersion, wHighVersion: int16
  378. szDescription: array[0..WSADESCRIPTION_LEN, char]
  379. szSystemStatus: array[0..WSASYS_STATUS_LEN, char]
  380. iMaxSockets, iMaxUdpDg: int16
  381. lpVendorInfo: cstring
  382. SockAddr* {.importc: "SOCKADDR", header: "winsock2.h".} = object
  383. sa_family*: uint16
  384. sa_data*: array[0..13, char]
  385. PSockAddr = ptr SockAddr
  386. InAddr* {.importc: "IN_ADDR", header: "winsock2.h".} = object
  387. s_addr*: uint32 # IP address
  388. Sockaddr_in* {.importc: "SOCKADDR_IN",
  389. header: "winsock2.h".} = object
  390. sin_family*: uint16
  391. sin_port*: uint16
  392. sin_addr*: InAddr
  393. sin_zero*: array[0..7, char]
  394. In6_addr* {.importc: "IN6_ADDR", header: "winsock2.h".} = object
  395. bytes* {.importc: "u.Byte".}: array[0..15, char]
  396. Sockaddr_in6* {.importc: "SOCKADDR_IN6",
  397. header: "ws2tcpip.h".} = object
  398. sin6_family*: uint16
  399. sin6_port*: uint16
  400. sin6_flowinfo*: int32 # unsigned
  401. sin6_addr*: In6_addr
  402. sin6_scope_id*: int32 # unsigned
  403. Sockaddr_in6_old* = object
  404. sin6_family*: uint16
  405. sin6_port*: int16 # unsigned
  406. sin6_flowinfo*: int32 # unsigned
  407. sin6_addr*: In6_addr
  408. Sockaddr_storage* {.importc: "SOCKADDR_STORAGE",
  409. header: "winsock2.h".} = object
  410. ss_family*: uint16
  411. ss_pad1: array[6, byte]
  412. ss_align: int64
  413. ss_pad2: array[112, byte]
  414. Servent* = object
  415. s_name*: cstring
  416. s_aliases*: cstringArray
  417. when defined(cpu64):
  418. s_proto*: cstring
  419. s_port*: int16
  420. else:
  421. s_port*: int16
  422. s_proto*: cstring
  423. Hostent* = object
  424. h_name*: cstring
  425. h_aliases*: cstringArray
  426. h_addrtype*: int16
  427. h_length*: int16
  428. h_addr_list*: cstringArray
  429. TFdSet* = object
  430. fd_count*: cint # unsigned
  431. fd_array*: array[0..FD_SETSIZE-1, SocketHandle]
  432. AddrInfo* = object
  433. ai_flags*: cint ## Input flags.
  434. ai_family*: cint ## Address family of socket.
  435. ai_socktype*: cint ## Socket type.
  436. ai_protocol*: cint ## Protocol of socket.
  437. ai_addrlen*: csize ## Length of socket address.
  438. ai_canonname*: cstring ## Canonical name of service location.
  439. ai_addr*: ptr SockAddr ## Socket address of socket.
  440. ai_next*: ptr AddrInfo ## Pointer to next in list.
  441. SockLen* = cuint
  442. when defined(cpp):
  443. type
  444. Timeval* {.importc: "timeval", header: "<time.h>".} = object
  445. tv_sec*, tv_usec*: int32
  446. else:
  447. type
  448. Timeval* = object
  449. tv_sec*, tv_usec*: int32
  450. var
  451. SOMAXCONN* {.importc, header: "winsock2.h".}: cint
  452. INVALID_SOCKET* {.importc, header: "winsock2.h".}: SocketHandle
  453. SOL_SOCKET* {.importc, header: "winsock2.h".}: cint
  454. SO_DEBUG* {.importc, header: "winsock2.h".}: cint ## turn on debugging info recording
  455. SO_ACCEPTCONN* {.importc, header: "winsock2.h".}: cint # socket has had listen()
  456. SO_REUSEADDR* {.importc, header: "winsock2.h".}: cint # allow local address reuse
  457. SO_REUSEPORT* {.importc: "SO_REUSEADDR", header: "winsock2.h".}: cint # allow port reuse. Since Windows does not really support it, mapped to SO_REUSEADDR. This shouldn't cause problems.
  458. SO_KEEPALIVE* {.importc, header: "winsock2.h".}: cint # keep connections alive
  459. SO_DONTROUTE* {.importc, header: "winsock2.h".}: cint # just use interface addresses
  460. SO_BROADCAST* {.importc, header: "winsock2.h".}: cint # permit sending of broadcast msgs
  461. SO_USELOOPBACK* {.importc, header: "winsock2.h".}: cint # bypass hardware when possible
  462. SO_LINGER* {.importc, header: "winsock2.h".}: cint # linger on close if data present
  463. SO_OOBINLINE* {.importc, header: "winsock2.h".}: cint # leave received OOB data in line
  464. SO_DONTLINGER* {.importc, header: "winsock2.h".}: cint
  465. SO_EXCLUSIVEADDRUSE* {.importc, header: "winsock2.h".}: cint # disallow local address reuse
  466. SO_ERROR* {.importc, header: "winsock2.h".}: cint
  467. TCP_NODELAY* {.importc, header: "winsock2.h".}: cint
  468. proc `==`*(x, y: SocketHandle): bool {.borrow.}
  469. proc getservbyname*(name, proto: cstring): ptr Servent {.
  470. stdcall, importc: "getservbyname", dynlib: ws2dll.}
  471. proc getservbyport*(port: cint, proto: cstring): ptr Servent {.
  472. stdcall, importc: "getservbyport", dynlib: ws2dll.}
  473. proc gethostbyaddr*(ip: ptr InAddr, len: cuint, theType: cint): ptr Hostent {.
  474. stdcall, importc: "gethostbyaddr", dynlib: ws2dll.}
  475. proc gethostbyname*(name: cstring): ptr Hostent {.
  476. stdcall, importc: "gethostbyname", dynlib: ws2dll.}
  477. proc gethostname*(hostname: cstring, len: cint): cint {.
  478. stdcall, importc: "gethostname", dynlib: ws2dll.}
  479. proc socket*(af, typ, protocol: cint): SocketHandle {.
  480. stdcall, importc: "socket", dynlib: ws2dll.}
  481. proc closesocket*(s: SocketHandle): cint {.
  482. stdcall, importc: "closesocket", dynlib: ws2dll.}
  483. proc accept*(s: SocketHandle, a: ptr SockAddr, addrlen: ptr SockLen): SocketHandle {.
  484. stdcall, importc: "accept", dynlib: ws2dll.}
  485. proc bindSocket*(s: SocketHandle, name: ptr SockAddr, namelen: SockLen): cint {.
  486. stdcall, importc: "bind", dynlib: ws2dll.}
  487. proc connect*(s: SocketHandle, name: ptr SockAddr, namelen: SockLen): cint {.
  488. stdcall, importc: "connect", dynlib: ws2dll.}
  489. proc getsockname*(s: SocketHandle, name: ptr SockAddr,
  490. namelen: ptr SockLen): cint {.
  491. stdcall, importc: "getsockname", dynlib: ws2dll.}
  492. proc getpeername*(s: SocketHandle, name: ptr SockAddr,
  493. namelen: ptr SockLen): cint {.
  494. stdcall, importc, dynlib: ws2dll.}
  495. proc getsockopt*(s: SocketHandle, level, optname: cint, optval: pointer,
  496. optlen: ptr SockLen): cint {.
  497. stdcall, importc: "getsockopt", dynlib: ws2dll.}
  498. proc setsockopt*(s: SocketHandle, level, optname: cint, optval: pointer,
  499. optlen: SockLen): cint {.
  500. stdcall, importc: "setsockopt", dynlib: ws2dll.}
  501. proc listen*(s: SocketHandle, backlog: cint): cint {.
  502. stdcall, importc: "listen", dynlib: ws2dll.}
  503. proc recv*(s: SocketHandle, buf: pointer, len, flags: cint): cint {.
  504. stdcall, importc: "recv", dynlib: ws2dll.}
  505. proc recvfrom*(s: SocketHandle, buf: cstring, len, flags: cint,
  506. fromm: ptr SockAddr, fromlen: ptr SockLen): cint {.
  507. stdcall, importc: "recvfrom", dynlib: ws2dll.}
  508. proc select*(nfds: cint, readfds, writefds, exceptfds: ptr TFdSet,
  509. timeout: ptr Timeval): cint {.
  510. stdcall, importc: "select", dynlib: ws2dll.}
  511. proc send*(s: SocketHandle, buf: pointer, len, flags: cint): cint {.
  512. stdcall, importc: "send", dynlib: ws2dll.}
  513. proc sendto*(s: SocketHandle, buf: pointer, len, flags: cint,
  514. to: ptr SockAddr, tolen: SockLen): cint {.
  515. stdcall, importc: "sendto", dynlib: ws2dll.}
  516. proc shutdown*(s: SocketHandle, how: cint): cint {.
  517. stdcall, importc: "shutdown", dynlib: ws2dll.}
  518. proc getnameinfo*(a1: ptr SockAddr, a2: SockLen,
  519. a3: cstring, a4: SockLen, a5: cstring,
  520. a6: SockLen, a7: cint): cint {.
  521. stdcall, importc: "getnameinfo", dynlib: ws2dll.}
  522. proc inet_addr*(cp: cstring): uint32 {.
  523. stdcall, importc: "inet_addr", dynlib: ws2dll.}
  524. proc WSAFDIsSet(s: SocketHandle, set: var TFdSet): bool {.
  525. stdcall, importc: "__WSAFDIsSet", dynlib: ws2dll, noSideEffect.}
  526. proc FD_ISSET*(socket: SocketHandle, set: var TFdSet): cint =
  527. result = if WSAFDIsSet(socket, set): 1'i32 else: 0'i32
  528. proc FD_SET*(socket: SocketHandle, s: var TFdSet) =
  529. if s.fd_count < FD_SETSIZE:
  530. s.fd_array[int(s.fd_count)] = socket
  531. inc(s.fd_count)
  532. proc FD_ZERO*(s: var TFdSet) =
  533. s.fd_count = 0
  534. proc wsaStartup*(wVersionRequired: int16, WSData: ptr WSAData): cint {.
  535. stdcall, importc: "WSAStartup", dynlib: ws2dll.}
  536. proc getaddrinfo*(nodename, servname: cstring, hints: ptr AddrInfo,
  537. res: var ptr AddrInfo): cint {.
  538. stdcall, importc: "getaddrinfo", dynlib: ws2dll.}
  539. proc freeaddrinfo*(ai: ptr AddrInfo) {.
  540. stdcall, importc: "freeaddrinfo", dynlib: ws2dll.}
  541. proc inet_ntoa*(i: InAddr): cstring {.
  542. stdcall, importc, dynlib: ws2dll.}
  543. const
  544. MAXIMUM_WAIT_OBJECTS* = 0x00000040
  545. type
  546. WOHandleArray* = array[0..MAXIMUM_WAIT_OBJECTS - 1, Handle]
  547. PWOHandleArray* = ptr WOHandleArray
  548. proc waitForMultipleObjects*(nCount: DWORD, lpHandles: PWOHandleArray,
  549. bWaitAll: WINBOOL, dwMilliseconds: DWORD): DWORD{.
  550. stdcall, dynlib: "kernel32", importc: "WaitForMultipleObjects".}
  551. # for memfiles.nim:
  552. const
  553. GENERIC_READ* = 0x80000000'i32
  554. GENERIC_WRITE* = 0x40000000'i32
  555. GENERIC_ALL* = 0x10000000'i32
  556. FILE_SHARE_READ* = 1'i32
  557. FILE_SHARE_DELETE* = 4'i32
  558. FILE_SHARE_WRITE* = 2'i32
  559. CREATE_ALWAYS* = 2'i32
  560. CREATE_NEW* = 1'i32
  561. OPEN_EXISTING* = 3'i32
  562. OPEN_ALWAYS* = 4'i32
  563. FILE_BEGIN* = 0'i32
  564. INVALID_SET_FILE_POINTER* = -1'i32
  565. NO_ERROR* = 0'i32
  566. PAGE_READONLY* = 2'i32
  567. PAGE_READWRITE* = 4'i32
  568. FILE_MAP_READ* = 4'i32
  569. FILE_MAP_WRITE* = 2'i32
  570. INVALID_FILE_SIZE* = -1'i32
  571. DUPLICATE_SAME_ACCESS* = 2
  572. FILE_READ_DATA* = 0x00000001 # file & pipe
  573. FILE_WRITE_DATA* = 0x00000002 # file & pipe
  574. # Error Constants
  575. const
  576. ERROR_FILE_NOT_FOUND* = 2
  577. ERROR_PATH_NOT_FOUND* = 3
  578. ERROR_ACCESS_DENIED* = 5
  579. ERROR_NO_MORE_FILES* = 18
  580. ERROR_LOCK_VIOLATION* = 33
  581. ERROR_HANDLE_EOF* = 38
  582. ERROR_BAD_ARGUMENTS* = 165
  583. proc duplicateHandle*(hSourceProcessHandle: HANDLE, hSourceHandle: HANDLE,
  584. hTargetProcessHandle: HANDLE,
  585. lpTargetHandle: ptr HANDLE,
  586. dwDesiredAccess: DWORD, bInheritHandle: WINBOOL,
  587. dwOptions: DWORD): WINBOOL{.stdcall, dynlib: "kernel32",
  588. importc: "DuplicateHandle".}
  589. proc setHandleInformation*(hObject: HANDLE, dwMask: DWORD,
  590. dwFlags: DWORD): WINBOOL {.stdcall,
  591. dynlib: "kernel32", importc: "SetHandleInformation".}
  592. proc getCurrentProcess*(): HANDLE{.stdcall, dynlib: "kernel32",
  593. importc: "GetCurrentProcess".}
  594. when useWinUnicode:
  595. proc createFileW*(lpFileName: WideCString, dwDesiredAccess, dwShareMode: DWORD,
  596. lpSecurityAttributes: pointer,
  597. dwCreationDisposition, dwFlagsAndAttributes: DWORD,
  598. hTemplateFile: Handle): Handle {.
  599. stdcall, dynlib: "kernel32", importc: "CreateFileW".}
  600. proc deleteFileW*(pathName: WideCString): int32 {.
  601. importc: "DeleteFileW", dynlib: "kernel32", stdcall.}
  602. else:
  603. proc createFileA*(lpFileName: cstring, dwDesiredAccess, dwShareMode: DWORD,
  604. lpSecurityAttributes: pointer,
  605. dwCreationDisposition, dwFlagsAndAttributes: DWORD,
  606. hTemplateFile: Handle): Handle {.
  607. stdcall, dynlib: "kernel32", importc: "CreateFileA".}
  608. proc deleteFileA*(pathName: cstring): int32 {.
  609. importc: "DeleteFileA", dynlib: "kernel32", stdcall.}
  610. proc setEndOfFile*(hFile: Handle): WINBOOL {.stdcall, dynlib: "kernel32",
  611. importc: "SetEndOfFile".}
  612. proc setFilePointer*(hFile: Handle, lDistanceToMove: LONG,
  613. lpDistanceToMoveHigh: ptr LONG,
  614. dwMoveMethod: DWORD): DWORD {.
  615. stdcall, dynlib: "kernel32", importc: "SetFilePointer".}
  616. proc getFileSize*(hFile: Handle, lpFileSizeHigh: ptr DWORD): DWORD{.stdcall,
  617. dynlib: "kernel32", importc: "GetFileSize".}
  618. proc mapViewOfFileEx*(hFileMappingObject: Handle, dwDesiredAccess: DWORD,
  619. dwFileOffsetHigh, dwFileOffsetLow: DWORD,
  620. dwNumberOfBytesToMap: DWORD,
  621. lpBaseAddress: pointer): pointer{.
  622. stdcall, dynlib: "kernel32", importc: "MapViewOfFileEx".}
  623. proc createFileMappingW*(hFile: Handle,
  624. lpFileMappingAttributes: pointer,
  625. flProtect, dwMaximumSizeHigh: DWORD,
  626. dwMaximumSizeLow: DWORD,
  627. lpName: pointer): Handle {.
  628. stdcall, dynlib: "kernel32", importc: "CreateFileMappingW".}
  629. when not useWinUnicode:
  630. proc createFileMappingA*(hFile: Handle,
  631. lpFileMappingAttributes: pointer,
  632. flProtect, dwMaximumSizeHigh: DWORD,
  633. dwMaximumSizeLow: DWORD, lpName: cstring): Handle {.
  634. stdcall, dynlib: "kernel32", importc: "CreateFileMappingA".}
  635. proc unmapViewOfFile*(lpBaseAddress: pointer): WINBOOL {.stdcall,
  636. dynlib: "kernel32", importc: "UnmapViewOfFile".}
  637. proc flushViewOfFile*(lpBaseAddress: pointer, dwNumberOfBytesToFlush: DWORD): WINBOOL {.
  638. stdcall, dynlib: "kernel32", importc: "FlushViewOfFile".}
  639. type
  640. OVERLAPPED* {.pure, inheritable.} = object
  641. internal*: PULONG
  642. internalHigh*: PULONG
  643. offset*: DWORD
  644. offsetHigh*: DWORD
  645. hEvent*: Handle
  646. POVERLAPPED* = ptr OVERLAPPED
  647. POVERLAPPED_COMPLETION_ROUTINE* = proc (para1: DWORD, para2: DWORD,
  648. para3: POVERLAPPED){.stdcall.}
  649. GUID* {.final, pure.} = object
  650. D1*: int32
  651. D2*: int16
  652. D3*: int16
  653. D4*: array[0..7, int8]
  654. const
  655. ERROR_IO_PENDING* = 997 # a.k.a WSA_IO_PENDING
  656. WSAECONNABORTED* = 10053
  657. WSAEADDRINUSE* = 10048
  658. WSAECONNRESET* = 10054
  659. WSAEDISCON* = 10101
  660. WSAENETRESET* = 10052
  661. WSAETIMEDOUT* = 10060
  662. WSANOTINITIALISED* = 10093
  663. WSAENOTSOCK* = 10038
  664. WSAEINPROGRESS* = 10036
  665. WSAEINTR* = 10004
  666. WSAEWOULDBLOCK* = 10035
  667. ERROR_NETNAME_DELETED* = 64
  668. STATUS_PENDING* = 0x103
  669. proc createIoCompletionPort*(FileHandle: Handle, ExistingCompletionPort: Handle,
  670. CompletionKey: ULONG_PTR,
  671. NumberOfConcurrentThreads: DWORD): Handle{.stdcall,
  672. dynlib: "kernel32", importc: "CreateIoCompletionPort".}
  673. proc getQueuedCompletionStatus*(CompletionPort: Handle,
  674. lpNumberOfBytesTransferred: PDWORD, lpCompletionKey: PULONG_PTR,
  675. lpOverlapped: ptr POVERLAPPED,
  676. dwMilliseconds: DWORD): WINBOOL{.stdcall,
  677. dynlib: "kernel32", importc: "GetQueuedCompletionStatus".}
  678. proc getOverlappedResult*(hFile: Handle, lpOverlapped: POVERLAPPED,
  679. lpNumberOfBytesTransferred: var DWORD, bWait: WINBOOL): WINBOOL{.
  680. stdcall, dynlib: "kernel32", importc: "GetOverlappedResult".}
  681. # this is copy of HasOverlappedIoCompleted() macro from <winbase.h>
  682. # because we have declared own OVERLAPPED structure with member names not
  683. # compatible with original names.
  684. template hasOverlappedIoCompleted*(lpOverlapped): bool =
  685. (cast[uint](lpOverlapped.internal) != STATUS_PENDING)
  686. const
  687. IOC_OUT* = 0x40000000
  688. IOC_IN* = 0x80000000
  689. IOC_WS2* = 0x08000000
  690. IOC_INOUT* = IOC_IN or IOC_OUT
  691. template WSAIORW*(x,y): untyped = (IOC_INOUT or x or y)
  692. const
  693. SIO_GET_EXTENSION_FUNCTION_POINTER* = WSAIORW(IOC_WS2,6).DWORD
  694. SO_UPDATE_ACCEPT_CONTEXT* = 0x700B
  695. AI_V4MAPPED* = 0x0008
  696. AF_UNSPEC* = 0
  697. AF_INET* = 2
  698. AF_INET6* = 23
  699. var
  700. WSAID_CONNECTEX*: GUID = GUID(D1: 0x25a207b9, D2: 0xddf3'i16, D3: 0x4660, D4: [
  701. 0x8e'i8, 0xe9'i8, 0x76'i8, 0xe5'i8, 0x8c'i8, 0x74'i8, 0x06'i8, 0x3e'i8])
  702. WSAID_ACCEPTEX*: GUID = GUID(D1: 0xb5367df1'i32, D2: 0xcbac'i16, D3: 0x11cf, D4: [
  703. 0x95'i8, 0xca'i8, 0x00'i8, 0x80'i8, 0x5f'i8, 0x48'i8, 0xa1'i8, 0x92'i8])
  704. WSAID_GETACCEPTEXSOCKADDRS*: GUID = GUID(D1: 0xb5367df2'i32, D2: 0xcbac'i16, D3: 0x11cf, D4: [
  705. 0x95'i8, 0xca'i8, 0x00'i8, 0x80'i8, 0x5f'i8, 0x48'i8, 0xa1'i8, 0x92'i8])
  706. proc WSAIoctl*(s: SocketHandle, dwIoControlCode: DWORD, lpvInBuffer: pointer,
  707. cbInBuffer: DWORD, lpvOutBuffer: pointer, cbOutBuffer: DWORD,
  708. lpcbBytesReturned: PDWORD, lpOverlapped: POVERLAPPED,
  709. lpCompletionRoutine: POVERLAPPED_COMPLETION_ROUTINE): cint
  710. {.stdcall, importc: "WSAIoctl", dynlib: "Ws2_32.dll".}
  711. type
  712. TWSABuf* {.importc: "WSABUF", header: "winsock2.h".} = object
  713. len*: ULONG
  714. buf*: cstring
  715. proc WSARecv*(s: SocketHandle, buf: ptr TWSABuf, bufCount: DWORD,
  716. bytesReceived, flags: PDWORD, lpOverlapped: POVERLAPPED,
  717. completionProc: POVERLAPPED_COMPLETION_ROUTINE): cint {.
  718. stdcall, importc: "WSARecv", dynlib: "Ws2_32.dll".}
  719. proc WSARecvFrom*(s: SocketHandle, buf: ptr TWSABuf, bufCount: DWORD,
  720. bytesReceived: PDWORD, flags: PDWORD, name: ptr SockAddr,
  721. namelen: ptr cint, lpOverlapped: POVERLAPPED,
  722. completionProc: POVERLAPPED_COMPLETION_ROUTINE): cint {.
  723. stdcall, importc: "WSARecvFrom", dynlib: "Ws2_32.dll".}
  724. proc WSASend*(s: SocketHandle, buf: ptr TWSABuf, bufCount: DWORD,
  725. bytesSent: PDWORD, flags: DWORD, lpOverlapped: POVERLAPPED,
  726. completionProc: POVERLAPPED_COMPLETION_ROUTINE): cint {.
  727. stdcall, importc: "WSASend", dynlib: "Ws2_32.dll".}
  728. proc WSASendTo*(s: SocketHandle, buf: ptr TWSABuf, bufCount: DWORD,
  729. bytesSent: PDWORD, flags: DWORD, name: ptr SockAddr,
  730. namelen: cint, lpOverlapped: POVERLAPPED,
  731. completionProc: POVERLAPPED_COMPLETION_ROUTINE): cint {.
  732. stdcall, importc: "WSASendTo", dynlib: "Ws2_32.dll".}
  733. proc get_osfhandle*(fd:FileHandle): Handle {.
  734. importc: "_get_osfhandle", header:"<io.h>".}
  735. proc getSystemTimes*(lpIdleTime, lpKernelTime,
  736. lpUserTime: var FILETIME): WINBOOL {.stdcall,
  737. dynlib: "kernel32", importc: "GetSystemTimes".}
  738. proc getProcessTimes*(hProcess: Handle; lpCreationTime, lpExitTime,
  739. lpKernelTime, lpUserTime: var FILETIME): WINBOOL {.stdcall,
  740. dynlib: "kernel32", importc: "GetProcessTimes".}
  741. type inet_ntop_proc = proc(family: cint, paddr: pointer, pStringBuffer: cstring,
  742. stringBufSize: int32): cstring {.gcsafe, stdcall.}
  743. var inet_ntop_real: inet_ntop_proc = nil
  744. let ws2 = loadLib(ws2dll)
  745. if ws2 != nil:
  746. inet_ntop_real = cast[inet_ntop_proc](symAddr(ws2, "inet_ntop"))
  747. proc WSAAddressToStringA(pAddr: ptr SockAddr, addrSize: DWORD, unused: pointer, pBuff: cstring, pBuffSize: ptr DWORD): cint {.stdcall, importc, dynlib: ws2dll.}
  748. proc inet_ntop_emulated(family: cint, paddr: pointer, pStringBuffer: cstring,
  749. stringBufSize: int32): cstring {.stdcall.} =
  750. case family
  751. of AF_INET:
  752. var sa: Sockaddr_in
  753. sa.sin_family = AF_INET
  754. sa.sin_addr = cast[ptr InAddr](paddr)[]
  755. var bs = stringBufSize.DWORD
  756. let r = WSAAddressToStringA(cast[ptr SockAddr](sa.addr), sa.sizeof.DWORD, nil, pStringBuffer, bs.addr)
  757. if r != 0:
  758. result = nil
  759. else:
  760. result = pStringBuffer
  761. of AF_INET6:
  762. var sa: Sockaddr_in6
  763. sa.sin6_family = AF_INET6
  764. sa.sin6_addr = cast[ptr In6_addr](paddr)[]
  765. var bs = stringBufSize.DWORD
  766. let r = WSAAddressToStringA(cast[ptr SockAddr](sa.addr), sa.sizeof.DWORD, nil, pStringBuffer, bs.addr)
  767. if r != 0:
  768. result = nil
  769. else:
  770. result = pStringBuffer
  771. else:
  772. setLastError(ERROR_BAD_ARGUMENTS)
  773. result = nil
  774. proc inet_ntop*(family: cint, paddr: pointer, pStringBuffer: cstring,
  775. stringBufSize: int32): cstring {.stdcall.} =
  776. var ver: OSVERSIONINFO
  777. ver.dwOSVersionInfoSize = sizeof(ver).DWORD
  778. let res = when useWinUnicode: getVersionExW(ver.addr) else: getVersionExA(ver.addr)
  779. if res == 0:
  780. result = nil
  781. elif ver.dwMajorVersion >= 6:
  782. if inet_ntop_real == nil:
  783. quit("Can't load inet_ntop proc from " & ws2dll)
  784. result = inet_ntop_real(family, paddr, pStringBuffer, stringBufSize)
  785. else:
  786. result = inet_ntop_emulated(family, paddr, pStringBuffer, stringBufSize)
  787. type
  788. WSAPROC_ACCEPTEX* = proc (sListenSocket: SocketHandle,
  789. sAcceptSocket: SocketHandle,
  790. lpOutputBuffer: pointer, dwReceiveDataLength: DWORD,
  791. dwLocalAddressLength: DWORD,
  792. dwRemoteAddressLength: DWORD,
  793. lpdwBytesReceived: ptr DWORD,
  794. lpOverlapped: POVERLAPPED): bool {.
  795. stdcall,gcsafe.}
  796. WSAPROC_CONNECTEX* = proc (s: SocketHandle, name: ptr SockAddr, namelen: cint,
  797. lpSendBuffer: pointer, dwSendDataLength: DWORD,
  798. lpdwBytesSent: ptr DWORD,
  799. lpOverlapped: POVERLAPPED): bool {.
  800. stdcall,gcsafe.}
  801. WSAPROC_GETACCEPTEXSOCKADDRS* = proc(lpOutputBuffer: pointer,
  802. dwReceiveDataLength: DWORD,
  803. dwLocalAddressLength: DWORD,
  804. dwRemoteAddressLength: DWORD,
  805. LocalSockaddr: ptr PSockAddr,
  806. LocalSockaddrLength: ptr cint,
  807. RemoteSockaddr: ptr PSockAddr,
  808. RemoteSockaddrLength: ptr cint) {.
  809. stdcall,gcsafe.}
  810. const
  811. WT_EXECUTEDEFAULT* = 0x00000000'i32
  812. WT_EXECUTEINIOTHREAD* = 0x00000001'i32
  813. WT_EXECUTEINUITHREAD* = 0x00000002'i32
  814. WT_EXECUTEINWAITTHREAD* = 0x00000004'i32
  815. WT_EXECUTEONLYONCE* = 0x00000008'i32
  816. WT_EXECUTELONGFUNCTION* = 0x00000010'i32
  817. WT_EXECUTEINTIMERTHREAD* = 0x00000020'i32
  818. WT_EXECUTEINPERSISTENTIOTHREAD* = 0x00000040'i32
  819. WT_EXECUTEINPERSISTENTTHREAD* = 0x00000080'i32
  820. WT_TRANSFER_IMPERSONATION* = 0x00000100'i32
  821. PROCESS_TERMINATE* = 0x00000001'i32
  822. PROCESS_CREATE_THREAD* = 0x00000002'i32
  823. PROCESS_SET_SESSIONID* = 0x00000004'i32
  824. PROCESS_VM_OPERATION* = 0x00000008'i32
  825. PROCESS_VM_READ* = 0x00000010'i32
  826. PROCESS_VM_WRITE* = 0x00000020'i32
  827. PROCESS_DUP_HANDLE* = 0x00000040'i32
  828. PROCESS_CREATE_PROCESS* = 0x00000080'i32
  829. PROCESS_SET_QUOTA* = 0x00000100'i32
  830. PROCESS_SET_INFORMATION* = 0x00000200'i32
  831. PROCESS_QUERY_INFORMATION* = 0x00000400'i32
  832. PROCESS_SUSPEND_RESUME* = 0x00000800'i32
  833. PROCESS_QUERY_LIMITED_INFORMATION* = 0x00001000'i32
  834. PROCESS_SET_LIMITED_INFORMATION* = 0x00002000'i32
  835. type
  836. WAITORTIMERCALLBACK* = proc(para1: pointer, para2: int32): void {.stdcall.}
  837. proc postQueuedCompletionStatus*(CompletionPort: HANDLE,
  838. dwNumberOfBytesTransferred: DWORD,
  839. dwCompletionKey: ULONG_PTR,
  840. lpOverlapped: pointer): bool
  841. {.stdcall, dynlib: "kernel32", importc: "PostQueuedCompletionStatus".}
  842. proc registerWaitForSingleObject*(phNewWaitObject: ptr Handle, hObject: Handle,
  843. Callback: WAITORTIMERCALLBACK,
  844. Context: pointer,
  845. dwMilliseconds: ULONG,
  846. dwFlags: ULONG): bool
  847. {.stdcall, dynlib: "kernel32", importc: "RegisterWaitForSingleObject".}
  848. proc unregisterWait*(WaitHandle: HANDLE): DWORD
  849. {.stdcall, dynlib: "kernel32", importc: "UnregisterWait".}
  850. proc openProcess*(dwDesiredAccess: DWORD, bInheritHandle: WINBOOL,
  851. dwProcessId: DWORD): Handle
  852. {.stdcall, dynlib: "kernel32", importc: "OpenProcess".}
  853. when defined(useWinAnsi):
  854. proc createEvent*(lpEventAttributes: ptr SECURITY_ATTRIBUTES,
  855. bManualReset: DWORD, bInitialState: DWORD,
  856. lpName: cstring): Handle
  857. {.stdcall, dynlib: "kernel32", importc: "CreateEventA".}
  858. else:
  859. proc createEvent*(lpEventAttributes: ptr SECURITY_ATTRIBUTES,
  860. bManualReset: DWORD, bInitialState: DWORD,
  861. lpName: ptr Utf16Char): Handle
  862. {.stdcall, dynlib: "kernel32", importc: "CreateEventW".}
  863. proc setEvent*(hEvent: Handle): cint
  864. {.stdcall, dynlib: "kernel32", importc: "SetEvent".}
  865. const
  866. FD_READ* = 0x00000001'i32
  867. FD_WRITE* = 0x00000002'i32
  868. FD_OOB* = 0x00000004'i32
  869. FD_ACCEPT* = 0x00000008'i32
  870. FD_CONNECT* = 0x00000010'i32
  871. FD_CLOSE* = 0x00000020'i32
  872. FD_QQS* = 0x00000040'i32
  873. FD_GROUP_QQS* = 0x00000080'i32
  874. FD_ROUTING_INTERFACE_CHANGE* = 0x00000100'i32
  875. FD_ADDRESS_LIST_CHANGE* = 0x00000200'i32
  876. FD_ALL_EVENTS* = 0x000003FF'i32
  877. proc wsaEventSelect*(s: SocketHandle, hEventObject: Handle,
  878. lNetworkEvents: clong): cint
  879. {.stdcall, importc: "WSAEventSelect", dynlib: "ws2_32.dll".}
  880. proc wsaCreateEvent*(): Handle
  881. {.stdcall, importc: "WSACreateEvent", dynlib: "ws2_32.dll".}
  882. proc wsaCloseEvent*(hEvent: Handle): bool
  883. {.stdcall, importc: "WSACloseEvent", dynlib: "ws2_32.dll".}
  884. proc wsaResetEvent*(hEvent: Handle): bool
  885. {.stdcall, importc: "WSAResetEvent", dynlib: "ws2_32.dll".}
  886. type
  887. KEY_EVENT_RECORD* {.final, pure.} = object
  888. eventType*: int16
  889. bKeyDown*: WINBOOL
  890. wRepeatCount*: int16
  891. wVirtualKeyCode*: int16
  892. wVirtualScanCode*: int16
  893. uChar*: int16
  894. dwControlKeyState*: DWORD
  895. when defined(useWinAnsi):
  896. proc readConsoleInput*(hConsoleInput: Handle, lpBuffer: pointer, nLength: cint,
  897. lpNumberOfEventsRead: ptr cint): cint
  898. {.stdcall, dynlib: "kernel32", importc: "ReadConsoleInputA".}
  899. else:
  900. proc readConsoleInput*(hConsoleInput: Handle, lpBuffer: pointer, nLength: cint,
  901. lpNumberOfEventsRead: ptr cint): cint
  902. {.stdcall, dynlib: "kernel32", importc: "ReadConsoleInputW".}
  903. type
  904. LPFIBER_START_ROUTINE* = proc (param: pointer): void {.stdcall.}
  905. const
  906. FIBER_FLAG_FLOAT_SWITCH* = 0x01
  907. proc CreateFiber*(stackSize: int, fn: LPFIBER_START_ROUTINE, param: pointer): pointer {.stdcall, discardable, dynlib: "kernel32", importc.}
  908. proc CreateFiberEx*(stkCommit: int, stkReserve: int, flags: int32, fn: LPFIBER_START_ROUTINE, param: pointer): pointer {.stdcall, discardable, dynlib: "kernel32", importc.}
  909. proc ConvertThreadToFiber*(param: pointer): pointer {.stdcall, discardable, dynlib: "kernel32", importc.}
  910. proc ConvertThreadToFiberEx*(param: pointer, flags: int32): pointer {.stdcall, discardable, dynlib: "kernel32", importc.}
  911. proc DeleteFiber*(fiber: pointer): void {.stdcall, discardable, dynlib: "kernel32", importc.}
  912. proc SwitchToFiber*(fiber: pointer): void {.stdcall, discardable, dynlib: "kernel32", importc.}
  913. proc GetCurrentFiber*(): pointer {.stdcall, importc, header: "windows.h".}
  914. proc toFILETIME*(t: int64): FILETIME =
  915. ## Convert the Windows file time timestamp ``t`` to ``FILETIME``.
  916. result = FILETIME(dwLowDateTime: cast[DWORD](t), dwHighDateTime: DWORD(t shr 32))
  917. type
  918. LPFILETIME* = ptr FILETIME
  919. proc setFileTime*(hFile: HANDLE, lpCreationTime: LPFILETIME,
  920. lpLastAccessTime: LPFILETIME, lpLastWriteTime: LPFILETIME): WINBOOL
  921. {.stdcall, dynlib: "kernel32", importc: "SetFileTime".}