Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2010-08-23
6
Medium Priority
?
907 Views
Last Modified: 2013-12-14
Experts,

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
0
Comment
Question by:php-newbie
[X]
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
6 Comments
 
LVL 33

Expert Comment

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

Expert Comment

by:pgnatyuk
ID: 33505235
>>public ref class MyClass : public MyParentClass
It's managed code. Check this CodeProject article:
Managed threads in Managed C++
http://www.codeproject.com/KB/threads/managedthreads.aspx

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

Expert Comment

by:pgnatyuk
ID: 33505259
One more:
Threads with Windows Forms Controls in Managed C++
http://www.codeproject.com/KB/mcpp/managedsafethreads.aspx
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33505295
Here is exactly same question in MSDN:
http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/accd9e40-92d6-49ff-ad47-5a06acc91a14
with an accepted answer and explanation.
0
 

Author Comment

by:php-newbie
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:
http://msdn.microsoft.com/en-us/library/system.threading.thread.aspx

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

Thanks!

0
 
LVL 33

Accepted Solution

by:
pgnatyuk earned 2000 total points
ID: 33507957
Almost. ParameterizedThreadStart should be used. It takes parameter of type object.
http://msdn.microsoft.com/en-us/library/system.threading.parameterizedthreadstart.aspx
http://msdn.microsoft.com/en-us/library/1h2f2459.aspx
There is a simple example on this MSDN page.
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

688 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