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

📄 unit2.cpp

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

#include <vcl.h>
#pragma hdrstop

#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
   : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm2::FormCreate(TObject *Sender)
{
   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)为空" );
   else
   {
      randomize();
      Pointer Address;

      nPosition = 0;
      do
      {
         nPosition++;
         Address = PChar(pMapFile) + (nPosition-1)*4;
      } while( *(PInteger(Address))!=0 );
      if( nPosition<=10 )
      {
         Label1->Caption = "工位: " + IntToStr(nPosition);
         *(PInteger(Address)) = *(PInteger(Address)) + (random(10)+1);
      }
      else
      {
         ShowMessage("已经没有工位(最多10个)。");
         exit(0);
      }
   }
}
//---------------------------------------------------------------------------

void __fastcall TForm2::FormDestroy(TObject *Sender)
{
   Pointer Address;

   // 删除内存映射文件中当前实例
   Address = PChar(pMapFile) +(nPosition-1)*4;
   *(PInteger(Address)) = 0;

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

void __fastcall TForm2::Timer1Timer(TObject *Sender)
{
   Pointer Address;

   Address = PChar(pMapFile) + (nPosition-1)*4;
   *(PInteger(Address)) = *(PInteger(Address)) + (random(10)+1);

   // 向监视程序发送WM_USER消息
   Address = PChar(pMapFile) +100*4;
   while( *(PInteger(Address))!=0 )
   {
      PostMessage((HANDLE)(*(PInteger(Address))), WM_USER, 0, 0);
      Address = PChar(Address) + 4;
   }
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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