GetBinaryType()返回与调用应用程序匹配的位数

问题描述 投票:0回答:1

我是using this,以确定Office是32位还是64位。我正在从C#应用程序调用它。

GetBinaryType(location, out bt)

完全相同的代码在winword.exe上调用,返回32位或64位,与我的C#应用​​程序的位匹配。在64位模式下运行时仍需要为Office返回32位,我需要做些什么吗?

I've put a very simple sample program here.

在x86模式下运行的输出(在64位Windows上是:]

以32位模式运行。 GetBinaryType()返回SCS_32BIT_BINARY文件C:\ Program Files(x86)\ Microsoft Office \ Root \ Office16 \ WINWORD.EXE

以64位模式运行的输出是:

以64位模式运行。 GetBinaryType()返回SCS_64BIT_BINARY文件C:\ Program Files(x86)\ Microsoft Office \ Root \ Office16 \ WINWORD.EXE

我的同事有32位Office。

代码(如果您不想下载邮政编码):

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.AccessControl;
using Microsoft.Win32;

namespace TestGetBinaryType
{
    class Program
    {
        static void Main(string[] args)
        {
            using (RegistryKey key = Registry.LocalMachine.OpenSubKey($"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\Winword.exe", RegistryRights.ReadKey))
            {
                if (key == null)
                {
                    Console.Error.WriteLine("Could not find the registry key SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\Winword.exe");
                    return;
                }
                string location = key.GetValue(null) as string;
                if (!File.Exists(location))
                {
                    Console.Error.WriteLine($"File is not at specified location: {location}");
                    return;
                }

                BinaryType bt;
                if (!GetBinaryType(location, out bt))
                    throw new ApplicationException($"Could not read binary type from: {location}");
                Console.Out.WriteLine($"Running in {IntPtr.Size * 8}-bit mode.");
                Console.Out.WriteLine($"GetBinaryType() returns {bt} for file {location}");
            }
        }

        [DllImport("kernel32.dll")]
        static extern bool GetBinaryType(string lpApplicationName, out BinaryType lpBinaryType);

        public enum BinaryType : uint
        {
            SCS_32BIT_BINARY = 0,   // A 32-bit Windows-based application
            SCS_64BIT_BINARY = 6,   // A 64-bit Windows-based application.
            SCS_DOS_BINARY = 1,     // An MS-DOS � based application
            SCS_OS216_BINARY = 5,   // A 16-bit OS/2-based application
            SCS_PIF_BINARY = 3,     // A PIF file that executes an MS-DOS � based application
            SCS_POSIX_BINARY = 4,   // A POSIX � based application
            SCS_WOW_BINARY = 2      // A 16-bit Windows-based application
        }

    }
}
c# ms-office 32bit-64bit
1个回答
0
投票

在64位Windows上运行的32位应用程序将置于file system redirectionregistry redirection下。许多注册表项将被重定向including HKEY_LOCAL_MACHINE

这意味着您的32位应用程序将看到HKEY_LOCAL_MACHINE而不是HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Office。要查看真实密钥,可以使用HKEY_LOCAL_MACHINE\Software\Microsoft\Office并指定所需的RegistryKey.OpenBaseKey

32 or 64-bit view

您不需要if块,只需使用using (var hklm = Registry.LocalMachine.OpenBaseKey( RegistryHive.LocalMachine, RegistryView.Registry64)) using (var key = hklm.OpenSubKey( $@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Winword.exe", RegistryRights.ReadKey)) ,因为即使在32位Windows上,它也将始终返回真正的非重定向键

或者直接调用RegistryView.Registry64 Win32API并将RegOpenKeyEx()RegOpenKeyEx()传递给KEY_WOW64_32KEY

请参见KEY_WOW64_64KEY

© www.soinside.com 2019 - 2024. All rights reserved.