dllinter-process-communicatpausing-execution

Pausing a DLL export until triggered by an EXE


Does anyone have any ideas of how a DLL function can be made to wait for “input”, and how to call a specific instance of a DLL export?


I’m trying to convert a Windows service to a DLL. It contains a function that logs some boot-up information and then waits until told to quit. The logging functionality is worked out, but I need to figure out two issues.

  1. After it performs its main functions, the export needs to sit there and wait (like the classic Press any key to continue…, but minus the interface)
  2. I need a way of having an executable subsequently tell the paused instance that it’s time to exit

For the first problem, I considered going into a loop and waiting on some sort of trigger, but of course it should not go into a 100%-CPU cycle, so maybe WaitForSingleObject or perhaps waiting for a message (eg WM_APP).

For the second, I thought of some kind of inter-process communication, but hopefully not something as messy as shared-memory or semaphores (I used shared-mem, semaphores, signals, etc. in Unix in uni, but this is on Windows). Of course I need a way of accessing the specific instance of the called export.


Solution

  • You can use CreateEvent, SetEvent, and WaitForSingleObject. If the dll was loaded by the executable that needs to signal the event that is all that is required. If it is from separate executables it is only slightly more complicated. When you call CreateEvent, create a named Event. This named event can be accessed by multiple processes. If it needs to work across different users logged in, prefix the name with "Global\" and it will be the same event for all processes for all users.

    //in dll
    HANDLE eventHandle = CreateEvent( NULL, TRUE, FALSE, "Global\\My-Unique-Trigger-Event" );
    //do stuff
    WaitForSingleObject( eventHandle, INFINITE);
    //exit
    
    //in executable
    HANDLE eventHandle = CreateEvent( NULL, TRUE, FALSE, "Global\\My-Unique-Trigger-Event" );
    SetEvent( eventHandle );