Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. EnumWindows...

EnumWindows...

Scheduled Pinned Locked Moved C / C++ / MFC
helpcomjsonquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    adamUK
    wrote on last edited by
    #1

    Just a quickie, I have one member function HWND CAutoRestartDlg::GetProcessWnd() { ... EnumWindows((WNDENUMPROC)CAutoRestartDlg::GetWndProc,(LPARAM)0); .... } and also my callback function: BOOL CALLBACK CAutoRestartDlg::GetWndProc(HWND hwnd, LPARAM lparam) { ... } When I try to compile, I get error C2440: 'type cast' : cannot convert from '' to 'int (__stdcall *)(struct HWND__ *,long)' What am I doing wrong? Would appreciate some help. Sorry if it sounds a bit thick but I am feeling very thick at the moment.:confused: Many thanks Adam. www.beachwizard.com/travelogue[^] "I spent a lot of my money on booze, birds and fast cars. The rest I just squandered" George Best.

    J T 2 Replies Last reply
    0
    • A adamUK

      Just a quickie, I have one member function HWND CAutoRestartDlg::GetProcessWnd() { ... EnumWindows((WNDENUMPROC)CAutoRestartDlg::GetWndProc,(LPARAM)0); .... } and also my callback function: BOOL CALLBACK CAutoRestartDlg::GetWndProc(HWND hwnd, LPARAM lparam) { ... } When I try to compile, I get error C2440: 'type cast' : cannot convert from '' to 'int (__stdcall *)(struct HWND__ *,long)' What am I doing wrong? Would appreciate some help. Sorry if it sounds a bit thick but I am feeling very thick at the moment.:confused: Many thanks Adam. www.beachwizard.com/travelogue[^] "I spent a lot of my money on booze, birds and fast cars. The rest I just squandered" George Best.

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      Check Mike Dunn's C++ FAQ[^], §6.1. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

      A 1 Reply Last reply
      0
      • A adamUK

        Just a quickie, I have one member function HWND CAutoRestartDlg::GetProcessWnd() { ... EnumWindows((WNDENUMPROC)CAutoRestartDlg::GetWndProc,(LPARAM)0); .... } and also my callback function: BOOL CALLBACK CAutoRestartDlg::GetWndProc(HWND hwnd, LPARAM lparam) { ... } When I try to compile, I get error C2440: 'type cast' : cannot convert from '' to 'int (__stdcall *)(struct HWND__ *,long)' What am I doing wrong? Would appreciate some help. Sorry if it sounds a bit thick but I am feeling very thick at the moment.:confused: Many thanks Adam. www.beachwizard.com/travelogue[^] "I spent a lot of my money on booze, birds and fast cars. The rest I just squandered" George Best.

        T Offline
        T Offline
        TyMatthews
        wrote on last edited by
        #3

        Make sure GetWndProc is declared as a static function in your CAutoRestartDlg class. Most any time you define a Win32 API callback function inside a class you need to make the function static. This is because the Win32 API is written in C and C can't handle C++ classes and their polymorphic abilities. The API needs to know at exactly what position in memory your function resides so that it can call it. Polymorphism allows run-time determination of function addresses. Static functions -and static member variables, too- are allocated once when your app begins and never move for the duration of your app's existence. Only one copy of a static function exists in memory for the entire class; regardless of how many instances of the class you create. Static functions are basically identical in nature to any old C-style function you'd create outside of your class. The only difference is that they're located in your classes' namespace, so you can keep things neat and know where to look for the code. That's really the only reason why they're even allowed in a class; you don't get any object-oriented features with static functions. One additional note to preempt further frustration... static functions inside C++ classes cannot directly access anything inside the class except other static member variables or static functions. IE if you had an integer member variable named m_iMyVar, you could not read it nor set it unless it were also declared as being static. This is because only one copy of the function for the entire class exists; it would not know which object instance to use when setting the member variable or calling the other function. If you want to actually use something inside your CAutoRestartDlg class you'll have to use the LPARAM portion of your call to EnumWindows() and pass along a pointer to the class or a struct which contains a pointer. I can whip up some code if you'd like to see an example of this. Sorry for the over-explanation if you knew this stuff already :) I guessed from your code that you probably already tried various casts.

        Ty

        "The significant problems we face cannot be solved at the same level of thinking we were at when we created them." -Albert Einstein

        A 1 Reply Last reply
        0
        • T TyMatthews

          Make sure GetWndProc is declared as a static function in your CAutoRestartDlg class. Most any time you define a Win32 API callback function inside a class you need to make the function static. This is because the Win32 API is written in C and C can't handle C++ classes and their polymorphic abilities. The API needs to know at exactly what position in memory your function resides so that it can call it. Polymorphism allows run-time determination of function addresses. Static functions -and static member variables, too- are allocated once when your app begins and never move for the duration of your app's existence. Only one copy of a static function exists in memory for the entire class; regardless of how many instances of the class you create. Static functions are basically identical in nature to any old C-style function you'd create outside of your class. The only difference is that they're located in your classes' namespace, so you can keep things neat and know where to look for the code. That's really the only reason why they're even allowed in a class; you don't get any object-oriented features with static functions. One additional note to preempt further frustration... static functions inside C++ classes cannot directly access anything inside the class except other static member variables or static functions. IE if you had an integer member variable named m_iMyVar, you could not read it nor set it unless it were also declared as being static. This is because only one copy of the function for the entire class exists; it would not know which object instance to use when setting the member variable or calling the other function. If you want to actually use something inside your CAutoRestartDlg class you'll have to use the LPARAM portion of your call to EnumWindows() and pass along a pointer to the class or a struct which contains a pointer. I can whip up some code if you'd like to see an example of this. Sorry for the over-explanation if you knew this stuff already :) I guessed from your code that you probably already tried various casts.

          Ty

          "The significant problems we face cannot be solved at the same level of thinking we were at when we created them." -Albert Einstein

          A Offline
          A Offline
          adamUK
          wrote on last edited by
          #4

          Hey Ty, That's excellent. A big THANKS! Really, more explanation is better than less, for me anyway. I got really fed up late last night wondering I kept getting the error message and not being able to figure it out. It's just that the explanation of the error does not give any indication that the function would have to be declared static. Hence my frustration! Thanks once again! Totally appreciated. :-D Adam. www.beachwizard.com/travelogue[^] "I spent a lot of my money on booze, birds and fast cars. The rest I just squandered" George Best.

          D 1 Reply Last reply
          0
          • J Joaquin M Lopez Munoz

            Check Mike Dunn's C++ FAQ[^], §6.1. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

            A Offline
            A Offline
            adamUK
            wrote on last edited by
            #5

            Thanks Joaquin, I can't see for looking some days. I appreciate your help!! Cheerio for now and thanks once again. Adam. www.beachwizard.com/travelogue[^] "I spent a lot of my money on booze, birds and fast cars. The rest I just squandered" George Best.

            1 Reply Last reply
            0
            • A adamUK

              Hey Ty, That's excellent. A big THANKS! Really, more explanation is better than less, for me anyway. I got really fed up late last night wondering I kept getting the error message and not being able to figure it out. It's just that the explanation of the error does not give any indication that the function would have to be declared static. Hence my frustration! Thanks once again! Totally appreciated. :-D Adam. www.beachwizard.com/travelogue[^] "I spent a lot of my money on booze, birds and fast cars. The rest I just squandered" George Best.

              D Offline
              D Offline
              Daniel Lohmann
              wrote on last edited by
              #6

              Okay, now you have managed level 1, let's go up to level 2 :-D Using only static member functions (also called class functions) or ordinary C functions as callbacks is boring and odd. But you can use adapters to "translate" a call to a static function seamless into a call to a real member function. If you are interested in more technical details about the "callback has to be static" issue and a complete solution for it you may want to check out my article Use member functions for C-style callbacks and threads - a general solution. However, you don't have to read the whole article, you could also just use the provided adapters. It allows you to write code as simple as the following:

              #include "stdafx.h"
              #include "win_adapter.h" // This file contains all you need

              struct DATA { int foo; double bar; };

              class Test
              {
              public:
              // Callback for EnumWindows using a user defined type for the UserData parameter
              // You can pass there anything you want
              virtual BOOL WindowLister( HWND hWnd, const DATA& data )
              {
              printf(" Window: %p\n", hWnd );
              return true;
              }

              // Some function that needs to enumerate windows
              void DoStuff()
              {
                  // Calling EnumWindows with a member function as callback
                  DATA someData;
                  win::EnumWindows( this, &Test::WindowLister, someData );
               }
              

              };

              Programming is fun! :cool: -- Daniel Lohmann http://www.losoft.de (Hey, this page is worth looking! You can find some free and handy NT tools there :-D )

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups