Article View: borland.public.cppbuilder.nativeapi
Article #10512Re: CopyMemory API problem
From: Yahia El-Qasem
Date: Tue, 13 Apr 2004 17:52
Date: Tue, 13 Apr 2004 17:52
103 lines
3753 bytes
3753 bytes
try this :
#define DIV 1024
#define WIDTH 7
int Granularity, PageSize;
MEMORYSTATUS stat;
//--------------------------------------------------------------------------
-
void __fastcall TForm1::Button2Click(TObject *Sender)
{
int UserEntry, PagesToFree, BytesToFree, WriteAddress, ReadAddress,
ProgressVal;
AnsiString strTemp;
LPVOID BaseAddr;
const int nkb = 1024;
UserEntry = (StrToInt(Edit1->Text) * nkb) * nkb;
PagesToFree = UserEntry / PageSize;
BytesToFree = PagesToFree * PageSize;
if(BytesToFree <= 0)
{ lblCantFree->Caption = "Not a valid positive number";
lblCantFree->Visible = true;
ShowMessage("Error");
return;
}
if(BytesToFree <= stat.dwAvailPhys)
{ lblCantFree->Caption = "That amount is already free";
lblCantFree->Visible = true;
ShowMessage("Error");
return;
}
ProgressBar1->Min = 0;
ProgressBar1->Max = PagesToFree;
ProgressBar1->Position = 0;
strTemp = Granularity;
BaseAddr = VirtualAlloc(NULL, BytesToFree, MEM_RESERVE, PAGE_NOCACHE |
PAGE_READWRITE);
if(BaseAddr == 0)
{ lblCantFree->Caption = "MEM_RESERVE failed";
lblCantFree->Visible = true;
ShowMessage("Error");
return;
}
BaseAddr = VirtualAlloc(BaseAddr, BytesToFree, MEM_COMMIT, PAGE_NOCACHE |
PAGE_READWRITE);
if(BaseAddr == 0)
{ VirtualFree(BaseAddr, 0, MEM_RELEASE);
lblCantFree->Caption = "MEM_COMMIT failed";
lblCantFree->Visible = true;
ShowMessage("Error");
return;
}
WriteAddress = (int)BaseAddr;
ReadAddress = StrToInt(strTemp);
ShowMessage(Granularity);
while(WriteAddress < ((int)BaseAddr + BytesToFree))
{
CopyMemory((void*)WriteAddress, (void*)ReadAddress, PageSize);
WriteAddress = WriteAddress + PageSize;
ProgressVal = ProgressVal + 1;
ProgressBar1->Position = ProgressVal;
}
VirtualFree(BaseAddr, BytesToFree, MEM_DECOMMIT);
VirtualFree(BaseAddr, 0, MEM_RELEASE);
ProgressBar1->Visible = false;
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::Button1Click(TObject *Sender)
{
GlobalMemoryStatus (&stat);
Label7->Caption=stat.dwMemoryLoad;
Label8->Caption=stat.dwTotalPhys;
Label9->Caption=stat.dwAvailPhys;
Label10->Caption=stat.dwTotalPageFile;
Label11->Caption=stat.dwAvailPageFile;
Label12->Caption=stat.dwTotalVirtual;
Label14->Caption=stat.dwAvailVirtual;
SYSTEM_INFO SysInfo;
GetSystemInfo(&SysInfo);
Granularity = SysInfo.dwAllocationGranularity;
PageSize = SysInfo.dwPageSize;
Label15->Caption = IntToStr(PageSize);
Label16->Caption = IntToStr(Granularity);
}
//--------------------------------------------------------------------------
-
Yahia
Technical Details
Message-ID:
<407c2923@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<40786e5f@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,aeea745eda602570 X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2004-04-13 11:10:09 PST Reply-To: "Yahia El-Qasem" <Yahia.El-Qasemno.spam@mgh.metro-ag.de> From: "Yahia El-Qasem" <Yahia.El-Qasem@mgh.metro-ag.de> Newsgroups: borland.public.cppbuilder.nativeapi References: <40786e5f@newsgroups.borland.com> Subject: Re: CopyMemory API problem Date: Tue, 13 Apr 2004 19:52:53 +0200 Lines: 105 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 NNTP-Posting-Host: 80.133.59.102 Message-ID: <407c2923@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1081878819 80.133.59.102 (13 Apr 2004 10:53:39 -0800) Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail