最快的检查方法是启动任何AutoCAD
这是检查是否启动了任何AutoCAD(acad.exe或accoreconsole.exe)的快速方法,我已经检查了通常的AutoCAD 2009-2016 x64的代码,但是我没有他们的x86版本,我没有较旧的AutoCAD版本,我也没有他们的垂直产品,因此我无法检查这些版本的代码。是类似的主题,但有C++代码。/*©Andrey Bushman,2015。http://bushman-andrey.blogspot.ru/2015/11/acadexe-accoreconsoleexe.html。
。
这是检查是否启动任何AutoCAD的快速方法(acad.exe。
或accoreconsole.exe)或不,我已经检查了我的代码是否正常。
AutoCAD 2009-2016 x64,但是我没有他们的x86版本,我没有。
旧的AutoCAD版本,我没有他们的垂直产品,。
因此,我无法检查这些版本的代码。。
。
此外,Alexander Rivilis为AutoCAD 2008 x86检查了此代码。
在Windows x64中启动,。
*/。
使用系统;。
使用System.Threading;。
。
命名空间Bushman.Sandbox.AutoCAD。
{。
类程序。
{。
静态布尔IsAnyAutoCadLaunked()。
{。
尝试。
{。
互斥m=Mutex.OpenExisting(。
//这适用于AutoCAD 2008和更新版本(我没有旧版本。
//AutoCAD版本,因此我无法检查它们)。。
"Global\\8C84DAD6-9865-400e-A6E3-686A61C16968"。
。
//这是AutoCAD 2009和更新版本。
//"本地\\AcadProfileStorage_54519085-6DDA-4070-BB93-3A095D7E1140"。
);。
m.Close();。
返回true;。
}。
捕捉。
{。
返回false;。
}。
}。
静态无效主(字符串[]参数)。
{。
字符串msg=IsAnyAutoCadLaunated()?“任何AutoCAD都已启动,”:。
"未启动任何AutoCAD,";。
。
Console.WriteLine(msg);。
。
Console.WriteLine("按任意键退出...");。
Console.ReadKey();。
}。
}。
}。
**** Hidden Message ***** 我只是使用系统。诊断。进程进程。GetProcessesByName(" acad ")
我知道这种方式,我在我的一些程序中使用它。但是可以将任何EXE文件重命名为acad.exe并且此代码无法定义此类替换。
当然,任何代码也可以创建这样命名的Mutex。所以我的变体也不是银弹,但他不能通过简单地重命名任何EXE文件来欺骗他。 此外,我期望进程的名称是从它的命令行中直接计算出来的。在这种情况下,如果命令行为空,它将不会被获取...为了检查这一点,我编写了这样的测试:使用System;。
使用System.Diagnostics;。
使用System.Runtime.InteropServices;。
。
使用cad=Autodesk.AutoCAD.ApplicationServices.Application;。
使用Autodesk.AutoCAD.ApplicationServices;。
使用Autodesk.AutoCAD.EditorInput;。
使用Autodesk.AutoCAD.Runtime;。
。
//http://stackoverflow.com/questions/6420752/how-can-i-set-the-name-of-a-process-created-with-createprocess。
。
命名空间Bushman.Sandbox.AutoCAD。
{。
公共类Class1。
{。
[Dll导入("Kernel32.dll",调用约定=CallingConvention.Cdecl,。
CharSet=CharSet.Unicode,EntryPoint="GetCommand dLine")]。
静态不安全的公共extern char*GetCommand dLine();。
。
[命令方法("cmd",CommandFlags.Modal)]。
静态不安全公共无效Cmd()。
{。
文档文档=cad.DocumentManager.MdiActiveDocument;。
编辑doc.Editor。
。
//我期望进程的名称是从。
//它的命令行,在这种情况下,如果。
//命令行为空,。
。
//我检查我的假设...。
。
ed.WriteMessage("在命令行错误之前...\n");。
。
进程proc=Process.GetCurrentProcess();。
ed.WriteMessage("进程名称:{0}\n",proc.ProcessName);。
。
char*cmdLine=GetCommand dLine();。
字符串str_cmdLine=Marshal.PtrToStringUni(IntPtr)cmdLine);。
。
ed.WriteMessage:{0}\n",str_cmdLine);。
。
ed.WriteMessage("命令行args:\n");。
foreach(Environment.GetCommandLineArgs()中的字符串arg)。
{。
ed.WriteMessage("\t{0}\n", arg);。
}。
ed.WriteMessage ("==============\n");。
。
ed.WriteMessage("命令行错误后...\n");。
。
*cmdLine='\0';//现在命令行为空。
。
str_cmdLine=Marshal.PtrToStringUni(IntPtr)cmdLine);。
。
//命令行现在为空。
ed.WriteMessage:{0}\n",str_cmdLine);。
。
ed.WriteMessage("命令行args:\n");。
。
//命令行现在为空。
foreach(Environment.GetCommandLineArgs()中的字符串arg)。
{。
ed.WriteMessage("\t{0}\n", arg);。
}。
ed.WriteMessage ("==============\n");。
。
proc=Process.GetCurrentProcess();。
。
//我看到进程名称是相同的...。
//所以,我错了。。
ed.WriteMessage("进程名称:{0}\n",proc.ProcessName);。
}。
}。
}输出:所以,我明白我错了。
页:
[1]