using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using cad = Autodesk.AutoCAD.ApplicationServices.Application;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
// http://stackoverflow.com/questions/6420752/how-can-i-set-the-name-of-a-process-created-with-createprocess
namespace Bushman.Sandbox.AutoCAD
{
public class Class1
{
[DllImport("Kernel32.dll", CallingConvention = CallingConvention.Cdecl,
CharSet = CharSet.Unicode, EntryPoint = "GetCommandLine" )]
static unsafe public extern char* GetCommandLine();
[CommandMethod("cmd", CommandFlags.Modal)]
static unsafe public void Cmd()
{
Document doc = cad.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
// I expected the name of the process is dinamically calculated from
// its command line. At this case it would be not gotten if the
// command line is empty.
// I check my assumption...
ed.WriteMessage("Before command line errasing...\n");
Process proc = Process.GetCurrentProcess();
ed.WriteMessage("Process name: {0}\n", proc.ProcessName);
char* cmdLine = GetCommandLine();
String str_cmdLine = Marshal.PtrToStringUni((IntPtr)cmdLine);
ed.WriteMessage("GetCommandLine: {0}\n", str_cmdLine);
ed.WriteMessage("Command line args:\n");
foreach (String arg in Environment.GetCommandLineArgs())
{
ed.WriteMessage("\t{0}\n", arg);
}
ed.WriteMessage("==============\n");
ed.WriteMessage("After command line errasing...\n");
*cmdLine = '\0'; // Now the command line is empty
str_cmdLine = Marshal.PtrToStringUni((IntPtr)cmdLine);
// command line is empty now
ed.WriteMessage("GetCommandLine: {0}\n", str_cmdLine);
ed.WriteMessage("Command line args:\n");
// command line is empty now
foreach (String arg in Environment.GetCommandLineArgs())
{
ed.WriteMessage("\t{0}\n", arg);
}
ed.WriteMessage("==============\n");
proc = Process.GetCurrentProcess();
// I see the process name IS THE SAME...
// So, I was mistaken.
ed.WriteMessage("Process name: {0}\n", proc.ProcessName);
}
}
}