如何知道应用程序是从控制台还是浏览器运行?

时间:2014-08-31 17:01:36

标签: vb.net winforms console

我正在编写一个应该以winform模式或控制台模式运行的小应用程序。问题是我不知道我的应用程序是如何启动的。如果是使用控制台启动它应该做一些工作人员,否则它应该显示一个winform。

Friend NotInheritable Class Program
    Shared Sub Main()

        If The application was run in console mode then
            'Run console processes
        Else
            'Open winform
            Application.Run(New Form1)
        End If
    End Sub
End Class

提前干杯:)!

1 个答案:

答案 0 :(得分:0)

我找到了你需要的答案,但它是在C#中。如果你不知道C#,你可以简单地使用C#到VB.net转换器,或者手动转换它

从类似的问题中找到:https://stackoverflow.com/a/3346055/1388267

/// <summary>
/// A utility class to determine a process parent.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct ParentProcessUtilities
{
    // These members must match PROCESS_BASIC_INFORMATION
    internal IntPtr Reserved1;
    internal IntPtr PebBaseAddress;
    internal IntPtr Reserved2_0;
    internal IntPtr Reserved2_1;
    internal IntPtr UniqueProcessId;
    internal IntPtr InheritedFromUniqueProcessId;

    [DllImport("ntdll.dll")]
    private static extern int NtQueryInformationProcess(IntPtr processHandle, int processInformationClass, ref ParentProcessUtilities processInformation, int processInformationLength, out int returnLength);

    /// <summary>
    /// Gets the parent process of the current process.
    /// </summary>
    /// <returns>An instance of the Process class.</returns>
    public static Process GetParentProcess()
    {
        return GetParentProcess(Process.GetCurrentProcess().Handle);
    }

    /// <summary>
    /// Gets the parent process of specified process.
    /// </summary>
    /// <param name="id">The process id.</param>
    /// <returns>An instance of the Process class.</returns>
    public static Process GetParentProcess(int id)
    {
        Process process = Process.GetProcessById(id);
        return GetParentProcess(process.Handle);
    }

    /// <summary>
    /// Gets the parent process of a specified process.
    /// </summary>
    /// <param name="handle">The process handle.</param>
    /// <returns>An instance of the Process class.</returns>
    public static Process GetParentProcess(IntPtr handle)
    {
        ParentProcessUtilities pbi = new ParentProcessUtilities();
        int returnLength;
        int status = NtQueryInformationProcess(handle, 0, ref pbi, Marshal.SizeOf(pbi), out returnLength);
        if (status != 0)
            throw new Win32Exception(status);

        try
        {
            return Process.GetProcessById(pbi.InheritedFromUniqueProcessId.ToInt32());
        }
        catch (ArgumentException)
        {
            // not found
            return null;
        }
    }
}

您可以获取父进程的句柄,然后检查其名称以了解它是资源管理器还是命令提示符

编辑:刚刚将它转换为VB.net,因此您可以直接使用它:

''' <summary>
''' A utility class to determine a process parent.
''' </summary>
<StructLayout(LayoutKind.Sequential)> _
Public Structure ParentProcessUtilities
    ' These members must match PROCESS_BASIC_INFORMATION
    Friend Reserved1 As IntPtr
    Friend PebBaseAddress As IntPtr
    Friend Reserved2_0 As IntPtr
    Friend Reserved2_1 As IntPtr
    Friend UniqueProcessId As IntPtr
    Friend InheritedFromUniqueProcessId As IntPtr

    <DllImport("ntdll.dll")> _
    Private Shared Function NtQueryInformationProcess(processHandle As IntPtr, processInformationClass As Integer, ByRef processInformation As ParentProcessUtilities, processInformationLength As Integer, ByRef returnLength As Integer) As Integer
    End Function

    ''' <summary>
    ''' Gets the parent process of the current process.
    ''' </summary>
    ''' <returns>An instance of the Process class.</returns>
    Public Shared Function GetParentProcess() As Process
        Return GetParentProcess(Process.GetCurrentProcess().Handle)
    End Function

    ''' <summary>
    ''' Gets the parent process of specified process.
    ''' </summary>
    ''' <param name="id">The process id.</param>
    ''' <returns>An instance of the Process class.</returns>
    Public Shared Function GetParentProcess(id As Integer) As Process
        Dim process__1 As Process = Process.GetProcessById(id)
        Return GetParentProcess(process__1.Handle)
    End Function

    ''' <summary>
    ''' Gets the parent process of a specified process.
    ''' </summary>
    ''' <param name="handle">The process handle.</param>
    ''' <returns>An instance of the Process class.</returns>
    Public Shared Function GetParentProcess(handle As IntPtr) As Process
        Dim pbi As New ParentProcessUtilities()
        Dim returnLength As Integer
        Dim status As Integer = NtQueryInformationProcess(handle, 0, pbi, Marshal.SizeOf(pbi), returnLength)
        If status <> 0 Then
            Throw New Win32Exception(status)
        End If

        Try
            Return Process.GetProcessById(pbi.InheritedFromUniqueProcessId.ToInt32())
        Catch generatedExceptionName As ArgumentException
            ' not found
            Return Nothing
        End Try
    End Function
End Structure