Solved

CreateThread wont work in 64bit win 7

Posted on 2014-03-05
8
823 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 30

Expert Comment

by:Zoppo
Comment Utility
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
Comment Utility
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 32

Expert Comment

by:sarabande
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 30

Accepted Solution

by:
Zoppo earned 500 total points
Comment Utility
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
Comment Utility
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 32

Expert Comment

by:sarabande
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

772 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

12 Experts available now in Live!

Get 1:1 Help Now