Re: Detect when in 64bit OS

Tech-Archive recommends: Fix windows errors by optimizing your registry



"Chris Mullins [MVP]" <cmullins@xxxxxxxxx> wrote in message news:OpbzC19oHHA.716@xxxxxxxxxxxxxxxxxxxxxxx
"Mark Rae" <mark@xxxxxxxxxxxxxxxxx> wrote:
"Chris Mullins [MVP]" <cmullins@xxxxxxxxx> wrote 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().Token))
// better add a manifest to your application if you end here ;-)

Willy.



.



Relevant Pages

  • Re: Detect when in 64bit OS
    ... For example, if you write to the registry from a 32 bit app, then try to ... Disabling "virtualization" will be done by default when using the Orcas ... static extern bool GetTokenInformationNative( ... int TokenInformationClass, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Detect when in 64bit OS
    ... the top of your head? ... Disabling "virtualization" will be done by default when using the Orcas ... static extern bool GetTokenInformationNative( ... int TokenInformationClass, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Registering ActiveX control on Vista
    ... meant to say that Vista does not have the fallback to HKCU\Software\Classes ... They normally extract the registry keys when creating the ... this is not related to Registry Virtualization. ...
    (microsoft.public.win32.programmer.ole)
  • Re: Ini File vs Registry
    ... The registry is a good place to store app data only to the ... the U3 Flash drive but to make it appear to be part of the registry ... ... in Windows itself, and as a result, U3 was no more. ... virtualization of the Registry is quite a good solution ... ...
    (microsoft.public.vc.mfc)
  • Re: Ini File vs Registry
    ... -- place for an application to store configuration that is not required ... The registry is a good place to store app data only to the ... the U3 Flash drive but to make it appear to be part of the registry ... ... virtualization of the Registry is quite a good solution ... ...
    (microsoft.public.vc.mfc)