Executable requested execution levels on Windows


 







Imports System.Runtime.InteropServices
Imports System.Security.Principal
Imports System.Threading
Module Module1
    Sub Main()
        If Not IsWindowsVersionOrGreater(10, 0, 0) Then
            Console.WriteLine("Your PC must at least be Windows 10")
            Console.ReadKey()
            Process.Start("rundll32.exe", "shell32.dll,ShellAbout")
            Return
        End If
        Dim ptr_ModuleFileName As IntPtr = Marshal.AllocHGlobal(4096)
        GetModuleFileName(Nothing, ptr_ModuleFileName, 2000)
        Dim ModuleFileName As String = Marshal.PtrToStringUni(ptr_ModuleFileName)
        Thread.GetDomain().SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal)
        Dim UserName As String = Thread.CurrentPrincipal.Identity.Name.Split(New Char() {"\"c}).Last()
        Console.WriteLine(UserName)
        Console.ReadKey()
        Dim WSHshell = CreateObject("Wscript.shell")
        Dim MyShortCut = WSHshell.CreateShortcut(ModuleFileName & $".{UserName}.lnk")
        MyShortCut.TargetPath = ModuleFileName
        MyShortCut.Iconlocation = $"{ModuleFileName}, 0"
        MyShortCut.WorkingDirectory = IO.Path.GetDirectoryName(ModuleFileName)
        MyShortCut.save()
        Process.Start("explorer.exe", IO.Path.GetDirectoryName(ModuleFileName))
        Marshal.FreeHGlobal(ptr_ModuleFileName)
    End Sub
    Declare Unicode Function GetModuleFileName Lib "Kernel32.dll" Alias "GetModuleFileNameW" (
            _In_opt_hModule As IntPtr,
            lpFilename As IntPtr,
            nSize As UInt32) As UInt32
    Declare Unicode Sub RtlZeroMemory Lib "Kernel32.dll" (
           Destination As IntPtr, Length As UInt32)
    Declare Unicode Function VerifyVersionInfo Lib "kernel32" Alias "VerifyVersionInfoW" (
          _Inout_LPOSVERSIONINFOEXW_lpVersionInformation As IntPtr,
          _In_DWORD_dwTypeMask As UInt32,
          _In_DWORDLONG_dwlConditionMask As UInt64) As Boolean
    Declare Function VerSetConditionMask Lib "kernel32" (
           _In_ULONGLONG_ConditionMask As UInt64,
           _In_ULONG_TypeMask As UInt32,
           _In_UCHAR_Condition As Byte) As UInt64
    Structure OSVERSIONINFOEXW
        Public dwOSVersionInfoSize As UInt32
        Public dwMajorVersion As UInt32
        Public dwMinorVersion As UInt32
        Public dwBuildNumber As UInt32
        Public dwPlatformId As UInt32                 '4 + 4 + 4 + 4 + 4 = 20
        Public szCSDVersion_Char_256 As Memory_256B   '128 * 2 = 256
        Public WORD_wServicePackMajor As UInt16
        Public WORD_wServicePackMinor As UInt16
        Public WORD_wSuiteMask As UInt16
        Public wProductType As Byte
        Public wReserved As Byte                      ' 2 + 2 + 2 + 1 + 1 = 8
    End Structure
    Function IsWindowsVersionOrGreater(wMajorVersion As UInt16, wMinorVersion As UInt16,
                                       wServicePackMajor As UInt16) As Boolean
        Dim ptr_WindowsVersionInfo As IntPtr = Marshal.AllocHGlobal(2000)
        RtlZeroMemory(ptr_WindowsVersionInfo, 2000)
        Dim SizeOfOSinfo As Int32 = Marshal.SizeOf(Of OSVERSIONINFOEXW)()
        Marshal.WriteInt32(ptr_WindowsVersionInfo, SizeOfOSinfo)
        Marshal.WriteInt32(ptr_WindowsVersionInfo + 4, wMajorVersion)
        Marshal.WriteInt32(ptr_WindowsVersionInfo + 8, wMinorVersion)
        Marshal.WriteInt32(ptr_WindowsVersionInfo + SizeOfOSinfo - 8, wServicePackMajor)
        Dim dwlConditionMask As UInt64 = VerSetConditionMask(
        VerSetConditionMask(
        VerSetConditionMask(0, VER_MAJORVERSION, VER_GREATER_EQUAL),
                               VER_MINORVERSION, VER_GREATER_EQUAL),
                               VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL)
        IsWindowsVersionOrGreater = VerifyVersionInfo(ptr_WindowsVersionInfo,
        VER_MAJORVERSION Or VER_MINORVERSION Or VER_SERVICEPACKMAJOR, dwlConditionMask) <> False
        Marshal.FreeHGlobal(ptr_WindowsVersionInfo)
    End Function
    Public Const VER_MINORVERSION As UInt32 = &H1
    Public Const VER_MAJORVERSION As UInt32 = &H2
    Public Const VER_BUILDNUMBER As UInt32 = &H4
    Public Const VER_PLATFORMID As UInt32 = &H8
    Public Const VER_SERVICEPACKMINOR As UInt32 = &H10
    Public Const VER_SERVICEPACKMAJOR As UInt32 = &H20
    Public Const VER_SUITENAME As UInt32 = &H40
    Public Const VER_PRODUCT_TYPE As UInt32 = &H80
    Public Const VER_EQUAL As UInt32 = 1
    Public Const VER_GREATER As UInt32 = 2
    Public Const VER_GREATER_EQUAL As UInt32 = 3
    Public Const VER_LESS As UInt32 = 4
    Public Const VER_LESS_EQUAL As UInt32 = 5
    Public Const VER_AND As UInt32 = 6
    Public Const VER_OR As UInt32 = 7
    Public Const VER_CONDITION_MASK As UInt32 = 7
    Public Const VER_NUM_BITS_PER_CONDITION_MASK As UInt32 = 3
    Structure Memory_16B
        Dim a, b, c, d As Int32
    End Structure
    Structure Memory_64B
        Dim a, b, c, d As Memory_16B
    End Structure
    Structure Memory_256B
        Dim a, b, c, d As Memory_64B
    End Structure
End Module
















留言

這個網誌中的熱門文章

Marshalling

Calling a C# WPF library from C++

Marshalling II