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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
LVL 33

Expert Comment

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

Expert Comment

ID: 33505235
>>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

ID: 33505259
One more:
Threads with Windows Forms Controls in Managed C++
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 33

Expert Comment

ID: 33505295
Here is exactly same question in MSDN:
with an accepted answer and explanation.

Author Comment

ID: 33506939
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
ID: 33507957
Almost. ParameterizedThreadStart should be used. It takes parameter of type object.
There is a simple example on this MSDN page.

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

726 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