By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,579 Members | 1,790 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,579 IT Pros & Developers. It's quick & easy.

Close a DOS window via VB.NET

P: n/a
Hi,
I'm using the following code to spawn another app from VB.NET..

Dim ShellProcess As New Process
ShellProcess.StartInfo.FileName = "xxxx.exe"
ShellProcess.StartInfo.UseShellExecute = True
ShellProcess.StartInfo.WorkingDirectory = "C:\Windows\System32"
ShellProcess.Start()
ShellProcess.WaitForInputIdle()

xxxx.exe creates a DOS window
I am unable to close it via.... ShellProcess.CloseMainWindow()
or kill it via....ShellProcess.Kill()
I know I can use API FindWindow, PostMessage etc. I'm looking for a true
..NET clean way to do this. Nothing I try works! Anybody run into this?
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Check help for GetProcessbyID or GetProcessbyName

I'm not at my VS.Net / MSDN machine right now or I'd post some syntax.
--
Steve Easton
Microsoft MVP FrontPage
95isalive
This site is best viewed..................
...............................with a computer

"Cutlass" <Cu*****@discussions.microsoft.com> wrote in message
news:3C**********************************@microsof t.com...
Hi,
I'm using the following code to spawn another app from VB.NET..

Dim ShellProcess As New Process
ShellProcess.StartInfo.FileName = "xxxx.exe"
ShellProcess.StartInfo.UseShellExecute = True
ShellProcess.StartInfo.WorkingDirectory = "C:\Windows\System32"
ShellProcess.Start()
ShellProcess.WaitForInputIdle()

xxxx.exe creates a DOS window
I am unable to close it via.... ShellProcess.CloseMainWindow()
or kill it via....ShellProcess.Kill()
I know I can use API FindWindow, PostMessage etc. I'm looking for a true
.NET clean way to do this. Nothing I try works! Anybody run into this?

Nov 21 '05 #2

P: n/a
Thanks
I tried all afternoon and can't get it to work, it's as if the DOS window
doesn't exist.
I must be coding something wrong ... when I go into task manager after the
spawn I can see the exe listed and I see it in the taskbar/screen too...I
just can't seem to program a Close for it.....HELP Very Appreciated...I can
use the API method..that works...but I want to code pure .NET if you know
what I mean...

"95isalive" wrote:
Check help for GetProcessbyID or GetProcessbyName

I'm not at my VS.Net / MSDN machine right now or I'd post some syntax.
--
Steve Easton
Microsoft MVP FrontPage
95isalive
This site is best viewed..................
...............................with a computer

"Cutlass" <Cu*****@discussions.microsoft.com> wrote in message
news:3C**********************************@microsof t.com...
Hi,
I'm using the following code to spawn another app from VB.NET..

Dim ShellProcess As New Process
ShellProcess.StartInfo.FileName = "xxxx.exe"
ShellProcess.StartInfo.UseShellExecute = True
ShellProcess.StartInfo.WorkingDirectory = "C:\Windows\System32"
ShellProcess.Start()
ShellProcess.WaitForInputIdle()

xxxx.exe creates a DOS window
I am unable to close it via.... ShellProcess.CloseMainWindow()
or kill it via....ShellProcess.Kill()
I know I can use API FindWindow, PostMessage etc. I'm looking for a true
.NET clean way to do this. Nothing I try works! Anybody run into this?


Nov 21 '05 #3

P: n/a
If you're using GetProcessbyName use the short or shell name of the process

GetProcessByName("cmd") or GetProcessByName("command") instead of using cmd.exe or
command.com

There's an example script in the MSDN library for closing instances of notepad, but once
again I'm away from that machine.

--
Steve Easton
Microsoft MVP FrontPage
95isalive
This site is best viewed..................
...............................with a computer
"Cutlass" <Cu*****@discussions.microsoft.com> wrote in message
news:DB**********************************@microsof t.com...
Thanks
I tried all afternoon and can't get it to work, it's as if the DOS window
doesn't exist.
I must be coding something wrong ... when I go into task manager after the
spawn I can see the exe listed and I see it in the taskbar/screen too...I
just can't seem to program a Close for it.....HELP Very Appreciated...I can
use the API method..that works...but I want to code pure .NET if you know
what I mean...

"95isalive" wrote:
Check help for GetProcessbyID or GetProcessbyName

I'm not at my VS.Net / MSDN machine right now or I'd post some syntax.
--
Steve Easton
Microsoft MVP FrontPage
95isalive
This site is best viewed..................
...............................with a computer

"Cutlass" <Cu*****@discussions.microsoft.com> wrote in message
news:3C**********************************@microsof t.com...
Hi,
I'm using the following code to spawn another app from VB.NET..

Dim ShellProcess As New Process
ShellProcess.StartInfo.FileName = "xxxx.exe"
ShellProcess.StartInfo.UseShellExecute = True
ShellProcess.StartInfo.WorkingDirectory = "C:\Windows\System32"
ShellProcess.Start()
ShellProcess.WaitForInputIdle()

xxxx.exe creates a DOS window
I am unable to close it via.... ShellProcess.CloseMainWindow()
or kill it via....ShellProcess.Kill()
I know I can use API FindWindow, PostMessage etc. I'm looking for a true
.NET clean way to do this. Nothing I try works! Anybody run into this?


Nov 21 '05 #4

P: n/a
The code works when I spawn notepad, it doesn't when I use my exe.

