⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.cpp

📁 操作系统知识
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
   : TForm(Owner)
{
}
//---------------------------------------------------------------------------


void __fastcall TForm1::FormCreate(TObject *Sender)
{
   int nHandleNum = 0;
   Pointer Address;
   int X, Y, Total;

   hMapFile = CreateFileMapping((HANDLE)(0xFFFFFFFF),
      NULL,
      PAGE_READWRITE,
      0,
      10000,
      "S08IMappingFile");
   if( hMapFile!=NULL )
      pMapFile = MapViewOfFile(hMapFile,
         FILE_MAP_ALL_ACCESS,
         0,
         0,
         0);
   else
      ShowMessage( "内存映射文件句柄(hMapFile)为空" );

   if( pMapFile==NULL )
   {
      ShowMessage( "获取的内存映射文件指针(pMapFile)为空" );
      exit(0);
   }
   else
   {
      Address = PChar(pMapFile) + 100*4;
      while( *(PInteger(Address))!=0 )
      {
         nHandleNum++;
         Address = PChar(Address) + 4;
      }
      if( *(PInteger(Address))==0 && nHandleNum<100 )
      {
         *(PInteger(Address)) = (Integer)Handle;   // 当前实例的句柄
      }
      else
      {
         ShowMessage("监视程序实例超过100个。");
         exit(0);
      }
   }

   // 初始化Chart1对象:
   Chart1->SeriesList->Series[0]->Clear();
   for( X=0; X<10; X++ )
   {
      Address = PChar(pMapFile) + X*4;
      Total = *(PInteger(Address));
      Chart1->SeriesList->Series[0]->AddY(Total, IntToStr(X+1), clRed);
   }
}
//---------------------------------------------------------------------------
MESSAGE void TForm1::WmUser(TMessage Msg)
{
   Integer X, Total;
   Pointer Address;

   // 初始化Chart1对象:
   Chart1->SeriesList->Series[0]->Clear();
   for( X=0; X<10; X++ )
   {
      Address = PChar(pMapFile) + X*4;
      Total = *(PInteger(Address));
      Chart1->SeriesList->Series[0]->AddY(Total, IntToStr(X+1), clRed);
   }
}
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
   bool bFindHandle = false;
   Pointer Address, NextAddress;

   // 删除内存映射文件中当前实例的句柄
   Address = PChar(pMapFile) +100*4;

   while( *(PInteger(Address))!=0 && !bFindHandle )
   {
      if( *(PInteger(Address))==(Integer)Handle )
      {
         bFindHandle = true;
         NextAddress = PChar(Address) + 4;
         while( *(PInteger(NextAddress))!=0 )
         {
            *(PInteger(Address)) = *(PInteger(NextAddress));
            Address = NextAddress;
            NextAddress = PChar(Address) + 4;
         }
         *(PInteger(Address)) = 0;
      }
      Address = PChar(Address) + 4;
   }

   UnmapViewOfFile(pMapFile);
   CloseHandle(hMapFile);
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -