Install driver inf from Visual Studio Setup project - vb.net

So.. We have a C# utility application for a usb/serial device. I would like to install the driver during the setup process. I know there are lots of posts about this, and I've gone through many msdn and stackoverflow articles, but I just do not seem to succeed.
The driver is OK, it is a stock Arduino driver, so it is signed and can be installed from windows gui (right click on the inf, install)
I created a visual studio setup project for our applcation, and a VB project for the custom setup actions. The setup copies the driver to the installation folder. The driver installation is performed from the "OnCommitted" method. Here I spawn a process by calling cmd.exe and would like to call some command to install the driver.
First I tried "RUNDLL32.EXE SETUPAPI.DLL,InstallHinfSection DefaultInstall 132 ", but it is always fails, although I acquire admin rights for this.
I tried to acquire admin rights at different places:
- I tried to add a "Launch Condition" with the condition "AdminUser"
- I tried to run cmd as admin from VB (Process.Startinfo.Verb = "runas")
none of the above solved my problem.
Then I tried alternative ways to install the driver and InfDefaultInstall.exe is almost fine, but sometimes it fails on some machines, and I don't know why. pnputil.exe always fails for me.
I guess the official way to do this is calling InstallHinfSection as MSDN suggests, and I would prefer to use that. We have to support Windows 7 and above.
Any thoughts on what I do wrong?
Thanks in advance!
here are my VB methods:
Private Sub InstallDriverPNPUtil()
Dim infPath As String = """" + Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\arduino.inf" + """"
Dim processInfo As New ProcessStartInfo("CMD", "/C pnputil -i -a " + infPath)
processInfo.WindowStyle = ProcessWindowStyle.Hidden
Dim p As System.Diagnostics.Process = Process.Start(processInfo)
p.WaitForExit()
End Sub
Private Sub InstallDriverInfDefaultInstall()
Dim infPath As String = """" + Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\arduino.inf" + """"
Dim processInfo As New ProcessStartInfo("CMD", "/C InfDefaultInstall.exe " + infPath)
processInfo.WindowStyle = ProcessWindowStyle.Hidden
Dim p As System.Diagnostics.Process = Process.Start(processInfo)
p.WaitForExit()
End Sub
Private Sub InstallDriverInstallHinfSection()
Dim infPath As String = """" + Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\arduino.inf" + """"
Dim process As New Process()
process.StartInfo.FileName = "cmd.exe"
'fails with and without this
process.StartInfo.UseShellExecute = True
process.StartInfo.Verb = "runas"
process.StartInfo.Arguments = "/C RUNDLL32.EXE SETUPAPI.DLL,InstallHinfSection DefaultInstall 132 " + infPath
'MsgBox(process.StartInfo.Arguments, MsgBoxStyle.OkOnly, "InstallHinfSection")
process.Start()
process.WaitForExit()
End Sub

Related

In .net vb is there a way to know if Yes or No was chosen in a SHELL command when User Account control appears

I am using .net vb ; I usually shell a program to run an update of my software; the problem I am facing is I don't know whether the user has chosen Yes or No when prompted to run the program in an alleviated mode i.e When the User Account control box appears.
How can I know whether Yes or No was chosen after the shell command was issued. .
This is my current code - I use runas for a higher windows then XP
Public Shared Sub UpdateElevated()
Try
'Shell(C_Drive & "\POS-UPD\pos-upd.exe", AppWinStyle.NormalFocus, False)
Dim osInfo As OperatingSystem = Environment.OSVersion
Dim startInfo As ProcessStartInfo = New ProcessStartInfo()
startInfo.UseShellExecute = True
startInfo.WorkingDirectory = C_Drive & "\POS-UPD"
startInfo.FileName = C_Drive & "\POS-UPD\pos-upd.exe"
If osInfo.Version.Major >= 6 Then startInfo.Verb = "runas"
startInfo.WindowStyle = ProcessWindowStyle.Normal
Process.Start(startInfo)
Catch ex As Exception
End Try
End Sub

Compile a visual studio solution programmatically

