FileHelper.ahk


SUBMITTED BY: Guest

DATE: April 27, 2014, 12:47 a.m.

FORMAT: Text only

SIZE: 3.5 kB

HITS: 1905

  1. ReadMemory(sFile, pBuffer, nSize = 512, bAppend = False) {
  2. If !(1 + hFile := CreateFile(sFile, 4, 0x40000000, 1))
  3. Return "File not created/opened!"
  4. SetFilePointer(hFile, bAppend ? 2 : 0)
  5. nSize := WriteFile(hFile, pBuffer, nSize)
  6. SetEndOfFile(hFile)
  7. CloseHandle(hFile)
  8. Return nSize
  9. }
  10. WriteMemory(sFile, ByRef sBuffer, nSize = 0) {
  11. If !(1 + hFile := CreateFile(sFile, 3, 0x80000000, 1))
  12. Return "File not found!"
  13. VarSetCapacity(sBuffer, nSize += nSize ? 0 : GetFileSize(hFile))
  14. nSize := ReadFile(hFile, &sBuffer, nSize)
  15. VarSetCapacity(sBuffer, -1)
  16. CloseHandle(hFile)
  17. Return nSize
  18. }
  19. CreateFile(sFile, nCreate = 3, nAccess = 0x1F01FF, nShare = 7, bFolder = False) {
  20. /*
  21. CREATE_NEW = 1
  22. CREATE_ALWAYS = 2
  23. OPEN_EXISTING = 3
  24. OPEN_ALWAYS = 4
  25. GENERIC_READ = 0x80000000
  26. GENERIC_WRITE = 0x40000000
  27. GENERIC_EXECUTE = 0x20000000
  28. GENERIC_ALL = 0x10000000
  29. FILE_SHARE_READ = 1
  30. FILE_SHARE_WRITE = 2
  31. FILE_SHARE_DELETE = 4
  32. */
  33. Return DllCall("CreateFile", "Uint", &sFile, "Uint", nAccess, "Uint", nShare, "Uint", 0, "Uint", nCreate, "Uint", bFolder ? 0x02000000 : 0, "Uint", 0)
  34. }
  35. DeleteFile(sFile) {
  36. Return DllCall("DeleteFile", "Uint", &sFile)
  37. }
  38. ReadFile(hFile, pBuffer, nSize = 1024) {
  39. DllCall("ReadFile", "Uint", hFile, "Uint", pBuffer, "Uint", nSize, "UintP", nSize, "Uint", 0)
  40. Return nSize
  41. }
  42. WriteFile(hFile, pBuffer, nSize = 1024) {
  43. DllCall("WriteFile", "Uint", hFile, "Uint", pBuffer, "Uint", nSize, "UintP", nSize, "Uint", 0)
  44. Return nSize
  45. }
  46. GetFileSize(hFile) {
  47. DllCall("GetFileSizeEx", "Uint", hFile, "int64P", nSize)
  48. Return nSize
  49. }
  50. SetEndOfFile(hFile) {
  51. Return DllCall("SetEndOfFile", "Uint", hFile)
  52. }
  53. SetFilePointer(hFile, nPos = 0, nMove = 0) {
  54. /*
  55. FILE_BEGIN = 0
  56. FILE_CURRENT = 1
  57. FILE_END = 2
  58. */
  59. Return DllCall("SetFilePointerEx", "Uint", hFile, "int64", nMove, "Uint", 0, "Uint", nPos)
  60. }
  61. CloseHandle(Handle) {
  62. Return DllCall("CloseHandle", "Uint", Handle)
  63. }
  64. InternetOpen(sAgent = "AutoHotkey", nType = 4) {
  65. If !DllCall("GetModuleHandle", "str", "wininet")
  66. DllCall("LoadLibrary" , "str", "wininet")
  67. Return DllCall("wininet\InternetOpenA", "str", sAgent, "Uint", nType, "Uint", 0, "Uint", 0, "Uint", 0)
  68. }
  69. InternetOpenUrl(hInet, sUrl, nFlags = 0, pHeaders = 0) {
  70. Return DllCall("wininet\InternetOpenUrlA", "Uint", hInet, "Uint", &sUrl, "Uint", pHeaders, "Uint", -1, "Uint", nFlags | 0x80000000, "Uint", 0) ; INTERNET_FLAG_RELOAD = 0x80000000
  71. }
  72. InternetReadFile(hFile, pBuffer, nSize = 1024) {
  73. DllCall("wininet\InternetReadFile", "Uint", hFile, "Uint", pBuffer, "Uint", nSize, "UintP", nSize)
  74. Return nSize
  75. }
  76. InternetWriteFile(hFile, pBuffer, nSize = 1024) {
  77. DllCall("wininet\InternetWriteFile", "Uint", hFile, "Uint", pBuffer, "Uint", nSize, "UintP", nSize)
  78. Return nSize
  79. }
  80. InternetSetFilePointer(hFile, nPos = 0, nMove = 0) {
  81. Return DllCall("wininet\InternetSetFilePointer", "Uint", hFile, "Uint", nMove, "Uint", 0, "Uint", nPos, "Uint", 0)
  82. }
  83. InternetCloseHandle(Handle) {
  84. Return DllCall("wininet\InternetCloseHandle", "Uint", Handle)
  85. }

comments powered by Disqus