Underlying OS architecture

D

David Jackson

Hello,

There was a thread recently about how to tell if the installed version of
Windows is 32-bit or 64-bit, but that seems to have disappeared from my
newsreader so apologies for starting a new thread.

Anyway, the first suggestion was to use IntPtr.Size. That is clearly wrong,
because that will only tell you if your *application* is 32-bit or 64-bit
e.g.

32-bit app on 32-bit OS - IntPtr.Size = 4
64-bit app on 64-bit OS - IntPtr.Size = 8
32-bit app on 64-bit OS - IntPtr.Size = 4

so that's no use, and similar API calls also seem to be unreliable for the
same reason.

The second suggestion was to use WMI, specifically the OSArchitecture value
of the Win32_OperatingSystem class in the System.Management namespace. The
problem with that is that it is available only in Vista -
http://msdn2.microsoft.com/En-US/library/aa394239.aspx which makes it almost
worse than useless.

[There was also a comment that nobody really needs to know this information
anyway, which may be true, but doesn't provide a solution.]

The only way that I can find to do this is:

1) find the drive letter that Windows is installed on (from WMI)

2) add ":\\Program Files (x86)" to it

3) see if the resulting folder exists with Directory.Exists

This is fairly obviously pretty awful but is the most reliable method I have
found so far.

Can anyone suggest a better way?

Thanks,

DJ
 
N

Nicholas Paldino [.NET/C# MVP]

David,

I am curious, what is it that you are trying to do exactly? Why do you
need to know whether the OS is 64 bit as opposed to the mode under which
your app is running?
 
K

ktrvnbq02

The only way that I can find to do this is:

1) find the drive letter that Windows is installed on (from WMI)

2) add ":\\Program Files (x86)" to it

3) see if the resulting folder exists with Directory.Exists

This is fairly obviously pretty awful but is the most reliable method I have
found so far.

Unfortunately I don't have a solution for you, but I just wanted to
note that this would fail on non-English builds of Windows.


Matt
 
D

David Jackson

I am curious, what is it that you are trying to do exactly?

Find out whether my app is running on 32-bit or 64-bit Windows.
Why do you need to know whether the OS is 64 bit

Does that actually matter...? Does that prevent you from helping me...?
 
D

David Jackson

Matt,

Thanks very much for the reply.
Unfortunately I don't have a solution for you,

I'm beginning to think there may not be one!
but I just wanted to note that this would fail on non-English
builds of Windows.

Yes it would. As I mentioned, it's a totally awful suggestion, but I have
nothing better at the moment.

DJ
 
N

Nicholas Paldino [.NET/C# MVP]

David,

As for whether or not it could matter, yes, it could (and not because of
personal reasons). More information about a problem ALWAYS helps, and by
having a greater understanding of what is going on in general, it could lead
to a different, but equally effective solution which is more appropriate for
what you are trying to do.

But without that information, it prevents myself, as well as a lot of
other people here from having a greater understanding of your problem, which
will prevent them from possibly providing a solution to your problem.

The next time you want to ask if the information will prevent people
from helping you, remember that when they ask, they are already trying to
help you by asking. They are already taking time to have a greater
understanding of YOUR problem. There is no need to ask them again. Rather,
enable the people that are trying to help you by giving them what they are
asking for, and I can guarantee that you will always get a better response
in terms of the quality of the answer.
 
D

David Jackson

message
Nicholas,

I apologize profusely if I have offended you. I had no intention to do that.

Fact is, I have no reason whatever for wanting to know how to do this other
than for my own interest and to increase my own knowledge.

It just seems to me a perfectly reasonable and straightforward thing to want
to know how to do!

Microsoft have provided a mechanism for it in WMI (even though that only
works in Vista), so somebody somewhere must have asked for it at some point
:)

DJ
 
W

Willy Denoyette [MVP]

David Jackson said:
Hello,

There was a thread recently about how to tell if the installed version of
Windows is 32-bit or 64-bit, but that seems to have disappeared from my
newsreader so apologies for starting a new thread.

Anyway, the first suggestion was to use IntPtr.Size. That is clearly
wrong, because that will only tell you if your *application* is 32-bit or
64-bit e.g.

32-bit app on 32-bit OS - IntPtr.Size = 4
64-bit app on 64-bit OS - IntPtr.Size = 8
32-bit app on 64-bit OS - IntPtr.Size = 4

so that's no use, and similar API calls also seem to be unreliable for the
same reason.

The second suggestion was to use WMI, specifically the OSArchitecture
value of the Win32_OperatingSystem class in the System.Management
namespace. The problem with that is that it is available only in Vista -
http://msdn2.microsoft.com/En-US/library/aa394239.aspx which makes it
almost worse than useless.

[There was also a comment that nobody really needs to know this
information anyway, which may be true, but doesn't provide a solution.]

The only way that I can find to do this is:

1) find the drive letter that Windows is installed on (from WMI)

2) add ":\\Program Files (x86)" to it

3) see if the resulting folder exists with Directory.Exists

This is fairly obviously pretty awful but is the most reliable method I
have found so far.

Can anyone suggest a better way?

Thanks,

DJ


In your code, you first need to check the size of IntPtr, if it returns 8
then you are running on a 64-bit OS.
If it returns 4, you are running a 32 bit application, so now you need to
know whether you are running natively or under WOW64.
To get this information you will need to call kernel32.dll API
"IsWow64Process" using PInvoke, this API returns a Boolean 'true' if you are
running under WOW64, that means you are running 32 bit application on a
64-bit Windows system.
Be careful however to check the OS version before calling this API, only XP
SP2 and implements this one.

Willy.
 
S

Stephen Martin

There are probably a few ways to find this out. The first one to pop into my
head would be to check the pointer size. If it is 8 then the OS is 64-bit.
If it is 4 then the OS may be 32-bit or 64-bit, just call IsWow64Process to
find out which.
 
N

Nicholas Paldino [.NET/C# MVP]

David,

You can get this without WMI. Check out the section of the MSDN
documentation titled "Running 32-bit Applications", located at:

http://msdn.microsoft.com/library/d...s/win64/win64/running_32_bit_applications.asp

You will have to call the API through the P/Invoke layer. You will also
have to call GetProcAddress to see if the IsWow64Process API is exposed.
From what I can tell, if it is not exposed, then you are running on a 32 bit
system, if it is, then you are running on a 64 bit system.
 
D

David Jackson

Hi Willy,

Thanks for the reply.
In your code, you first need to check the size of IntPtr, if it returns 8
then you are running on a 64-bit OS.
If it returns 4, you are running a 32 bit application, so now you need to
know whether you are running natively or under WOW64.
To get this information you will need to call kernel32.dll API
"IsWow64Process" using PInvoke, this API returns a Boolean 'true' if you
are running under WOW64, that means you are running 32 bit application on
a 64-bit Windows system.
Be careful however to check the OS version before calling this API, only
XP SP2 and implements this one.

OK, so if I've understood correctly...

If IntPtr.Size is 8, it must be 64-bit Windows - if it's 4, it could be
32-bit or 64-bit.

If it's 4, we firstly need to check if we're running Vista or XP.

If it's Vista, we use OSArchitecture from WMI

If it's XP, and only then if it's SP2, we use the IsWow64Process API call

If it's not XP+SP2, we have no means of knowing.

Is that correct?

DJ
 
W

Willy Denoyette [MVP]

David Jackson said:
Hi Willy,

Thanks for the reply.


OK, so if I've understood correctly...

If IntPtr.Size is 8, it must be 64-bit Windows - if it's 4, it could be
32-bit or 64-bit.
Right. Pointers are 8 bytes long on a 64-bit OS.
If it's 4, we firstly need to check if we're running Vista or XP.
Right, check the OperatingSystem.Version and OperatingSystem.ServicePack
properties.
If it's Vista, we use OSArchitecture from WMI
Not necessarely, I would currently opt for a single API solution:
IsWow64Process.
If it's XP, and only then if it's SP2, we use the IsWow64Process API call

No, when it is XP SP2 or higher!. On XP SP2, Windows 2003, Vista, Windows
2008, and .... you can use the IsWow64Process API.
If it's not XP+SP2, we have no means of knowing.

If it's not XP2 or higher, It can't be an 64--bit OS.

Willy.
 
D

David Jackson

Hi Willy,
Not necessarely, I would currently opt for a single API solution:
IsWow64Process.

OK - I was obviously taking your previous statement too literally: "Be
careful however to check the OS version before calling this API, only XP SP2
implements this one."
No, when it is XP SP2 or higher!. On XP SP2, Windows 2003, Vista, Windows
2008, and .... you can use the IsWow64Process API.

Understood now.
If it's not XP2 or higher, It can't be an 64--bit OS.

Oh right. So 64-bit XP was released already at SP2? I wasn't aware of
that...

In which case I think we have a solution!

Thanks for all you help.

DJ
 
W

Willy Denoyette [MVP]

David Jackson said:
Hi Willy,


OK - I was obviously taking your previous statement too literally: "Be
careful however to check the OS version before calling this API, only XP
SP2 implements this one."

My bad, sorry for the confusion.
Understood now.


Oh right. So 64-bit XP was released already at SP2? I wasn't aware of
that...

Not really, it's a different code base really, but XP64 is guranteed to have
the API IsWow64Process ;-)


In which case I think we have a solution!

Thanks for all you help.

You're welcome.

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top