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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CALLBACK aka __stdcall

CALLBACK aka __stdcall

Scheduled Pinned Locked Moved C / C++ / MFC
c++jsonhelpquestion
8 Posts 6 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.
  • I Offline
    I Offline
    Igor Mihailov
    wrote on last edited by
    #1

    Hello all. Help me please: I need to make a callback function as a member of my class (Win32 API, no MFC). Is it possible? Thanks.

    P M I A 4 Replies Last reply
    0
    • I Igor Mihailov

      Hello all. Help me please: I need to make a callback function as a member of my class (Win32 API, no MFC). Is it possible? Thanks.

      P Offline
      P Offline
      palbano
      wrote on last edited by
      #2

      sure but it has to be static which means there is no object instance scope when the function is called. -pete

      L 1 Reply Last reply
      0
      • P palbano

        sure but it has to be static which means there is no object instance scope when the function is called. -pete

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Sorry, I can't fully understand the idea. Can you give a code example?

        P 1 Reply Last reply
        0
        • I Igor Mihailov

          Hello all. Help me please: I need to make a callback function as a member of my class (Win32 API, no MFC). Is it possible? Thanks.

          M Offline
          M Offline
          Michael Dunn
          wrote on last edited by
          #4

          See the FAQ 6.1 Why can't I use a member function as a callback?[^] --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- There is a saying in statistics that a million monkeys pounding on typewriters would eventually create a work of Shakespeare. Thanks to the Internet, we now know that this is not true.

          1 Reply Last reply
          0
          • L Lost User

            Sorry, I can't fully understand the idea. Can you give a code example?

            P Offline
            P Offline
            palbano
            wrote on last edited by
            #5

            /** Win32 CreateThread class wrapper */
            class base_w32thread{
            protected:
            HANDLE _handle;
            DWORD _dwTID;
            base_w32thread():_handle(0), _dwTID(0L){}
            virtual ~base_w32thread()=0;

            public:
            bool start(LPSECURITY_ATTRIBUTES psecattrs = NULL, DWORD dwCreateFlags = 0L);
            operator HANDLE(){ return _handle; }
            protected:
            virtual void run()=0;
            virtual void onEndThread(){}
            static long WINAPI threadfnc( LPARAM lp);
            };

            threadfnc is the callback that is sent as the argument to CreateThread() below

            /** start the thread */
            bool base_w32thread::start(LPSECURITY_ATTRIBUTES psecattrs, DWORD dwCreateFlags){

            assert( !\_handle);
            if( \_handle)
            	return false;
            
            \_handle = ::CreateThread( psecattrs, 0, 
            	(LPTHREAD\_START\_ROUTINE)threadfnc, this, dwCreateFlags, &\_dwTID);
            return (\_handle)?true:false;
            

            }

            Then in the callback function there is no "this" pointer so we have sent the object pointer as the User Parameter to CreateThread and we cast it to the correct type

            /** Thread function */
            long WINAPI base_w32thread::threadfnc( LPARAM lp){

            assert( lp);
            base\_w32thread\* pThis = (base\_w32thread\*)lp;
            if( pThis)
            	pThis->run();
            
            pThis->\_handle = 0L;		// thread exited
            pThis->onEndThread();
            return 0L;
            

            }

            Hope that helps

            "No matter where you go, there your are." - Buckaroo Banzai

            -pete

            I 1 Reply Last reply
            0
            • P palbano

              /** Win32 CreateThread class wrapper */
              class base_w32thread{
              protected:
              HANDLE _handle;
              DWORD _dwTID;
              base_w32thread():_handle(0), _dwTID(0L){}
              virtual ~base_w32thread()=0;

              public:
              bool start(LPSECURITY_ATTRIBUTES psecattrs = NULL, DWORD dwCreateFlags = 0L);
              operator HANDLE(){ return _handle; }
              protected:
              virtual void run()=0;
              virtual void onEndThread(){}
              static long WINAPI threadfnc( LPARAM lp);
              };

              threadfnc is the callback that is sent as the argument to CreateThread() below

              /** start the thread */
              bool base_w32thread::start(LPSECURITY_ATTRIBUTES psecattrs, DWORD dwCreateFlags){

              assert( !\_handle);
              if( \_handle)
              	return false;
              
              \_handle = ::CreateThread( psecattrs, 0, 
              	(LPTHREAD\_START\_ROUTINE)threadfnc, this, dwCreateFlags, &\_dwTID);
              return (\_handle)?true:false;
              

              }

              Then in the callback function there is no "this" pointer so we have sent the object pointer as the User Parameter to CreateThread and we cast it to the correct type

              /** Thread function */
              long WINAPI base_w32thread::threadfnc( LPARAM lp){

              assert( lp);
              base\_w32thread\* pThis = (base\_w32thread\*)lp;
              if( pThis)
              	pThis->run();
              
              pThis->\_handle = 0L;		// thread exited
              pThis->onEndThread();
              return 0L;
              

              }

              Hope that helps

              "No matter where you go, there your are." - Buckaroo Banzai

              -pete

              I Offline
              I Offline
              Igor Mihailov
              wrote on last edited by
              #6

              Thanks a lot. I'll try my best.

              1 Reply Last reply
              0
              • I Igor Mihailov

                Hello all. Help me please: I need to make a callback function as a member of my class (Win32 API, no MFC). Is it possible? Thanks.

                I Offline
                I Offline
                Indrawati
                wrote on last edited by
                #7

                Just curious... is CALLBACK exaactly the same ad __stdcall? Can I use them interchangeably?

                1 Reply Last reply
                0
                • I Igor Mihailov

                  Hello all. Help me please: I need to make a callback function as a member of my class (Win32 API, no MFC). Is it possible? Thanks.

                  A Offline
                  A Offline
                  Archer282
                  wrote on last edited by
                  #8

                  I had a problem with CALLBACK functions in MFC when i made my process/windows viewer someone on CodePrject showed me a way to get around this, ill declare one function as static BOOL CALLBACK myfunc( LPARAM lParam ); and one as (you can use the same name if you like VC++ will pick the right one ) BOOL myfunc2( LPARAM lParam ); when call your callback call the static one with the lparam as "this" ie. EnumWindows( myfunc, (LPARAM) this); then have your static function call the second BOOL CALLBACK myfunc( LPARAM lParam ) { CMyDialog * me = (CMyDialog*) lParam; return me->myfunc2(); }

                  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