C#:限制控制台应用程序的调用方式
我们有一个 C# 控制台应用程序产品。是否可以限制它仅从命令行运行?换句话说,用户将无法从脚本或其他应用程序调用它。
如果是的话,一些示例代码将不胜感激。谢谢。
We have a product that is a C# console app. Is it possible to restrict it to run from the command line only? In other words, users wouldn't be able to call it from a script or another app.
If it is, some example code would be much appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用此处给出的代码检查创建应用程序的进程:http: //msdn.microsoft.com/en-us/netframework/aa569609.aspx#Question3 。要在 DOS 命令行启动,父进程应该是 cmd.exe。但请注意,正如 Colin 指出的那样,可以使用批处理脚本轻松绕过此问题。您也可以通过确保 cmd.exe 的命令提示符参数为空来禁用它。为此,您需要使用 WMI:
http://skysanders.net/subtext/archive/2010/04/11/using-wmi-to-fetch-the-command-line-that-started-all.aspx
您还应该检查 cmd.exe 映像是否来自 system32 文件夹。
You can check the process that created your application using the code given here: http://msdn.microsoft.com/en-us/netframework/aa569609.aspx#Question3 . To be started at the DOS command line the parent process should be cmd.exe. Note however as pointed out by Colin this can be bypassed easily using a batch script. You can disable that as well by making sure that the command prompt arguments to cmd.exe are null. For this you will need to use WMI :
http://skysanders.net/subtext/archive/2010/04/11/using-wmi-to-fetch-the-command-line-that-started-all.aspx
You should also check the cmd.exe image is from system32 folder.
我认为不可能区分。
当然,父进程并不是一个有用的指示器。这就是您在父进程中得到的:
如果您打算 1. 成为启动程序的有效方式,那么我认为您无法停止 2. 这意味着您的应用程序可以从任何脚本或任何程序调用(因为另一个程序创建一个 1 行批处理脚本并执行它很简单)
(顺便说一句,有人知道如何在 StackOverflow 上获取表格吗?)
I don't think it is possible to tell the difference.
Certainly the parent process is not a useful indicator. This is what you get in the parent process:
If you intend for 1. to be a valid way to start your program, then I don't think you can stop 2. which means your app can be called from any script or any program (since it's simple for another program to create a 1 line batch script and execute it)
(BTW, does anyone know a way to get a table on StackOverflow?)
@swisston如果您从另一个自己的应用程序启动控制台应用程序,那么我想推荐您“命名内核对象”。例如互斥体。您可以在父应用程序中创建命名互斥体。然后在子控制台应用程序的主线程中尝试打开此互斥体。如果互斥体未打开(未找到):控制台应用程序没有权限继续,必须关闭;)等等,我将为您编写一些代码;)
编辑:
所以这是非常简单的战术。在父应用程序中创建您的命名互斥体:
然后在您的子控制台应用程序中检查您的互斥体是否存在:
如果您的控制台应用程序在没有父应用程序的情况下运行,则 CheckPermissions 方法将返回 false,并且必须关闭控制台;)
@swisston if you start your console application from your another own application, than i want to recommend you "named kernel objects". For example mutex. You can create named mutex in your parent app. Then in main thread of your child console app try to open this mutex. If mutex not opened (not found): console app has no permissions to continue and must be closed;) wait, i'll make some code for you;)
Edit:
So it is very easy tactics. In parent app create your named mutex:
Then in your child console application check if your mutex exists:
If your console application was run without your parent application CheckPermissions method will return false and console must be closed;)
我不同意你想要做的事情,但这里有一个可行的想法:在程序开始时需要某种用户输入,也许是某种验证码(在命令行中很难做到,但是理论上是可能的。
I don't agree with what you're trying to do, but here is an idea that could work: require some sort of user input at the beginning of the program, maybe some sort of CAPTCHA (difficult to do in command line, but theoretically possible. Think ASCII art).