Process.StartInfo and Sending arguments - vb.net

I'm making a compiled program for my company. Currently, we have several functions in separate programs, so to make it easier, decide to combine them all in one. However, the license generation program is closed source, so I don't know how to generate the license. My idea was, open the license generation program by my program in VB and run the command lines in it.
With Process.Start, I can open the program, however, I cannot send information from one program to another. I figured this could be done as follows:
Dim proc As New Process()
proc.StartInfo.FileName = "C:\folder\geralicensa.exe"
proc.StartInfo.Arguments = "/C arguments"
proc.Start()
the arguments would be two numbers representing the customer. In the license generation program, we just write the first number, press enter, write the second, and then press enter again, but I can't even send the first argument, let alone "press enter"

Related

How to run DOS/CMD/Command Prompt commands from VB.NET and want to hold its terminal output for few seconds?

How to run DOS/CMD/Command Prompt commands from VB.NET and want to hold its terminal output for few seconds. After that it should be close automatically.
Depending on your goal, there could be multiple solutions to this request.
This SO post details how you can run a command from cmd.exe, passing arguments.
How to run DOS/CMD/Command Prompt commands from VB.NET?
Another idea, depending on the goal, would be to save the cmd as a batch file or similar, where you could have a pause or timed wait before the cmd window closes.
Then you could call the batch file from vb.net
I don't want to dive too deep into how each of these would be done without knowing more about the goal at hand.

Is it possible to have .net console application that embed another executable file?

I have a single command line windows executable that has many options built into this exe file.
Eg:
(It can take screenshot)
ToolGo.exe printscreen c:\temp\filename.jpg yyyymmdd
(It can show up)
ToolGo.exe showIP machineA
I want to write another command line application, possibly in .net , where it can embed/build a wrapper around this ToolGo.exe file into my application without the user be able to use the ToolGo.exe, and also users can only access one function of this main exe file.
In the example I want this other tool to access only the print screen function in this new exe file.
The new application will have this:
Tool2go.exe printscreen c:\temp\filename.jpg yyyymmdd
But if someone types the following, it will not work:
Tool2go.exe showIP machineA
Or
ToolGo.exe showIP machineA
Any ideas how I can write this code to do this in a .net command line application?
This is a multi-part question, so I'll just give the main part of the issue as the answer with suggestions on handling the rest.
You can embed a .exe into your program by clicking on Properties and navigating the the Resources section, and adding that .exe to it.
After that, it's just a matter of extracting it locally so you can pass your commands to it, and handle it's responses. (I'm not really aware of any way to do so w/out first extracting the. exe; the .exe itself needs to run somehow after all).
To extract the embedded .exe, you do this:
' Extract the MyProgram resource (i.e. your .exe)
Dim b() As Byte = My.Resources.MyProgram
' Write it to the user's Temp folder
File.WriteAllBytes(Environment.ExpandEnvironmentVariables("%TEMP%\MyProgram.exe"), b)
By extracting it to the user's Temp folder, you can pass it your commands, and since it's 'out of sight' the user probably won't even know it's there to directly use it themselves, unless they're a bit more advanced and visit their Temp folder often. You can slightly help to avoid this, but extracting the .exe when your program starts, and then deleting it when it exits, so it only exists on the user's system while your program is running.
As far as what the user can and cannot type in order to pass to the program, you can simply handle the filtering with your program; since your program is the one passing the commands to the .exe, just don't pass any commands that you don't allowed, and pass the ones you do want allowed.

How would I switch to and maximize an outside application

Someone may have already posted about this, but I am working on an application in VB.NET that requires me to switch to another open window and maximize it. I cannot for my life figure out a reliable way to do this (I gave up and called an autohotkey script, but even that broke). Any help is greatly appreciated!
Edit: I saw the forum post here, but it doesn't seem to work
If you take a look at the post you already mentioned, you will notice that it contains following line:
Dim localByName As Process() = Process.GetProcessesByName("met2")
You need to replace "met2" string with exact name of process whose window you're trying to maximize. If you don't know the name of targeted process, do the following:
run the application you intend to maximize
open Task Manager
switch to "Processes" tab for complete list of running processes
pinpoint the process you're looking for (commonly the filename of running application)
use process name (commonly filename of the application) without extension as a friendly process name
For example, if you want to maximize running Notepad application using methodology explanied in this post you will need to change following in existing code:
Dim localByName As Process() = Process.GetProcessesByName("notepad")

Call an external program to run as standard user from a requiredAdministrator program

My Visual Basic program will copy files to a program files folder, so I have to use requiredAdministrator privileges since asInvoker won't allow to write in the program files folder.
After I copy the files I invoke an AutoIt script automating setup of files within the external program (for that the script calls the external program to start automation). The program that creates and copies the files to the "end" program functions fine. The script that calls the "end" program and does the automatic setup also works.
When I combine the 2 the "end" program (which I didn't write nor have the source code of) behaves erratically when run as admin (doesn't read the database or the needed files return an error and terminates itself). So run as admin is not an option. But since my program has to run as admin it looks like it passes the sames privileges to the AutoIt script which calls the "end" program as admin as well. It also happens if I call the "end" program from my app instead of the AutoIt script.
Is there any way to demote my app from admin to standard user after it copies the files, right before it calls either the AutoIt script or the "end" program so that the "end" program is not run as admin or a parameter that specifically makes the app to call the external program as standard user?
I'm using Process.start("autoitscript.exe") to call it. Or any other workaround that doesn't involve the AutoIt script calling the "end" program and my app because that works but not as I intent.
This is a tricky task to perform, but how about this:
Have your application start asInvoker, don't show any windows, and make check if it runs with elevated privileges using this code:
Public Shared Function IsAdministrator() As Boolean
Return (New WindowsPrincipal(WindowsIdentity.GetCurrent())).IsInRole(WindowsBuiltInRole.Administrator)
End Function
If it's not running with elevated privileges make it start an invisible cmd instance where you redirect the standard input.
Get the cmd process's PID and now start a new instance of your application with elevated privileges (can be done by setting StartInfo.Verb = "runas") and pass the PID as a command-line parameter.
Now your new instance of the app starts and IsAdministrator() should return True.
So now that you know that your app has administrator privileges you can check if the app has a command-line parameter that is parsable to an Integer. If so, store that Integer somewhere and then do all your admin-required work.
In the end where you want to start the autoitscript.exe application you create a process variable and assigns Process.GetProcessById(<your PID Integer here>) to it.
For example:
Dim cmdProcess As Process = Process.GetProcessById(cmdPID)
Now that you have control over the cmd instance again you just have to write to it's standard input (this article describes a little how it works).
You want to write two lines. The first is to start the other application:
autoitscript.exe
and the second is to close the cmd instance:
exit
If anything's unclear just let me know.

Getting the type (x64 or x86) of a running process in vb.net macro code

I'm writing a macro to automate the process of attaching to the IIS worker process (w3wp.exe, Windows Server 2k8) from Visual Studio. The trouble is that I often two app pools running at any given time, one in x64 mode and one in x86 mode. This means there are two processes called w3wp.exe running at any given time, and the only way to distinguish between them is the mode they are running in. When I use the "Attach to Process" dialog, there is a "Type" column that shows that information so I know which w3wp.exe to attach to, but I can't figure out how to get that information in my macro.
Based on information here, I was able to come up with the following:
Function AttachToProcess(ByVal processName As String) As Boolean
Dim proc As EnvDTE.Process
Dim attached As Boolean
For Each proc In DTE.Debugger.LocalProcesses
If proc.Name = "w3wp.exe" Then
proc.Attach()
attached = True
End If
Next
Return attached
End Function
But half the time this just grabs the wrong process. I need a second if statement to check the mode/type of the process. I've spelunked through the classes using quickwatch as best I can, but just cannot figure out where the information is. Can anyone help? Thanks!
There's not enough info in the Process class to let you find out. You can only get the ProcessID for the process. From there, you'd have to P/Invoke OpenProcess() to get the process handle, then IsWow64Process() to find out if it is a 32-bit process. CloseHandle() to close the process handle. Not actually sure if P/Invoke is possible in a macro. Visit pinvoke.net to get the declarations you'll need.