Using _beginthreadex in an MFC dll
-
Hi, I'm trying to convert a line of code that calls the API CreateThread function to the _beginthreadex function, but I get the compile error: "cannot convert parameter 3 from 'unsigned long (__stdcall *)(void *)' to 'unsigned int (__stdcall *)(void *)" Parameter 3 is declared as type LPTHREAD_START_ROUTINE. What type should I use to avoid this compile error? Thanks Paul Jahans
-
Hi, I'm trying to convert a line of code that calls the API CreateThread function to the _beginthreadex function, but I get the compile error: "cannot convert parameter 3 from 'unsigned long (__stdcall *)(void *)' to 'unsigned int (__stdcall *)(void *)" Parameter 3 is declared as type LPTHREAD_START_ROUTINE. What type should I use to avoid this compile error? Thanks Paul Jahans
Try using this sort of thread proc function :- DWORD ThreadFunc(DWORD data) { //thread stuff in here } Sonork ID 100.9786 voidmain www.busterboy.org If you don't find me on CP, I'll be at Bob's HungOut
-
Try using this sort of thread proc function :- DWORD ThreadFunc(DWORD data) { //thread stuff in here } Sonork ID 100.9786 voidmain www.busterboy.org If you don't find me on CP, I'll be at Bob's HungOut
The thread function is in the calling program and is declared like this: DWORD WINAPI CGUITermDoc::CommReader(void *pvData) From calling program, I call the DLL function: StartCommThread(LPTHREAD_START_ROUTINE CommProc, void *pvData) In this function, I try to pass CommProc to the _beginthreadex function, but this is where I get the compile error. Paul Jahans
-
The thread function is in the calling program and is declared like this: DWORD WINAPI CGUITermDoc::CommReader(void *pvData) From calling program, I call the DLL function: StartCommThread(LPTHREAD_START_ROUTINE CommProc, void *pvData) In this function, I try to pass CommProc to the _beginthreadex function, but this is where I get the compile error. Paul Jahans
See the VC forum FAQ. --Mike-- My really out-of-date homepage "Hey, you wanna go to the Espresso Pump and get sugared up on mochas?" -- Willow Rosenberg Sonork - 100.10414 AcidHelm Big fan of Alyson Hannigan.
-
The thread function is in the calling program and is declared like this: DWORD WINAPI CGUITermDoc::CommReader(void *pvData) From calling program, I call the DLL function: StartCommThread(LPTHREAD_START_ROUTINE CommProc, void *pvData) In this function, I try to pass CommProc to the _beginthreadex function, but this is where I get the compile error. Paul Jahans
Seems like
_beginthreadex
does not accept functions of typeLPTHREAD_START_ROUTINE
but rather functions with the signature(unsigned int (__stdcall *)(void *))
(i.e., returningunsigned int
instead ofDWORD
). Joaquín M López Muñoz Telefónica, Investigación y Desarrollo -
Hi, I'm trying to convert a line of code that calls the API CreateThread function to the _beginthreadex function, but I get the compile error: "cannot convert parameter 3 from 'unsigned long (__stdcall *)(void *)' to 'unsigned int (__stdcall *)(void *)" Parameter 3 is declared as type LPTHREAD_START_ROUTINE. What type should I use to avoid this compile error? Thanks Paul Jahans
This doesn't answer your question, but might be of interest From the docs for CWinThread: "The CWinThread class is necessary to make your code and MFC fully thread-safe. Thread-local data used by the framework to maintain thread-specific information is managed by CWinThread objects. Because of this dependence on CWinThread to handle thread-local data, any thread that uses MFC must be created by MFC. For example, a thread created by the run-time function _beginthreadex cannot use any MFC APIs."
-
See the VC forum FAQ. --Mike-- My really out-of-date homepage "Hey, you wanna go to the Espresso Pump and get sugared up on mochas?" -- Willow Rosenberg Sonork - 100.10414 AcidHelm Big fan of Alyson Hannigan.
Yes, that's it! you are missing the
static
in your function definition. (Just in case it was not clear yet!)static UINT WINAPI CommReader(void *pvData);
-
The thread function is in the calling program and is declared like this: DWORD WINAPI CGUITermDoc::CommReader(void *pvData) From calling program, I call the DLL function: StartCommThread(LPTHREAD_START_ROUTINE CommProc, void *pvData) In this function, I try to pass CommProc to the _beginthreadex function, but this is where I get the compile error. Paul Jahans
You cannot use a non-static member function of a class as your thread proc. Nish p.s. make it static, or make it a global function not part of any class Sonork ID 100.9786 voidmain www.busterboy.org If you don't find me on CP, I'll be at Bob's HungOut
-
This doesn't answer your question, but might be of interest From the docs for CWinThread: "The CWinThread class is necessary to make your code and MFC fully thread-safe. Thread-local data used by the framework to maintain thread-specific information is managed by CWinThread objects. Because of this dependence on CWinThread to handle thread-local data, any thread that uses MFC must be created by MFC. For example, a thread created by the run-time function _beginthreadex cannot use any MFC APIs."
Tim This is serious stuff. This means we cannot use MFC functions and CRT functions together in any thread. I mean to use CRT functions safely it is always advised to use _beginthreadex. Now you say MFC wont go along with that. This is a bad situation Nish Sonork ID 100.9786 voidmain www.busterboy.org If you don't find me on CP, I'll be at Bob's HungOut
-
See the VC forum FAQ. --Mike-- My really out-of-date homepage "Hey, you wanna go to the Espresso Pump and get sugared up on mochas?" -- Willow Rosenberg Sonork - 100.10414 AcidHelm Big fan of Alyson Hannigan.
Funny how almost 30% of questions asked here are already in the FAQ Nish Sonork ID 100.9786 voidmain www.busterboy.org If you don't find me on CP, I'll be at Bob's HungOut
-
Tim This is serious stuff. This means we cannot use MFC functions and CRT functions together in any thread. I mean to use CRT functions safely it is always advised to use _beginthreadex. Now you say MFC wont go along with that. This is a bad situation Nish Sonork ID 100.9786 voidmain www.busterboy.org If you don't find me on CP, I'll be at Bob's HungOut
Nish [BusterBoy] wrote: This means we cannot use MFC functions and CRT functions together in any thread. No, that's incorrect. Look at the source for
CWinThread::CreateThread()
and you'll see that it calls_beginthreadex()
. The restriction on how you create the thread ensures that MFC is correctly initialized. Since MFC uses the CRT, MFC handles initializing the CRT itself. --Mike-- My really out-of-date homepage "Hey, you wanna go to the Espresso Pump and get sugared up on mochas?" -- Willow Rosenberg Sonork - 100.10414 AcidHelm Big fan of Alyson Hannigan. -
Nish [BusterBoy] wrote: This means we cannot use MFC functions and CRT functions together in any thread. No, that's incorrect. Look at the source for
CWinThread::CreateThread()
and you'll see that it calls_beginthreadex()
. The restriction on how you create the thread ensures that MFC is correctly initialized. Since MFC uses the CRT, MFC handles initializing the CRT itself. --Mike-- My really out-of-date homepage "Hey, you wanna go to the Espresso Pump and get sugared up on mochas?" -- Willow Rosenberg Sonork - 100.10414 AcidHelm Big fan of Alyson Hannigan.Okay, MIke. But here is one more question! Say I am writing a console based app [NT service] that uses MFC. Now can I use _beginthreadex to start my worker threads and expect to use MFC functions safely? Nish Sonork ID 100.9786 voidmain www.busterboy.org If you don't find me on CP, I'll be at Bob's HungOut
-
Okay, MIke. But here is one more question! Say I am writing a console based app [NT service] that uses MFC. Now can I use _beginthreadex to start my worker threads and expect to use MFC functions safely? Nish Sonork ID 100.9786 voidmain www.busterboy.org If you don't find me on CP, I'll be at Bob's HungOut
Use AfxBeginThread() to start them. :) --Mike-- My really out-of-date homepage "Hey, you wanna go to the Espresso Pump and get sugared up on mochas?" -- Willow Rosenberg Sonork - 100.10414 AcidHelm Big fan of Alyson Hannigan.
-
Use AfxBeginThread() to start them. :) --Mike-- My really out-of-date homepage "Hey, you wanna go to the Espresso Pump and get sugared up on mochas?" -- Willow Rosenberg Sonork - 100.10414 AcidHelm Big fan of Alyson Hannigan.
:-) Thanks! Nish Sonork ID 100.9786 voidmain www.busterboy.org If you don't find me on CP, I'll be at Bob's HungOut