What is the proper VB.NET way to declare these VB6 dlls?
Public Declare Function WaitForSingleObject Lib "kernel32" _
(ByVal hHandle As Long, _
ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long
Public Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Public Declare Function PostMessage Lib "user32" _
Alias "PostMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Public Declare Function IsWindow Lib "user32" _
(ByVal hwnd As Long) As Long
Public Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
Public Declare Function GetWindowThreadProcessId Lib "user32" _
(ByVal hwnd As Long, _
lpdwProcessId As Long) As Long

"95isalive" wrote:
If you're using GetProcessbyName use the short or shell name of the process

GetProcessByName("cmd") or GetProcessByName("command") instead of using cmd.exe or
command.com

There's an example script in the MSDN library for closing instances of notepad, but once
again I'm away from that machine.

--
Steve Easton
Microsoft MVP FrontPage
95isalive
This site is best viewed..................
...............................with a computer
"Cutlass" <Cu*****@discussions.microsoft.com> wrote in message
news:DB**********************************@microsof t.com...
Thanks
I tried all afternoon and can't get it to work, it's as if the DOS window
doesn't exist.
I must be coding something wrong ... when I go into task manager after the
spawn I can see the exe listed and I see it in the taskbar/screen too...I
just can't seem to program a Close for it.....HELP Very Appreciated...I can
use the API method..that works...but I want to code pure .NET if you know
what I mean...

"95isalive" wrote:
Check help for GetProcessbyID or GetProcessbyName

I'm not at my VS.Net / MSDN machine right now or I'd post some syntax.
--
Steve Easton
Microsoft MVP FrontPage
95isalive
This site is best viewed..................
...............................with a computer

"Cutlass" <Cu*****@discussions.microsoft.com> wrote in message
news:3C**********************************@microsof t.com...
> Hi,
> I'm using the following code to spawn another app from VB.NET..
>
> Dim ShellProcess As New Process
> ShellProcess.StartInfo.FileName = "xxxx.exe"
> ShellProcess.StartInfo.UseShellExecute = True
> ShellProcess.StartInfo.WorkingDirectory = "C:\Windows\System32"
> ShellProcess.Start()
> ShellProcess.WaitForInputIdle()
>
> xxxx.exe creates a DOS window
> I am unable to close it via.... ShellProcess.CloseMainWindow()
> or kill it via....ShellProcess.Kill()
> I know I can use API FindWindow, PostMessage etc. I'm looking for a true
> .NET clean way to do this. Nothing I try works! Anybody run into this?
>
>


Nov 21 '05 #5

P: n/a
Never mind, figured the declares out, but my VB6 calls to these functions
fail. Would you know how to properly convert them?
I'm getting messsages like:
Value of type 'Integer' cannot be converted to 'System.IntPtr'. yada yada

Dim hWindow As Long
Dim hThread As Long
Dim hProcess As Long
Dim lProcessId As Long
Dim lngResult As Long
Dim lngReturnValue As Long
Dim iLoopCnt As Integer

hWindow = FindWindow(vbNullString, "(Inactive " & sDir & "\EDIAPIS.EXE)")
hThread = GetWindowThreadProcessId(hWindow, lProcessId)
hProcess = OpenProcess(SYNCHRONIZE, 0&, lProcessId)
lngReturnValue = PostMessage(hWindow, WM_CLOSE, 0&, 0&)
lngResult = WaitForSingleObject(hProcess, 20000)

"Cutlass" wrote:
The code works when I spawn notepad, it doesn't when I use my exe.

What is the proper VB.NET way to declare these VB6 dlls?
Public Declare Function WaitForSingleObject Lib "kernel32" _
(ByVal hHandle As Long, _
ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long
Public Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Public Declare Function PostMessage Lib "user32" _
Alias "PostMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Public Declare Function IsWindow Lib "user32" _
(ByVal hwnd As Long) As Long
Public Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
Public Declare Function GetWindowThreadProcessId Lib "user32" _
(ByVal hwnd As Long, _
lpdwProcessId As Long) As Long

"95isalive" wrote:
If you're using GetProcessbyName use the short or shell name of the process

GetProcessByName("cmd") or GetProcessByName("command") instead of using cmd.exe or
command.com

There's an example script in the MSDN library for closing instances of notepad, but once
again I'm away from that machine.

--
Steve Easton
Microsoft MVP FrontPage
95isalive
This site is best viewed..................
...............................with a computer
"Cutlass" <Cu*****@discussions.microsoft.com> wrote in message
news:DB**********************************@microsof t.com...
Thanks
I tried all afternoon and can't get it to work, it's as if the DOS window
doesn't exist.
I must be coding something wrong ... when I go into task manager after the
spawn I can see the exe listed and I see it in the taskbar/screen too...I
just can't seem to program a Close for it.....HELP Very Appreciated...I can
use the API method..that works...but I want to code pure .NET if you know
what I mean...

"95isalive" wrote:

> Check help for GetProcessbyID or GetProcessbyName
>
> I'm not at my VS.Net / MSDN machine right now or I'd post some syntax.
>
>
> --
> Steve Easton
> Microsoft MVP FrontPage
> 95isalive
> This site is best viewed..................
> ...............................with a computer
>
> "Cutlass" <Cu*****@discussions.microsoft.com> wrote in message
> news:3C**********************************@microsof t.com...
> > Hi,
> > I'm using the following code to spawn another app from VB.NET..
> >
> > Dim ShellProcess As New Process
> > ShellProcess.StartInfo.FileName = "xxxx.exe"
> > ShellProcess.StartInfo.UseShellExecute = True
> > ShellProcess.StartInfo.WorkingDirectory = "C:\Windows\System32"
> > ShellProcess.Start()
> > ShellProcess.WaitForInputIdle()
> >
> > xxxx.exe creates a DOS window
> > I am unable to close it via.... ShellProcess.CloseMainWindow()
> > or kill it via....ShellProcess.Kill()
> > I know I can use API FindWindow, PostMessage etc. I'm looking for a true
> > .NET clean way to do this. Nothing I try works! Anybody run into this?
> >
> >
>
>
>


Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.