Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java in General and the fly likes Accurately detecting 64 bit Windows architecture Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Accurately detecting 64 bit Windows architecture" Watch "Accurately detecting 64 bit Windows architecture" New topic
Author

Accurately detecting 64 bit Windows architecture

Eric Daly
Ranch Hand

Joined: Jul 11, 2006
Posts: 143
I've been working on this for probably a month now... Basically I can get the Windows version and Service pack level, but the bit isn't accurately determined. The bit is always returned as the bit of the JRE that the code is running on (usually the 32 bit JRE is running, even on 64 bit sytems). Can anyone tell me a way to find the bit of the actual OS that is running, not just the JRE? Or even point me in the right direction? I tried doing this in C and C++, but it's just so complicated that it's not really worth my time to figure it out... Maybe I can just check if there's a folder called "Program Files (x86)" or some roundabout trick like that... it just seems like Microsoft should have made a way to check this without extensive coding in C or C++. Thanks for any help.


Studying for SCJP 6
Eric Daly
Ranch Hand

Joined: Jul 11, 2006
Posts: 143
This must be a bug in java... why else would os.name and os.patch.level return the actual OS attributes? os.arch only returns the JVM architecture (so how can they call this os.arch?). Anyway, I found a way around this, but so far it's somewhat of a messy solution. I created a C# program that solely runs and then returns the exit code of the architecture (either 32 or 64). This so far has been accurate. Then I just run that bit.exe file from java and read the return code to determine the real arch of the OS. The problem is that it's difficult (for me at least) to run that bit.exe from within the jar file, requiring me to now have the jar file and the exe file alongside one another to properly use my application. I'd rather have a simple java app and move on with my life, but it looks like java isn't really equipped for that yet (again, it doesn't make sense to me, but I'll have to live with it until they improve their OS details features). Anyway, here's my java code so far if anyone is interested (and so you know I'm not just asking for answers, I'm still making progress):

Joshua Barrett
Greenhorn

Joined: Sep 08, 2010
Posts: 27
Hi,

May I see your C# code just to satisfy my curiosity.

Have you heard or possibly considered JNI (Java Native Interface). I had to dabble with it in the past to access a bluetooth device or more specifically the HID list.

http://download.oracle.com/javase/1.5.0/docs/guide/jni/spec/jniTOC.html
Eric Daly
Ranch Hand

Joined: Jul 11, 2006
Posts: 143
Here's the C# code (I got the idea from someone on the internet):


I looked into JNI a while back, but I don't remember what I found. I guess I should check into it again when I get some more time. Thanks for the suggestion.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19725
    
  20

I think the following would do the same, in C:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Eric Daly
Ranch Hand

Joined: Jul 11, 2006
Posts: 143
Thanks for the response. Well that will print the size of a pointer to void. Maybe if we got the size of an int... But then we still need to see if it's 4 or 8 bytes and then print (or return) either 32 or 64 respectively. That actually might be pretty helpful to me though because to run any code generated in C# you have to first install the dot net framework (which for me defeats the purpose of this project because you then have to determine if you should install the 32 bit framework out the 64 bit framework...). So I'll definitely try that out, because I think it will work.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19725
    
  20

A pointer will have the same size, regardless if it is to an int, long or anything else.

If I changed "void *" with "long long" it printed 8. If I replaced that to "long long *" it was 4 again.
Eric Daly
Ranch Hand

Joined: Jul 11, 2006
Posts: 143
Ok. So it looks like possibly the pointer size is 4 on a 32 bit system, and 8 on a 64 bit system. I'll have to try it out later today. Thanks for the feedback.
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

In 32-bit architecture, Memory has 2^32 addresses... Pointer, in order to refer to the memory location, needs 32 bits (or) 4 bytes to store the memory addresses... For 64-bit, pointer needs 8 bytes...


SCJP 6 [SCJP - Old is Gold]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accurately detecting 64 bit Windows architecture