none
GetBinaryType() returns the bitness to match the calling app RRS feed

  • Question

  • Hi all;

    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
            }
    
        }
    }


    What we did for the last 6 months - Made the world's coolest reporting & docgen system even more amazing

    Monday, January 27, 2020 2:14 AM

Answers

  • Hi DavidThi808,

    According to following thread explain, it seems that the issue comes form GetBinaryType, please try the method provided in the thread.

    public enum BinaryType : uint
    {
        SCS_32BIT_BINARY = 0,
        SCS_64BIT_BINARY = 6,
        SCS_DOS_BINARY = 1,
        SCS_OS216_BINARY = 5,
        SCS_PIF_BINARY = 3,
        SCS_POSIX_BINARY = 4,
        SCS_WOW_BINARY = 2
    }
    
    public static BinaryType? GetBinaryType(string path)
    {
        using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
        {
            stream.Seek(0x3C, SeekOrigin.Begin);
            using (var reader = new BinaryReader(stream))
            {
                if (stream.Position + sizeof(int) > stream.Length)
                    return null;
                var peOffset = reader.ReadInt32();
                stream.Seek(peOffset, SeekOrigin.Begin);
                if (stream.Position + sizeof(uint) > stream.Length)
                    return null;
                var peHead = reader.ReadUInt32();
                if (peHead != 0x00004550) // "PE\0\0"
                    return null;
                if (stream.Position + sizeof(ushort) > stream.Length)
                    return null;
                switch (reader.ReadUInt16())
                {
                    case 0x14c:
                        return BinaryType.SCS_32BIT_BINARY;
                    case 0x8664:
                        return BinaryType.SCS_64BIT_BINARY;
                    default:
                        return null;
                }
            }
        }
    }

    https://stackoverflow.com/questions/44337501/get-type-of-binary-on-filesystem-via-c-sharp-running-in-64-bit

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by DavidThi808 Tuesday, January 28, 2020 11:47 AM
    Tuesday, January 28, 2020 1:36 AM
    Moderator

All replies

  • Hi DavidThi808,

    According to following thread explain, it seems that the issue comes form GetBinaryType, please try the method provided in the thread.

    public enum BinaryType : uint
    {
        SCS_32BIT_BINARY = 0,
        SCS_64BIT_BINARY = 6,
        SCS_DOS_BINARY = 1,
        SCS_OS216_BINARY = 5,
        SCS_PIF_BINARY = 3,
        SCS_POSIX_BINARY = 4,
        SCS_WOW_BINARY = 2
    }
    
    public static BinaryType? GetBinaryType(string path)
    {
        using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
        {
            stream.Seek(0x3C, SeekOrigin.Begin);
            using (var reader = new BinaryReader(stream))
            {
                if (stream.Position + sizeof(int) > stream.Length)
                    return null;
                var peOffset = reader.ReadInt32();
                stream.Seek(peOffset, SeekOrigin.Begin);
                if (stream.Position + sizeof(uint) > stream.Length)
                    return null;
                var peHead = reader.ReadUInt32();
                if (peHead != 0x00004550) // "PE\0\0"
                    return null;
                if (stream.Position + sizeof(ushort) > stream.Length)
                    return null;
                switch (reader.ReadUInt16())
                {
                    case 0x14c:
                        return BinaryType.SCS_32BIT_BINARY;
                    case 0x8664:
                        return BinaryType.SCS_64BIT_BINARY;
                    default:
                        return null;
                }
            }
        }
    }

    https://stackoverflow.com/questions/44337501/get-type-of-binary-on-filesystem-via-c-sharp-running-in-64-bit

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by DavidThi808 Tuesday, January 28, 2020 11:47 AM
    Tuesday, January 28, 2020 1:36 AM
    Moderator
  • Zhanglong;

    Thank you. And can you please report this as a bug to the Windows team?

    thanks - dave


    What we did for the last 6 months - Made the world's coolest reporting & docgen system even more amazing

    Tuesday, January 28, 2020 2:17 AM
  • Hi DavidThi808,

    Thanks for your effort, I'll report it to related teams. 

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 28, 2020 2:27 AM
    Moderator