Marco's Web Center

Menu for Development

Site Menu
Object Pascal Handbook
Delphi Handbooks Collection
Mastering Borland Delphi 2005
(Old) White Papers
(Old)Tools
(Old) Conferences

My Other Sites
Italian Site (www.marcocantu.it)
the delphi search

Spirit of delphi

Advertising
Home My Blog Books Object Pascal Marco

Home: Code Repository: Mastering Delphi 6

Chapter 12 - Project Usemem

Project Structure

Usemem.dpr
program usemem;

uses
  Forms,
  UseMemF in 'UseMemF.pas' {Form1};

{$R *.RES}

begin
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.
UseMemF.pas
unit UseMemF;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Spin, ComCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Label1: TLabel;
    Edit1: TEdit;
    UpDown1: TUpDown;
    Edit2: TEdit;
    Button4: TButton;
    Edit3: TEdit;
    UpDown2: TUpDown;
    Button5: TButton;
    Edit4: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

function GetData: Integer; stdcall;
  external 'dllmem.dll';
procedure SetData (I: Integer); stdcall;
  external 'dllmem.dll';
function GetShareData: Integer; stdcall;
  external 'dllmem.dll';
procedure SetShareData (I: Integer); stdcall;
  external 'dllmem.dll';

procedure TForm1.Button1Click(Sender: TObject);
begin
  SetData (UpDown1.Position);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Edit2.Text := IntToStr (GetData);
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  HDLLInst: THandle;
begin
  HDLLInst := SafeLoadLibrary ('dllmem.dll');
  Label1.Caption := Format ('Address: %p', [
    GetProcAddress (HDLLInst, 'SetData')]);
  FreeLibrary (HDLLInst);
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  SetShareData (UpDown2.Position);
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
  Edit4.Text := IntToStr (GetShareData);
end;

end.
UseMemF.dfm
object Form1: TForm1
  Left = 222
  Top = 157
  Width = 305
  Height = 257
  ActiveControl = Button1
  Caption = 'DLL Memory Test'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  Visible = True
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 144
    Top = 102
    Width = 121
    Height = 13
    AutoSize = False
    Caption = 'Address'
  end
  object Button1: TButton
    Left = 32
    Top = 14
    Width = 81
    Height = 25
    Caption = '&Set'
    Default = True
    TabOrder = 0
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 32
    Top = 48
    Width = 81
    Height = 25
    Caption = '&Get'
    TabOrder = 1
    OnClick = Button2Click
  end
  object Button3: TButton
    Left = 32
    Top = 96
    Width = 81
    Height = 25
    Caption = 'Code &Address'
    TabOrder = 2
    OnClick = Button3Click
  end
  object Edit1: TEdit
    Left = 144
    Top = 16
    Width = 105
    Height = 21
    Color = clWindow
    TabOrder = 3
    Text = '20'
  end
  object UpDown1: TUpDown
    Left = 249
    Top = 16
    Width = 15
    Height = 21
    Associate = Edit1
    Min = 0
    Increment = 5
    Position = 20
    TabOrder = 4
    Wrap = False
  end
  object Edit2: TEdit
    Left = 144
    Top = 48
    Width = 121
    Height = 21
    Color = clWindow
    TabOrder = 5
    Text = '0'
  end
  object Button4: TButton
    Left = 32
    Top = 150
    Width = 81
    Height = 25
    Caption = 'Se&t Share'
    TabOrder = 6
    OnClick = Button4Click
  end
  object Edit3: TEdit
    Left = 144
    Top = 152
    Width = 105
    Height = 21
    Color = clWindow
    TabOrder = 7
    Text = '30'
  end
  object UpDown2: TUpDown
    Left = 249
    Top = 152
    Width = 15
    Height = 21
    Associate = Edit3
    Min = 0
    Increment = 5
    Position = 30
    TabOrder = 8
    Wrap = False
  end
  object Button5: TButton
    Left = 32
    Top = 184
    Width = 81
    Height = 25
    Caption = 'Get S&hare'
    TabOrder = 9
    OnClick = Button5Click
  end
  object Edit4: TEdit
    Left = 144
    Top = 184
    Width = 121
    Height = 21
    Color = clWindow
    TabOrder = 10
    Text = '0'
  end
end