Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6063985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:13:50+00:00 2026-05-23T09:13:50+00:00

How do I convert a member function pointer to the TIMERPROC type for use

  • 0

How do I convert a member function pointer to the TIMERPROC type for use with the WINAPI SetTimer? The code snippet below shows how I’m doing it now, but when I compile I get this error:

error C2664: ‘SetTimer’ : cannot convert parameter 4 from ‘void (__stdcall CBuildAndSend::* )(HWND,UINT,UINT_PTR,DWORD)’ to ‘TIMERPROC’

The callback needs to be tied to its originating class instance. If there is a better way to do that, I’m all ears. Thanks.

class CMyClass
{
public:
    void (CALLBACK CBuildAndSend::*TimerCbfn)( HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime );

private:
    void CALLBACK TimeoutTimerProc( HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime );
};

CMyClass::CMyClass()
{
    ...

    this->TimerCbfn = &CBuildAndSend::TimeoutTimerProc;

    ...

    ::CreateThread(
        NULL,                           // no security attributes
        0,                              // use default initial stack size
        reinterpret_cast<LPTHREAD_START_ROUTINE>(BasThreadFn), // function to execute in new thread
        this,                           // thread parameters
        0,                              // use default creation settings
        NULL                            // thread ID is not needed
        )
}

void CALLBACK CMyClass::TimeoutTimerProc( HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime )
{
    ...
}

static DWORD MyThreadFn( LPVOID pParam )
{
    CMyClass * pMyClass = (CMyClass *)pParam;

    ...

    ::SetTimer( NULL, 0, BAS_DEFAULT_TIMEOUT, pMyClass->TimerCbfn ); // <-- Error Here

    ...
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:13:50+00:00Added an answer on May 23, 2026 at 9:13 am

    Member-function and TIMEPROC are not compatible types.

    You need to make the member function static. Then it will work, assuming parameter-list is same in both, static member function and TIMEPROC.

    class CMyClass
    {
    public:
        //modified
        void (CALLBACK *TimerCbfn)(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);
    
    private:
        //modified
        static void CALLBACK TimeoutTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime );
    };
    

    Function pointer as well as member function both are modified. Now it should work.

    Now since the callback function became static, it cannot access the non-static members of the class, because you don’t have this pointer in the function.

    To access the non-static members, you can do this:

    class CMyClass
    {
    public:
    
        static void CALLBACK TimerProc( HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime );
    
        //add this static member
        static std::map<UINT_PTR, CMyClass*> m_CMyClassMap; //declaration
    };
    
    //this should go in the  CMyClass.cpp file
    std::map<UINT_PTR, CMyClass*> CMyClass::m_CMyClassMap;  //definition
    
    static DWORD MyThreadFn( LPVOID pParam )
    {
        CMyClass * pMyClass = (CMyClass *)pParam;
    
        UINT_PTR id = ::SetTimer( NULL, 0, BAS_DEFAULT_TIMEOUT, CMyClass::TimerProc);
    
        //store the class instance with the id as key!        
        m_CMyClassMap[id]= pMyClass; 
    }
    
    void CALLBACK CMyClass::TimerProc( HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime )
    {
        //retrieve the class instance
        CMyClass *pMyClass= m_CMyClassMap[idEvent];
    
        /*
          now using pMyClass, you can access the non-static 
          members of the class. e.g
          pMyClass->NonStaticMemberFunction();
        */
    }
    

    I removed TimerCbfn from my implementation, as it doesn’t really needed. You can pass TimerProc directly to SetTimer as last argument.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I convert function input parameters to the right type? I want to
I am trying to create a pointer to a member function which has default
I want to convert function object to function. I wrote this code, but it
I want to pass a member function of class A to class B via
What's the best way to call a member function if you have an object
I have a problem with using a pointer to function in C++. Here is
I'm trying to use a class member as a callback but the compiler gives
I'm trying to get a pointer to a specific version of an overloaded member
Now I am try to use boost bind & mem_fn . But there's a
There is a C code, which we are trying to convert into C++ code.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.