超难....的内存映像权限问题,请达人指点!!!,不够分再加
2009-04-08 09:16
323 查看
超难....的内存映像权限问题,请达人指点!!!,不够分再加 Delphi / Windows SDK/API
http://www.delphi2007.net/DelphiAPI/html/delphi_20061106100206298.html
一个普通程序和一个SERVER级程序交换内存映像
unit Unit1;
interface
uses
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 Rights
The 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)); 可以执行成功
关注!
http://www.delphi2007.net/DelphiAPI/html/delphi_20061106100206298.html
一个普通程序和一个SERVER级程序交换内存映像
unit Unit1;
interface
uses
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 Rights
The 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)); 可以执行成功
关注!
相关文章推荐
- Eclipse处理图像,内存分配不够的问题
- Ubuntu_“bash: /home/user/java/jdk1.7.0/bin/java: 权限不够”的问题
- 跨进程访问共享内存的权限问题
- hadoop编程遇到的jvm问题为内存不够的解决办法
- 遇到个问题想发帖、发现积分不够、有高手路过指点一下吧!
- 提问 问题标签用户 当向Bitmap对象加载图片时,出现奇怪内存不够问题。
- 跨进程访问共享内存的权限问题
- 前一段时间编译xmms后运行遇到libmpg123.so权限不够,不能播放mp3的问题
- VNC viewer 客户端权限不够的问题
- 解决Myeclipse8.5总是报内存不够。修改了Myeclipse.ini配置文件却不生效问题
- 在删除电脑上一些东西权限不够问题
- PHP使用fopen()函数打开文件提示权限不够问题
- Eclpise设置内存大小,解决登录项目页面时,内存不够问题
- Ubuntu desktop 权限设置 用root登录 以及 声音问题 、显示windows文本的中文乱码 、内存大关swap、亮度、装QQ 、 鼠标和触控板、cacti系统性能监控
- 跨进程访问共享内存的权限问题
- 全志H3平台应用层使用串口出现权限不够问题解决
- 编译android遇到java虚拟机堆内存不够的问题
- linux下共享内存等文件的权限问题
- Android权限不够问题
- scott下没有创建视图权限不够问题