Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Using _beginthreadex in an MFC dll

Using _beginthreadex in an MFC dll

Scheduled Pinned Locked Moved C / C++ / MFC
c++jsonhelpquestion
14 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P PaulJ

    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

    M Offline
    M Offline
    Michael Dunn
    wrote on last edited by
    #4

    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.

    E N 2 Replies Last reply
    0
    • P PaulJ

      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

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #5

      Seems like _beginthreadex does not accept functions of type LPTHREAD_START_ROUTINE but rather functions with the signature (unsigned int (__stdcall *)(void *)) (i.e., returning unsigned int instead of DWORD). Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

      1 Reply Last reply
      0
      • P PaulJ

        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

        T Offline
        T Offline
        Tim Deveaux
        wrote on last edited by
        #6

        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."

        N 1 Reply Last reply
        0
        • M Michael Dunn

          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.

          E Offline
          E Offline
          Ernest Laurentin
          wrote on last edited by
          #7

          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);

          1 Reply Last reply
          0
          • P PaulJ

            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

            N Offline
            N Offline
            Nish Nishant
            wrote on last edited by
            #8

            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

            1 Reply Last reply
            0
            • T Tim Deveaux

              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."

              N Offline
              N Offline
              Nish Nishant
              wrote on last edited by
              #9

              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

              M 1 Reply Last reply
              0
              • M Michael Dunn

                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.

                N Offline
                N Offline
                Nish Nishant
                wrote on last edited by
                #10

                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

                1 Reply Last reply
                0
                • N Nish Nishant

                  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

                  M Offline
                  M Offline
                  Michael Dunn
                  wrote on last edited by
                  #11

                  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.

                  N 1 Reply Last reply
                  0
                  • M Michael Dunn

                    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.

                    N Offline
                    N Offline
                    Nish Nishant
                    wrote on last edited by
                    #12

                    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

                    M 1 Reply Last reply
                    0
                    • N Nish Nishant

                      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

                      M Offline
                      M Offline
                      Michael Dunn
                      wrote on last edited by
                      #13

                      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.

                      N 1 Reply Last reply
                      0
                      • M Michael Dunn

                        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.

                        N Offline
                        N Offline
                        Nish Nishant
                        wrote on last edited by
                        #14

                        :-) 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

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups