This site uses cookies own and third. If you continue to browse consider to accept the use of cookies. OK More Info.

ReadWriteAPI [UNIT] [Fakedo0r]

  • 0 Replies
  • 2399 Views

0 Members and 1 Guest are viewing this topic.

Fakedo0r

  • *
  • Guest

ReadWriteAPI [UNIT] [Fakedo0r]

  • on: April 19, 2012, 05:08:16 pm
Permite leer textos / binarios e escribirlos.

Code: (delphi) You are not allowed to view links. Register or Login
//******************************************************************************
//* UNIT:         UNT_ReadWriteAPI
//* AUTOR:        Fakedo0r
//* CORREO:       [email protected]
//* BLOG:         Sub-Soul.blogspot.com
//* FECHA:        07.04.2012
//* REFERENCIA:   http://msdn.microsoft.com/en-us/library
//*               /windows/desktop/aa363858(v=vs.85).aspx
//*               /windows/desktop/aa365467(v=vs.85).aspx
//*               /windows/desktop/aa365747(v=vs.85).aspx
//* ----------------------------------------------------------------------------
//* FUNCIONES:    ReadFileA
//*               WriteFileA
//*               StringToByteArray
//*               ByteArrayToString
//*               MsgBoxA
//*
//*
//* USO:          sVar := ReadFileA('C:\Origen.exe');
//*               WriteFileA('C:\Destino.exe', sVar);
//******************************************************************************
unit UNT_ReadWriteAPI;
//******************************************************************************
//DECLARACION LIBRERIAS / CLASES
//******************************************************************************
interface

uses
  Winapi.Windows, System.SysUtils, ShlObj;
//******************************************************************************
//DECLARACION DE FUNCIONES / PROCEDIMIENTOS
//******************************************************************************
function MsgBoxA(sMensaje: String): SmallInt;
function ReadFileA(sFile: String): String;
function WriteFileA(sFile: String; const sBuffer: String): Bool;
function StringToByteArray(sCadena: AnsiString): TBytes;
function ByteArrayToString(bByteArray: TBytes): AnsiString;
//******************************************************************************
implementation
//******************************************************************************
//******************************************************************************
//<--- LEE --->
//******************************************************************************
function ReadFileA(sFile: String): String;
var
  dwRet:    DWORD;
  hFile:    THandle;
  iSize:    Integer;
  bRead:    TBytes;
begin
  hFile := CreateFile(PChar(sFile), GENERIC_READ, FILE_SHARE_READ, nil,
                      OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

  if hFile = INVALID_HANDLE_VALUE then
  begin
    MsgBoxA('Se produjo algun error !');
    Exit;
  end;

  iSize := GetFileSize(hFile, nil);
  SetFilePointer(hFile, 0, nil, FILE_BEGIN);
  SetLength(bRead, iSize);

  ReadFile(hFile, bRead[0], iSize, dwRet, nil);
  CloseHandle(hFile);

  Result := ByteArrayToString(bRead);
end;
//******************************************************************************
//<--- ESCRIBE --->
//******************************************************************************
function WriteFileA(sFile: String; const sBuffer: String): Bool;
var
  dwRet:    DWORD;
  hFile:    THandle;
  iSize:    Integer;
  bWrite:   TBytes;
begin
  Result := True;
  bWrite := StringToByteArray(sBuffer);
  iSize := Length(bWrite);
  hFile := CreateFile(PChar(sFile), GENERIC_WRITE, FILE_SHARE_READ, nil,
                      OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);

  WriteFile(hFile, bWrite[0], iSize, dwRet, nil);
  CloseHandle(hFile);
end;
//******************************************************************************
//<--- CONVIERTE ARRAY DE BYTES EN STRING --->
//******************************************************************************
function ByteArrayToString(bByteArray: TBytes): AnsiString;
var
  i: Integer;
begin
  SetLength(Result, Length(bByteArray));

  for i := 0 to Length(bByteArray) do
    Result[i + 1] := AnsiChar(bByteArray[i]);
end;
//******************************************************************************
//<--- CONVIERTE STRING EN ARRAY DE BYTES --->
//******************************************************************************
function StringToByteArray(sCadena: AnsiString): TBytes;
var
  i: Integer;
begin
  SetLength(Result, Length(sCadena));

  for i := 0 to Length(sCadena) do
    Result[i - 1] := Byte(sCadena[i]);
end;
//******************************************************************************
//<--- MSGBOX --->
//******************************************************************************
function MsgBoxA(sMensaje: String): SmallInt;
begin
  Result := MessageBoxEx(0, PChar(sMensaje), 'Mensaje', MB_ICONINFORMATION, 0);
end;
end.

Saludo.
« Last Edit: April 21, 2013, 01:04:14 pm by Expermicid »