无法隐藏进程

发布于 2024-11-14 05:08:39 字数 603 浏览 3 评论 0原文

我想启动 calc.exe 并隐藏它
但只能启动exex但无法隐藏
我的代码有什么错误??

Imports System.Runtime.InteropServices
Imports System.IntPtr
Public Class Form1
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _  
    Private Shared Function ShowWindowAsync(ByVal hwnd As IntPtr, ByVal nCmdShow As Integer) As Boolean
    End Function 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim a As IntPtr = Process.GetProcessesByName("Calc")(0).Id
        ShowWindowAsync(a, 0)
    End Sub
End Class

i want to start calc.exe and hide it
but only can start the exex but cant hide
what error in my code??

Imports System.Runtime.InteropServices
Imports System.IntPtr
Public Class Form1
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _  
    Private Shared Function ShowWindowAsync(ByVal hwnd As IntPtr, ByVal nCmdShow As Integer) As Boolean
    End Function 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim a As IntPtr = Process.GetProcessesByName("Calc")(0).Id
        ShowWindowAsync(a, 0)
    End Sub
End Class

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瘫痪情歌 2024-11-21 05:08:39

以下代码应该使用托管代码执行您想要的操作,但是当我使用 calc 尝试它时,它不会开始隐藏或最小化(其他 exe 按预期工作,所以它可能是 calc 特有的东西?)

    Dim procStartInfo As New ProcessStartInfo
    Dim procExecuting As New Process

    With procStartInfo
        .FileName = "calc"
        .WindowStyle = ProcessWindowStyle.Hidden
    End With

    procExecuting = Process.Start(procStartInfo)

The following code should do what you want using managed code, but when I tried it using calc it would not start hidden or minimized (other exe's worked as expected so it may be something peculiar with calc?)

    Dim procStartInfo As New ProcessStartInfo
    Dim procExecuting As New Process

    With procStartInfo
        .FileName = "calc"
        .WindowStyle = ProcessWindowStyle.Hidden
    End With

    procExecuting = Process.Start(procStartInfo)
他是夢罘是命 2024-11-21 05:08:39

我不久前做了一个适用于任何进程的代码片段,是两个通用函数,并且还具有递归模式并且易于使用:

#Region " Hide-Restore Process "

    ' [ Hide-Restore Process Function ]
    '
    ' // By Elektro H@cker
    '
    ' Examples :
    '
    ' Hide_Process(Process.GetCurrentProcess().MainModule.ModuleName, False)
    ' Hide_Process("notepad.exe", False)
    ' Hide_Process("notepad", True)
    '
    ' Restore_Process(Process.GetCurrentProcess().MainModule.ModuleName, False)
    ' Restore_Process("notepad.exe", False)
    ' Restore_Process("notepad", True)

    Dim Process_Handle_Dictionary As New Dictionary(Of String, IntPtr)

    <System.Runtime.InteropServices.DllImport("User32")> Private Shared Function ShowWindow(ByVal hwnd As IntPtr, ByVal nCmdShow As Int32) As Int32
    End Function

    Private Sub Hide_Process(ByVal Process_Name As String, Optional ByVal Recursive As Boolean = False)

        If Process_Name.ToLower.EndsWith(".exe") Then Process_Name = Process_Name.Substring(0, Process_Name.Length - 4)

        Dim proc() As Process = Process.GetProcessesByName(Process_Name)

        If Recursive Then
            For proc_num As Integer = 0 To proc.Length - 1
                Try
                    Process_Handle_Dictionary.Add(Process_Name & ";" & proc(proc_num).Handle.ToString, proc(proc_num).MainWindowHandle)
                    ShowWindow(proc(proc_num).MainWindowHandle, 0)
                Catch ex As Exception
                    ' MsgBox(ex.Message) ' The handle already exist in the Dictionary
                End Try
                Application.DoEvents()
            Next
        Else
            If Not proc.Length = 0 AndAlso Not proc(0).MainWindowHandle = 0 Then
                Process_Handle_Dictionary.Add(Process_Name & ";" & proc(0).Handle.ToString, proc(0).MainWindowHandle)
                ShowWindow(proc(0).MainWindowHandle, 0)
            End If
        End If

    End Sub

    Private Sub Restore_Process(ByVal Process_Name As String, Optional ByVal Recursive As Boolean = False)

        If Process_Name.ToLower.EndsWith(".exe") Then Process_Name = Process_Name.Substring(0, Process_Name.Length - 4)

        Dim Temp_Dictionary As New Dictionary(Of String, IntPtr) ' Replic of the "Process_Handle_Dictionary" dictionary
        For Each Process In Process_Handle_Dictionary : Temp_Dictionary.Add(Process.Key, Process.Value) : Next

        If Recursive Then
            For Each Process In Temp_Dictionary
                If Process.Key.ToLower.Contains(Process_Name.ToLower) Then
                    ShowWindow(Process.Value, 9)
                    Process_Handle_Dictionary.Remove(Process.Key)
                End If
                Application.DoEvents()
            Next
        Else
            For Each Process In Temp_Dictionary
                If Process.Key.ToLower.Contains(Process_Name.ToLower) Then
                    ShowWindow(Process.Value, 9)
                    Process_Handle_Dictionary.Remove(Process.Key)
                    Exit For
                End If
                Application.DoEvents()
            Next
        End If

    End Sub

#End Region

I did time ago a snippet which works with any proccess, are two generic functions and also has recursive mode and is easy to use:

#Region " Hide-Restore Process "

    ' [ Hide-Restore Process Function ]
    '
    ' // By Elektro H@cker
    '
    ' Examples :
    '
    ' Hide_Process(Process.GetCurrentProcess().MainModule.ModuleName, False)
    ' Hide_Process("notepad.exe", False)
    ' Hide_Process("notepad", True)
    '
    ' Restore_Process(Process.GetCurrentProcess().MainModule.ModuleName, False)
    ' Restore_Process("notepad.exe", False)
    ' Restore_Process("notepad", True)

    Dim Process_Handle_Dictionary As New Dictionary(Of String, IntPtr)

    <System.Runtime.InteropServices.DllImport("User32")> Private Shared Function ShowWindow(ByVal hwnd As IntPtr, ByVal nCmdShow As Int32) As Int32
    End Function

    Private Sub Hide_Process(ByVal Process_Name As String, Optional ByVal Recursive As Boolean = False)

        If Process_Name.ToLower.EndsWith(".exe") Then Process_Name = Process_Name.Substring(0, Process_Name.Length - 4)

        Dim proc() As Process = Process.GetProcessesByName(Process_Name)

        If Recursive Then
            For proc_num As Integer = 0 To proc.Length - 1
                Try
                    Process_Handle_Dictionary.Add(Process_Name & ";" & proc(proc_num).Handle.ToString, proc(proc_num).MainWindowHandle)
                    ShowWindow(proc(proc_num).MainWindowHandle, 0)
                Catch ex As Exception
                    ' MsgBox(ex.Message) ' The handle already exist in the Dictionary
                End Try
                Application.DoEvents()
            Next
        Else
            If Not proc.Length = 0 AndAlso Not proc(0).MainWindowHandle = 0 Then
                Process_Handle_Dictionary.Add(Process_Name & ";" & proc(0).Handle.ToString, proc(0).MainWindowHandle)
                ShowWindow(proc(0).MainWindowHandle, 0)
            End If
        End If

    End Sub

    Private Sub Restore_Process(ByVal Process_Name As String, Optional ByVal Recursive As Boolean = False)

        If Process_Name.ToLower.EndsWith(".exe") Then Process_Name = Process_Name.Substring(0, Process_Name.Length - 4)

        Dim Temp_Dictionary As New Dictionary(Of String, IntPtr) ' Replic of the "Process_Handle_Dictionary" dictionary
        For Each Process In Process_Handle_Dictionary : Temp_Dictionary.Add(Process.Key, Process.Value) : Next

        If Recursive Then
            For Each Process In Temp_Dictionary
                If Process.Key.ToLower.Contains(Process_Name.ToLower) Then
                    ShowWindow(Process.Value, 9)
                    Process_Handle_Dictionary.Remove(Process.Key)
                End If
                Application.DoEvents()
            Next
        Else
            For Each Process In Temp_Dictionary
                If Process.Key.ToLower.Contains(Process_Name.ToLower) Then
                    ShowWindow(Process.Value, 9)
                    Process_Handle_Dictionary.Remove(Process.Key)
                    Exit For
                End If
                Application.DoEvents()
            Next
        End If

    End Sub

#End Region
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文