473,387 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Detect when in 64bit OS

Is there something in System.Environment that can tell me if the program is
running on a 64bit OS? What I am thinking is right now is to check the size
of IntPtr and if it is 4 then it is 32bit, but if it is 8 then it is 64bit.

Thanks,

Eric Renken
May 31 '07 #1
15 17400
Can you determine it from Environment.OSVersion.Version?

"Eric Renken" wrote:
Is there something in System.Environment that can tell me if the program is
running on a 64bit OS? What I am thinking is right now is to check the size
of IntPtr and if it is 4 then it is 32bit, but if it is 8 then it is 64bit.

Thanks,

Eric Renken
May 31 '07 #2
"Eric Renken" <Er********@newsgroup.nospamwrote in message
news:ex**************@TK2MSFTNGP05.phx.gbl...
Is there something in System.Environment that can tell me if the program
is running on a 64bit OS? What I am thinking is right now is to check the
size of IntPtr and if it is 4 then it is 32bit, but if it is 8 then it is
64bit.
No need to mess around with IntPtr:
http://msdn2.microsoft.com/En-US/library/aa394239.aspx

If you're running on a 64-bit OS, the OSArchitecture property will return
"64-bit" as a string...
--
http://www.markrae.net

May 31 '07 #3
>No need to mess around with IntPtr:
>http://msdn2.microsoft.com/En-US/library/aa394239.aspx
What exactly is messy with checking IntPtr.Size? I can't think of
anything more straight forward. Call it premature optimization if you
want, but I also bet that it's significantly faster than anything
involving WMI.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
May 31 '07 #4
"Eric Renken" <Er********@newsgroup.nospamwrote:
Is there something in System.Environment that can tell me if the program
is running on a 64bit OS? What I am thinking is right now is to check the
size of IntPtr and if it is 4 then it is 32bit, but if it is 8 then it is
64bit.
If you're trying to figure out, "Is my app running in 32 or 64 bit mode?"
then IntPtr is the best way to go. That's how we do it in our applications,
and it works great.

If you're trying to figure out, "Am I running in 32 bit mode on a 64 bit
machine?" then things are a bit harder. For instance, it bit us in an
installer which did some registry settings stuff, and ended up with
Windows-On-Windows issues
(http://www.thescripts.com/forum/thread558929.html)

--
Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
http://www.coversant.com/blogs/cmullins
May 31 '07 #5
The only time I think it could be an issue is when running under 32-bit
mode, in which case, I would suspect IntPtr would be sized to 32 bits. If
the OP wants to get the platform regardless of which mode he is running
under, then the WMI way might be better.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mattias Sjögren" <ma********************@mvps.orgwrote in message
news:u4**************@TK2MSFTNGP06.phx.gbl...
No need to mess around with IntPtr:
http://msdn2.microsoft.com/En-US/library/aa394239.aspx

What exactly is messy with checking IntPtr.Size? I can't think of
anything more straight forward. Call it premature optimization if you
want, but I also bet that it's significantly faster than anything
involving WMI.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

May 31 '07 #6
"Eric Renken" <Er********@newsgroup.nospamwrote in message
news:ex**************@TK2MSFTNGP05.phx.gbl...
Is there something in System.Environment that can tell me if the program
is running on a 64bit OS? What I am thinking is right now is to check the
size of IntPtr and if it is 4 then it is 32bit, but if it is 8 then it is
64bit.

Thanks,

Eric Renken


It's not that important to know whether you are running on 64 bit Windows,
what counts is whether you are running as a 64 bit or 32 bit application,
and the sizeof IntPtr is the right way to check this.

Willy.

May 31 '07 #7
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:OI**************@TK2MSFTNGP06.phx.gbl...
The only time I think it could be an issue is when running under 32-bit
mode, in which case, I would suspect IntPtr would be sized to 32 bits. If
the OP wants to get the platform regardless of which mode he is running
under, then the WMI way might be better.
Exactly.
--
http://www.markrae.net

May 31 '07 #8
"Chris Mullins [MVP]" <cm******@yahoo.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
If you're trying to figure out, "Is my app running in 32 or 64 bit mode?"
then IntPtr is the best way to go. That's how we do it in our
applications, and it works great.

If you're trying to figure out, "Am I running in 32 bit mode on a 64 bit
machine?" then things are a bit harder. For instance, it bit us in an
installer which did some registry settings stuff, and ended up with
Windows-On-Windows issues
That's exactly right, which is why WMI is the right way to do it.
--
http://www.markrae.net

May 31 '07 #9
"Mark Rae" <ma**@markNOSPAMrae.netwrote:
"Chris Mullins [MVP]" <cm******@yahoo.comwrote in message
>If you're trying to figure out, "Am I running in 32 bit mode on a 64 bit
machine?" then things are a bit harder. For instance, it bit us in an
installer which did some registry settings stuff, and ended up with
Windows-On-Windows issues

That's exactly right, which is why WMI is the right way to do it.
I think you misunderstood what I meant. Telling which OS and which
architecture you're running on are pretty easy, really. Knowing what to do
with that data is pretty hard.

For example, if you write to the registry from a 32 bit app, then try to
read back the same key in a 64 bit app, you'll get totally different data.
This is due to the Wow6432Node, which does some tricky things that you just
have to know about.

This bit us during install - our installer is a 32 bit app, that pokes
around a bit then installs a 64 bit application. Everything worked great,
expect for the fact that it didn't work at all. Using WMI or using the size
of the IntPtr wouldn't have made any difference at all. The installer
correctly figured everything out, it was just unable to do what it needed to
do...

There are (I suspect) a number of other areas like this, although none
spring to mind immediatly. Well, ok, a few do - Mostly memory management
related, such as where the DLL's are mapped into the process space, where
the real Windows DLL's live in memory, I bet there are all sorts of Loader
and Rebasing differences, etc.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
http://www.coversant.com/blogs/cmullins
May 31 '07 #10
"Chris Mullins [MVP]" <cm******@yahoo.comwrote in message
news:Op*************@TK2MSFTNGP05.phx.gbl...
"Mark Rae" <ma**@markNOSPAMrae.netwrote:
>"Chris Mullins [MVP]" <cm******@yahoo.comwrote in message
>>If you're trying to figure out, "Am I running in 32 bit mode on a 64 bit
machine?" then things are a bit harder. For instance, it bit us in an
installer which did some registry settings stuff, and ended up with
Windows-On-Windows issues

That's exactly right, which is why WMI is the right way to do it.

I think you misunderstood what I meant. Telling which OS and which
architecture you're running on are pretty easy, really. Knowing what to do
with that data is pretty hard.

For example, if you write to the registry from a 32 bit app, then try to
read back the same key in a 64 bit app, you'll get totally different data.
This is due to the Wow6432Node, which does some tricky things that you
just have to know about.

This is the result of the "virtualization" of the registry, done when
running "legacy" 32 bit interactive applications under WOW64.
64 bit applications never run virtualized, nor do 32 or 64 bit services and
drivers.
Disabling "virtualization" will be done by default when using the Orcas
csharp compiler (and with the upcomming SP1 of Framework V2), pre-Orcas CSC
buids should include a manifest by running mt.exe in order to disable
"virtualization".
You can check whether "virtualization" is effective by inspecting the
access token of the current (non-impersonating) user like this:

[DllImport("advapi32.dll", EntryPoint = "GetTokenInformation",
SetLastError = true)]
static extern bool GetTokenInformationNative(
IntPtr TokenHandle,
int TokenInformationClass,
ref int TokenInformation,
int TokenInformationLength,
out int ReturnLength);

public bool IsVirtualized(IntPtr token)
{
bool virtualized = false;
int len = 4;
int info = 0;
if (!GetTokenInformationNative(token, 24, ref info, len, out
len)) // 24 = TokenVirtualizationEnabled
{
string s = "Win32 error " +
Marshal.GetLastWin32Error().ToString();
throw new Exception(s);
}
if(info != 0)
virtualized = true;
return virtualized;
}

// usage...

if(IsVirtualized(WindowsIdentity.GetCurrent().Toke n))
// better add a manifest to your application if you end here ;-)

Willy.

Jun 1 '07 #11
What kind of software do you work on, that you know this kind of stuff off
the top of your head?

It's just... scary! (I mean that in the best possible way)

--
Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
http://www.coversant.com/blogs/cmullins

"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:C2**********************************@microsof t.com...
"Chris Mullins [MVP]" <cm******@yahoo.comwrote in message
news:Op*************@TK2MSFTNGP05.phx.gbl...
>"Mark Rae" <ma**@markNOSPAMrae.netwrote:
>>"Chris Mullins [MVP]" <cm******@yahoo.comwrote in message
If you're trying to figure out, "Am I running in 32 bit mode on a 64
bit machine?" then things are a bit harder. For instance, it bit us in
an installer which did some registry settings stuff, and ended up with
Windows-On-Windows issues

That's exactly right, which is why WMI is the right way to do it.

I think you misunderstood what I meant. Telling which OS and which
architecture you're running on are pretty easy, really. Knowing what to
do with that data is pretty hard.

For example, if you write to the registry from a 32 bit app, then try to
read back the same key in a 64 bit app, you'll get totally different
data. This is due to the Wow6432Node, which does some tricky things that
you just have to know about.


This is the result of the "virtualization" of the registry, done when
running "legacy" 32 bit interactive applications under WOW64.
64 bit applications never run virtualized, nor do 32 or 64 bit services
and drivers.
Disabling "virtualization" will be done by default when using the Orcas
csharp compiler (and with the upcomming SP1 of Framework V2), pre-Orcas
CSC buids should include a manifest by running mt.exe in order to disable
"virtualization".
You can check whether "virtualization" is effective by inspecting the
access token of the current (non-impersonating) user like this:

[DllImport("advapi32.dll", EntryPoint = "GetTokenInformation",
SetLastError = true)]
static extern bool GetTokenInformationNative(
IntPtr TokenHandle,
int TokenInformationClass,
ref int TokenInformation,
int TokenInformationLength,
out int ReturnLength);

public bool IsVirtualized(IntPtr token)
{
bool virtualized = false;
int len = 4;
int info = 0;
if (!GetTokenInformationNative(token, 24, ref info, len, out
len)) // 24 = TokenVirtualizationEnabled
{
string s = "Win32 error " +
Marshal.GetLastWin32Error().ToString();
throw new Exception(s);
}
if(info != 0)
virtualized = true;
return virtualized;
}

// usage...

if(IsVirtualized(WindowsIdentity.GetCurrent().Toke n))
// better add a manifest to your application if you end here
;-)

Willy.

Jun 1 '07 #12
Well I guess I should have been a little more clear on why I am asking. A
hardware manufacture Dallas Maxim is updating their USB 1-wire device to
work on 64 and 32 bit machines, and I will need to know what mode the OS is
running under so I can call the correct DLL in my code. I think I will
stick with the size of the IntPrt for now.

And actually right now I am working on some communication with a HID device
from C# and have a grand old time trying to get them to work in 64bit mode.
I have fixed some of the problems by replacing the Int32 in the definitions
with IntPtr.

My program itself doesn't care what architecture it is running on it all
those darn hardware devices I need to communicate with that are the pain.

Thanks everyone and I have learned some stuff about virtualation reading all
this.

Eric Renken

"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:98**********************************@microsof t.com...
"Eric Renken" <Er********@newsgroup.nospamwrote in message
news:ex**************@TK2MSFTNGP05.phx.gbl...
>Is there something in System.Environment that can tell me if the program
is running on a 64bit OS? What I am thinking is right now is to check
the size of IntPtr and if it is 4 then it is 32bit, but if it is 8 then
it is 64bit.

Thanks,

Eric Renken



It's not that important to know whether you are running on 64 bit Windows,
what counts is whether you are running as a 64 bit or 32 bit application,
and the sizeof IntPtr is the right way to check this.

Willy.

Jun 1 '07 #13
What kind of software do you work on, that you know this kind of stuff off
the top of your head?

It's just... scary! (I mean that in the best possible way)
I know for a fact that Willy wrote a large part of the CLR all by himself
and then sold it to MS. He is quite rich as well!

--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET is coming...
https://www.microsoft.com/MSPress/books/10933.aspx
OWC Black Book www.lulu.com/owc
Professional VSTO 2005 - Wrox/Wiley
"Chris Mullins [MVP]" <cm******@yahoo.comwrote in message
news:uE**************@TK2MSFTNGP06.phx.gbl...
What kind of software do you work on, that you know this kind of stuff off
the top of your head?

It's just... scary! (I mean that in the best possible way)

--
Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
http://www.coversant.com/blogs/cmullins

"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:C2**********************************@microsof t.com...
>"Chris Mullins [MVP]" <cm******@yahoo.comwrote in message
news:Op*************@TK2MSFTNGP05.phx.gbl...
>>"Mark Rae" <ma**@markNOSPAMrae.netwrote:
"Chris Mullins [MVP]" <cm******@yahoo.comwrote in message
If you're trying to figure out, "Am I running in 32 bit mode on a 64
bit machine?" then things are a bit harder. For instance, it bit us in
an installer which did some registry settings stuff, and ended up with
Windows-On-Windows issues

That's exactly right, which is why WMI is the right way to do it.

I think you misunderstood what I meant. Telling which OS and which
architecture you're running on are pretty easy, really. Knowing what to
do with that data is pretty hard.

For example, if you write to the registry from a 32 bit app, then try to
read back the same key in a 64 bit app, you'll get totally different
data. This is due to the Wow6432Node, which does some tricky things that
you just have to know about.


