Using MSDN I got the class to write a wrapper for my command line tool.
I now am facing a problem, if I execute the exe through the command line with arguments, it works perfect without any errors.
But when I try to pass the arguments from the Wrapper it crashes the program.
Wanted to know if I am passing the arguments properly and if I am wrong, could somebody point out please.
This is the LaunchEXE class from MSDN
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
namespace SPDB
{
/// <summary>
/// Class to run any external command line tool with arguments
/// </summary>
public class LaunchEXE
{
internal static string Run(string exeName, string argsLine, int timeoutSeconds)
{
StreamReader outputStream = StreamReader.Null;
string output = "";
bool success = false;
try
{
Process newProcess = new Process();
newProcess.StartInfo.FileName = exeName;
newProcess.StartInfo.Arguments = argsLine;
newProcess.StartInfo.UseShellExecute = false;
newProcess.StartInfo.CreateNoWindow = true; //The command line is supressed to keep the process in the background
newProcess.StartInfo.RedirectStandardOutput = true;
newProcess.Start();
if (0 == timeoutSeconds)
{
outputStream = newProcess.StandardOutput;
output = outputStream.ReadToEnd();
newProcess.WaitForExit();
}
else
{
success = newProcess.WaitForExit(timeoutSeconds * 1000);
if (success)
{
outputStream = newProcess.StandardOutput;
output = outputStream.ReadToEnd();
}
else
{
output = "Timed out at " + timeoutSeconds + " seconds waiting for " + exeName + " to exit.";
}
}
}
catch (Exception e)
{
throw (new Exception("An error occurred running " + exeName + ".", e));
}
finally
{
outputStream.Close();
}
return "\t" + output;
}
}
}
This is the way I am passing arguments from my main program (Form1.cs)
private void button1_Click(object sender, EventArgs e)
{
string output;
output = LaunchEXE.Run(#"C:\Program Files (x86)\MyFolder\MyConsole.exe", "/BACKUP C:\\MyBackupProfile.txt", 100);
System.Windows.Forms.MessageBox.Show(output);
}
The command line tool accepts the following command and works perfectly:
C:\Program Files (x86)\MyFolder>MyConsole.exe /BACKUP C:\MyBackupProfile.txt
I have two options for you -
1) Please try running your Visual Studio on "administrator mode".
or 2) Try to implement this instead. https://github.com/commandlineparser/commandline.
"The Command Line Parser Library offers to CLR applications a clean and concise API for manipulating command line arguments and related tasks. It allows you to display an help screen with an high degree of customization and a simple way to report syntax errors to the user. Everything that is boring and repetitive to be programmed stands up on library shoulders, letting you concentrate yourself on core logic. This library provides hassle free command line parsing with a constantly updated API since 2005."
Worked great for me.
I have a feeling it doesn't like the spaces in your path. See this post: C# How to use Directory White Spaces into process.arguements?
Related
I am trying to make a KI or something which is more like a Bot which can learn a little bit. For example I want to give him new commands. Therefor the Bot has to create new Methods due run-time so it can react on my inputs with the right Method. I wanted to know if and how it is possible to add a method into my existing class due run-time.
I have found some links already and examples like the CodeDomProvider, CSharpCodeProvider and the DynamicMethod but it seems like they can only create new runables (exe files) or create a preset which one can execute with new parameters.
What I need is a way to create a new Method in my existing class or a way to interact with my existing class. I was already thinking about Plugins but in my opinion it would be much work to create a plugin for each method and also not efficient am I right?
You may also know a better way then creating Methods for each command?
Edit 1:
With Assembly.CreateInstane("path"); I could "clone" my running program and together with a CSharpCodeProvider I could create a new exe with these Methods. But there is a problem. When I use a Method where is no Reference to in the Class such as using System.Windows.Forms gives me the error:
Line number 3, error number: CS0234, 'The type or namespace name' Windows' does not exist in the namespace 'System'. (Is an assembly reference missing?);
That would have been my Testcode right now:
//The String I am going to Add through my textfield
using System;
using System.Reflection;
using System.Windows.Forms;
public class Example
{
public static void Main()
{
Assembly assem = typeof(View).Assembly;
View v = (View ) assem.CreateInstance("Usopis");
if (! (v == null)) {
v.Height = 300;
MessageBox.Show("Instantiated a {0} object whose value is '{1}'",
v.GetType().Name, v);
}
else {
MessageBox.Show("Unable to instantiate a View object.");
}
}
}
//Code which should compile my String to a exe
private void Button_Click(object sender, EventArgs e) {
textBox2.Text = "";
CodeDomProvider codeProvider = CodeDomProvider.CreateProvider("CSharp");
CompilerParameters parameters = new CompilerParameters();
parameters.GenerateExecutable = true;
CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, textBox1.Text);
if(results.Errors.Count > 0) {
textBox2.ForeColor = Color.Red;
foreach(CompilerError CompErr in results.Errors) {
textBox2.Text = textBox2.Text +
"Line number " + CompErr.Line +
", Error Number: " + CompErr.ErrorNumber +
", '" + CompErr.ErrorText + ";" +
Environment.NewLine + Environment.NewLine;
}
} else {
//Successful Compile
textBox2.ForeColor = Color.Blue;
textBox2.Text = "Success!";
}
}
To fix the missing namespace error you have to add the missing reference:
parameters.ReferencedAssemblies.Add("System.Web.dll");
But that won`t solve all of your problems.
Maybe look into Lamdas...
That could be something for you:
https://www.strathweb.com/2018/01/easy-way-to-create-a-c-lambda-expression-from-a-string-with-roslyn/
I have the following code:
Process scriptProc = new Process();
scriptProc.StartInfo.FileName = #"cscript";
scriptProc.StartInfo.WorkingDirectory = #"C:\MyPath\";
scriptProc.StartInfo.Arguments = "filename.vbs //X";
scriptProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
scriptProc.Start();
scriptProc.WaitForExit();
scriptProc.Close();
My VBS opens in an editor(Visual Studio) which is specified by the //X attribute, but this only opens if the script has no syntax errors, it is not opening in the editor if I have script errors, which basically makes the use of the debugger as redundant.
Is there any way with which I can debug a VBScript using C# only?
A debugger is a tool for dealing with run-time errors. So it can't be used to check for compile-time errors.
Unfortunately, the c|wscript.exe script hosts don't have an option like Perl's -c (syntax check). Running cscript maybebad.vbs to catch syntax errors may be not convenient if that executes a flawless shutdown/format my harddisk/... script accidentally/unwittingly. You could write a script that Execute(Global) the code of maybebad.vbs with a WScript.Quit 1
prepended.
There is the MS ScriptControl that could be used to avoid the shelling out; I'm not sure, whether that will streamline your 'debugging experience'.
The code below uses #Ekkehard.Horner approaches. Compile it, then drag and drop .vbs files onto executable to test whether the file has syntax errors or not:
using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using System.Runtime.InteropServices;
// Add reference to COM Microsoft Script Control 1.0
// Code works for .Net 2.0 and above
class Program
{
static void Main(string[] args)
{
// Check whether a file was dragged onto executable
if (args.Length != 1)
{
MessageBox.Show("Drag'n'drop .vbs file onto this executable to check syntax");
return;
}
MessageBox.Show("Syntax will be checked for\r\n" + args[0]);
String vbscode = "";
// Read the content of the file
try
{
StreamReader sr = new StreamReader(args[0]);
vbscode = sr.ReadToEnd();
}
catch (Exception e)
{
MessageBox.Show("File reading error " + e.Message);
return;
}
// Add statement raising runtime error -2147483648 in the first line to ScriptControl
int hr = 0;
try
{
vbscode = "Err.Raise &H80000000\r\n" + vbscode;
MSScriptControl.ScriptControl sc = new MSScriptControl.ScriptControl();
sc.Language = "VBScript";
sc.AddCode(vbscode);
}
catch (Exception e)
{
hr = Marshal.GetHRForException(e);
// First line of code executed if no syntax errors only
if (hr == -2147483648)
{
// Run time error -2147483648 shows that execution started without syntax errors
MessageBox.Show("Syntax OK");
}
else
{
// Otherwise there are syntax errors
MessageBox.Show("Syntax error");
}
}
}
}
In answer to your question, no, I'm afraid you cannot debug the VBScript from within a debugging context of C#. Try debugging your script directly with something like http://www.vbsedit.com. By launching the script in C# first, you're complicating matters.
I'm trying to build a GUI app that has an interactive console, much like the one found in SublimeText.
I hope it is a valid question because it seems to be "a practical, answerable problem that is unique to software development".
In short, I see huge benefits having an interactive console inside a GUI app for
debugging, probing internal variables at runtime
logging
quick configuration changes
However, I have not come across any existing open-source applications that uses such a design.
I'm hoping someone has done it before and can share his/her design approach.
While I do have a semi-working solution using reflection and invoke in .NET, it is limited to only function calls and I'm not able to probe into nested internal variables (e.g. object.property.property).
To make the question more specific, these are the problems I'm facing:
Not easily extensible (Need to wire every new GUI command to a console command, vice-versa), any design tips? Routed commands (I could not find a useful example either)?
How to execute dynamic code that can access all existing object instances in the entire .NET app?
Thank you.
So here comes the code which worked for me:
namespace ReflectionsTest
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
//Events excluded
private void ExecuteCommand(string command)
{
string cmd = "";
cmd += #"using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Linq;
using Microsoft.CSharp;
using System.Reflection;
using ReflectionsTest;";
// i included a using statement for every namespace i want to adress direct
cmd += #"namespace ReflectionConsole
{
public class RuntimeExecution
{
public static void Main(MainForm parent, TextBox output, FieldInfo[] privateFields)
{
try {";
//the code in a trycatch because i can send every error to a specific output defined as output parameter
cmd += command;
cmd += "}catch (Exception ex) { if(output != null){" +
"output.Text += ex.Message + \"\\n\\r\";"
+"}else{MessageBox.Show(ex.Message);}}}}}";
try {
ExecuteCSharp(cmd);
}
catch (Exception ex) {
textBox2.Text += ex.Message + "\n\r";
}
}
private void ExecuteCSharp(string code)
{
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters();
List<AssemblyName> assemblys = (Assembly.GetExecutingAssembly().GetReferencedAssemblies()).ToList<AssemblyName>();
foreach (var item in assemblys) {
parameters.ReferencedAssemblies.Add(item.Name + ".dll");
}
string t = Assembly.GetExecutingAssembly().GetName().Name;
parameters.ReferencedAssemblies.Add(t + ".exe");
//Here you have to reference every assembly the console wants access
parameters.GenerateInMemory = true;
parameters.GenerateExecutable = false;
CompilerResults results = provider.CompileAssemblyFromSource(parameters, code);
if (results.Errors.HasErrors) {
StringBuilder sb = new StringBuilder();
foreach (CompilerError error in results.Errors) {
sb.AppendLine(String.Format("Error ({0}): {1}", error.ErrorNumber, error.ErrorText));
}
throw new InvalidOperationException(sb.ToString());
}
else {
Assembly assembly = results.CompiledAssembly;
Type program = assembly.GetType("ReflectionConsole.RuntimeExecution");
MethodInfo main = program.GetMethod("Main");
FieldInfo[] fields = this.GetType().GetFields(
BindingFlags.NonPublic |
BindingFlags.Instance);
//if everything is correct start the method with some arguments:
// containing class, output, private fields of the containing class for easier access
main.Invoke(null, new object[]{this, textBox2, fields});
}
}
}
}
Some Explanations:
You have pass the highest class of your program which contains everything else, because it is easier to access members than parent objects.
public objects you can access like parent.obect1.Text = "textXYZ";
private objects you can access by name. These objects are listed in privateFields.
for the subclasses you have two options:
change the first and third parameter when calling main.Invoke([...])
or
recollect the private fields.
as Suggestion you could include a .dll in the command which already gives you methods to achieve this much faster.
For example GetValueFromFieldByName(object class, string name, Type resultType)
I hope that is what you've hoped for ^^
I've been browsing for a good hour and have yet to find something that would help with this. I'm working on opening AutoCAD from the .NET API in VS2013 using C#, but for some reason, I can never get AutoCAD to actually launch. I'm using the following code:
using System;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
namespace IOAutoCADHandler
{
public static class ACADDocumentManagement
{
[CommandMethod("ConnectToAcad")]
public static void ConnectToAcad()
{
AcadApplication acAppComObj = null;
// no version number so it will run with any version
const string strProgId = "AutoCAD.Application";
// Get a running instance of AutoCAD
try
{
acAppComObj = (AcadApplication)Marshal.GetActiveObject(strProgId);
}
catch // An error occurs if no instance is running
{
try
{
// Create a new instance of AutoCAD
acAppComObj = (AcadApplication)Activator.CreateInstance(Type.GetTypeFromProgID(strProgId), true);
}
catch //// STOPS HERE
{
// If an instance of AutoCAD is not created then message and exit
// NOTE: always shows this box and never opens AutoCAD
System.Windows.Forms.MessageBox.Show("Instance of 'AutoCAD.Application'" +
" could not be created.");
return;
}
}
// Display the application and return the name and version
acAppComObj.Visible = true;
System.Windows.Forms.MessageBox.Show("Now running " + acAppComObj.Name +
" version " + acAppComObj.Version);
// Get the active document
AcadDocument acDocComObj;
acDocComObj = acAppComObj.ActiveDocument;
// Optionally, load your assembly and start your command or if your assembly
// is demandloaded, simply start the command of your in-process assembly.
acDocComObj.SendCommand("(command " + (char)34 + "NETLOAD" + (char)34 + " " +
(char)34 + #"C:\Users\Administrator\Documents\All Code\main-libraries\IOAutoCADHandler\bin\Debug\IOAutoCADHandler.dll" + (char)34 + ") ");
acDocComObj.SendCommand("DRAWCOMPONENT");
}
}
Unfortunately, it always stops at the nested catch statement and always displays the popup box without opening AutoCAD. Any suggestions on how to at least make AutoCAD open for me?
EDIT: Error message
The issue is you're coding (correctly) to the AutoCAD interop interface. I recommend against that (due to potential version changes).
The other issue is that the documentation for AutoCAD plugins using the newer .net api is for plugins when AutoCAD is already running.
Final issue could be that the program Id of AutCAD is a mystery. I have resorted to making that a configurable setting, but default to "AutoCAD.Application", which will take the currently registered AutoCAD.Application on the production machine. If there are multiple versions installed on the machine and you want to be specific, then you could append the version number (which you'll need to research) to the ProgID like: "AutoCAD.Application.19", or "AutoCAD.Application.20" for 2015.
For the first issue, one technique is to use dynamics for the autoCad objects, particularly for creating instances. I have used the ObjectARX api for creating my application in a dummy project, and then switching to dynamics when I'm happy with the properties and method names.
In a standalone .Net application that starts AutoCAD you could use something like:
// I comment these out in production
//using Autodesk.AutoCAD.Interop;
//using Autodesk.AutoCAD.Interop.Common;
//...
//private static AcadApplication _application;
private static dynamic _application;
static string _autocadClassId = "AutoCAD.Application";
private static void GetAutoCAD()
{
_application = Marshal.GetActiveObject(_autocadClassId);
}
private static void StartAutoCad()
{
var t = Type.GetTypeFromProgID(_autocadClassId, true);
// Create a new instance Autocad.
var obj = Activator.CreateInstance(t, true);
// No need for casting with dynamics
_application = obj;
}
public static void EnsureAutoCadIsRunning(string classId)
{
if (!string.IsNullOrEmpty(classId) && classId != _autocadClassId)
_autocadClassId = classId;
Log.Activity("Loading Autocad: {0}", _autocadClassId);
if (_application == null)
{
try
{
GetAutoCAD();
}
catch (COMException ex)
{
try
{
StartAutoCad();
}
catch (Exception e2x)
{
Log.Error(e2x);
ThrowComException(ex);
}
}
catch (Exception ex)
{
ThrowComException(ex);
}
}
}
When there are several versions of AutoCAD installed on a computer, creating an instance with the ProgID "AutoCAD.Application" will run the latest version started on this computer by the current user. If the version of the Interop assemblies used does not match the version that is starting, you'll get a System.InvalidCastException with an HRESULT 0x80004002 (E_NOINTERFACE).
In your specific case, the {070AA05D-DFC1-4E64-8379-432269B48B07} IID in your error message is the GUID for the AcadApplicationinterface in R19 64-bit (AutoCAD 2013 & 2014). So there is an AutoCAD 2013 or 2014 that is starting, and you cannot cast this COM object to a 2015 type because 2015 is R20 (not binary compatible).
To avoid that, you can add a specific version to your ProgID (like "AutoCAD.Application.20" for AutoCAD 2015 (R20.0) to 2016 (R20.1)) to start the version matching your Interop assemblies or you can use late binding (eg. remove your references to Autodesk.AutoCAD.Interop* and use the dynamic keyword instead of the AutoCAD types).
In the last case, you will lost autocompletion, but your program will work with all the versions of AutoCAD.
Check also 32-bit vs 64-bit because TypeLib/Interop assemblies are not the same.
I open the application in a much straight-forward way. First, be sure to reference the correct type library. The one I am using is AutoCAD 2014 Type Library, located at:
c:\program files\common files\autodesk shared\acax19enu.tlb
To initialize the application:
using AutoCAD;
namespace test
{
class Program
{
static void Main(string[] args)
{
AutoCAD.AcadApplication app;
app = new AcadApplication();
app.Visible = true;
Console.Read();
}
}
}
Try this:
"sourcefile" is the original file
"newfile" is the new file
[CommandMethod("ModifyAndSaveas", CommandFlags.Redraw | CommandFlags.Session)]
public void ModifyAndSaveAs()
{
Document acDoc = Application.DocumentManager.Open(sourcefile);
Database acDB = acDoc.Database;
Transaction AcTran = acDoc.Database.TransactionManager.StartTransaction();
using (DocumentLock acLckDoc = acDoc.LockDocument())
{
using (AcTran)
{
BlockTable acBLT = (BlockTable)AcTran.GetObject(acDB.BlockTableId, OpenMode.ForRead);
BlockTableRecord acBLTR = (BlockTableRecord)AcTran.GetObject(acBLT[BlockTableRecord.ModelSpace], OpenMode.ForRead);
var editor = acDoc.Editor;
var SelectionSet = editor.SelectAll().Value;
foreach (ObjectId id in SelectionSet.GetObjectIds())
{
Entity ent = AcTran.GetObject(id, OpenMode.ForRead) as Entity;
//modify entities
}
AcTran.Commit();
}
}
acDB.SaveAs(newfile, DwgVersion.AC1021);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Tekkit
{
class Program
{
static void Main(string[] args)
{
//make sure to add last 2 using statements
ProcessStartInfo start = new ProcessStartInfo("calc.exe");
Process.Start(start);//starts the process
}
}
}
When I start my .net console application from a bat file e.g. start myapp.exe
myapp.exe then tries to write a file to its current directory, although I get a .net runtime error claiming that the file is in use by another application (there is nothing else running)
http://i.stack.imgur.com/XLmnR.png
Although when I launch it normally with out a batch file e.g. double click on it, it functions fine and outputs the file fine. I thought it might be something to do with privilages, although tried running the batch file as an administrator and I got the same error "File is in use..."
Could anyone shed any light on this?
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace FileSearcher
{
class Program
{
static void Main(string[] args)
{
string dirPath = "C:\\";
FileSystemWatcher fileWatcher = new FileSystemWatcher(dirPath);
fileWatcher.IncludeSubdirectories = true;
fileWatcher.Filter = "*.exe";
// fileWatcher.Filter = "C:\\$Recycle.Bin";
// fileWatcher.Changed += new FileSystemEventHandler(FileWatcher_Changed);
fileWatcher.Created += new FileSystemEventHandler(FileWatcher_Created);
// fileWatcher.Deleted += new FileSystemEventHandler(FileWatcher_Deleted);
// fileWatcher.Renamed += new RenamedEventHandler(FileWatcher_Renamed);
fileWatcher.EnableRaisingEvents = true;
// updated code
Console.ReadKey();
}
static void FileWatcher_Renamed(object sender, RenamedEventArgs e)
{
Console.WriteLine(e.OldName + " was renamed to " + e.Name);
}
static void FileWatcher_Deleted(object sender, FileSystemEventArgs e)
{
Console.WriteLine(e.Name + " was deleted");
}
static void FileWatcher_Created(object sender, FileSystemEventArgs e)
{
using (StreamWriter fileWriter = new StreamWriter("process.lst", true))
{
var data = true;
fileWriter.Write("C:\\" + e.Name + Environment.NewLine);
}
}
static void FileWatcher_Changed(object sender, FileSystemEventArgs e)
{
Console.WriteLine(e.Name + "");
}
}
}
It looks like from your batch file that you are sending stdout (1>) to the same file (process.lst) that you are writing to within your application. You can do one or the other, not both.
For example, this application works fine when run by itself:
static void Main(string[] args)
{
StreamWriter writer = File.CreateText("process.lst");
Console.WriteLine("Writing to the file.");
writer.Write("Testing 1.2.3.4");
Console.WriteLine("Finished.");
}
But, when run from the command line like myTestApp.exe 1> process.lst yields the same exception that you have:
The process cannot access the file 'process.lst' because it is being used by another process.
Try doing your process after the OnCreated event has finished. Maybe starting a short timer and writing the file when the timer ticks (remember to stop the timer)
The event that a file has been created is fired immediately, even if the file is not written yet. You should always try to open the file and wait a little bit, when you get the IOException. You can find a solution here: http://bloggingabout.net/blogs/jschreuder/archive/2006/07/06/12886.aspx