写入一些文本到文件, 然后从文件读取回内存
[erphpdown]
; 在 AHK_L 42+, 使用 FileOpen 可以实现同样的目的. FileSelectFile, FileName, S16,, Create a new file: if FileName = return GENERIC_WRITE = 0x40000000 ; 以写入而不是读取的方式打开文件. CREATE_ALWAYS = 2 ; 创建新文件 (覆盖任何现有的文件). hFile := DllCall("CreateFile", Str, FileName, UInt, GENERIC_WRITE, UInt, 0, Ptr, 0, UInt, CREATE_ALWAYS, UInt, 0, Ptr, 0, Ptr) if not hFile { MsgBox Can't open "%FileName%" for writing. return } TestString = This is a test string.`r`n ; 通过这种方式写入内容到文件时, 要使用 `r`n 而不是 `n 来开始新行. DllCall("WriteFile", Ptr, hFile, Str, TestString, UInt, StrLen(TestString), UIntP, BytesActuallyWritten, Ptr, 0) DllCall("CloseHandle", Ptr, hFile) ; Close the file. ; 现在已经把内容写入文件了, 重新把它们读取回内存中. GENERIC_READ = 0x80000000 ; 以读取而不是写入的方式来打开文件. OPEN_EXISTING = 3 ; 此标志表示要打开的文件必须已经存在. FILE_SHARE_READ = 0x1 ; 这个和下一个标志表示其他进程是否可以打开我们已经打开的文件. FILE_SHARE_WRITE = 0x2 hFile := DllCall("CreateFile", Str, FileName, UInt, GENERIC_READ, UInt, FILE_SHARE_READ|FILE_SHARE_WRITE, Ptr, 0, UInt, OPEN_EXISTING, UInt, 0, Ptr, 0) if not hFile { MsgBox Can't open "%FileName%" for reading. return } ; 清空变量以便进行测试, 但需要确保它含有足够的容量: BytesToRead := VarSetCapacity(TestString, StrLen(TestString)) DllCall("ReadFile", Ptr, hFile, Str, TestString, UInt, BytesToRead, UIntP, BytesActuallyRead, Ptr, 0) DllCall("CloseHandle", Ptr, hFile) ; 关闭文件. MsgBox The following string was read from the file: %TestString%
[/erphpdown]