0

All, I'm trying to implement EnumWindows behavior in order to close a running executable. I have created a standard class (h/cpp) and only have a couple simple functions in there. The callback function i have defined is causing compilation errors, which clear up as soon as i remove the class namespacing. See code below.

class ProcessLauncher()
{
public:
    ProcessLauncher() {}
    ~ProcessLauncher() {}

    bool CloseApplicationByTitle(const std::string& title)
    {
        //error says: argument of type "BOOL (__stdcall ProcessLauncher::*)(HWND hwnd, LPARAM lParam)" is incompatible with parameter of type "WNDENUMPROC"
        EnumWindows(DoActionByTitle, (LPARAM)WM_CLOSE);
    }

private:
    BOOL CALLBACK DoActionByTitle(HWND hwnd, LPARAM lParam)
    {
        //do stuff
    }
};

SO what in the world am i doing wrong here?

Jason
  • 2,147
  • 6
  • 32
  • 40
  • Change the `DoActionByTitle` function to a `static` one. A pointer to a non-static member function is not the same animal as a "normal" function pointer. There must be a dup for this on SO somewhere. – PaulMcKenzie Sep 28 '18 at 17:39
  • Unfortunately, i can't. the code within the DoActionByTitle uses a member variable. – Jason Sep 28 '18 at 17:46
  • @OP -- How would the `EnumWindows` callback be able to call your function? The Windows API is C-based -- it knows nothing about objects, member functions, etc. To call `DoActionByTitle`, it needs to do something like `obj->DoActionByTitle()` or `obj.DoActionByTitle()` -- somewhere the object has to be stated. But again, Windows API knows absolutely nothing about objects or classes. – PaulMcKenzie Sep 28 '18 at 17:47
  • The simplest thing to do is to pass `this` as the `LPARAM` of EnumWindows and cast the `LPARAM` back into the object inside `DoActionByTitle`. I would post an answer, but I would rather you search SO as to why you get the error. It is basically because function pointers are not the same type. Now how to overcome this issue of having non-static data -- you ask 10 programmers, and you will get 11 answers each with their advantages and disadvantages. The `LPARAM` is one way, but if you need more sophistication, there are other techniques, too many to have a simple answer for. – PaulMcKenzie Sep 28 '18 at 17:52
  • Appreciate the comments. This code was ported from a really old cold base and i was trying to replicate exactly how it was done originally. I underestimated how much the call was going to be affected by the slight change of putting it within a proper class. i will continue to research. This was just a squirrel i was chasing for a friday afternoon activity. – Jason Sep 28 '18 at 18:03

0 Answers0