Home > Enterprise >  Catch dll function that does not return
Catch dll function that does not return

Time:05-20

i do have to use a .dll library in order to have access to hardware i want to control.

The problem is, that some functions of that dll sometimes do not return. They seem to be stuck in an infinite loop or something.

My idea was to run the function calls in a different thread and kill the thread if it is stuck/does not return after some time. In Order to do that i wrote something like that:

template<typename... multiple>
void MyClass::genericCommandHandler(void *function, multiple... args)
{
    //timer start --> check somewhere else
    std::thread t{&MyClass::threadWorker<void(*)(...)>, function, args...};
    t.join();
}

But it does not compile: error C2672: 'std::invoke': no matching overloaded function found

And another Problem is that std::thread does not have a kill or stop function.

Does anyone have a good idea on how to deal with a dll that does not return?

CodePudding user response:

Isolating the DLL in its own thread does not really help a lot, but the idea makes sense. It might be better to isolate the DLL in its own helper process. You would then terminate the entire helper process.

This is by necessity not a clean exit. That is inherent in the problem. You don't know how the DLL corrupted internal state. That is why the helper process exists: to avoid mingling DLL state and your state.

The C Standard way to mange subprocesses is std::system, but that has the same problem with control. You'll need the native CreateProcess.

As a shotgun method, you could try CancelSynchronousIo on the DLL's thread.

  • Related