Question
Execute this code with screen shot of result unit UnMW; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 =
Execute this code with screen shot of result
unit UnMW;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Label1: TLabel;
Label2: TLabel;
Edit2: TEdit;
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
var
hMem : HGLOBAL;
mSize : Cardinal;
MemPtr : Pointer;
implementation
{$R *.dfm}
function IntToStrKMG(a : Cardinal) : String;
var
b : Cardinal;
pr : String;
begin
b := a;
pr := ' bite';
if b mod 1024 = 0 then
pr := ' '
else
Result := IntToStr(b mod 1024);
b := b div 1024;
if b > 0 then
begin
if b mod 1024 = 0 then
pr := ' '
else
Result := IntToStr(b mod 1024) + ' ' + Result;
b := b div 1024;
if b > 0 then
begin
if b mod 1024 = 0 then
pr := ' G'
else
Result := IntToStr(b mod 1024) + ' ' + Result;
b := b div 1024;
if b > 0 then
Result := IntToStr(b mod 1024) + ' ' + Result;
end;
end;
Result := Result + pr;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
mSize := Length(Edit1.Text) + 1;
// ShowMessage(IntToStr(mSize));
hMem := GlobalAlloc(GMEM_MOVEABLE or GMEM_DDESHARE, mSize);
MemPtr := GlobalLock(hMem);
CopyMemory(MemPtr, PChar(Edit1.Text), mSize);
GlobalUnlock(hMem);
end;
procedure TForm1.Button3Click(Sender: TObject);
var
Buf : _MEMORYSTATUS;
begin
GlobalMemoryStatus(Buf);
ShowMessage('used memory: ' + IntToStr(Buf.dwMemoryLoad) + '%'#10#13 +
'Total physical memory: ' + IntToStrKMG(Buf.dwTotalPhys) + #10#13 +
'Available Physical Memory: ' + IntToStrKMG(Buf.dwAvailPhys) + #10#13 +
'The swap file: ' + IntToStrKMG(Buf.dwTotalPageFile) + #10#13 +
'Available in the paging file: ' + IntToStrKMG(Buf.dwAvailPageFile) + #10#13 +
'Total logical memory: ' + IntToStrKMG(Buf.dwTotalVirtual) + #10#13 +
'Available logical memory: ' + IntToStrKMG(Buf.dwAvailVirtual) + #10#13);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
GlobalFree(hMem);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
mSize := GlobalSize(hMem);
// ShowMessage(IntToStrKMG(mSize));
MemPtr := GlobalLock(hMem);
if MemPtr = NIL then
ShowMessage('Memory is empty.')
else
Edit2.SetTextBuf(MemPtr);
GlobalUnlock(hMem);
end;
end.
An example of to virtual memory
unit UnMW;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Label1: TLabel;
Label2: TLabel;
Edit2: TEdit;
Button1: TButton;
Button2: TButton;
procedure Button2Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
var
mSize : Cardinal;
MemPtr : Pointer;
implementation
{$R *.dfm}
procedure TForm1.Button2Click(Sender: TObject);
begin
mSize := Length(Edit1.Text) + 1;
MemPtr := VirtualAlloc(NIL, mSize, MEM_COMMIT, PAGE_READWRITE);
VirtualLock(MemPtr, mSize);
CopyMemory(MemPtr, PChar(Edit1.Text), mSize);
VirtualUnlock(MemPtr, mSize);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if MemPtr <> NIL then
VirtualFree(MemPtr, MDIChildCount, MEM_RELEASE);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if MemPtr = NIL then
ShowMessage(' ')
else
begin
VirtualLock(MemPtr, mSize);
Edit2.SetTextBuf(MemPtr);
VirtualUnlock(MemPtr, mSize);
end;
end;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started