Advertisement
AyrA

Visual C++ Runtime detection in C#

Dec 11th, 2018
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.42 KB | None | 0 0
  1. using System;
  2. using System.Runtime.InteropServices;
  3.  
  4. //Detects the presence of the Microsoft Visual C++ Runtime in C# by attempting to use a function from it.
  5. //#REF:https://redd.it/a58pka
  6.  
  7. namespace CppRuntime
  8. {
  9.     public static class NativeMethods
  10.     {
  11.         [DllImport("vcruntime140.dll", EntryPoint = "memcmp")]
  12.         private static extern int memcmp140(byte[] A, byte[] B, int C);
  13.         [DllImport("msvcr120.dll", EntryPoint = "memcmp")]
  14.         private static extern int memcmp120(byte[] A, byte[] B, int C);
  15.         [DllImport("msvcr110.dll", EntryPoint = "memcmp")]
  16.         private static extern int memcmp110(byte[] A, byte[] B, int C);
  17.         [DllImport("msvcr100.dll", EntryPoint = "memcmp")]
  18.         private static extern int memcmp100(byte[] A, byte[] B, int C);
  19.         [DllImport("msvcrt.dll", EntryPoint = "memcmp")]
  20.         private static extern int memcmp(byte[] A, byte[] B, int C);
  21.  
  22.         public static bool HasCppRuntime140()
  23.         {
  24.             try
  25.             {
  26.                 memcmp140(new byte[1], new byte[1], 1);
  27.                 return true;
  28.             }
  29.             catch (DllNotFoundException)
  30.             {
  31.                 return false;
  32.             }
  33.         }
  34.  
  35.         public static bool HasCppRuntime120()
  36.         {
  37.             try
  38.             {
  39.                 memcmp120(new byte[1], new byte[1], 1);
  40.                 return true;
  41.             }
  42.             catch (DllNotFoundException)
  43.             {
  44.                 return false;
  45.             }
  46.         }
  47.  
  48.         public static bool HasCppRuntime110()
  49.         {
  50.             try
  51.             {
  52.                 memcmp110(new byte[1], new byte[1], 1);
  53.                 return true;
  54.             }
  55.             catch (DllNotFoundException)
  56.             {
  57.                 return false;
  58.             }
  59.         }
  60.  
  61.         public static bool HasCppRuntime100()
  62.         {
  63.             try
  64.             {
  65.                 memcmp100(new byte[1], new byte[1], 1);
  66.                 return true;
  67.             }
  68.             catch (DllNotFoundException)
  69.             {
  70.                 return false;
  71.             }
  72.         }
  73.  
  74.         public static bool HasCppRuntime()
  75.         {
  76.             try
  77.             {
  78.                 memcmp(new byte[1], new byte[1], 1);
  79.                 return true;
  80.             }
  81.             catch (DllNotFoundException)
  82.             {
  83.                 return false;
  84.             }
  85.         }
  86.  
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement