CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2004
    Location
    Denmark
    Posts
    64

    [RESOLVED] EnumWindowsProc as a member function?

    Hi all

    I'm going to enum all windows using EnumWindows, as I need to check some Z order between some window handles that my program has.

    I know I have to make a callback function to use EnumWindows, but as I'm relatively new to pointers to functions, I'm wondering if it's possible to make the callback function a member function of the class that is calling EnumWindows.

    I know pointers to functions and pointers to member functions doesn't like to be mixed, and that the size of the address can be different. So is it impossible?

    To clearly specify what I want, here is some pseudo code.
    Code:
    class CMyClass
    {
        public:
            BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam);
            void SomeFunction()
            {
                BOOL (CMyClass::*pEnumProc)(HWND hwnd, LPARAM lParam);
                pEnumProc = CMyClass::EnumWindowsProc;
                EnumWindows(pEnumProc, 0);
            }
    };
    Some syntax might be wrong, I just wrote it, and the editor can't compile it. But I think it shows what it is I want to archive.

    So, can this be done, or is not possible?

    Thanks in advance!

  2. #2
    Join Date
    May 2005
    Posts
    4,954

    Re: EnumWindowsProc as a member function?

    You should declare it as static.
    Code:
    class CMyClass
    {
        public:
            static BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam);
    :
    :
    };
    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  3. #3
    Join Date
    Jan 2004
    Location
    Denmark
    Posts
    64

    Re: EnumWindowsProc as a member function?

    Is that a requirement to reach my goal, or just a suggestion?

  4. #4
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: EnumWindowsProc as a member function?

    Quote Originally Posted by ReneG
    Is that a requirement to reach my goal?
    Definitely.
    See THIS ARTICLE.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  5. #5
    Join Date
    Jan 2004
    Location
    Denmark
    Posts
    64

    Re: EnumWindowsProc as a member function?

    Great link ovidiucucu! Thanks alot! It explains all the questions I had about pointers to member functions!

    And FYI, I've rated both of your posts. Thanks for your replies!

  6. #6
    Join Date
    Jan 2004
    Location
    Denmark
    Posts
    64

    Re: EnumWindowsProc as a member function?

    Okay, I tried implementing the EnumWindowsProc callback function, but I get a compiler error.
    Code:
    error C2664: 'EnumWindows' : cannot convert parameter 1 from 'bool (__stdcall *)(HWND,LPARAM)' to 'WNDENUMPROC'
    The callback function is defined like this in my class header file:
    Code:
    	static bool CALLBACK EnumCallback(HWND hwnd, LPARAM lParam);
    The function is defined in the protected section of my class.

    I try to use the EnumWindows in a member function of the same class, like this:
    Code:
    ::EnumWindows(&CSnapWebConference::EnumCallback, (LPARAM)this);
    That gives the error I mentioned above.

    I've tried changing it to
    Code:
    ::EnumWindows(&EnumCallback, (LPARAM)this);
    and
    Code:
    ::EnumWindows(EnumCallback, (LPARAM)this);
    Neither one works.

    I can't figure out what i'm doing wrong, but I'm guessing my function signature is incorrect, or incompatible with WNDENUMPROC, that EnumWindows expects.

    Any help is highly appreciated!
    Last edited by ReneG; June 2nd, 2006 at 05:43 AM.

  7. #7
    Join Date
    Jan 2004
    Location
    Denmark
    Posts
    64

    Re: EnumWindowsProc as a member function?

    I solved it!

    The problem was not with the function signature as expected, but with the syntax in the call to EnumWindows. All I had to change was the call to EnumWindows. The changed line looks like this:
    Code:
    ::EnumWindows((WNDENUMPROC)this->EnumCallback, (LPARAM)this);
    I found out after reading this article from Microsoft.

    Thanks alot anyway.

  8. #8
    Join Date
    Jul 2020
    Location
    SLC
    Posts
    1

    Re: [RESOLVED] EnumWindowsProc as a member function?

    Here is another point that might save some of you visitors some time if you have this problem:

    Make sure you are using the return type BOOL and not bool since callbacks require the old school int version!

    I could not get it working even with all the above help until I realized I also need to change this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured