GetBinaryType() returns the bitness to match the calling app

与世无争的帅哥 提交于 2021-02-20 05:10:52

问题


I am using this to determine if Office is 32-bit or 64-bit. I am calling it from a C# app.

GetBinaryType(location, out bt)

The exact same code, calling this on winword.exe, returns either 32-bit or 64-bit, matching the bitness of my C# app. Is there something I need to do when running in 64-bit mode that it still returns 32-bit for Office?

I've put a very simple sample program here.

The output running in x86 mode (on 64-bit Windows is):

Running in 32-bit mode. GetBinaryType() returns SCS_32BIT_BINARY for file C:\Program Files (x86)\Microsoft Office\Root\Office16\WINWORD.EXE

The output running in 64=bit mode is:

Running in 64-bit mode. GetBinaryType() returns SCS_64BIT_BINARY for file C:\Program Files (x86)\Microsoft Office\Root\Office16\WINWORD.EXE

I have 32-bit Office on my comouter.

code (if you don't want to download the zip):

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
        }

    }
}

来源:https://stackoverflow.com/questions/59922838/getbinarytype-returns-the-bitness-to-match-the-calling-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!