Solved

CreateThread wont work in 64bit win 7

Posted on 2014-03-05
8
853 Views
Last Modified: 2014-03-10
Hi can someone show me what needs to be changed to make CreateThread work in 64bit windows OS. I am trying to port my 32bit application to 64bit. Currently my Handle in 64bit is returning NULL I understand I think that under 32bit the data is 8bit aligned and in 64bit it is 16bit aligned but im not sure how to implement CreateThread to work with 16bit aligned any code samples would be great
Thanks
0
Comment
Question by:nchannon
  • 3
  • 2
  • 2
8 Comments
 
LVL 31

Expert Comment

by:Zoppo
ID: 39906063
Hi nchannon,

usually there's nothing special about using CreateThread in a 64-bit application.

IMO data alignment shouldn't be an issue here as long as you don't use any wrong casts. BTW: the data alignment is 4 bytes in a 32 bit environment and 8 bytes in a 64 bit environment (found at http://www.codeproject.com/Articles/526984/bit-vs-bit-memory)

Could you post some relevant parts of your code? Especially the signature of your thread function and the part where you call CreateThread?

ZOPPO
0
 

Author Comment

by:nchannon
ID: 39906254
Hi ZOPPO,
This is the code that is used to create the threads THREADCOUNT is 3 currently under 32bit g_hThread gets assigned a value but under 64bit NULL or 0 is assigned
//== Start Thread ==//
void CSocketClass::StartThread()
{
   DWORD threadID;
   for(int n=0;n<THREADCOUNT;n++)
     {
        g_hThread[n]=(HANDLE)CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ProcessThread,NULL,NULL,&threadID);
     }
	
}

Open in new window

0
 
LVL 33

Expert Comment

by:sarabande
ID: 39906675
you should call GetLastError to find out what goes wrong. the error codes you can find in winerror.h.

writing an application for a 64-bit os is not the same as writing a 64-bit application.

in visual studio configuration manager you need to switch the solution platform from WIN32 to x64. if the x64 is not available in the combobox, the installation of visual studio didn't include the 64-bit part or the edition doesn't support it.

(LPTHREAD_START_ROUTINE)ProcessThread
normally it is not so good an idea to cast the thread function. it is much better to use a thread function which already has the correct signature and therefore could be used without cast.

Sara
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 31

Accepted Solution

by:
Zoppo earned 500 total points
ID: 39906677
Hm - this code IMO looks ok. And you say the same code works fine with 32 bit, right?

Hard to say what's going on there. do you think you could a.) post the declaration of ProcessThread and b.) check the return value GetLastError by adding something like this directly after the line 8 which calls CreateThread:

   TRACE( "CreateThread returned 0x%x, last error is %d\n", g_hThread[n], GetLastError() );

ZOPPO
0
 

Author Comment

by:nchannon
ID: 39915734
Hi yes the code is correct I found the problem it wasn't the code its self but visual studio 10 in 64bit debug mode not behaving correctly which I do not know why and though me out completely. Basically what VS is doing in 64bit mode is it goes into every line of code even if it is not meant to and returns false values giving you the impression there an error in your code but in 32bit debug mode the debugger behaves correctly. I have the thread working fine in 64bit mode now but I must use the 32 debug version to debug correctly do you know why VS 10 behave this way in 64bit debug mode as it is useless what it is doing
0
 
LVL 33

Expert Comment

by:sarabande
ID: 39916431
hmm. in vs2010 debugger 32-bit there is a bug like the one you described. if an if condition does not hit and you step forward, the debugger seems to step into the first line of the if block nevertheless. but actually it is not in the if block but outside the block what you could see by the fact that the variables in the if block were not watchable.

I don't know whether that is the phenomena you experienced, but if so, it is not a 64-bit only bug.

Sara
0
 

Author Comment

by:nchannon
ID: 39917160
Yes very similar thing happening in the 64bit version I guess its a typical Microsoft flaw I ll check it in VS 2012 see if it behaves the same
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Template syntax for variable length arrays 9 75
VS2015 compilation and missing DLLs 9 147
Problem with SqlConnection 4 168
Issues with C++ Class 19 92
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

813 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

13 Experts available now in Live!

Get 1:1 Help Now