Solved

CreateThread wont work in 64bit win 7

Posted on 2014-03-05
8
885 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
[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
  • 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 34

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
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 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 34

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

Independent Software Vendors: 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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
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…

749 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