Solved

Memory mapped files troubles

Posted on 2003-11-26
4
1,087 Views
Last Modified: 2007-12-19
I’m having a bit of trouble with a dll that uses memory mapped files. Since my application must receive key presses when out of focus, I used a small bit of delphi code I found on the net to build a keyhook. The dll in in the apps resources as a piece of rcdata, and is unpacked to a temporary file, then loaded, and finally deleted when the app is done. Or it should be. If I just open the app, then close it, the file is deleted fine. But if I trigger a keypress, then close it, nu-uh. After reading the sdk doc for DeleteFile, I figured it was the memory mapped files doing this. But I can’t figure what I am doing wrong. Here’s the applicable code:

{ Dll loading code }

var
    DllHandle, MemFile: THandle;
    Receiver: PHandle;
    HookOn, HookOff: procedure; stdcall;

...

  MemFile := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, SizeOf(Integer), 'KeyReceiver');
  if (MemFile = 0) then
    raise Exception.Create('Error while creating file');

  Receiver := MapViewOfFile(MemFile, FILE_MAP_WRITE, 0, 0, 0);
  Receiver^ := Handle;

  // Load the keyhook
  DllHandle := LoadLibrary(PChar(DllFile)); //DllFile contains the temp name
  if DllHandle = 0 then
    raise Exception.Create('Unable to load the required DLL');

  @HookOn := GetProcAddress(DllHandle, '_HookOn@0');
  @HookOff := GetProcAddress(DllHandle, '_HookOff@0');
  if (not Assigned(HookOn)) or (not Assigned(HookOff)) then
    raise Exception.Create('Unable to locate the required DLL functions');

  HookOn;

Here’s the DLL init code:

HHOOK Keybrd;
HANDLE MemFile, DllInst;
HWND *Receiver;

...

  switch (ul_reason_for_call)
  {
    case DLL_PROCESS_ATTACH:
      DllInst = hModule;
      MemFile = OpenFileMapping(FILE_MAP_READ, FALSE, "KeyReceiver");
      if (MemFile != 0)
        Receiver = MapViewOfFile(MemFile, FILE_MAP_READ, 0, 0, 0);      
      break;
    case DLL_PROCESS_DETACH:
      if (MemFile != 0) {
        UnmapViewOfFile(Receiver);
        CloseHandle(MemFile);
      }
      break;
  }

Hook activation code:

Keybrd = SetWindowsHookEx(WH_KEYBOARD, &HookCallBack, DllInst, 0);

Deactivation code:

UnhookWindowsHookEx(Keybrd);

The code to send a message to the app:

  if (Code == HC_ACTION)
    PostMessage(*Receiver, CM_SEND_KEY, wParam, lParam);

  return CallNextHookEx(Keybrd, Code, wParam, lParam);

And the code that frees the app’s vars and deletes (or tries to) the dll:

  if Assigned(HookOff) then
    HookOff;

  if (DllHandle > 0) then
    FreeLibrary(DllHandle);

  if (MemFile > 0) then begin
    UnmapViewOfFile(Receiver);
    CloseHandle(MemFile);
  end;

  if FileExists(DllFile) then
    DeleteFile(DllFile);

The most I can find is that windows reports Access is denied when it tries to delete the file (if I have pressed a key, if not, it deletes fine). Also, the file can be deleted manually once the app is closed.

If any more info is needed, just say.

Thanks!
0
Comment
Question by:SpiderVenom
4 Comments
 
LVL 6

Accepted Solution

by:
GloomyFriar earned 125 total points
ID: 9831382
Does the DllFile contain right pathname at the error point?
If so, then someone is using the dll and that is why you can't delete it.
0
 
LVL 1

Expert Comment

by:pucko
ID: 25505444
Hmm force accept on a Question the same day?
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question