I need to write a callback function because I will be using the following function:

EnumChildWindows(windowHandle, EnumChildProc, 0);

where the second variable "EnumChildProc" needs to be a callback function.

The problem is that I am having difficulties when writing the function in both the header and source file. Normally when I write a boolean function or any other type, I would first delcare it in the header the following way:

bool myFunction(int var1);

and if the previous file was in file.h, I would then write the following code in the source file:

bool file::myFunction (int var1)
{
  ///here would be my code
}

but with a callback function I tried doing the following and it didn't work:

in the file.h:

BOOL CALLBACK EnumChildProc(HWND windowHandle, LPARAM lParam);

in the file.cpp:

BOOL file::CALLBACK EnumChildProc(HWND windowHandle, LPARAM lParam)
{
   ///code goes here
}

It doesnt work because I get the following error:

error: expected unqualified-id before '__attribute__'

and the error refers to the following line in the source code:

BOOL programLoop::CALLBACK EnumChildProc(HWND windowHandle, LPARAM lParam)

any ideas what I am doing wrong? I don't know if I am doing something wrong when I declare the callback function in the header or source file or somewhere... what do you guys think?

有帮助吗?

解决方案

CALLBACK is just a macro that adds a specialized Microsoft attribute to to a function declaration. Use it ONLY in a declaration, not where you use the function.

The second parameter of EnumChildWindows is declared as a WNDENUMPROC, which is declared as

    typedef BOOL (CALLBACK* WNDENUMPROC)(HWND, LPARAM);

so you would declare your callback function as

    BOOL CALLBACK myFunction (HWND w, LPARAM var1);

or

    BOOL CALLBACK file::myFunction (HWND w, LPARAM var1);

The scope qualifier :: always comes right between the scope name (in this case file) and the dependent name (myFunction)

If file is a namespace then you're all set; if file is a class remember to also declare myFunction static in the class. You may also need extern in certain circumstances.

When you enumerate child windows

    HWND hwparent=/* handle of the window you want to enumerate */;
    LPARAM var1=/* the value you want to pass in*/;

    EnumChildWindows (hwParent, myFunction, var1);

Windows calls your function for each window, passing the w parameter in so you know which window it is and var1 which is your parameter.


As for where: It is not necessary to declare myFunction in the header file. Most likely only the call to EnumChildWindows uses myFunction. It's not going to be called from any other file. So you're OK as long as myFunction comes before the call to EnumChildWindows in the .cpp file.

Of course, if file is a class, then the static member function myFunction has to be inside the class declaration, which usually goes in a header somewhere.

When you use the function, you only need to add file:: if the place you call it from is not already in the file (class or namespace) scope.

If file is a class you need file:: where you define myFunction, but don't need file:: if you're calling EnumChildWindows in another class member function.

  // .h file

  class file 
  { 
   private:

   HWND m_hwParent;

   static HRESULT myFunction (HWND hw, LPARAM arg1); 

   public:

   HRESULT OnAllChildWindows (LPARAM arg1);

   // more stuff
  };

  // .cpp file 

  HRESULT file::myFunction (HWND hw, LPARAM arg1) 
  {
   // whatever this does
  }

  HRESULT file::OnAllChildWindows (LPARAM arg1)
  {
   EnumChildWindows (m_hwParent, myFunction, arg1);
  }

If file is a namespace you don't need file:: if you wrapped all your code in a namespace block:

  namespace file {

      HRESULT myFunction (HWND hw, LPARAM arg1) 
      {
       // whatever this does
      }

      HRESULT OnAllChildWindows (HWND hwParent, LPARAM arg1)
      {
       EnumChildWindows (myFunction, arg1);
      }
   }

but if you didn't, or if it's a different scope:

  HRESULT someting_else::OnAllChildWindows (HWND hwParent, LPARAM arg1)
  {
   EnumChildWindows (hwParent, file::myFunction, arg1);
  }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top