一个普通程序和一个SERVER级程序交换内存映像
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
Count: PInteger;
MappingHandle,HookMutex: THandle;implementation{$R *.dfm}const
SHAREMAPPING = 'ShareMapping';
Hook_MUTEX_NAME = 'MUTEX_NAME';function ReadCount: Integer;
begin
Result := Count^;
end;
procedure WriteCount(const I: Integer);begin
Count^ := I;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
Edit1.Text:=IntToStr(ReadCount);
end;procedure TForm1.Button2Click(Sender: TObject);
var
i:integer;
begin
i:=0;
HookMutex:=CreateMutex(nil,false,Hook_MUTEX_NAME);
if GetLastError() = ERROR_ALREADY_EXISTS then
WaitForSingleObject(HookMutex,infinite); //等待斥元完成 WriteCount(I);
ReleaseMutex(HookMutex);
CloseHandle(HookMutex);
end;procedure AdjustToken;
var
hdlProcessHandle : Cardinal;
hdlTokenHandle : Cardinal;
tmpLuid : Int64;
tkpPrivilegeCount : Int64;
tkp : TOKEN_PRIVILEGES;
tkpNewButIgnored : TOKEN_PRIVILEGES;
lBufferNeeded : Cardinal;
Privilege : array[0..0] of _LUID_AND_ATTRIBUTES;
begin
hdlProcessHandle := GetCurrentProcess;
OpenProcessToken(hdlProcessHandle,
(TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY),
hdlTokenHandle); // Get the LUID for shutdown privilege.
LookupPrivilegeValue('', 'SeShutdownPrivilege', tmpLuid);
Privilege[0].Luid := tmpLuid;
Privilege[0].Attributes := SE_PRIVILEGE_ENABLED;
tkp.PrivilegeCount := 1; // One privilege to set
tkp.Privileges[0] := Privilege[0];
// Enable the shutdown privilege in the access token of this
// process.
AdjustTokenPrivileges(hdlTokenHandle,
False,
tkp,
Sizeof(tkpNewButIgnored),
tkpNewButIgnored,
lBufferNeeded); end;procedure TForm1.FormCreate(Sender: TObject);
begin
AdjustToken; // FILE_MAP_ALL_ACCESS PAGE_READWRITE
MappingHandle:=Openfilemapping(PAGE_READWRITE,false,ShareMapping);
Count :=MapViewOfFile(MappingHandle, FILE_MAP_READ,0, 0, SizeOf(Count)); //不是FILE_MAP_READ的话, Count总是Nil
if MappingHandle=0 then
begin
Button1.Enabled:=false;
Button2.Enabled:=false;
end;
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
UnMapViewOfFile(Count);
end;end.
看了一下帮助File Mapping Security and Access RightsThe Windows security model enables you to control access to file mapping objects. For more information, see Access-Control Model.
You can specify a security descriptor for a file mapping object when you call the CreateFileMapping function. If you specify NULL, the object gets a default security descriptor. The ACLs in the default security descriptor for a file mapping object come from the primary or impersonation token of the creator.To retrieve the security descriptor of a file mapping object, call the GetNamedSecurityInfo or GetSecurityInfo function. To set the security descriptor of a file mapping object, call the SetNamedSecurityInfo or SetSecurityInfo function.The valid access rights for file mapping objects include the DELETE, READ_CONTROL, WRITE_DAC, and WRITE_OWNER standard access rights. File mapping objects do not support the SYNCHRONIZE standard access right. The following table lists the specific access rights for file mapping objects.Access right Meaning
FILE_MAP_ALL_ACCESS Includes all access rights to a file mapping object. The MapViewOfFile and MapViewOfFileEx functions treat this the same as if you had specified FILE_MAP_WRITE.
FILE_MAP_COPY Copy-on-write access. If you create the map with PAGE_WRITECOPY and the view with FILE_MAP_COPY, you will receive a view to the file.
If you share the mapping between multiple processes using DuplicateHandle or OpenFileMapping and one process writes to a view, the modification is not propagated to the other process.
Windows Me/98/95: If you share the mapping between multiple processes using DuplicateHandle or OpenFileMapping and one process writes to a view, the modification is propagated to the other process.
FILE_MAP_READ Read-only access. The file mapping object must be created with PAGE_READWRITE or PAGE_READONLY protection. A read-only view of the file is mapped.
FILE_MAP_WRITE Read and write access. The file mapping object must be created with PAGE_READWRITE protection. A read/write view of the file is mapped. You can request the ACCESS_SYSTEM_SECURITY access right to a file mapping object if you want to read or write the object's SACL. For more information, see Access-Control Lists (ACLs) and SACL Access Right.说权限不够,请高手帮忙写写代码 让Count :=MapViewOfFile(MappingHandle, FILE_MAP_ALL_ACCESS,0, 0, SizeOf(Count)); 可以执行成功
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
Count: PInteger;
MappingHandle,HookMutex: THandle;implementation{$R *.dfm}const
SHAREMAPPING = 'ShareMapping';
Hook_MUTEX_NAME = 'MUTEX_NAME';function ReadCount: Integer;
begin
Result := Count^;
end;
procedure WriteCount(const I: Integer);begin
Count^ := I;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
Edit1.Text:=IntToStr(ReadCount);
end;procedure TForm1.Button2Click(Sender: TObject);
var
i:integer;
begin
i:=0;
HookMutex:=CreateMutex(nil,false,Hook_MUTEX_NAME);
if GetLastError() = ERROR_ALREADY_EXISTS then
WaitForSingleObject(HookMutex,infinite); //等待斥元完成 WriteCount(I);
ReleaseMutex(HookMutex);
CloseHandle(HookMutex);
end;procedure AdjustToken;
var
hdlProcessHandle : Cardinal;
hdlTokenHandle : Cardinal;
tmpLuid : Int64;
tkpPrivilegeCount : Int64;
tkp : TOKEN_PRIVILEGES;
tkpNewButIgnored : TOKEN_PRIVILEGES;
lBufferNeeded : Cardinal;
Privilege : array[0..0] of _LUID_AND_ATTRIBUTES;
begin
hdlProcessHandle := GetCurrentProcess;
OpenProcessToken(hdlProcessHandle,
(TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY),
hdlTokenHandle); // Get the LUID for shutdown privilege.
LookupPrivilegeValue('', 'SeShutdownPrivilege', tmpLuid);
Privilege[0].Luid := tmpLuid;
Privilege[0].Attributes := SE_PRIVILEGE_ENABLED;
tkp.PrivilegeCount := 1; // One privilege to set
tkp.Privileges[0] := Privilege[0];
// Enable the shutdown privilege in the access token of this
// process.
AdjustTokenPrivileges(hdlTokenHandle,
False,
tkp,
Sizeof(tkpNewButIgnored),
tkpNewButIgnored,
lBufferNeeded); end;procedure TForm1.FormCreate(Sender: TObject);
begin
AdjustToken; // FILE_MAP_ALL_ACCESS PAGE_READWRITE
MappingHandle:=Openfilemapping(PAGE_READWRITE,false,ShareMapping);
Count :=MapViewOfFile(MappingHandle, FILE_MAP_READ,0, 0, SizeOf(Count)); //不是FILE_MAP_READ的话, Count总是Nil
if MappingHandle=0 then
begin
Button1.Enabled:=false;
Button2.Enabled:=false;
end;
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
UnMapViewOfFile(Count);
end;end.
看了一下帮助File Mapping Security and Access RightsThe Windows security model enables you to control access to file mapping objects. For more information, see Access-Control Model.
You can specify a security descriptor for a file mapping object when you call the CreateFileMapping function. If you specify NULL, the object gets a default security descriptor. The ACLs in the default security descriptor for a file mapping object come from the primary or impersonation token of the creator.To retrieve the security descriptor of a file mapping object, call the GetNamedSecurityInfo or GetSecurityInfo function. To set the security descriptor of a file mapping object, call the SetNamedSecurityInfo or SetSecurityInfo function.The valid access rights for file mapping objects include the DELETE, READ_CONTROL, WRITE_DAC, and WRITE_OWNER standard access rights. File mapping objects do not support the SYNCHRONIZE standard access right. The following table lists the specific access rights for file mapping objects.Access right Meaning
FILE_MAP_ALL_ACCESS Includes all access rights to a file mapping object. The MapViewOfFile and MapViewOfFileEx functions treat this the same as if you had specified FILE_MAP_WRITE.
FILE_MAP_COPY Copy-on-write access. If you create the map with PAGE_WRITECOPY and the view with FILE_MAP_COPY, you will receive a view to the file.
If you share the mapping between multiple processes using DuplicateHandle or OpenFileMapping and one process writes to a view, the modification is not propagated to the other process.
Windows Me/98/95: If you share the mapping between multiple processes using DuplicateHandle or OpenFileMapping and one process writes to a view, the modification is propagated to the other process.
FILE_MAP_READ Read-only access. The file mapping object must be created with PAGE_READWRITE or PAGE_READONLY protection. A read-only view of the file is mapped.
FILE_MAP_WRITE Read and write access. The file mapping object must be created with PAGE_READWRITE protection. A read/write view of the file is mapped. You can request the ACCESS_SYSTEM_SECURITY access right to a file mapping object if you want to read or write the object's SACL. For more information, see Access-Control Lists (ACLs) and SACL Access Right.说权限不够,请高手帮忙写写代码 让Count :=MapViewOfFile(MappingHandle, FILE_MAP_ALL_ACCESS,0, 0, SizeOf(Count)); 可以执行成功
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货