Close VB.NET app when main VB6 form is closed - vb.net

I have 2 VB6 application which may launch the same VB.NET application (same exe) via a subMenu. The VB6 code to start this application is a Shell command :
ret = Shell(exe, 1)
My problem is when the user exit the main application (VB6), it won't close the VB.NET application if he opened it.
I tried to use the Taskkill command. It effectively kills the application, but it kills this app for every users on the server !!
I need to do it only for the current user. Can anyone help ?
EDIT : I found some documentation with Taskkill.
It seems that i can precise a user and a domain in the command. I use the following code :
Dim cmdShell As String
quote = Chr$(34)
cmdShell = "taskkill.exe /f /fi " & quote & "USERNAME eq " & _
LCase(connection_User) & quote & " /im " & exeToKill
If someone have a better idea, i'm in :)

Related

Access VBA Shell command not working

so I'm trying to get a very simple shell command to execute a batch file that's created, but for some reason the shell command doesn't do anything.
The batch file gets created and when I double click on the batch file it executes the script just fine, but if I try to call the shell command in VBA it appears that it tries to open a command window, but immediately closes it after, not producing any errors. This code used to work fine before I reformatted this laptop AND this code works on another system running Server 2012 R2 (I'm running windows 10)
Dim SedjaShell
f = "C:\Temp\_SedjaScript.bat"
Open f For Output As #1
Print #1, Chr(34) & SedjaDir & Chr(34) & " merge --files " & Page1 & " " & Page2 & " " & TOS & " " & Disclosure & " --output " & OutputDir
Print #1, "Pause"
Close #1
SedjaShell = Shell("C:\Temp\_SedjaScript.bat", 1)
Is there something I might be missing?
EDIT
Adding the contents of _SedjaScript.Bat
"C:\Users\User\Desktop\DealTracker\MergeFiles\sejda-console-3.0.4\bin\sejda-console.bat" merge --files C:\Temp\ContractSummary.pdf C:\Temp\Page2.pdf C:\Temp\TOS.pdf C:\Temp\Disclosure.pdf --output "C:\Users\User\Desktop\DealTracker\Completed Contracts\Company-Name-Inc-07-19-2017-Contract.pdf"
Pause
Thanks to Andre I was able to get the script to work inside VBA's Shell command.
By setting the PATH for my JRE to JAVA_HOME I was able to successfully launch the script.
setx JAVA_HOME "C:\Program Files (x86)\Java\jre1.8.0_141"
setx PATH "%PATH%;%JAVA_HOME%\bin";

Execute .bat file vb.net

I need to install my win service. With installUtil it is just few lines of code.
#ECHO OFF
REM The following directory is for .NET 2.0
set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v2.0.50727
set PATH=%PATH%;%DOTNETFX2%
echo Installing MyService...
echo ---------------------------------------------------
InstallUtil /i MyService.exe
echo ---------------------------------------------------
echo Done.
pause
But my thoughts are without creating .bat file and then runing it.
Is there any way i can ".execute" those lines of code above without creating .bat file runing it and then deleting it ?.
I will need to dynamically create this code every time because i need to enter the username/password depending what user entered on .net form.
You could start cmd and doing it in one line via it's arguments:
Process.Start("cmd.exe", "/k set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v2.0.50727 & set PATH=%PATH%;%DOTNETFX2% & InstallUtil /i MyService.exe")
And if you want it to show the text you wrote and to "pause" (stay open):
Process.Start("cmd.exe", "/k set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v2.0.50727 & set PATH=%PATH%;%DOTNETFX2% & echo Installing MyService... & echo --------------------------------------------------- & InstallUtil /i MyService.exe & echo --------------------------------------------------- & echo Done. & pause")
Commands are separated by " & ".
I know it's been a month since you first asked this, but I recently came up with a pretty good solution to this - only using this simple VB.NET code:
Public Sub InstallService(ByVal ServicePath As String)
Dim InstallUtilPath As String = IO.Path.Combine(System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory(), "installutil.exe")
Dim InstallUtilProcess As Process = Process.Start(InstallUtilPath, """" & ServicePath & """")
InstallUtilProcess.WaitForExit()
'Service is now installed.
InstallUtilProcess = Process.Start(InstallUtilPath, "/i """ & ServicePath & """")
InstallUtilProcess.WaitForExit()
'The second action is now done. Show a MessageBox or something if you'd like.
End Sub
The ServicePath parameter is the path to the service that you want to install.
The InstallUtilPath variable will be set to the path of the installutil.exe application. It will get the path for the current framework you're running.
As I'm running .NET Framework 4 the path is C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe
Hope this helps!

Equivalent to Run Method(VBA) in OpenOffice Calc

I have been trying to convert a VBA for Excel code to OpenOffice Calc (basic) and now Im having trouble accessing an external program (and it´s database) to generate an output which I will use later in my code. In other words, I give an input, call the program and later I want an output file.
In VBA:
Prog = Worksheets("Settings").Cells(2, 2) & IPRODB & " -i " & DateiAll_in
Wait = True
Set sh = CreateObject("WScript.Shell")
RetVal = sh.Run(Prog, 0, Wait)
Worksheets("Settings").Cells(2, 2) is the program's path and IPRODB is the database's path, "-i" seems to be a command to the program and DateiAll_in is the Input file (.csv) I created before in the code.
The OpenOffice basic offers the function Shell(Pathname, Windowstyle, Param, bSync) to open external programs but It would not be the same since in the "run method" in VBA I am running a macro containing the program, its´s Database and Input File. (expression.Run(MacroName, varg1,varg2, (...))
Is there any alternative to the Shell function or can I use it in the same way as I used the "run method" in VBA?
From the OpenOffice.org forums (didn't test it myself):
Dim oSvc as object
oSvc = createUnoService("com.sun.star.system.SystemShellExecute")
Rem Launch notepad
oSvc.execute(ConvertToUrl("C:\windows\notepad.exe"), "", 0)
I used the Shell function. In the first argument of the function I added all the equivalents of "Worksheets("Settings").Cells(2, 2) & IPRODB & " -i " & DateiAll_in". I substituted Worksheets("Settings").Cells(2, 2) for the direct path of the program only because it was easier. So it worked with this code below:
Shell("C:\Program Files\*******\*****\*****.exe & IPRODB & "-i" & DateiAll_in",1,,True)

Windows Service Seems not work appropriately

It is my first time to create a Windows Service and after read many articles on the internet, I made one by myself. It installs successfully, runs, but does not work as expected.
For example:
Inside of a Timer, I call the following code:
Public Sub WriteLog(log As String)
log = DateTime.Now.ToLongTimeString() & ", " & DateTime.Now.ToLongDateString() & vbNewLine & " -> " & log
Dim path As String = "c:\Temp\z1111.log"
Dim sw As StreamWriter
sw = File.AppendText(path)
sw.WriteLine(log)
sw.Flush()
sw.Close()
End Sub
The code above works in a Windows Form project, but as Windows Service it creates a file named z1111.log, add the content overwriting and does not do anything else, but when I delete the file, it creates again with a new date and time.
The question is: why as Windows Form it appends and as Windows Service it overwrites and stop, only works when I delete the file?
It is possible to download the project: LINK
Does anyone know how to solve it?
I solved this problem by changing the log file to the Windows folder. It has been a security issue.

Possibility of VB.net and VBScript

Is it possible to include in a VB.net 2008 Project a VBScript (test.vbs) and run it if its while the processing necessary? But the main thing is it should be possible to BUILD just one .exe.
If so, can you also receive values / arguments from the VBS file?
Here is an example, although it's pointless, but it is used for unterstanding:
VB.net -> exe is running
the exe runs please_find_the_coputername.vbs
The script please_find_the_coputername.vbs -> obtained the computer name and sends this variable to VB.net
VB.Net displays the computer name via Msgbox().
Note: I know that I can read out the computer name with VB.net but this example is only for understanding my questions.
Edit:
HI #maxedev thank you for your answer.
Wow.. its nice trick.
But I want only to do this VBScript code in VB.net:
Dim strComputer
strComputer = "LP-BKR"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
For Each objComputer in colComputer
Wscript.Echo "Logged-on Domain: " & objComputer.Domain
Wscript.Echo "Logged-on UserName: " & objComputer.UserName
Wscript.Echo "Logged-on ComputerName: " & objComputer.Name
Next
set objWMIService = Nothing
set colComputer = Nothing
I searched the whole day to get the same Value... but didn't find anything. That's why I decide to do that in this way. But if I think, the trick with clipboard is risky. It pushes the still clipboard text away. How can I realize it?
I'm not sure exactly what you're trying to accomplish, but you could write to a text file and then read it through vb.net - or you could do something like this post to use the clipboard to pass info ie :
VBS:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd.exe /c echo hello world | clip", 0, TRUE
VB.NET:
MessageBox.Show(Clipboard.GetText)
--shows "hello world"
One solution would be to add a reference to the MS Script Control:
http://msdn.microsoft.com/en-us/library/aa227400(v=vs.60).aspx
Using that, you can add literally add code (VBScript) with the AddCode() method then run it and get the output back. I have a tiny example here.
Windows automatically provides the information you're looking for in environment variables:
%USERNAME% -> username of the logged in user
%USERDOMAIN% -> WINS name of the domain the user is logged into
%USERDNSDOMAIN% -> FQDN of the domain the user is logged into
%COMPUTERNAME% -> hostname of the computer