1

invalid conversion from 'DWORD (*)(void*)' to 'DWORD (*)(void*)' . cake==lie 1==0

I have no idea what this means ... I get it in this code

HANDLE CPlugin::CreateWinampThread()    ||
{                                  __VVVVVVVV__
    hWinampThreadHandle = (HANDLE)CreateThread(NULL, 0, StartWinampThread, (void*)this, 0, &dwWinampThreadID);
    if (!hWinampThreadHandle)
        return 0;

     CloseHandle(hWinampThreadHandle);
     return hWinampThreadHandle;
}

.

DWORD  WINAPI CPlugin::StartWinampThread(void* lpParam)[...]
n00b
  • 5,642
  • 2
  • 30
  • 48
  • 1
    Is CPlugin::StartWinampThread static? You can't pass a non-static member function as a callback, although I'd expect the compiler to mention something about "thiscall" if you were trying to (but maybe your compiler doesn't?). – Leo Davidson Dec 20 '10 at 17:11
  • Why don't you tell us the *type* of the variables you're using in CreateThread()? also, in which line you're getting that error? – Nawaz Dec 20 '10 at 17:12
  • nope its not...oh stupid me .... so i guess ill make a proxy function for it... – n00b Dec 20 '10 at 17:13
  • leo post it as answer ^^ – n00b Dec 20 '10 at 17:15
  • @noob32, S'okay, might as well accept Dialecticus's answer. He was probably typing at the same time as me anyway. :) – Leo Davidson Dec 20 '10 at 17:23
  • hmm if i make the same simplified in another project it works here not o0 still same error so its not related to this ;/ – n00b Dec 20 '10 at 17:24

3 Answers3

5

StartWinampThread must be static if it is member function.

Dialecticus
  • 16,400
  • 7
  • 43
  • 103
  • because this points out a uber failure on my side i accept it as answer but the thing that solved the error is adding a (LPTHREAD_START_ROUTINE) cast – n00b Dec 20 '10 at 17:34
  • Don't cast, that just hides a problem. Whatever it might be. – Hans Passant Dec 20 '10 at 17:54
  • @n00b32: If createThread is a C function that expects a function then passing a static member function is the wrong solution it must be a extern "C" function. Using a static member function you are just getting lucky that the current version of the compiler uses the same ABI for static members as C functions (this is not guaranteed). – Martin York Dec 20 '10 at 18:32
  • Also: http://stackoverflow.com/questions/2068022/in-c-is-it-safe-portable-to-use-static-member-function-pointer-for-c-api-callb – Martin York Dec 20 '10 at 18:40
2

See here: in-c-is-it-safe-portable-to-use-static-member-function-pointer-for-c-api-callb for why you need to use a extern "C"

The correct way would be somthing like this:

HANDLE CPlugin::CreateWinampThread()    ||
{                                  __VVVVVVVV__
    hWinampThreadHandle = (HANDLE)CreateThread(NULL, 0, ::StartWinampThread, (void*)this, 0, &dwWinampThreadID);
    if (!hWinampThreadHandle)
        return 0;

     CloseHandle(hWinampThreadHandle);
     return hWinampThreadHandle;
}

.

// A callback function for C code must have C linkage.
// That is not possable for C++ static member functions.
extern "C" DWORD  WINAPI StartWinampThread(void* lpParam)
{
    // You can always call your static member function here.
    CPlugin::StartWinampThread(lpParam)
}
Community
  • 1
  • 1
Martin York
  • 257,169
  • 86
  • 333
  • 562
0

Is it to do with objects - there's an implicit 'this' paramter to your object's StartWinampThread method because it's a member of a class.

What happens if you change it to be a standalone method but keep the same signature i.e. from

DWORD  WINAPI CPlugin::StartWinampThread(void* lpParam)[...]

to DWORD WINAPI StartWinampThread(void* lpParam)[...]

(I know it won't work for you, I'm just interested to see if it removes the compiler's complaint)

deanWombourne
  • 38,189
  • 13
  • 98
  • 110
  • changed it to DWORD WINAPI StartWinampThreadProxy(void* param){ CPlugin* ths=(CPlugin*)param; ths->StartWinampThread(param); } same error – n00b Dec 20 '10 at 17:20