Question

Im modifiy my display drivers to get update notifcation sent from the USB port. So far so good, but i got stock on follow:

    GPEFlat::GPEFlat()
{
    PBOOT_ARGS args;
    ULONG      fbSize;
    ULONG      fbOffset;
    ULONG      offsetX;
    ULONG      offsetY;
    BOOL       bFoundArgs = FALSE;

    BOOL        m_MouseDisabled = TRUE;
    HANDLE      m_hAttachEvent = CreateEvent(NULL, FALSE, FALSE, L"MouseAttached");
    HANDLE      m_hDetachEvent = CreateEvent(NULL, FALSE, FALSE, L"MouseDetached");
    HANDLE      m_hCursorThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)MouseEventThread, NULL, 0, NULL);

DWORD 
GPEFlat::MouseEventThread(void)
{
    DWORD   rc = TRUE;
    HANDLE  handles[2];
    handles[0] = m_hAttachEvent;
    handles[1] = m_hDetachEvent;

The resulting error is: Error 1 error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'LPTHREAD_START_ROUTINE' drivers\display\vgaflat

So the line : HANDLE m_hCursorThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)MouseEventThread, NULL, 0, NULL); Dosnt work. Got some pointers that it may be to non static method. How should i do this? Greetings

Was it helpful?

Solution

The thing to understand is that functions (including static methods) and non-static methods are different things. CreateEvent expects a function. You must give it that, it will not work with GPEFlat::MouseEventThread because that's a method. What you can do though is give it a function which calls GPEFlat::MouseEventThread. Usually this is done like this

DWORD WINAPI thread_starter(LPVOID that)
{
    return ((GPEFlat*)that)->MouseEventThread();
}

...

CreateThread(NULL, 0, thread_starter, this, 0, NULL);

Note that I pass this to CreateThread, that's very important. CreateThread passes it to the parameter that in thread_starter, which uses that to call the method you wanted to call all along.

OTHER TIPS

The MouseEventThread have to be a static function, because a function pointer is not the same as a member function pointer. Static methods can be used as normal function pointers, but non-static member functions can not.

If you need to reference class members, then one very simple solution is to have a static wrapper functions, which takes the instance of the object (this in the constructor) and then calls the actual member function using that instance pointer.

Something like

class GPEFlat
{
    // ...

private:
    static DWORD MouseEventThreadWrapper(LPVOID instance)
        { return reinterpret_cast<GPEFlat*>(instance)->MouseEventThread(); }

    // ...
};

Create the thread with this wrapper function instead, passing this as argument to it:

GPEFlat::GPEFlat()
{
    // ...

    HANDLE      m_hCursorThread = CreateThread(
        NULL, 0, (LPTHREAD_START_ROUTINE)MouseEventThreadWrapper, this, 0, NULL);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top