I have a requirement of programmatically compiling a solution. I cannot directly give the path of MSBuild because it differs between 2013 and earlier versions.
I am sharing my code below -
Using exeprocess As New System.Diagnostics.Process
exeprocess.StartInfo.FileName = "cmd"
exeprocess.StartInfo.CreateNoWindow = True
exeprocess.StartInfo.UseShellExecute = False
exeprocess.StartInfo.RedirectStandardInput = True
exeprocess.StartInfo.RedirectStandardOutput = True
exeprocess.Start()
Dim sw As StreamWriter = exeprocess.StandardInput
Dim sr As StreamReader = exeprocess.StandardOutput
sw.WriteLine("PUSHD C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\")
sw.WriteLine("call vcvarsall.bat")
sw.WriteLine("#MSBuild /t:Rebuild" & " /flp1:logfile=" & """" & logFilePath & """;errorsonly" & " " & """" & solutionPath & """")
sr.ReadLine()
While Not sr.EndOfStream()
sr.ReadLine()
End While
End Using
My requirement is to wait until the compilation is over.
The issue is that it hangs at the line "While Not sr.EndOfStream()".
I am unable to understand the reason for the issue. Not sure if this is the right way of ensuring that the compilation is over.
Any help is highly appreciated.
I can't tell what your specific problem is, but a few recommendations:
Don't bother writing to input stream, just generate a simple .bat text file and launch it.
You can use Microsoft.Build.Utilities.ToolLocationHelper to get any of the paths.
You can use Microsoft.Build.Execution.BuildManager to build programmatically.

Running CMD command on windows service

I have created a windows service that requires executing an EXE file with the CMD process. I have used the following code:
Str = "C:\PCounter\Staff\account.exe CHARGE " & Name & " " & Amount & " TO" & Id
Dim procStartInfo As New System.Diagnostics.ProcessStartInfo(Str)
procStartInfo.RedirectStandardOutput = True
procStartInfo.UseShellExecute = False
procStartInfo.CreateNoWindow = True
Dim proc As New System.Diagnostics.Process
proc.StartInfo = procStartInfo
proc.Start()
proc.Dispose()
However the above code will return
system cannot find the file specified
I have tried same code on the Windows form, and its works fine. To make sure the path is correct I have added a text file in the same location as EXE file, and load the content of the text file in the service. It works fine.
I can't think of anything else; I really would appreciate it if you can help me on this.
ProcessStartInfo has two properties. One for the executable to run, and the other for the arguments to pass to the executable. The symantics for the Arguments property are the exact same as the command line.
You can not include the arguments in the same property as the executable. They must be separated.
Create service:
sc create Vm-Symantec04 binPath= "\"C:\App32\VMware\VMware Workstation\vmrun.exe\" -T ws start \"D:\VM\Sym04\Sym04.vmx\" nogui" depend= "VMAuthdService/VMnetDHCP/VMUSBArbService/VMware NAT Service" start= auto
Delete service:
sc delete Vm-Symantec04

Running a advanced java call from VB.net

I need to run a small piece of Java code (Java is the only option in this case)
I have the jar file in the VB.net resources as JSSMCL(the extension is not required to run it, of this I am sure :P) I know I use Path.GetFullPath(My.Resources.ResourceManager.BaseName)
but no mater how I do it it fails, I have tried so many ways i have lost count!
this is the command that I need to run:
java -cp "JSSMCL.jar" net.minecraft.MinecraftLauncher username false
You can use System.Diagnostics.Process class and its method to start/run the external process.
Refer to the following code part to run the Command using Process
Sub Main()
' One file parameter to the executable
Dim sourceName As String = "ExampleText.txt"
' The second file parameter to the executable
Dim targetName As String = "Example.gz"
' New ProcessStartInfo created
Dim p As New ProcessStartInfo
' Specify the location of the binary
p.FileName = "C:\7za.exe"
' Use these arguments for the process
p.Arguments = "a -tgzip """ & targetName & """ """ & sourceName & """ -mx=9"
' Use a hidden window
p.WindowStyle = ProcessWindowStyle.Hidden
' Start the process
Process.Start(p)
End Sub
EDIT:
Use the Coding part like below, may be it works
-jar "compiler.jar" --js_output_file="myOutput.min.js" --js="input1.js" --js="input2.js"
Have a look at this link for your problem

Running powershell scripts from within a .NET windows app

I'm needing to run scripts from within a vb.net windows app.
I've got the scripts running in the background fine;
Using MyRunSpace As Runspace = RunspaceFactory.CreateRunspace()
MyRunSpace.Open()
Using MyPipeline As Pipeline = MyRunSpace.CreatePipeline()
MyPipeline.Commands.AddScript("import-module -name " & moduleName &
vbCrLf &
"(get-module -name " & moduleName & ").version")
Dim results = MyPipeline.Invoke()
'Do something with the results
End Using
MyRunSpace.Close()
End Using
However, i now need to be able to have the powershell run (not in the background) eg. When prompts occur;
Set-ExecutionPolicy unrestricted
I'm currently looking into the Microsoft.PowerShell.ConsoleHost namespace to see if i can use something like;
Dim config = RunspaceConfiguration.Create
ConsoleShell.Start(config, "Windows PowerShell", "", New String() {""})
Can anyone advise me please???
EDIT: I've fudged it a bit with this;
Public Function RunPowershellViaShell(ByVal scriptText As String) As Integer
Dim execProcess As New System.Diagnostics.Process
Dim psScriptTextArg = "-NoExit -Command ""& get-module -list"""
'Dim psScriptTextArg = "-NoExit -Command ""& set-executionPolicy unrestricted"""
'Dim psScriptTextArg = ""-NoExit -Command """ & scriptText & """"
execProcess.StartInfo.WorkingDirectory = Environment.SystemDirectory & "\WindowsPowershell\v1.0\"
execProcess.StartInfo.FileName = "powershell.exe"
execProcess.StartInfo.Arguments = psScriptTextArg
execProcess.StartInfo.UseShellExecute = True
Return execProcess.Start
End Function
But there's gotta be a better way??
There is a distinction between the PowerShell engine and its host. What you're wanting is to run the engine within your application but then fire up a separate host (which also is hosting the PowerShell engine) to handle prompts. You might want to look into modifying your application to act as a host itself. You could then react to prompts (read-host) and pop dialog boxes or whatever. Take a look at this relevant PowerShell namespace. Also check out this blog post on creating a simple PSHost.