Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

memory mapping

Status
Not open for further replies.

csbrmg

Programmer
Joined
Jul 1, 2003
Messages
9
Location
ZA
I seem have a few problems returning a reference to a shared memory location and keep on getting a run time at the specified location below...
this is the code

class function TActivityLogger.NewInstance: TObject;
var
llInit:Boolean;
temp:LoggerPointer;
begin
HMapping:=CreateFileMapping($FFFFFFFF,nil,PAGE_READWRITE,0,MAPFILESIZE,pchar('my mem'));
...
llInit:=(getLastError()<>ERROR_ALREADY_EXISTS);
if ( llInit ) then //first instance
begin
temp:=mapViewOfFile(HMapping,fILE_MAP_all_access,0,0,0);
....
Result:=temp^.instance ;
end
else
begin
HMapping:=openfilemapping( FILE_MAP_all_access,true, pchar('my mem'));
...
temp:=MapViewOfFile(HMapping,FILE_MAP_all_access,0,0,0);
...
Result:=temp^.instance;//<-Runtime error occurs here
end;
end;
 
From reading the doco, I'd hazard a guess that the way you are cerating the HMapping ref is the problem:

HMapping:=CreateFileMapping($FFFFFFFF,nil,PAGE_READWRITE,0,MAPFILESIZE,pchar('my mem'));


According the the doco you need to specify the LPSECURITY_ATTRIBUTES so that you can inherit the handle later on (as you are attempting with):

HMapping:=openfilemapping( FILE_MAP_all_access,true, pchar('my mem'));


I HATE working with security attributes and almost always create them as 'anyone/everyone has access'. In you case, its not a problem as y're obviously using hte mapping file for sharing info with other processes or the like - have fun with the synchronisation :).

Here is some sample code for creating nice, insecure security attributes:

var
FSecurityAttributes: TSecurityAttributes;
FSD: TSecurityDescriptor;
begin
InitializeSecurityDescriptor(@FSD, SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(@FSD, True, nil, False);
FSecurityAttributes.nLength := Sizeof(FSecurityAttributes);
FSecurityAttributes.lpSecurityDescriptor := @FSD;
FSecurityAttributes.bInheritHandle := True;
end;


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top