Link to home
Start Free TrialLog in
Avatar of snehanshu
snehanshu

asked on

Security Attributes in CreateFileMapping

Hi!
 This is my first post to the exchange.
 I managed to create a DLL that hooks to keyboard and mouse events.
 It runs fine with normal applications, but when I try to use it from a windows service (TService), I get an access violation error.

The code that does not work is:

     hObjHandle := CreateFileMapping ($FFFFFFFF, nil, PAGE_READWRITE, 0,
       dwAllocSize, 'HookRecMemBlock');

GetLastError returns 5 (Access denied)
I searched the web and it seems that there is a windows security related issue.  I think the problem lies in the second parameter, but do not know what should I replace the nil with.

It seems for now that I found a workaround: by using journalrecord (WH_JOURNALRECORD) within the service instead of using a DLL,
(Ref: http://www.swissdelphicenter.ch/torry/showcode.php?id=1729),
but still, would like to know about the security attributes in delphi.

Can some body help please?

Thank you,
...Snehanshu
ASKER CERTIFIED SOLUTION
Avatar of DaFox
DaFox

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of snehanshu
snehanshu

ASKER

Thank you Mark!
That did solve my problem. What a relief!
But the version check did not work (Version = WinNT), the compiler says WinNT is an undeclared identifier. Any fix for that? (I am compiling with Delphi 5).
The answer very much solved my problem, so I shall mark the answer as accepted, but it would be nice if you could let me know the solution for the version check.
Also, I have a query on whether I should use a hook or a journalrecord. It would be nice if you could have a look at that to.
(Ref: https://www.experts-exchange.com/questions/20750833/Difference-between-JournalRecord-and-Other-hooks.html)

Thanks again,
...Snehanshu
Hi Snehanshu,

sorry, my fault. You just have to check for the platform (NT platform does need the SecurityDescriptor, 9x doesn't).
Just use this function instead of Version = WinNT:

function IsNT: Boolean;
var
  OS: TOSVERSIONINFO;
begin
  OS.dwOSVersionInfoSize := SizeOf(TOSVERSIONINFO);
  GetVersionEx(OS);
  Result := (OS.dwPlatformId = VER_PLATFORM_WIN32_NT);
end;

-

// ...
  lpHookRec:=NIL;
  if (isNT) then
  begin
    SecurityAttr.nLength:=SizeOf(SECURITY_ATTRIBUTES);
// ...

Markus

PS: I will take a look at your other question. Dunno if I have an answer for you though.
Thank you Markus!
Now, my DLL is all-platform compatible!
:)

...Snehanshu