Author Topic: the quick way of checking is any AutoCAD launched  (Read 2078 times)

0 Members and 1 Guest are viewing this topic.

Andrey Bushman

  • Swamp Rat
  • Posts: 864
the quick way of checking is any AutoCAD launched
« on: November 11, 2015, 05:14:47 AM »
This is the quick way of checking is any AutoCAD launched (acad.exe or accoreconsole.exe) or not. I've checked my code for the usual AutoCAD 2009-2016 x64. But I haven't their x86 versions, I haven't older AutoCAD versions, and I haven't their vertical products, therefore I can't check my code for these versions.

Here is similar theme but with C++ code.

Code - C#: [Select]
  1. /*  © Andrey Bushman, 2015
  2.     http://bushman-andrey.blogspot.ru/2015/11/acadexe-accoreconsoleexe.html
  3.  
  4.     This is the quick way of checking is any AutoCAD launched (acad.exe
  5.     or accoreconsole.exe) or not. I've checked my code for the usual
  6.     AutoCAD 2009-2016 x64. But I haven't their x86 versions, I haven't
  7.     older AutoCAD versions, and I haven't their vertical products,
  8.     therefore I can't check my code for these versions.
  9.  
  10.     Additionally, Alexander Rivilis checked this code for AutoCAD 2008 x86
  11.     which was launched in Windows x64.
  12. */
  13. using System;
  14. using System.Threading;
  15.  
  16. namespace Bushman.Sandbox.AutoCAD
  17. {
  18.     class Program
  19.     {
  20.         static Boolean IsAnyAutoCadLaunched()
  21.         {
  22.             try
  23.             {
  24.                 Mutex m = Mutex.OpenExisting(
  25.                     // This works for AutoCAD 2008 and newer (I haven't older
  26.                     // AutoCAD versions, therefore I can't check it for them).
  27.                     "Global\\8C84DAD6-9865-400e-A6E3-686A61C16968"
  28.  
  29.                     // This is for AutoCAD 2009 and newer
  30.                     // "Local\\AcadProfileStorage_54519085-6DDA-4070-BB93-3A095D7E1140"
  31.                     );
  32.                 m.Close();
  33.                 return true;
  34.             }
  35.             catch
  36.             {
  37.                 return false;
  38.             }
  39.         }
  40.         static void Main(string[] args)
  41.         {
  42.             String msg = IsAnyAutoCadLaunched() ? "Any AutoCAD is launched." :
  43.                 "Any AutoCAD is not launched.";
  44.  
  45.             Console.WriteLine(msg);
  46.  
  47.             Console.WriteLine("Press any key for exit...");
  48.             Console.ReadKey();
  49.         }
  50.     }
  51. }

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: the quick way of checking is any AutoCAD launched
« Reply #1 on: November 11, 2015, 01:12:24 PM »
I just use System.Diagnostics.Process
 
Code - C#: [Select]
  1. Process.GetProcessesByName("acad")
Revit 2019, AMEP 2019 64bit Win 10

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: the quick way of checking is any AutoCAD launched
« Reply #2 on: November 11, 2015, 03:32:41 PM »
I just use System.Diagnostics.Process
I know about this way and I used it in my some programs. But it is possible to rename any EXE file into acad.exe and this code can't define such substitution.

Of course, any code also can to create such named Mutex :). So my variant also is not a silver bullet, but he can't be deceived by simple renaming of any EXE-file.

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: the quick way of checking is any AutoCAD launched
« Reply #3 on: November 11, 2015, 03:42:01 PM »
Also, 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... For checking this I wrote such test:
Code - C#: [Select]
  1. using System;
  2. using System.Diagnostics;
  3. using System.Runtime.InteropServices;
  4.  
  5. using cad = Autodesk.AutoCAD.ApplicationServices.Application;
  6. using Autodesk.AutoCAD.ApplicationServices;
  7. using Autodesk.AutoCAD.EditorInput;
  8. using Autodesk.AutoCAD.Runtime;
  9.  
  10. // http://stackoverflow.com/questions/6420752/how-can-i-set-the-name-of-a-process-created-with-createprocess
  11.  
  12. namespace Bushman.Sandbox.AutoCAD
  13. {
  14.     public class Class1
  15.     {
  16.         [DllImport("Kernel32.dll", CallingConvention = CallingConvention.Cdecl,
  17.             CharSet = CharSet.Unicode, EntryPoint = "GetCommandLine" )]
  18.         static unsafe public extern char* GetCommandLine();
  19.  
  20.         [CommandMethod("cmd", CommandFlags.Modal)]
  21.         static unsafe public void Cmd()
  22.         {
  23.             Document doc = cad.DocumentManager.MdiActiveDocument;
  24.             Editor ed = doc.Editor;
  25.  
  26.             // I expected the name of the process is dinamically calculated from
  27.             // its command line. At this case it would be not gotten if the
  28.             // command line is empty.
  29.  
  30.             // I check my assumption...
  31.  
  32.             ed.WriteMessage("Before command line errasing...\n");
  33.  
  34.             Process proc = Process.GetCurrentProcess();
  35.             ed.WriteMessage("Process name: {0}\n", proc.ProcessName);
  36.  
  37.             char* cmdLine = GetCommandLine();
  38.             String str_cmdLine = Marshal.PtrToStringUni((IntPtr)cmdLine);
  39.  
  40.             ed.WriteMessage("GetCommandLine: {0}\n", str_cmdLine);
  41.  
  42.             ed.WriteMessage("Command line args:\n");
  43.             foreach (String arg in Environment.GetCommandLineArgs())
  44.             {
  45.                 ed.WriteMessage("\t{0}\n", arg);
  46.             }
  47.             ed.WriteMessage("==============\n");
  48.  
  49.             ed.WriteMessage("After command line errasing...\n");
  50.  
  51.             *cmdLine = '\0'; // Now the command line is empty
  52.            
  53.             str_cmdLine = Marshal.PtrToStringUni((IntPtr)cmdLine);
  54.  
  55.             // command line is empty now
  56.             ed.WriteMessage("GetCommandLine: {0}\n", str_cmdLine);
  57.  
  58.             ed.WriteMessage("Command line args:\n");
  59.  
  60.             // command line is empty now
  61.             foreach (String arg in Environment.GetCommandLineArgs())
  62.             {
  63.                 ed.WriteMessage("\t{0}\n", arg);
  64.             }
  65.             ed.WriteMessage("==============\n");
  66.  
  67.             proc = Process.GetCurrentProcess();
  68.  
  69.             // I see the process name IS THE SAME...
  70.             // So, I was mistaken.
  71.             ed.WriteMessage("Process name: {0}\n", proc.ProcessName);
  72.         }
  73.     }
  74. }

The output:
Quote
Command: cmd
Before command line errasing...
Process name: acad
GetCommandLine: "C:\Program Files\Autodesk\AutoCAD 2009\acad.exe"
Command line args:
 C:\Program Files\Autodesk\AutoCAD 2009\acad.exe
==============
After command line errasing...
Process name: acad
GetCommandLine:
Command line args:

==============
So, I see I was mistaken.