0

I'm having a problem with _beginthread in microsoft visual studio c++ 10 express: my code:

void __cdecl DashThread( void * Args ) // function without any class refs
{
    while(1){
        MessageBox::Show("work");
        Sleep(5000);
    }
    _endthread();
}

private:
    System::Void button8_Click_1(System::Object^  sender, System::EventArgs^  e) {
        HANDLE HDash = ( HANDLE ) _beginthread(DashThread, 0, NULL );
    }

and errors:

error C3641: 'DashThread' : invalid calling convention '__cdecl ' for function compiled with /clr:pure or /clr:safe

error C2664: 'beginthread' : cannot convert parameter 1 from 'void (_cdecl *)(void *)' to 'void (__cdecl *)(void *)'

Christian Rau
  • 45,360
  • 10
  • 108
  • 185
Luke
  • 2,350
  • 6
  • 26
  • 41

2 Answers2

1

Try to buid your program with /clr instead of /clr:pure.

See http://msdn.microsoft.com/en-us/library/k8d11d4s.aspx

qianfg
  • 878
  • 5
  • 8
  • 1
    or better still, either don't build it with /clr at all, or use the CLR's threading facilities – jalf Jun 28 '12 at 13:57
  • msv says that i can't edit commandline of compilator ;/ when i'm trying to add /clr as additional option i'm having next errors in linker: 1>Memory.obj : error LNK2005: "void __cdecl DashThread(void *)" (?DashThread@@$$FYAXPAX@Z) already defined in General.obj 1>Memory.obj : error LNK2005: "void __cdecl DashThread(void *)" (?DashThread@@YAXPAX@Z) already defined in General.obj 1>tb.obj : error LNK2005: "void __cdecl DashThread(void *)" (?DashThread@@$$FYAXPAX@Z) already defined in General.obj 1>tb.obj : error LNK2005: "void __cdecl DashThread(void *)" (?DashThread@@YAXPAX@Z) already... – Luke Jun 28 '12 at 14:18
  • @user1100671 You don't need to change the compiler command line. There is an entry in the project properties dialog for this option. – Christian Rau Jun 28 '12 at 15:18
1

From the compiler error it seems you are compiling your project with /clr:pure or /clr:safe (in which case you are not programming in C++, but C++/CLI by the way) and thus cannot use the __cdecl calling convention, which is in turn required by _beginthread.

If you are programming in C++/CLI (and thus .NET) anyway, then why not just use .NET's threading facilities instead of the strange pseudo-standard-Win32-wrapper _beginthread?

If you want to use C++/CLI, but still be able to use good old _beginthread, then try to compile it with just /clr instead of /clr:pure, which allows non-managed functions that can have __cdecl calling convention.

Christian Rau
  • 45,360
  • 10
  • 108
  • 185
  • @user1100671 Don't know, I don't have much experience with .NET. But I'm pretty sure this large framework has some nice and easy to use object oriented threading components, which at least fit better to the rest of your code (of course you cannot program in .NET and then use some "ugly-non-OOP" free functions ;)). Or was this a rethorical question? – Christian Rau Jun 28 '12 at 15:17