Having problems passing an object as param to CreateThread (C++)

Posted on 2010-08-23
Last Modified: 2013-12-14

I'm having problems passing an object as param to CreateThread in C++. Any help greatly appreciated!!!

This is my Thread function, defined outside the class:
static DWORD WINAPI myThreadFunction(LPVOID param);

This is the class definition:
      public ref class MyClass : public MyParentClass

This is my CreateThread Call from within the MyClass object:
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)myThreadFunction, this, 0, NULL);

error C2664: 'CreateThread' : cannot convert parameter 4 from 'MyClass::MyClass ^const ' to 'LPVOID'
1> No user-defined-conversion operator available, or
1> Cannot convert a managed type to an unmanaged type
Question by:php-newbie
  • 5
LVL 33

Expert Comment

Comment Utility
Make it so:
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)myThreadFunction, (LPVOID)this, 0, NULL);
LVL 33

Expert Comment

Comment Utility
>>public ref class MyClass : public MyParentClass
It's managed code. Check this CodeProject article:
Managed threads in Managed C++

I hope it will give a bit different idea.
LVL 33

Expert Comment

Comment Utility
One more:
Threads with Windows Forms Controls in Managed C++
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

LVL 33

Expert Comment

Comment Utility
Here is exactly same question in MSDN:
with an accepted answer and explanation.

Author Comment

Comment Utility
Thanks for the info, pgnatyuk!

Let's say I go the route of creating a managed thread class as shown in this example:

How can I pass the 'this' pointer of MyClass (which creates the thread) to the Thread function?
Can I pass it in the definition and how would I do this?
Thread^ oThread = gcnew Thread( gcnew ThreadStart( &MyThread::myThreadFunction ) );


LVL 33

Accepted Solution

pgnatyuk earned 500 total points
Comment Utility
Almost. ParameterizedThreadStart should be used. It takes parameter of type object.
There is a simple example on this MSDN page.

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now