This is the result of the "virtualization" of the registry, done when
running "legacy" 32 bit interactive applications under WOW64.
64 bit applications never run virtualized, nor do 32 or 64 bit services
and drivers.
Disabling "virtualization" will be done by default when using the Orcas
csharp compiler (and with the upcomming SP1 of Framework V2), pre-Orcas
CSC buids should include a manifest by running mt.exe in order to disable
"virtualization".
You can check whether "virtualization" is effective by inspecting the
access token of the current (non-impersonating) user like this:

[DllImport("advapi32.dll", EntryPoint = "GetTokenInformation",
SetLastError = true)]
static extern bool GetTokenInformationNative(
IntPtr TokenHandle,
int TokenInformationClass,
ref int TokenInformation,
int TokenInformationLength,
out int ReturnLength);

public bool IsVirtualized(IntPtr token)
{
bool virtualized = false;
int len = 4;
int info = 0;
if (!GetTokenInformationNative(token, 24, ref info, len, out
len)) // 24 = TokenVirtualizationEnabled
{
string s = "Win32 error " +
Marshal.GetLastWin32Error().ToString();
throw new Exception(s);
}
if(info != 0)
virtualized = true;
return virtualized;
}

// usage...

if(IsVirtualized(WindowsIdentity.GetCurrent().Toke n))
// better add a manifest to your application if you end here
;-)

Willy.


Jun 2 '07 #14
"Alvin Bruney [MVP]" <some guy without an email addresswrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>What kind of software do you work on, that you know this kind of stuff
off the top of your head?

It's just... scary! (I mean that in the best possible way)

I know for a fact that Willy wrote a large part of the CLR all by himself
and then sold it to MS. He is quite rich as well!

--
Alvin, weren't you told this was highly confidential? Now I'll have to
invite all MVP's to a party on my yacht I guess :-)).

Willy.

Jun 3 '07 #15
"Willy Denoyette [MVP]" <wi*************@telenet.bewrote
"Alvin Bruney [MVP]" <some guy without an email addresswrote:
>>What kind of software do you work on, that you know this kind of stuff
off the top of your head?

It's just... scary! (I mean that in the best possible way)

I know for a fact that Willy wrote a large part of the CLR all by himself
and then sold it to MS. He is quite rich as well!

Alvin, weren't you told this was highly confidential? Now I'll have to
invite all MVP's to a party on my yacht I guess :-)).
Well, in retrospect, I guess it's pretty obvious.

The first names are identical - William is both Willy or Bill.

It took me a bit to find the right anagram of Gates, but once I went
old-school ROT-13, ROT-47 and the original 1977 RSA algorithm (implemented
in BASIC) it was only a matter of time...

--
Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
http://www.coversant.com/blogs/cmullins
Jun 4 '07 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mat | last post by:
How can I detect when a link has been clicked but the new page is still in the process of loading? The document.location.href property still displays the current location (understandably) not the...
4
by: Doug R | last post by:
Hello, I could use a little help from you Gurus out there. I have an aplication that watches a directory and detects when a PGP encrypted file lands in the directory and starts a process that...
6
by: joebob | last post by:
I've got two forms, Form1 and Form2. Form1 opens invisibly when the database opens. From Form1 (or from a regular module or class module accessed by Form1), is there a way to detect when Form2...
3
by: UJ | last post by:
How do you detect when your session is going to timeout and display a message saying 'unless you do something you will be logged out' much the way bank web pages do? Can you do it for the site...
2
by: Mike Stephens | last post by:
I have an application to minimizes when X is clicked. If the user wants to close the application they click the Exit Application button.This works fine and does exactly what I need. I have since...
1
by: Eric Rupp via .NET 247 | last post by:
I am designing a program that will detect when a computer becomes locked (screen saver comes on/Ctrl+Alt+Del). I am looking to detect that and update a database for a custom Time Tracker program. ...
2
by: Rich | last post by:
Hello, I need to trap/detect when a textbox is entered via the tabkey. If the textbox is not empty when entered via the tabkey then set focus to next textbox. To enter that textbox would then...
0
by: Paul | last post by:
Hello, I have a datagridview with a checkbox in a column, and I want to detect when the user change the value of the checkbox in the event CheckedChanged, but I only detect that the value is...
12
by: Phil | last post by:
I can check for MdiChildren.Length=0, but which event handler should I put this in to detect when a child window is closed? TIA Phil.
0
by: john ciriello | last post by:
Hi, I am trying to detect when the help window that pops up for a control can be detected when it is closing. Right now when you click anywhere the window closes but there does not seem to be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.