Hello. I am trying to convert my chat client from a C++ program that runs in DOS into one that runs in a Windows Dialog. I am using Visual C++. However, I am already getting a second error (please understand that there have been many before this that I have been able to fix....):

_beginthread(CIlikerpsChatClientDlg::ThreadMessages, 0, NULL);

Here is the declaration of ThreadMessages:

void CIlikerpsChatClientDlg::ThreadMessages(void *dummy)

On my DOS server (I hadn't had to use multithreading in my client), this worked fine, although now I am getting the error "error C2664: '_beginthread' : cannot convert parameter 1 from 'void (void *)' to 'void (__cdecl *)(void *)'"

Recommended Answers

All 8 Replies

ThreadMessages() must be a static method of the c++ class or a global function outside any class. I prefer using CreateThread() because it does not require linking to special multi-threading libraries like _beginthread().

Ah, that makes sense now; my server had it as a global class. CreateProcess() has so many parameters. Well, better start learning now; maybe I'll get through them by the end of the month. :P

Most of the parameters to CreateThread are 0 (NULL). The only required parameters are the name of the thread proc and the last one -- pointer to DWORD threadID. I leave all others 0.

The parameter immediately following the thread proc is a parameter you want to pass to the thread -- it can be a pointer to any object or a simple integer. Sometimes I pass the "this" pointer so that the thread will have access to c++ class instance.

Er, could someone possible provide a simple example (it would be best if you could do one that might be similar to the setting I described) in which the function CreateThread is used? The one at microsoft.com for CreateThread, like pretty much all of their examples, is too much, rather than just a simple example.

This is the minimum required -- all parameters but two are optional and left 0. On W2K and XP the last parameter can be 0 too, but it comes in useful sometimes.

#include <windows.h>

DWORD WINAPI ThreadProc(LPVOID lpParameter)
{

	return 0;
}

int main()
{
	DWORD dwThreadID;
	HANDLE hThread = CreateThread(0, //lpThreadAttributes 
		0,	//dwStackSize
		ThreadProc,//lpStartAddress
		0,	// lpParameter 
		0,	// dwCreationFlags 
		&dwThreadID);//lpThreadId 

							return 0;
}

Thank you, that works :)

However, I need it to be able to use some variables in the class that creates the thread. You mentioned something about using "this", but I don't know if I should put that as a parameter for the ThreadProc function as well as in the CreateThread function.

Just think about it -- its is nothing more than like passing a variable to any other function

DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
      // typecast lpParameter back to desired c++ class
       CIlikerpsChatClientDlg* pDlg = (CIlikerpsChatClientDlg*)lpParameter;
       pDlg->DoSomething();
       // blabla
	return 0;
}
void CIlikerpsChatClientDlg::foo()
{
	DWORD dwThreadID;
	HANDLE hThread = CreateThread(0, //lpThreadAttributes 
		0,	//dwStackSize
		ThreadProc,//lpStartAddress
		this,	// lpParameter 
		0,	// dwCreationFlags 
		&dwThreadID);//lpThreadId 
}

Oh! I apolgize for my stupidity. I hadn't known it could be type-casted. I have almost never found type-casting to be useful, because when I need it, there is no type cast conversion avaliable for that type to the one I want, so therefore I didn't think of it. I was unsure that the parameter actually had the data. Again, I apologize for being dimwitted. :(

Thank you, however, for being patient enough to say that, though :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.