AyrA icon

Visual C++ Runtime detection in C#

AyrA | PRO | 12/12/18 02:00:07 AM UTC | 0 ⭐ | 500 👁️ | Never ⏰ | []
C# |

2.42 KB

|

None

|

0 👍

/

0 👎

using System;
using System.Runtime.InteropServices;
 
//Detects the presence of the Microsoft Visual C++ Runtime in C# by attempting to use a function from it.
//#REF:https://redd.it/a58pka
 
namespace CppRuntime
{
    public static class NativeMethods
    {
        [DllImport("vcruntime140.dll", EntryPoint = "memcmp")]
        private static extern int memcmp140(byte[] A, byte[] B, int C);
        [DllImport("msvcr120.dll", EntryPoint = "memcmp")]
        private static extern int memcmp120(byte[] A, byte[] B, int C);
        [DllImport("msvcr110.dll", EntryPoint = "memcmp")]
        private static extern int memcmp110(byte[] A, byte[] B, int C);
        [DllImport("msvcr100.dll", EntryPoint = "memcmp")]
        private static extern int memcmp100(byte[] A, byte[] B, int C);
        [DllImport("msvcrt.dll", EntryPoint = "memcmp")]
        private static extern int memcmp(byte[] A, byte[] B, int C);
 
        public static bool HasCppRuntime140()
        {
            try
            {
                memcmp140(new byte[1], new byte[1], 1);
                return true;
            }
            catch (DllNotFoundException)
            {
                return false;
            }
        }
 
        public static bool HasCppRuntime120()
        {
            try
            {
                memcmp120(new byte[1], new byte[1], 1);
                return true;
            }
            catch (DllNotFoundException)
            {
                return false;
            }
        }
 
        public static bool HasCppRuntime110()
        {
            try
            {
                memcmp110(new byte[1], new byte[1], 1);
                return true;
            }
            catch (DllNotFoundException)
            {
                return false;
            }
        }
 
        public static bool HasCppRuntime100()
        {
            try
            {
                memcmp100(new byte[1], new byte[1], 1);
                return true;
            }
            catch (DllNotFoundException)
            {
                return false;
            }
        }
 
        public static bool HasCppRuntime()
        {
            try
            {
                memcmp(new byte[1], new byte[1], 1);
                return true;
            }
            catch (DllNotFoundException)
            {
                return false;
            }
        }
 
    }
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