Jump to content

How to use the _WinAPI_ReadFile function


Recommended Posts

I want to read some data from some file my code looks like this:

#Include <WinAPI.au3>
$hFile="C:\boot.ini"
Global $pBuffer
$ile=256
$tBuffer = DllStructCreate("char Text[4096]"); Step 1 - Create the buffer.
$pBuffer = DllStructGetPtr($tBuffer); Step 2 - Get a pointer to that buffer.
$wynik=_WinAPI_ReadFile($hFile, $pBuffer, 256,$ile)
MsgBox(0,"",$wynik)
MsgBox(0,"",$pBuffer)

$wynik returns me "False"

$pBuffer returns me adress for the buffer

And where is data from the readed file ? Why false ? HELP PLEASE :P

And another question, can I read with thif function images to bianry code? If no, then with function allow me to do this ? I just want to read some picture to string and next convert this text,string,hex or whatever again to picture. :D

Link to post
Share on other sites

Ok nevermind, i maked it with this code:

#include <File.au3>
$file = FileOpen ( "C:\boot.jpg", 16 )
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
$size=FileGetSize ("C:\boot.jpg")
MsgBox(0,"",$size)
    $chars = FileRead($file, $size)
 MsgBox(0, "Char read:", $chars)
 $tresc=$chars
 FileClose($file)
 _FileCreate( "C:\bootnowy.jpg")
 Sleep(50)
 $file2 = FileOpen ( "C:\bootnowy.jpg", 16 )
 If $file2 = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
 FileWrite("C:\bootnowy.jpg", $tresc)
  FileClose($file2)
Link to post
Share on other sites

In answer to your question:

#include <WinAPI.au3>
Global Const $OF_READ = 0x00000000
Global Const $OFS_MAXPATHNAME = 128

Global Const $tag_OFSTRUCT = "byte cBytes;byte fFixedDisk;long nErrCode;long Reserved1;long Reserved2;char szPathName[" & $OFS_MAXPATHNAME & "]"

_Main()

Func _Main()
    Local $t_OFSTRUCT = DllStructCreate($tag_OFSTRUCT)
    Local $hFile, $ile, $tBuffer, $pBuffer, $wynik

    DllStructSetData($t_OFSTRUCT, "cBytes", DllStructGetSize($t_OFSTRUCT))
    $hFile = _WinAPI_OpenFile("C:\boot.ini", $t_OFSTRUCT, $OF_READ)
    ConsoleWrite($hFile & @LF)
    Global $pBuffer
    $ile = 0
    $tBuffer = DllStructCreate("char Text[4096]"); Step 1 - Create the buffer.
    $pBuffer = DllStructGetPtr($tBuffer); Step 2 - Get a pointer to that buffer.
    $wynik = _WinAPI_ReadFile($hFile, $pBuffer, 256, $ile)
    ConsoleWrite($wynik & @LF)
    ConsoleWrite(DllStructGetData($tBuffer, "Text") & @LF)
    ConsoleWrite($ile & @LF)
    ConsoleWrite(_WinAPI_CloseHandle($hFile) & @LF)
EndFunc   ;==>_Main

Func _WinAPI_OpenFile($sFile, ByRef $tOFSTRUCT, $iStyle)
    $pOFSTRUCT = DllStructGetPtr($tOFSTRUCT)
    $iResult = DllCall("kernel32.dll", "hwnd", "OpenFile", "str", $sFile, "ptr", $pOFSTRUCT, "uint", $iStyle)
    If @error Then Return SetError(-1, -1, 0)
    Return $iResult[0]
EndFunc   ;==>_WinAPI_OpenFile

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...