cast pointer to member to pointer


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: cast pointer to member to pointer

  1. #1
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604

    cast pointer to member to pointer

    Hi,

    I have a problem programming a Windows service where I use a generic CService class to encapsulate the functionality. The Win API requires for the functions RegisterServiceCtrlHandler(...) and StartServiceCtrlDispatcher(...) callback-functions of the form:

    VOID WINAPI ServiceMain(
    DWORD dwArgc, // number of arguments
    LPTSTR *lpszArgv // array of argument string pointers
    );

    VOID WINAPI Handler(
    DWORD fdwControl // requested control code
    );

    When I try to make the Handler and ServiceMain functions members of my class I get type cast errors
    Error C2440: '=' : cannot convert from 'void (__stdcall CNTService::*)(unsigned long,char ** )' to 'void (__stdcall *)(unsigned long,char ** )'and
    error C2664: 'RegisterServiceCtrlHandlerA' : cannot convert parameter 2 from 'void (unsigned long)' to 'void (__stdcall *)(unsigned long)'

    Is there an elegant way to cast a pointer to a member to a simple pointer?

    Thanks,

    Dieter

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    There's no way to convert a pointer to a nonstatic member function to a pointer to an ordinary function. The common trick is to declare the member function as static. Static member functions' addresses are in essence good old C pointers to functions.
    As for the second argument: the problem is probably with the calling convention used. You need to cast the void* argument to the correct type explicitly:

    typedef VOID (*PF)(DWORD);

    func( (PF) your_void_ptr );

    I believe that __stdcall is the default calling convention so there's no need to specify it.
    Danny Kalev

  3. #3
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    Thanks Danny,
    I knew about that work-around. I just wanted to be able to use non-static members inside my callback. I resorted to adding a static pointer to "this" to my class initialised with NULL and subsequently set it to the fully initialised object. I think this is a bit untidy, but it's at least workable...
    Cheers,

    Dieter

  4. #4
    Join Date
    Nov 2003
    Posts
    4,118
    Remember that a pointer to member is not really a pointer but a data structure conatining 2-3 integers so there's no way to convert it to an ordinary pointer without losing data. There is a workaround though: http://gethelp.devx.com/techtips/cpp.../10min0800.asp
    Danny Kalev

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center