-1

Can I use member function as a callback in SERVICE_TABLE_ENTRYA structure? It has the type LPSERVICE_MAIN_FUNCTION that is defined as

typedef VOID (WINAPI *LPSERVICE_MAIN_FUNCTIONA)(
    DWORD   dwNumServicesArgs,
    LPSTR   *lpServiceArgVectors
);

The main problem here is WINAPI calling convention (I can't use lambdas like in my previous question). I'm also don't want to use static member function.

Community
  • 1
  • 1
FrozenHeart
  • 19,844
  • 33
  • 126
  • 242
  • Good, because static member functions won't always work properly. Anyway, as it doesn't have any way of passing arbitrary data, you might find this useful: http://stackoverflow.com/questions/18169180/whats-the-best-way-to-wrap-a-c-callback-with-a-c11-interface – chris Dec 12 '13 at 08:55
  • I do suppose you might be able to set up some form communication with the service to send over the object, though. – chris Dec 12 '13 at 09:03
  • 1
    It's difficult to know why you would want a non-static member function. I wonder if there is some deeper mis-understanding that lies behind this question. – David Heffernan Dec 12 '13 at 09:25
  • @DavidHeffernan, As for using a static one, [this page](http://stackoverflow.com/questions/1738313/c-using-class-method-as-a-function-pointer-type#comment1618920_1738425) was quite useful. – chris Dec 12 '13 at 09:27
  • @chris Clearly you need to specify the calling convention on the function, be it static member or non-member. – David Heffernan Dec 12 '13 at 09:29
  • @DavidHeffernan, I feel like I've been doing something wrong for the past half a year since I saw that. I'm so confused at how I managed to mangle that text in such a bad way. – chris Dec 12 '13 at 09:33
  • @chris I must say that I have no idea what point you are making here. I'm confused! – David Heffernan Dec 12 '13 at 09:35
  • 1
    Note that VC++ automatically uses the correct calling convention, so if you're using that compiler you can safely use lambdas with WinAPI callback functions. – Jonathan Potter Dec 12 '13 at 09:37
  • @DavidHeffernan, I managed to ignore the fact that it was just the calling convention and attach some other magical property to static member functions. Well, no harm done for any companies at least, and I'm pretty sure nothing of my own in that time. – chris Dec 12 '13 at 09:37
  • You're using the Win32 API, so you have to follow its rules. If you explain *why* you're trying to use a member function here we can probably tell you how to use an ordinary function to achieve the same purpose. – Harry Johnston Dec 12 '13 at 23:02

1 Answers1

1

You cannot use a non-static member function. They are simply not compatible with that prototype.

You need to something that is compatible, for example a non-member function, or a static member function.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490