WinExec





VB.Net


Imports System.Diagnostics

Module Module1

    Sub Main5()
        ' Example usage
        Dim command As String = "notepad.exe" ' Example command to execute
        Dim windowStyle As ProcessWindowStyle = ProcessWindowStyle.Normal ' Example window style

        ExecuteCommand(command, windowStyle)
    End Sub

    Sub ExecuteCommand(command As String, windowStyle As ProcessWindowStyle)
        Dim startInfo As New ProcessStartInfo()
        startInfo.FileName = command
        startInfo.WindowStyle = windowStyle

        Try
            Process.Start(startInfo)
        Catch ex As Exception
            Console.WriteLine("An error occurred: " & ex.Message)
        End Try
    End Sub
End Module


API


Imports System.Runtime.InteropServices

Module Module1

    ' P/Invoke signature for WinExec
    
        Public Function WinExec(lpCmdLine As String, uCmdShow As UInteger) As UInteger
        End Function

        ' Constants for the uCmdShow parameter
        Private Const SW_SHOW As UInteger = 5       ' Show the window
        Private Const SW_HIDE As UInteger = 0       ' Hide the window

        Sub Main()
            ' Example usage
            Dim command As String = "notepad.exe" ' Command to execute

            ' Call WinExec to execute the command
            Dim result As UInteger = WinExec(command, SW_SHOW)

            If result = 0 Then
                ' If WinExec fails, you can use Marshal.GetLastWin32Error to get the error code
                Console.WriteLine("WinExec failed with error code: " & Marshal.GetLastWin32Error())
            Else
                Console.WriteLine("WinExec executed successfully.")
            End If
        End Sub

    End Module