Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8692667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:21:42+00:00 2026-06-13T00:21:42+00:00

How do I start a thread using _beginthreadex() , making it execute void myFunction(wchar_t

  • 0

How do I start a thread using _beginthreadex(), making it execute void myFunction(wchar_t *param);? I try to use this:

_beginthread(NULL, 0, myFunction, L"someParam", 0, &ThreadID);

but there is compilation error:

error C2664: ‘beginthreadex’ : cannot convert parameter 3 from ‘void (_cdecl *)(wchar_t *)’ to ‘unsigned int (__stdcall *)(void *)’.

How I can resolve this error? I seem able to do _beginthread((void(*)(void*))myFunction, 0 , (void *)L"someParam");. But for _beginthreadex() these casts don’t seem to work. What do
I need to do?
This code doesn’t output anything. What’s wrong?

unsigned int __stdcall myFunction( void *someParam )
{
    printf("Hello world!");
    return 0;
}


int _tmain(int argc, _TCHAR* argv[])
{
    _beginthreadex(NULL, 0, myFunction, L"param", 0, NULL);
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:21:43+00:00Added an answer on June 13, 2026 at 12:21 am

    Function prototype requirements for each Microsoft CRT thread launch functions are:

    • _beginthread: void __cdecl procname(void * arg);

    • _beginthreadex: unsigned int __stdcall procname(void *arg);

    You should also be aware of the differences between the two.

    • _beginthread: allocates a new thread and invokes the thread procedure passed as the argument. Using this API the thread-creation parameters are somewhat limited. The return value of this function is a uintptr_t, but is actually a Windows thread handle of type HANDLE. It must be cast to a HANDLE variable to be used in such functions as WaitForSingleObject, etc. When the thread procedure finishes through normal function-exit the thread handle is automatically closed for you by the runtime. This is important. Although this function returns a thread handle just like _beginthreadex, it is conceivable for the thread to start and finish before you can do anything with the handle (such as a wait, suspend, etc.). Once the thread procedure finishes the RT will close the handle, and therefore your local variable holding the initial returned handle is now invalid.

    • _beginthreadex: allocates a new thread and invokes the thread procedure passed as the argument. This version allows significantly more control over how the thread is created, including the stack size, initial suspended state, etc. The return value of this function is a uintptr_t, but is actually a Windows thread handle of type HANDLE. It must be cast to a HANDLE variable to be used in such functions as WaitForSingleObject, etc. When the thread procedure finishes through normal function-exit the thread handle is NOT automatically closed for you by the runtime. You are responsible for closing the thread handle returned by this function, and should do so as soon as it is no longer needed.

    Which to use: Use _beginthread if you have no need for the thread handle to be used in Wait functions and such, and have no special thread-creation needs (like creating a thread with an initially-suspended state). Use _beginthreadex when you need a wait’able thread handle, finer control over the creation parameters, etc.

    EDIT: Sample for the OP

    #include <windows.h>
    #include <tchar.h>
    #include <stdio.h>
    #include <process.h>
    
    unsigned int __stdcall MyThread(void *p)
    {
        _tprintf(_T("%s\n"), p);
        _tprintf(_T("Thread finishing!\n"));
        return 0;
    }
    
    int _tmain(int argc, TCHAR *argv[])
    {
        HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, MyThread, _T("Hello, World!"), 0, NULL);
        if (hThread != INVALID_HANDLE_VALUE)
        {
            WaitForSingleObject(hThread, INFINITE);
            CloseHandle(hThread);
            _tprintf(_T("Thread finished!\n"));
        }
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If i use thread like this: void foo() { new Thread().Start(); } since the
When i try to start Thread (u) it does nothing! this is what i
I start a thread using the following code. t = thread.start_new_thread(myfunction) How can I
I want to start a new thread using a C function, not an objective-C
I have the main thread from which I start a window using invokeLater .
When a Thread is finished, you cannot run it once more, using start() method:
In this code I want to Pause/Resume a thread using an AutoResetEvent and a
what is the difference between create thread using thread.start and using background worker ?
I have the following code, where I start a Thread using a ParameterizedThreadStart object
I am using thread to start a public function of a class. The problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.