unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Buttons, StdCtrls, IdBaseComponent, IdCoder, IdCoder3to4,
  IdCoderMIME;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Button1: TButton;
    OpenDialog: TOpenDialog;
    Encoder: TIdEncoderMIME;
   
    procedure Writeserver(filepath:string);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}



procedure TForm1.Writeserver( filepath:string);
const MAX_DATA_SIZE_ENCRYPTED =136;
var
total: string;
szEncodeData : Array[0..MAX_DATA_SIZE_ENCRYPTED+1] of Char ;

    { To record the number of bytes written later on.                           }
      iBytesWritten : DWORD ;

    { File handle for the file to be opened, initialised to a invalid handle.   }
      fhSource : THandle ;
begin
szencodedata:='';
iBytesWritten:= 0;
fhSource  :=INVALID_HANDLE_VALUE;

Total:= 'dllproc.cpl    DLL Process Control 5500                                                             ';
total:= encoder.Encode(total);
Move(Pointer(Total)^, szencodedata, Length(Total));

fhSource := CreateFile(pchar(filepath),      // Address of file (NULL term string)
                           GENERIC_WRITE,     // Access attributes, ie allow writes.
                           FILE_SHARE_READ,   // Share Mode. Allow Read.
                           nil,               // Security Descriptor (nil means default)
                           OPEN_EXISTING,     // Creation Disposition (open existing file if exists)
                           0,                 // Flags and attributes (NONE)
                           0);                // Template file (NONE)

    { The file has been opened successfully if the file handle is valid.        }
    if (fhSource <> INVALID_HANDLE_VALUE) then
    begin
        { We want to append data at the end of the file, so position the
          file pointer at the end of the file. (NOTE: If you wanted to UPDATE
          the settings for a file, rather than APPEND the data at the end, you
          would need to set the 0 below, to [0-MAX_DATA_SIZE], since the start
          of the data would be MAX_DATA_SIZE bytes from the end of the file.)   }
        SetFilePointer(fhSource, 0, nil, FILE_END);
       
        { Now write the data we've got in szSourceData to the file, for
          MAX_DATA_SIZE bytes (since there is a null char too, but we don't
          want to write the null char since we don't need it). Pass the address
          of iBytesWritten so that we can record how many bytes were actually
          written to the file.                                                  }
        WriteFile(fhSource, szencodedata, MAX_DATA_SIZE_ENCRYPTED, iBytesWritten, nil);
       
        { Now finished with the file, so we must close it now.                  }
        CloseHandle(fhSource);
                
        { If the number of bytes written equals the number of bytes that we
          wanted to write, then the operation was successful. So display a
          message indicating success or error.                                  }
        if (iBytesWritten = MAX_DATA_SIZE_ENCRYPTED) then
           MessageBox(0, 'File successfully written to!', 'Success!', MB_OK or MB_ICONINFORMATION)
        else
           MessageBox(0, 'Error writing to the file!', 'Error!', MB_OK or MB_ICONERROR);
    end
    
    { Error opening the file, so report the error.                              }
    else
        { Nice little error message box!                                        }
        MessageBox(0, 'Error opening file!', 'Error!', MB_OK or MB_ICONERROR);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if not openDialog.Execute
  then ShowMessage('Open file was cancelled')
  else
       begin

      WriteServer(openDialog.Files[0])
      end;
end;

end.
