Click here to Skip to main content
15,892,517 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created test application and opened one process, when i trying to hide it by clicking hide button the message i get : Object reference not set to an instance of an object.

The code i use:

Imports System.Runtime.InteropServices

Public Class Form1

    Const SW_HIDE As Integer = 0
    Const SW_SHOW As Integer = 1

    Dim process As Process


    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function ShowWindow(ByVal hwnd As IntPtr, nCmdShow As Integer) As Boolean
    End Function


    'Button for Hide process
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        ShowWindow(process.MainWindowHandle, SW_HIDE)

    End Sub

    'Button for Show process
    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click

        ShowWindow(process.MainWindowHandle, SW_SHOW)

    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Dim psi As ProcessStartInfo = New ProcessStartInfo()

        psi.FileName = "Calc.exe"
        TextBox1.Text = "Start calculator"

        process.Start(psi)

        'Wait until the process has a main window handle.
        While process.MainWindowHandle = IntPtr.Zero
            process.Refresh()
        End While

    End Sub
End Class


The Error Message appears when i push Hide button
Line : ShowWindow(process.MainWindowHandle, SW_HIDE)

Thank you.
Posted
Updated 3-Nov-14 18:48pm
v3
Comments
Ch Smrutiranjan 3-Nov-14 8:07am    
Could you paste the exact error so that we can get a idea of what causes error and help you.It seems fishy about the ShowWindow() method. if you could paste the code for that method it will be good.
Sinisa Hajnal 4-Nov-14 2:01am    
Please post the stack trace or at least full error message. Thank you.
[no name] 4-Nov-14 2:30am    
Full Exception details:

System.NullReferenceException was unhandled
Message=Object reference not set to an instance of an object.
Source=WindowsApplication1
StackTrace:
at WindowsApplication1.Form1.Button1_Click(Object sender, EventArgs e) in C:\hideTestApp\WindowsApplication1\WindowsApplication1\Form1.vb:line 19
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at WindowsApplication1.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
Sinisa Hajnal 4-Nov-14 3:52am    
Can you check that process is not nothing? Same for WinHandle...

1 solution

Ok,try this:

Instead of this line:
VB
process.Start(psi)


Use this:
VB
process.StartInfo = psi
process.Start()


EDIT
After looking at MSDN, I see that Process.Start(ProcessStartInfo) returns a process,so this would also work:

VB
process = process.Start(psi)
 
Share this answer
 
v2
Comments
Sinisa Hajnal 4-Nov-14 3:54am    
Why would that help?
Pikoh 4-Nov-14 4:02am    
Well...i'm not an expert in VB,but it seems in VB Process works slightly different to C#. If you process.Start(ProcesssStartInfo) it seems that the process started does not get attached to the process variable. In the way i suggested,the process get correctly attached.
Pikoh 4-Nov-14 4:03am    
And by the way,i tried it and it works,so i think yes,it would help :)
Sinisa Hajnal 4-Nov-14 5:29am    
Well done! :)
Pikoh 4-Nov-14 4:08am    
And see updated solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900